1 /* This is part of the netCDF package.
2 Copyright 2005 University Corporation for Atmospheric Research/Unidata
3 See COPYRIGHT file for conditions of use.
4
5 Common includes, defines, etc., for test code in the libsrc4 and
6 nc_test4 directories.
7
8 Ed Hartnett, Russ Rew, Dennis Heimbigner
9 */
10
11 #ifndef _ERR_MACROS_H
12 #define _ERR_MACROS_H
13
14 #include "config.h"
15 #include <assert.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19
20 /* Err is used to keep track of errors within each set of tests,
21 * total_err is the number of errors in the entire test program, which
22 * generally cosists of several sets of tests. */
23 static int total_err = 0, err = 0;
24
25 /* This macro prints an error message with line number and name of
26 * test program. */
27 #define ERR do { \
28 fflush(stdout); /* Make sure our stdout is synced with stderr. */ \
29 err++; \
30 fprintf(stderr, "Sorry! Unexpected result, %s, line: %d\n", \
31 __FILE__, __LINE__); \
32 fflush(stderr); \
33 return 2; \
34 } while (0)
35
36 /* Duplicate with different name. */
37 #define ERR_RET ERR
38
39 #define ERR_GOTO do { \
40 fflush(stdout); /* Make sure our stdout is synced with stderr. */ \
41 fprintf(stderr, "Sorry! Unexpected result, %s, line: %d\n", \
42 __FILE__, __LINE__); \
43 goto error; \
44 } while (0)
45
ERR_report(int stat,const char * file,int line)46 int ERR_report(int stat, const char* file, int line)
47 {
48 fflush(stdout);
49 fprintf(stderr, "Sorry! Unexpected result, %s, line: %d; status=%d\n",
50 file,line,stat);
51 fflush(stderr);
52 return 1;
53 }
54
55 #define ERRSTAT(stat) {err+=ERR_report(stat,__FILE__,__LINE__);}
56
57 /* After a set of tests, report the number of errors, and increment
58 * total_err. */
59 #define SUMMARIZE_ERR do { \
60 if (err) \
61 { \
62 printf("%d failures\n", err); \
63 total_err += err; \
64 err = 0; \
65 } \
66 else \
67 printf("ok.\n"); \
68 } while (0)
69
70 /* This macro prints out our total number of errors, if any, and exits
71 * with a 0 if there are not, or a 2 if there were errors. Make will
72 * stop if a non-zero value is returned from a test program. */
73 #define FINAL_RESULTS do { \
74 if (total_err) \
75 { \
76 printf("%d errors detected! Sorry!\n", total_err); \
77 return 2; \
78 } \
79 printf("*** Tests successful!\n"); \
80 return 0; \
81 } while (0)
82
83 /* This macro does the same as FINAL_RESULTS, but without the success
84 * message. */
85 #define FINAL_RESULTS_QUIET do { \
86 if (total_err) \
87 { \
88 printf("%d errors detected! Sorry!\n", total_err); \
89 return 2; \
90 } \
91 return 0; \
92 } while (0)
93
94 #endif /* _ERR_MACROS_H */
95