File: cpl_test.h

package info (click to toggle)
cpl 6.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 18,764 kB
  • sloc: ansic: 111,368; sh: 14,549; makefile: 626
file content (1167 lines) | stat: -rw-r--r-- 48,096 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
/* $Id: cpl_test.h,v 1.60 2011/07/20 13:40:49 llundin Exp $
 *
 * This file is part of the ESO Common Pipeline Library
 * Copyright (C) 2001-2008 European Southern Observatory
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * $Author: llundin $
 * $Date: 2011/07/20 13:40:49 $
 * $Revision: 1.60 $
 * $Name: cpl-6_1_1 $
 */

#ifndef CPL_TEST_H
#define CPL_TEST_H

/*-----------------------------------------------------------------------------
                                    Includes
 -----------------------------------------------------------------------------*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <cpl_macros.h>
#include <cpl_msg.h>
#include <cpl_type.h>

#include <cpl_array.h>
#include <cpl_imagelist.h>
#include <cpl_polynomial.h>
#include <cpl_errorstate.h>

#include <errno.h>

/*-----------------------------------------------------------------------------
                                    Defines
 -----------------------------------------------------------------------------*/

#ifndef CPL_TEST_FITS
#define CPL_TEST_FITS "CPL_TEST_FITS"
#endif

CPL_BEGIN_DECLS

/*----------------------------------------------------------------------------*/
/**
   @hideinitializer
   @ingroup cpl_test
   @brief Initialize CPL + CPL messaging + unit test
   @param REPORT  The email address for the error message e.g. PACKAGE_BUGREPORT
   @param LEVEL   The default messaging level, e.g. CPL_MSG_WARNING
   @return void
   @see cpl_init()
   @note  This macro should be used at the beginning of main() of a unit test
   instead of cpl_init() and before any other CPL function call.
 */
/*----------------------------------------------------------------------------*/
#define cpl_test_init(REPORT, LEVEL) \
  cpl_test_init_macro(__FILE__, REPORT, LEVEL)

