xref: /titanic_51/usr/src/cmd/sendmail/libsm/test.c (revision bbaa8b60dd95d714741fc474adad3cf710ef4efd)
1 /*
2  * Copyright (c) 2000-2002 Sendmail, Inc. and its suppliers.
3  *	All rights reserved.
4  *
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  */
9 
10 #pragma ident	"%Z%%M%	%I%	%E% SMI"
11 
12 #include <sm/gen.h>
13 SM_IDSTR(Id, "@(#)$Id: test.c,v 1.16 2002/01/08 17:54:40 ca Exp $")
14 
15 /*
16 **  Abstractions for writing libsm test programs.
17 */
18 
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <sm/debug.h>
23 #include <sm/test.h>
24 
25 extern char *optarg;
26 extern int optind;
27 extern int optopt;
28 extern int opterr;
29 
30 int SmTestIndex;
31 int SmTestNumErrors;
32 bool SmTestVerbose;
33 
34 static char Help[] = "\
35 %s [-h] [-d debugging] [-v]\n\
36 \n\
37 %s\n\
38 \n\
39 -h		Display this help information.\n\
40 -d debugging	Set debug activation levels.\n\
41 -v		Verbose output.\n\
42 ";
43 
44 static char Usage[] = "\
45 Usage: %s [-h] [-v]\n\
46 Use %s -h for help.\n\
47 ";
48 
49 /*
50 **  SM_TEST_BEGIN -- initialize test system.
51 **
52 **	Parameters:
53 **		argc -- argument counter.
54 **		argv -- argument vector.
55 **		testname -- description of tests.
56 **
57 **	Results:
58 **		none.
59 */
60 
61 void
62 sm_test_begin(argc, argv, testname)
63 	int argc;
64 	char **argv;
65 	char *testname;
66 {
67 	int c;
68 
69 	SmTestIndex = 0;
70 	SmTestNumErrors = 0;
71 	SmTestVerbose = false;
72 	opterr = 0;
73 
74 	while ((c = getopt(argc, argv, "vhd:")) != -1)
75 	{
76 		switch (c)
77 		{
78 		  case 'v':
79 			SmTestVerbose = true;
80 			break;
81 		  case 'd':
82 			sm_debug_addsettings_x(optarg);
83 			break;
84 		  case 'h':
85 			(void) fprintf(stdout, Help, argv[0], testname);
86 			exit(0);
87 		  default:
88 			(void) fprintf(stderr,
89 					"Unknown command line option -%c\n",
90 					optopt);
91 			(void) fprintf(stderr, Usage, argv[0], argv[0]);
92 			exit(1);
93 		}
94 	}
95 }
96 
97 /*
98 **  SM_TEST -- single test.
99 **
100 **	Parameters:
101 **		success -- did test succeeed?
102 **		expr -- expression that has been evaluated.
103 **		filename -- guess...
104 **		lineno -- line number.
105 **
106 **	Results:
107 **		value of success.
108 */
109 
110 bool
111 sm_test(success, expr, filename, lineno)
112 	bool success;
113 	char *expr;
114 	char *filename;
115 	int lineno;
116 {
117 	++SmTestIndex;
118 	if (SmTestVerbose)
119 		(void) fprintf(stderr, "%d..", SmTestIndex);
120 	if (!success)
121 	{
122 		++SmTestNumErrors;
123 		if (!SmTestVerbose)
124 			(void) fprintf(stderr, "%d..", SmTestIndex);
125 		(void) fprintf(stderr, "bad! %s:%d %s\n", filename, lineno,
126 				expr);
127 	}
128 	else
129 	{
130 		if (SmTestVerbose)
131 			(void) fprintf(stderr, "ok\n");
132 	}
133 	return success;
134 }
135 
136 /*
137 **  SM_TEST_END -- end of test system.
138 **
139 **	Parameters:
140 **		none.
141 **
142 **	Results:
143 **		number of errors.
144 */
145 
146 int
147 sm_test_end()
148 {
149 	(void) fprintf(stderr, "%d of %d tests completed successfully\n",
150 			SmTestIndex - SmTestNumErrors, SmTestIndex);
151 	if (SmTestNumErrors != 0)
152 		(void) fprintf(stderr, "*** %d error%s in test! ***\n",
153 				SmTestNumErrors,
154 				SmTestNumErrors > 1 ? "s" : "");
155 
156 	return SmTestNumErrors;
157 }
158