xref: /freebsd/tools/regression/netinet6/inet6_rth/test_subr.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * Copyright (c) 2007 Michael Telahun Makonnen
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 #include <sys/types.h>
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 #include "test_subr.h"
36 
37 int g_total;
38 int g_pass;
39 int g_fail;
40 char g_funcname[FUNCNAMESIZE];
41 char g_testdesc[LINESIZE];
42 char g_errbuf[LINESIZE];
43 
44 void
45 set_funcname(char *bufp, size_t bufsize)
46 {
47 	strlcpy(g_funcname, bufp,
48 	    bufsize < FUNCNAMESIZE ? bufsize : FUNCNAMESIZE);
49 }
50 
51 /*
52  * desc is a NULL-terminated string.
53  */
54 void
55 checkptr(caddr_t expected, caddr_t got, const char *desc)
56 {
57 	int  len;
58 	int  failed;
59 	char sbuf[LINESIZE];
60 
61 	memset((void *)sbuf, 0, LINESIZE);
62 	snprintf(g_testdesc, LINESIZE, desc);
63 
64 	failed = 1;
65 	g_total++;
66 	if (got == expected) {
67 		len = snprintf(sbuf, LINESIZE, "ok");
68 		g_pass++;
69 		failed = 0;
70 	} else {
71 		len = snprintf(sbuf, LINESIZE, "not ok");
72 		snprintf(g_errbuf, LINESIZE, " : Expected %#x, but got %#x",
73 		    (unsigned int)expected, (unsigned int)got);
74 		g_fail++;
75 	}
76 	snprintf(sbuf + len, LINESIZE - len, " %d - %s (%s)",
77 	    g_total, g_funcname, g_testdesc);
78 	printf(sbuf);
79 	if (failed)
80 		printf(g_errbuf);
81 	printf("\n");
82 	fflush(NULL);
83 	memset((void *)g_errbuf, 0, LINESIZE);
84 	memset((void *)g_testdesc, 0, LINESIZE);
85 }
86 
87 void
88 checkstr(const char *expected, const char *got, size_t explen, const char *desc)
89 {
90 	int  len;
91 	int  failed;
92 	char sbuf[LINESIZE];
93 
94 	memset((void *)sbuf, 0, LINESIZE);
95 	snprintf(g_testdesc, LINESIZE, desc);
96 
97 	failed = 1;
98 	g_total++;
99 	if (strncmp(expected, got, explen) == 0) {
100 		len = snprintf(sbuf, LINESIZE, "ok");
101 		g_pass++;
102 		failed = 0;
103 	} else {
104 		len = snprintf(sbuf, LINESIZE, "not ok");
105 		snprintf(g_errbuf, LINESIZE,
106 		    " : Expected %s, but got %s", expected, got);
107 		g_fail++;
108 	}
109 	snprintf(sbuf + len, LINESIZE - len, " %d - %s (%s)",
110 	    g_total, g_funcname, g_testdesc);
111 	printf(sbuf);
112 	if (failed)
113 		printf(g_errbuf);
114 	printf("\n");
115 	fflush(NULL);
116 	memset((void *)g_errbuf, 0, LINESIZE);
117 	memset((void *)g_testdesc, 0, LINESIZE);
118 }
119 
120 void
121 checknum(int expected, int got, int cmp, const char *desc)
122 {
123 	int  len;
124 	int  pass;
125 	int  failed;
126 	char sbuf[LINESIZE];
127 
128 	memset((void *)sbuf, 0, LINESIZE);
129 	snprintf(g_testdesc, LINESIZE, desc);
130 
131 	failed = 1;
132 	pass = 0;
133 	g_total++;
134 	switch(cmp) {
135 	case 0:
136 		pass = (got == expected) ? 1 : 0;
137 		break;
138 	case 1:
139 		pass = (got > expected) ? 1 : 0;
140 		break;
141 	case -1:
142 		pass = (got < expected) ? 1 : 0;
143 		break;
144 	}
145 	if (pass != 0) {
146 		len = snprintf(sbuf, LINESIZE, "ok");
147 		g_pass++;
148 		failed = 0;
149 	} else {
150 		len = snprintf(sbuf, LINESIZE, "not ok");
151 		snprintf(g_errbuf, LINESIZE,
152 		    " : Expected %d, but got %d", expected, got);
153 		g_fail++;
154 	}
155 	snprintf(sbuf + len, LINESIZE - len, " %d - %s (%s)",
156 	    g_total, g_funcname, g_testdesc);
157 	printf(sbuf);
158 	if (failed)
159 		printf(g_errbuf);
160 	printf("\n");
161 	fflush(NULL);
162 	memset((void *)g_errbuf, 0, LINESIZE);
163 	memset((void *)g_testdesc, 0, LINESIZE);
164 }
165