/*----------------------------------------------------------------------------*/
/**
   @hideinitializer
   @ingroup cpl_test
   @brief  Evaluate an expression and increment an internal counter if zero
   @param  bool   The expression to evaluate, side-effects are allowed
   @note   A zero value of the expression is a failure, other values are not
   @return void
   @see cpl_test_init()
   @note  This macro should be used for unit tests

  Example of usage:
   @code

   cpl_test(myfunc()); // myfunc() is expected to return non-zero
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test(bool) do {                                             \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_macro(cpl_test_errno, cpl_test_time, cpl_test_flops,   \
                       cpl_test_state, (bool) ? 1 : 0, CPL_TRUE, #bool, \
                       cpl_func, __FILE__, __LINE__);                   \
    } while (0)

/*----------------------------------------------------------------------------*/
/**
   @hideinitializer
   @ingroup cpl_test
   @brief  Evaluate an expression and increment an internal counter if non-zero
   @param  zero   The numerical expression to evaluate, side-effects are allowed
   @note   A zero value of the expression is a success, other values are not
   @return void
   @see cpl_test()
   @note  This macro should be used for unit tests

  Example of usage:
   @code

   cpl_test_zero(myfunc()); // myfunc() is expected to return zero
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_zero(zero) do {                                        \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_macro(cpl_test_errno, cpl_test_time, cpl_test_flops,   \
                       cpl_test_state, (zero), CPL_FALSE,               \
                       #zero, cpl_func, __FILE__, __LINE__);            \
    } while (0)

/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test if a pointer is NULL and update an internal counter on failure
  @param  pointer    The NULL-pointer to check, side-effects are allowed

  @see cpl_test()
  @return void

  Example of usage:
   @code

   cpl_test_null(pointer); // pointer is expected to be NULL
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_null(pointer) do {                                     \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_null_macro(cpl_test_errno, cpl_test_time,              \
                            cpl_test_flops, cpl_test_state, pointer,    \
                            #pointer,  cpl_func, __FILE__, __LINE__);   \
    } while (0)

/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test if a pointer is non-NULL
  @param  pointer    The pointer to check, side-effects are allowed

  @see cpl_test_nonnull()
  @return void

  Example of usage:
   @code

   cpl_test_nonnull(pointer); // pointer is expected to be non-NULL
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_nonnull(pointer) do {                                  \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_nonnull_macro(cpl_test_errno, cpl_test_time,           \
                               cpl_test_flops, cpl_test_state,          \
                               pointer, #pointer, cpl_func,             \
                               __FILE__, __LINE__);                     \
    } while (0)


/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test and if necessary reset the CPL errorstate
  @param  errorstate The expected CPL errorstate

  @see cpl_test()
  @note After the test, the CPL errorstate is set to the provided state
  @return void

  This function is useful for verifying that a successful call to a function
  does not modify any pre-existing errors.

  Example of usage:
   @code

   const cpl_error_code error = cpl_error_set(cpl_func, CPL_ERROR_EOL);
   cpl_errorstate prestate = cpl_errorstate_get();
   const cpl_error_code ok = my_func(); // Expected to succeed

   cpl_test_errorstate(prestate); // Verify that no additional errors occurred
   cpl_test_error(CPL_ERROR_EOL); // Reset error
   cpl_test_eq(ok, CPL_ERROR_NONE); // Verify that my_func() succeeded
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_errorstate(errorstate) do {                            \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_errorstate_macro(cpl_test_errno, cpl_test_time,        \
                                  cpl_test_flops, cpl_test_state,       \
                                  errorstate, #errorstate, cpl_func,    \
                                  __FILE__, __LINE__);                  \
    } while (0)

/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test and reset the CPL error code
  @param  error The expected CPL error code (incl. CPL_ERROR_NONE)

  @see cpl_test()
  @note After the test, the CPL errorstate is reset
  @return void

  Example of usage:
   @code

   cpl_test( my_func(NULL) ); // my_func(NULL) is expected to return non-zero

   cpl_test_error(CPL_ERROR_NULL_INPUT); // ... and to set this error code

   // The errorstate has been reset.

   cpl_test( !my_func(p) ); // my_func(p) is expected to return zero
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_error(error) do {                                      \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_error_macro(cpl_test_errno, cpl_test_time,             \
                             cpl_test_flops, cpl_test_state, error,     \
                             #error, cpl_func, __FILE__, __LINE__);     \
    } while (0)

/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test if two error expressions are equal and reset the CPL error code
  @param  first            The first value in the comparison
  @param  second           The second value in the comparison
  @see    cpl_test_error
  @note   If the two CPL error expressions are equal they will also be tested
          against the current CPL error code. After the test(s), the CPL
          errorstate is reset.

  Example of usage:
   @code

   cpl_error_code error = my_func(NULL);

   // my_func(NULL) is expected to return CPL_ERROR_NULL_INPUT
   // and to set the same error code
   cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
   
   // The errorstate has been reset.

   error = my_func(p); // Successful call
   cpl_test_eq_error(error, CPL_ERROR_NONE);
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_eq_error(first, second) do {                           \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_eq_error_macro(cpl_test_errno, cpl_test_time,          \
                                cpl_test_flops, cpl_test_state, first,  \
                                #first, second, #second, cpl_func,      \
                                __FILE__, __LINE__);                    \
    } while (0)

/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief Test if two integer expressions are equal
  @param first   The first value in the comparison, side-effects are allowed
  @param second  The second value in the comparison, side-effects are allowed

  @see cpl_test()
  @return void

  Example of usage:
   @code

   cpl_test_eq(computed, expected);
   
   @endcode

  For comparison of floating point values, see cpl_test_abs() and
  cpl_test_rel().

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_eq(first, second) do {                                 \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_eq_macro(cpl_test_errno, cpl_test_time, cpl_test_flops, \
                          cpl_test_state, first, #first,                \
                          second, #second, cpl_func, __FILE__, __LINE__); \
    } while (0)

/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief Test if two integer expressions are not equal
  @param first   The first value in the comparison, side-effects are allowed
  @param second  The second value in the comparison, side-effects are allowed

  @see cpl_test_eq()
  @return void

  Example of usage:
   @code

   cpl_test_noneq(computed, wrong);
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_noneq(first, second) do {                              \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_noneq_macro(cpl_test_errno, cpl_test_time,             \
                             cpl_test_flops, cpl_test_state, first,     \
                             #first,  second, #second, cpl_func,        \
                             __FILE__, __LINE__);                       \
    } while (0)


/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test if two pointer expressions are equal
  @param  first    The first value in the comparison, side-effects are allowed
  @param  second   The second value in the comparison, side-effects are allowed
  @see cpl_test_eq()
  @return void

  Example of usage:
   @code

   cpl_test_eq_ptr(computed, expected);
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_eq_ptr(first, second) do {                             \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_eq_ptr_macro(cpl_test_errno, cpl_test_time,            \
                              cpl_test_flops, cpl_test_state, first,    \
                              #first, second, #second, cpl_func,        \
                              __FILE__, __LINE__);                      \
    } while (0)

/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test if two pointer expressions are not equal
  @param  first    The first value in the comparison, side-effects are allowed
  @param  second   The second value in the comparison, side-effects are allowed
  @see cpl_test_eq_ptr()
  @return void

  Example of usage:
   @code

   cpl_test_noneq_ptr(computed, wrong);
   
   @endcode

*/
/*----------------------------------------------------------------------------*/
#define cpl_test_noneq_ptr(first, second) do {                          \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_noneq_ptr_macro(cpl_test_errno, cpl_test_time,         \
                                 cpl_test_flops, cpl_test_state, first, \
                                 #first,  second, #second, cpl_func,    \
                                 __FILE__, __LINE__);                   \
    } while (0)


