xref: /freebsd/tools/regression/netinet6/inet6_rth/test_subr.c (revision 02e9120893770924227138ba49df1edb3896112a)
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/types.h>
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include "test_subr.h"
35 
36 int g_total;
37 int g_pass;
38 int g_fail;
39 char g_funcname[FUNCNAMESIZE];
40 char g_testdesc[LINESIZE];
41 char g_errbuf[LINESIZE];
42 
43 void
44 set_funcname(char *bufp, size_t bufsize)
45 {
46 	strlcpy(g_funcname, bufp,
47 	    bufsize < FUNCNAMESIZE ? bufsize : FUNCNAMESIZE);
48 }
49 
50 /*
51  * desc is a NULL-terminated string.
52  */
53 void
54 checkptr(caddr_t expected, caddr_t got, const char *desc)
55 {
56 	int  len;
57 	int  failed;
58 	char sbuf[LINESIZE];
59 
60 	memset((void *)sbuf, 0, LINESIZE);
61 	snprintf(g_testdesc, LINESIZE, desc);
62 
63 	failed = 1;
64 	g_total++;
65 	if (got == expected) {
66 		len = snprintf(sbuf, LINESIZE, "ok");
67 		g_pass++;
68 		failed = 0;
69 	} else {
70 		len = snprintf(sbuf, LINESIZE, "not ok");
71 		snprintf(g_errbuf, LINESIZE, " : Expected %#x, but got %#x",
72 		    (unsigned int)expected, (unsigned int)got);
73 		g_fail++;
74 	}
75 	snprintf(sbuf + len, LINESIZE - len, " %d - %s (%s)",
76 	    g_total, g_funcname, g_testdesc);
77 	printf(sbuf);
78 	if (failed)
79 		printf(g_errbuf);
80 	printf("\n");
81 	fflush(NULL);
82 	memset((void *)g_errbuf, 0, LINESIZE);
83 	memset((void *)g_testdesc, 0, LINESIZE);
84 }
85 
86 void
87 checkstr(const char *expected, const char *got, size_t explen, const char *desc)
88 {
89 	int  len;
90 	int  failed;
91 	char sbuf[LINESIZE];
92 
93 	memset((void *)sbuf, 0, LINESIZE);
94 	snprintf(g_testdesc, LINESIZE, desc);
95 
96 	failed = 1;
97 	g_total++;
98 	if (strncmp(expected, got, explen) == 0) {
99 		len = snprintf(sbuf, LINESIZE, "ok");
100 		g_pass++;
101 		failed = 0;
102 	} else {
103 		len = snprintf(sbuf, LINESIZE, "not ok");
104 		snprintf(g_errbuf, LINESIZE,
105 		    " : Expected %s, but got %s", expected, got);
106 		g_fail++;
107 	}
108 	snprintf(sbuf + len, LINESIZE - len, " %d - %s (%s)",
109 	    g_total, g_funcname, g_testdesc);
110 	printf(sbuf);
111 	if (failed)
112 		printf(g_errbuf);
113 	printf("\n");
114 	fflush(NULL);
115 	memset((void *)g_errbuf, 0, LINESIZE);
116 	memset((void *)g_testdesc, 0, LINESIZE);
117 }
118 
119 void
120 checknum(int expected, int got, int cmp, const char *desc)
121 {
122 	int  len;
123 	int  pass;
124 	int  failed;
125 	char sbuf[LINESIZE];
126 
127 	memset((void *)sbuf, 0, LINESIZE);
128 	snprintf(g_testdesc, LINESIZE, desc);
129 
130 	failed = 1;
131 	pass = 0;
132 	g_total++;
133 	switch(cmp) {
134 	case 0:
135 		pass = (got == expected) ? 1 : 0;
136 		break;
137 	case 1:
138 		pass = (got > expected) ? 1 : 0;
139 		break;
140 	case -1:
141 		pass = (got < expected) ? 1 : 0;
142 		break;
143 	}
144 	if (pass != 0) {
145 		len = snprintf(sbuf, LINESIZE, "ok");
146 		g_pass++;
147 		failed = 0;
148 	} else {
149 		len = snprintf(sbuf, LINESIZE, "not ok");
150 		snprintf(g_errbuf, LINESIZE,
151 		    " : Expected %d, but got %d", expected, got);
152 		g_fail++;
153 	}
154 	snprintf(sbuf + len, LINESIZE - len, " %d - %s (%s)",
155 	    g_total, g_funcname, g_testdesc);
156 	printf(sbuf);
157 	if (failed)
158 		printf(g_errbuf);
159 	printf("\n");
160 	fflush(NULL);
161 	memset((void *)g_errbuf, 0, LINESIZE);
162 	memset((void *)g_testdesc, 0, LINESIZE);
163 }
164