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