/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test if two strings are equal
  @param  first      The first string or NULL of the comparison
  @param  second     The second string or NULL of the comparison
  @note One or two NULL pointer(s) is considered a failure.

  Example of usage:
   @code

   cpl_test_eq_string(computed, expected);
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_eq_string(first, second) do {                          \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_eq_string_macro(cpl_test_errno, cpl_test_time,         \
                                 cpl_test_flops, cpl_test_state, first, \
                                 #first, second, #second, cpl_func,     \
                                 __FILE__, __LINE__);                   \
    } while (0)


/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test if two strings are not equal
  @param  first      The first string or NULL of the comparison
  @param  second     The second string or NULL of the comparison
  @note One or two NULL pointer(s) is considered a failure.

  Example of usage:
   @code

   cpl_test_noneq_string(computed, expected);
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_noneq_string(first, second) do {                       \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_noneq_string_macro(cpl_test_errno, cpl_test_time,      \
                                    cpl_test_flops, cpl_test_state,     \
                                    first, #first, second, #second,     \
                                    cpl_func, __FILE__, __LINE__);      \
    } while (0)


/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test if a file is valid FITS using an external verification utility
  @param  fitsfile  The file to verify, NULL causes failure
  @note The external verification utility is specified with the environemt
        variable CPL_TEST_FITS, if is not set the test will pass on any
        non-NULL file.

  Example of usage:
   @code

   export CPL_TEST_FITS=/usr/local/bin/fitsverify

   @endcode

   @code

   cpl_test_fits(fitsfile);
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_fits(fitsfile) do {                                    \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_fits_macro(cpl_test_errno, cpl_test_time,              \
                            cpl_test_flops, cpl_test_state, fitsfile,   \
                            #fitsfile,  cpl_func, __FILE__, __LINE__);  \
    } while (0)

/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test if two CPL masks are equal
  @param  first      The first mask or NULL of the comparison
  @param  second     The second mask or NULL of the comparison
  @note One or two NULL pointer(s) is considered a failure.

  Example of usage:
   @code

   cpl_test_eq_mask(computed, expected);
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_eq_mask(first, second) do {                            \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_eq_mask_macro(cpl_test_errno, cpl_test_time,           \
                               cpl_test_flops, cpl_test_state, first,   \
                               #first,  second, #second, cpl_func,      \
                               __FILE__, __LINE__);                     \
    } while (0)

/*----------------------------------------------------------------------------*/
/**
   @hideinitializer
   @ingroup cpl_test
   @brief  Evaluate A <= B and increment an internal counter if it is not true
   @param  value     The number to test
   @param  tolerance The upper limit to compare against
   @return void
   @see cpl_test_init()
   @note  This macro should be used for unit tests

  Example of usage:
   @code

   cpl_test_leq(fabs(myfunc(&p)), DBL_EPSILON);
   cpl_test_nonnull(p);
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_leq(value, tolerance) do {                             \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_leq_macro(cpl_test_errno, cpl_test_time,               \
                           cpl_test_flops, cpl_test_state, value,       \
                           #value,  tolerance, #tolerance, cpl_func,    \
                           __FILE__, __LINE__);                         \
    } while (0)

/*----------------------------------------------------------------------------*/
/**
   @hideinitializer
   @ingroup cpl_test
   @brief  Evaluate A < B and increment an internal counter if it is not true
   @param  value     The number to test
   @param  tolerance The upper limit to compare against
   @return void
   @see cpl_test_init()
   @note  This macro should be used for unit tests

  Example of usage:
   @code

   cpl_test_lt(0.0, myfunc());
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_lt(value, tolerance) do {                              \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_lt_macro(cpl_test_errno, cpl_test_time,                \
                          cpl_test_flops, cpl_test_state, value,        \
                          #value,  tolerance, #tolerance, cpl_func,     \
                          __FILE__, __LINE__);                          \
    } while (0)

/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief Test if two numerical expressions are within a given absolute tolerance
  @param first     The first value in the comparison, side-effects are allowed
  @param second    The second value in the comparison, side-effects are allowed
  @param tolerance A non-negative tolerance
  @note  If the tolerance is negative, the test will always fail
  @see   cpl_test()

  Example of usage:
   @code

   cpl_test_abs(computed, expected, DBL_EPSILON);
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_abs(first, second, tolerance) do {                     \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_abs_macro(cpl_test_errno, cpl_test_time,               \
                           cpl_test_flops, cpl_test_state, first,       \
                           #first, second, #second, tolerance,          \
                           #tolerance, cpl_func, __FILE__, __LINE__);   \
    } while (0)

#ifdef _Complex_I
/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief Test if two complex expressions are within a given absolute tolerance
  @param first     The first value in the comparison, side-effects are allowed
  @param second    The second value in the comparison, side-effects are allowed
  @param tolerance A non-negative tolerance
  @note  If the tolerance is negative, the test will always fail
  @see   cpl_test()

  Example of usage:
   @code

   cpl_test_abs_complex(computed, expected, DBL_EPSILON);
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_abs_complex(first, second, tolerance) do {             \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_abs_complex_macro(cpl_test_errno, cpl_test_time,       \
                           cpl_test_flops, cpl_test_state, first,       \
                           #first, second, #second, tolerance,          \
                           #tolerance, cpl_func, __FILE__, __LINE__);   \
    } while (0)

#endif

/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test if two vectors are identical within a given (absolute) tolerance
  @param  first       The first vector in the comparison
  @param  second      The second vector of identical size in the comparison
  @param  tolerance   A non-negative tolerance
  @return void
  @see cpl_test_abs()
  @note The test will fail if one or both the vectors are NULL

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_vector_abs(first, second, tolerance) do {              \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_vector_abs_macro(cpl_test_errno, cpl_test_time,        \
                                  cpl_test_flops, cpl_test_state,       \
                                  first, #first,  second, #second,      \
                                  tolerance, #tolerance,  cpl_func,     \
                                  __FILE__, __LINE__);                  \
    } while (0)

/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test if two matrices are identical within a given (absolute) tolerance
  @param  first       The first matrix in the comparison
  @param  second      The second matrix of identical size in the comparison
  @param  tolerance   A non-negative tolerance
  @return void
  @see cpl_test_abs()
  @note The test will fail if one or both the matrices are NULL

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_matrix_abs(first, second, tolerance) do {              \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_matrix_abs_macro(cpl_test_errno, cpl_test_time,        \
                                  cpl_test_flops, cpl_test_state,       \
                                  first, #first,  second, #second,      \
                                  tolerance, #tolerance,  cpl_func,     \
                                  __FILE__, __LINE__);                  \
    } while (0)

/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test if two numerical arrays are identical within a given (absolute)
          tolerance
  @param  first       The first array in the comparison
  @param  second      The second array of identical size in the comparison
  @param  tolerance   A non-negative tolerance
  @return void
  @see cpl_test_abs()
  @note The test will fail if one or both the arrays are NULL or of a
        non-numerical type

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_array_abs(first, second, tolerance) do {               \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_array_abs_macro(cpl_test_errno, cpl_test_time,         \
                                 cpl_test_flops, cpl_test_state, first, \
                                 #first,  second, #second, tolerance,   \
                                 #tolerance,  cpl_func, __FILE__, __LINE__); \
    } while (0)


/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test if two images are identical within a given (absolute) tolerance
  @param  first       The first image in the comparison
  @param  second      The second image of identical size in the comparison
  @param  tolerance   A non-negative tolerance
  @return void
  @see cpl_test_abs()
  @note The test will fail if one or both the images are NULL

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_image_abs(first, second, tolerance) do {               \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_image_abs_macro(cpl_test_errno, cpl_test_time,         \
                                 cpl_test_flops, cpl_test_state, first, \
                                 #first,  second, #second, tolerance,   \
                                 #tolerance,  cpl_func, __FILE__, __LINE__); \
    } while (0)


/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test if two imagelists are identical within a given (absolute)
          tolerance
  @param  first       The first imagelist in the comparison
  @param  second      The second imagelist of identical size in the comparison
  @param  tolerance   A non-negative tolerance
  @return void
  @see cpl_test_image_abs()
  @note The test will fail if one or both the imagelists are NULL

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_imagelist_abs(first, second, tolerance) do {           \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_imagelist_abs_macro(cpl_test_errno, cpl_test_time,     \
                                     cpl_test_flops, cpl_test_state,    \
                                     first, #first, second, #second,    \
                                     tolerance, #tolerance, cpl_func,   \
                                     __FILE__, __LINE__);               \
    } while (0)


/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test if two polynomials are identical within a given (absolute)
          tolerance
  @param  first       The first polynomial in the comparison
  @param  second      The second polynomial in the comparison
  @param  tolerance   A non-negative tolerance
  @return void
  @see cpl_test_abs()
  @note The test will fail if one or both the polynomials are NULL

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_polynomial_abs(first, second, tolerance) do {          \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_polynomial_abs_macro(cpl_test_errno, cpl_test_time,    \
                                      cpl_test_flops, cpl_test_state,   \
                                      first, #first, second, #second,   \
                                      tolerance,  #tolerance, cpl_func, \
                                      __FILE__, __LINE__);              \
    } while (0)


/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief Test if two numerical expressions are within a given relative tolerance
  @param first     The first value in the comparison, side-effects are allowed
  @param second    The second value in the comparison, side-effects are allowed
  @param tolerance A non-negative tolerance
  @note  If the tolerance is negative or if one but not both of the two values
         is zero, the test will always fail. if both values are zero, the test
         will succeed for any non-negative tolerance. The test is commutative
         in the two values.
  @see   cpl_test()

  The test is carried out by comparing the absolute value of the difference 
  abs (@em first - @em second)
  to the product of the tolerance and the minimum of the absolute value of
  the two values,
  tolerance * min(abs(@em first), abs(@em second))
  (The test is implemented like this to avoid division with a number that may
  be zero.

  Example of usage:
   @code

   cpl_test_rel(computed, expected, 0.001);
   
   @endcode

 */
/*----------------------------------------------------------------------------*/
#define cpl_test_rel(first, second, tolerance) do {                     \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_rel_macro(cpl_test_errno, cpl_test_time,               \
                           cpl_test_flops, cpl_test_state, first,       \
                           #first, second, #second, tolerance,          \
                           #tolerance, cpl_func, __FILE__, __LINE__);   \
    } while (0)


