Import sendmail 8.13.7
[dragonfly.git] / contrib / sendmail-8.13.7 / libsm / test.c
blob361cc45576a140ae6391c88fb40f9508dc397092
1 /*
2 * Copyright (c) 2000-2002 Sendmail, Inc. and its suppliers.
3 * All rights reserved.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
8 */
10 #include <sm/gen.h>
11 SM_IDSTR(Id, "@(#)$Id: test.c,v 1.16 2002/01/08 17:54:40 ca Exp $")
14 ** Abstractions for writing libsm test programs.
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <sm/debug.h>
21 #include <sm/test.h>
23 extern char *optarg;
24 extern int optind;
25 extern int optopt;
26 extern int opterr;
28 int SmTestIndex;
29 int SmTestNumErrors;
30 bool SmTestVerbose;
32 static char Help[] = "\
33 %s [-h] [-d debugging] [-v]\n\
34 \n\
35 %s\n\
36 \n\
37 -h Display this help information.\n\
38 -d debugging Set debug activation levels.\n\
39 -v Verbose output.\n\
42 static char Usage[] = "\
43 Usage: %s [-h] [-v]\n\
44 Use %s -h for help.\n\
48 ** SM_TEST_BEGIN -- initialize test system.
50 ** Parameters:
51 ** argc -- argument counter.
52 ** argv -- argument vector.
53 ** testname -- description of tests.
55 ** Results:
56 ** none.
59 void
60 sm_test_begin(argc, argv, testname)
61 int argc;
62 char **argv;
63 char *testname;
65 int c;
67 SmTestIndex = 0;
68 SmTestNumErrors = 0;
69 SmTestVerbose = false;
70 opterr = 0;
72 while ((c = getopt(argc, argv, "vhd:")) != -1)
74 switch (c)
76 case 'v':
77 SmTestVerbose = true;
78 break;
79 case 'd':
80 sm_debug_addsettings_x(optarg);
81 break;
82 case 'h':
83 (void) fprintf(stdout, Help, argv[0], testname);
84 exit(0);
85 default:
86 (void) fprintf(stderr,
87 "Unknown command line option -%c\n",
88 optopt);
89 (void) fprintf(stderr, Usage, argv[0], argv[0]);
90 exit(1);
96 ** SM_TEST -- single test.
98 ** Parameters:
99 ** success -- did test succeeed?
100 ** expr -- expression that has been evaluated.
101 ** filename -- guess...
102 ** lineno -- line number.
104 ** Results:
105 ** value of success.
108 bool
109 sm_test(success, expr, filename, lineno)
110 bool success;
111 char *expr;
112 char *filename;
113 int lineno;
115 ++SmTestIndex;
116 if (SmTestVerbose)
117 (void) fprintf(stderr, "%d..", SmTestIndex);
118 if (!success)
120 ++SmTestNumErrors;
121 if (!SmTestVerbose)
122 (void) fprintf(stderr, "%d..", SmTestIndex);
123 (void) fprintf(stderr, "bad! %s:%d %s\n", filename, lineno,
124 expr);
126 else
128 if (SmTestVerbose)
129 (void) fprintf(stderr, "ok\n");
131 return success;
135 ** SM_TEST_END -- end of test system.
137 ** Parameters:
138 ** none.
140 ** Results:
141 ** number of errors.
145 sm_test_end()
147 (void) fprintf(stderr, "%d of %d tests completed successfully\n",
148 SmTestIndex - SmTestNumErrors, SmTestIndex);
149 if (SmTestNumErrors != 0)
150 (void) fprintf(stderr, "*** %d error%s in test! ***\n",
151 SmTestNumErrors,
152 SmTestNumErrors > 1 ? "s" : "");
154 return SmTestNumErrors;