/*----------------------------------------------------------------------------*/
/**
  @hideinitializer
  @ingroup cpl_test
  @brief  Test if the memory system is empty
  @see    cpl_memory_is_empty()
 */
/*----------------------------------------------------------------------------*/
#define cpl_test_memory_is_empty() do {                                 \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        cpl_test_memory_is_empty_macro(cpl_test_errno, cpl_test_time,   \
                                       cpl_test_flops, cpl_test_state,  \
                                       cpl_func, __FILE__, __LINE__)

/*----------------------------------------------------------------------------*/
/**
   @hideinitializer
   @ingroup cpl_test
   @brief  Evaluate an expression and return if it fails
   @param  bool   The (boolean) expression to evaluate, side-effects are allowed
   @note   A zero value of the expression is a failure, other values are not
   @return void
   @see cpl_test()
   @note  This macro should be used for unit tests that cannot continue after
          a failure.

  Example of usage:
    @code

    int main (void)
    {

        cpl_test_init(CPL_MSG_WARNING);

        cpl_test(myfunc(&p));
        cpl_test_assert(p != NULL);
        cpl_test(*p);

        return cpl_test_end(0);
    }
   
    @endcode
 */
/*----------------------------------------------------------------------------*/
#define cpl_test_assert(bool) do {                                      \
        const int cpl_test_errno = errno;                               \
        const cpl_flops cpl_test_flops = cpl_test_get_flops();          \
        const double cpl_test_time = cpl_test_get_walltime();           \
        cpl_errorstate cpl_test_state = cpl_errorstate_get();           \
        /* Evaluate bool just once */                                   \
        const cpl_boolean cpl_test_ok = (bool) ? CPL_TRUE : CPL_FALSE;  \
        cpl_test_macro(cpl_test_errno, cpl_test_time, cpl_test_flops,   \
                       cpl_test_state, cpl_test_ok, CPL_TRUE, #bool,    \
                       cpl_func, __FILE__, __LINE__);                   \
        if (cpl_test_ok == CPL_FALSE) return cpl_test_end(0);           \
    } while (0)


/* Deprecated. Do not use */
#define cpl_assert cpl_test_assert

#ifndef cpl_error_margin
/* Deprecated. Do not use */
#define cpl_error_margin 2.0
#endif

typedef unsigned long long int cpl_flops;

/*-----------------------------------------------------------------------------
                            Function prototypes
 -----------------------------------------------------------------------------*/

void cpl_test_init_macro(const char *, const char *,
                         cpl_msg_severity) CPL_ATTR_NONNULL;
int cpl_test_end(cpl_size);
void cpl_test_macro(int, double, cpl_flops, cpl_errorstate, cpl_size,
                    cpl_boolean, const char *, const char *,
                    const char *, unsigned) CPL_ATTR_NONNULL;

void cpl_test_errorstate_macro(int, double, cpl_flops, cpl_errorstate,
                               cpl_errorstate, const char *, const char *,
                               const char *, unsigned) CPL_ATTR_NONNULL;

void cpl_test_error_macro(int, double, cpl_flops, cpl_errorstate,
                          cpl_error_code, const char *, const char *,
                          const char *, unsigned) CPL_ATTR_NONNULL;

void cpl_test_eq_error_macro(int, double, cpl_flops, cpl_errorstate,
                             cpl_error_code, const char *, cpl_error_code,
                             const char *, const char *,
                             const char *, unsigned) CPL_ATTR_NONNULL;

void cpl_test_null_macro(int, double, cpl_flops, cpl_errorstate, const void *,
                         const char *, const char *, const char *, unsigned)
#ifdef CPL_HAVE_ATTR_NONNULL
    __attribute__((nonnull(6, 7, 8)))
#endif
    ;
void cpl_test_nonnull_macro(int, double, cpl_flops, cpl_errorstate,
                            const void *, const char *, const char *,
                            const char *, unsigned)
#ifdef CPL_HAVE_ATTR_NONNULL
    __attribute__((nonnull(6, 7, 8)))
#endif
    ;
void cpl_test_eq_macro(int, double, cpl_flops, cpl_errorstate, cpl_size,
                       const char *, cpl_size, const char *, const char *,
                       const char *, unsigned) CPL_ATTR_NONNULL;

void cpl_test_noneq_macro(int, double, cpl_flops, cpl_errorstate, cpl_size,
                          const char *, cpl_size, const char *, const char *,
                          const char *, unsigned) CPL_ATTR_NONNULL;

void cpl_test_eq_ptr_macro(int, double, cpl_flops, cpl_errorstate, const void *,
                           const char *, const void *, const char *,
                           const char *, const char *, unsigned)
#ifdef CPL_HAVE_ATTR_NONNULL
    __attribute__((nonnull(6, 8, 9, 10)))
#endif
    ;

void cpl_test_noneq_ptr_macro(int, double, cpl_flops, cpl_errorstate,
                              const void *, const char *, const void *,
                              const char *, const char *, const char *,
                              unsigned)
#ifdef CPL_HAVE_ATTR_NONNULL
    __attribute__((nonnull(6, 8, 9, 10)))
#endif
    ;

void cpl_test_eq_string_macro(int, double, cpl_flops, cpl_errorstate,
                              const char *, const char *, const char *,
                              const char *, const char *, const char *,
                              unsigned)
#ifdef CPL_HAVE_ATTR_NONNULL
    __attribute__((nonnull(6, 8, 9, 10)))
#endif
    ;

void cpl_test_noneq_string_macro(int, double, cpl_flops, cpl_errorstate,
                                 const char *, const char *, const char *,
                                 const char *, const char *, const char *,
                                 unsigned)
#ifdef CPL_HAVE_ATTR_NONNULL
    __attribute__((nonnull(6, 8, 9, 10)))
#endif
    ;

void cpl_test_fits_macro(int, double, cpl_flops, cpl_errorstate, const char *,
                         const char *, const char *, const char *, unsigned)
#ifdef CPL_HAVE_ATTR_NONNULL
    __attribute__((nonnull(6, 7, 8)))
#endif
    ;

void cpl_test_eq_mask_macro(int, double, cpl_flops, cpl_errorstate,
                            const cpl_mask *, const char *, const cpl_mask *,
                            const char *, const char *, const char *, unsigned)
#ifdef CPL_HAVE_ATTR_NONNULL
    __attribute__((nonnull(6, 8, 9, 10)))
#endif
    ;

void cpl_test_leq_macro(int, double, cpl_flops, cpl_errorstate, double,
                        const char *, double, const char *, const char *,
                        const char *, unsigned) CPL_ATTR_NONNULL;

void cpl_test_lt_macro(int, double, cpl_flops, cpl_errorstate, double,
                       const char *, double, const char *, const char *,
                       const char *, unsigned) CPL_ATTR_NONNULL;

void cpl_test_abs_macro(int, double, cpl_flops, cpl_errorstate, double,
                        const char *, double, const char *, double,
                        const char *, const char *, const char *,
                        unsigned) CPL_ATTR_NONNULL;

#ifdef _Complex_I
void cpl_test_abs_complex_macro(int, double, cpl_flops, cpl_errorstate, 
                                double complex, const char *, 
                                double complex, const char *, double,
                                const char *, const char *, const char *,
                                unsigned) CPL_ATTR_NONNULL;
#endif

void cpl_test_rel_macro(int, double, cpl_flops, cpl_errorstate, double,
                        const char *, double, const char *, double,
                        const char *, const char *, const char *,
                        unsigned) CPL_ATTR_NONNULL;

void cpl_test_vector_abs_macro(int, double, cpl_flops, cpl_errorstate,
                               const cpl_vector *, const char *,
                               const cpl_vector *, const char *,
                               double, const char *, const char *,
                               const char *, unsigned)
#ifdef CPL_HAVE_ATTR_NONNULL
    __attribute__((nonnull(6, 8, 10, 11, 12)))
#endif
    ;

void cpl_test_matrix_abs_macro(int, double, cpl_flops, cpl_errorstate,
                               const cpl_matrix *, const char *,
                               const cpl_matrix *, const char *,
                               double, const char *, const char *,
                               const char *, unsigned)
#ifdef CPL_HAVE_ATTR_NONNULL
    __attribute__((nonnull(6, 8, 10, 11, 12)))
#endif
    ;

void cpl_test_array_abs_macro(int, double, cpl_flops, cpl_errorstate,
                              const cpl_array *, const char *,
                              const cpl_array *, const char *, double,
                              const char *, const char *, const char *,
                              unsigned)
#ifdef CPL_HAVE_ATTR_NONNULL
    __attribute__((nonnull(6, 8, 10, 11, 12)))
#endif
    ;

void cpl_test_image_abs_macro(int, double, cpl_flops, cpl_errorstate,
                              const cpl_image *, const char *,
                              const cpl_image *, const char *, double,
                              const char *, const char *, const char *,
                              unsigned)
#ifdef CPL_HAVE_ATTR_NONNULL
    __attribute__((nonnull(6, 8, 10, 11, 12)))
#endif
    ;

void cpl_test_imagelist_abs_macro(int, double, cpl_flops, cpl_errorstate,
                                  const cpl_imagelist *,
                                  const char *,
                                  const cpl_imagelist *,
                                  const char *,
                                  double, const char *,
                                  const char *, const char *,
                                  unsigned )
#ifdef CPL_HAVE_ATTR_NONNULL
    __attribute__((nonnull(6, 8, 10, 11, 12)))
#endif
    ;

void cpl_test_polynomial_abs_macro(int, double, cpl_flops, cpl_errorstate,
                                   const cpl_polynomial *,
                                   const char *, const cpl_polynomial *,
                                   const char *, double, const char *,
                                   const char *, const char *, unsigned)
#ifdef CPL_HAVE_ATTR_NONNULL
    __attribute__((nonnull(6, 8, 10, 11, 12)))
#endif
    ;

void cpl_test_memory_is_empty_macro(int, double, cpl_flops, cpl_errorstate,
                                    const char *, const char *, unsigned)
    CPL_ATTR_NONNULL;

double cpl_test_get_cputime(void);
double cpl_test_get_walltime(void);

size_t cpl_test_get_bytes_vector(const cpl_vector *) CPL_ATTR_PURE;
size_t cpl_test_get_bytes_matrix(const cpl_matrix *) CPL_ATTR_PURE;
size_t cpl_test_get_bytes_image(const cpl_image *) CPL_ATTR_PURE;
size_t cpl_test_get_bytes_imagelist(const cpl_imagelist *) CPL_ATTR_PURE;

cpl_flops cpl_test_get_flops(void) CPL_ATTR_PURE;

CPL_END_DECLS

#endif