*** empty log message ***
[arla.git] / util / util-tester.c
blob1adb53a841b428ad3f16af78d072e41daab5e6c0
1 /*
2 * Copyright (c) 1998 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
38 #include <roken.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <sys/time.h>
45 #include <parse_units.h>
47 #include "bool.h"
48 #include "hash.h"
49 #include "log.h"
50 #include "arlamath.h"
52 struct timeval time1, time2;
54 static void
55 starttesting(char *msg)
57 printf("--------------------------------------------\n");
58 printf("testing %s...\n", msg);
59 fflush (stdout);
60 gettimeofday(&time1, NULL);
63 static int
64 endtesting(int bool)
66 gettimeofday(&time2, NULL);
67 printf("-%s--------------------------------------\n",
68 !bool ? "ok --" : "fail ");
69 time2.tv_usec -= time1.tv_usec;
70 if (time2.tv_usec < 0) {
71 time2.tv_usec += 10000;
72 --time2.tv_sec;
74 time2.tv_sec -= time1.tv_sec;
75 printf("timing: %ld.%ld\n", (long)time2.tv_sec, (long)time2.tv_usec);
77 return bool;
81 static int
82 hash_cmp(void *foo, void *bar)
84 return strcmp((char *) foo, (char *)bar);
87 static unsigned
88 hash_hash(void *foo)
90 return hashcaseadd((char *) foo);
93 static Bool
94 hash_print(void *foo, void *bar)
96 printf("%s\n", (char *) foo);
97 return FALSE;
100 static int
101 test_hash(void)
103 Hashtab *h;
105 starttesting("hashtab");
107 h = hashtabnew(100, hash_cmp, hash_hash);
108 if (!h)
109 return endtesting(1);
111 if (!hashtabadd(h, "one")||
112 !hashtabadd(h, "two")||
113 !hashtabadd(h, "three")||
114 !hashtabadd(h, "four"))
115 return endtesting(1);
117 printf("foreach ----\n");
118 hashtabforeach(h, hash_print, NULL);
120 printf("search ----\none == %s\ntwo == %s\nthree == %s\nfour == %s\n",
121 (char *)hashtabsearch(h, "one"),
122 (char *)hashtabsearch(h, "two"),
123 (char *)hashtabsearch(h, "three"),
124 (char *)hashtabsearch(h, "four"));
126 hashtabrelease(h);
128 return endtesting(0);
131 struct units u1_units[] = {
132 { "all", 0xff },
133 { "u1-hack", 0x04 },
134 { "warning", 0x02 },
135 { "debug", 0x01 },
136 { NULL, 0 }
139 struct units u2_units[] = {
140 { "all", 0xff },
141 { "u2-hack2", 0x08 },
142 { "u2-hack1", 0x04 },
143 { "warning", 0x02 },
144 { "debug", 0x01 },
145 { NULL, 0 }
148 static int
149 test_log (void)
151 Log_method *m;
152 Log_unit *u1, *u2;
153 char buf[1024];
155 starttesting ("log");
157 m = log_open ("util-tester", "/dev/stderr:notime");
158 if (m == NULL)
159 return endtesting(1);
161 u1 = log_unit_init (m, "u1", u1_units, 0x3);
162 if (u1 == NULL)
163 return endtesting(1);
165 u2 = log_unit_init (m, "u2", u2_units, 0x0);
166 if (u2 == NULL)
167 return endtesting(1);
169 log_mask2str (m, NULL, buf, sizeof(buf));
170 printf ("%s\n", buf); fflush (stdout);
171 log_set_mask_str (m, NULL, buf);
172 log_log (u1, 0x1, "1. this should show");
173 log_log (u2, 0x1, "X. this should NOT show");
175 log_set_mask_str (m, NULL, "u1:-debug;u2:+debug");
176 log_log (u1, 0x1, "X. now this should NOT show");
177 log_log (u2, 0x1, "2. now this should show");
178 log_mask2str (m, NULL, buf, sizeof(buf));
179 printf ("%s\n", buf); fflush (stdout);
180 log_set_mask_str (m, NULL, buf);
182 log_set_mask_str (m, NULL, "u1:-debug;u2:-debug");
183 log_log (u1, 0x1, "X. now this should NOT show");
184 log_log (u2, 0x1, "X. now this should NOT show");
185 log_mask2str (m, NULL, buf, sizeof(buf));
186 printf ("%s\n", buf); fflush (stdout);
187 log_set_mask_str (m, NULL, buf);
189 log_set_mask_str (m, NULL, "+debug");
190 log_log (u1, 0x1, "3. now this should show");
191 log_log (u2, 0x1, "4. now this should show");
192 log_mask2str (m, NULL, buf, sizeof(buf));
193 printf ("%s\n", buf); fflush (stdout);
194 log_set_mask_str (m, NULL, buf);
196 log_set_mask_str (m, NULL, "-debug");
197 log_log (u1, 0x1, "X. now this should NOT show");
198 log_log (u2, 0x1, "X. now this should NOT show");
199 log_mask2str (m, NULL, buf, sizeof(buf));
200 printf ("%s\n", buf); fflush (stdout);
201 log_set_mask_str (m, NULL, buf);
203 log_set_mask_str (m, NULL, "+debug,+warning");
204 log_log (u1, 0x1, "5. now this should show");
205 log_log (u2, 0x1, "6. now this should show");
206 log_log (u1, 0x2, "7. now this should show");
207 log_log (u2, 0x2, "8. now this should show");
208 log_mask2str (m, NULL, buf, sizeof(buf));
209 printf ("%s\n", buf); fflush (stdout);
210 log_set_mask_str (m, NULL, buf);
212 log_set_mask_str (m, u1, "-debug,-warning");
213 log_log (u1, 0x1, "X. now this should NOT show");
214 log_log (u2, 0x1, "9. now this should show");
215 log_log (u1, 0x2, "X. now this should NOT show");
216 log_log (u2, 0x2, "10. now this should show");
218 log_mask2str (m, NULL, buf, sizeof(buf));
219 printf ("%s\n", buf); fflush (stdout);
220 log_set_mask_str (m, NULL, buf);
222 log_set_mask (u1, 0x4 + 0x2 + 0x1);
223 log_set_mask (u2, 0x8 + 0x4 + 0x2 + 0x1);
225 log_mask2str (m, NULL, buf, sizeof(buf));
226 printf ("%s\n", buf); fflush (stdout);
227 log_set_mask_str (m, NULL, buf);
229 log_set_mask_str (m, NULL, "all");
230 log_mask2str (m, NULL, buf, sizeof(buf));
231 printf ("all: %s\n", buf); fflush (stdout);
233 log_set_mask_str (m, NULL, "-all");
234 log_mask2str (m, NULL, buf, sizeof(buf));
235 printf ("none: %s\n", buf); fflush (stdout);
238 log_close (m);
239 return endtesting (0);
242 static int
243 test_math (void)
245 starttesting ("math");
247 if (arlautil_findprime(17) != 17)
248 return endtesting (1);
249 if (arlautil_findprime(18) != 19)
250 return endtesting (1);
251 if (arlautil_findprime(11412) != 11423)
252 return endtesting (1);
254 if (arlautil_findprime(11412) != 11423)
255 return endtesting (1);
257 if (arlautil_isprime(20897) == 0)
258 return endtesting (1);
259 if (arlautil_isprime(49037) == 0)
260 return endtesting (1);
262 return endtesting (0);
265 int
266 main(int argc, char **argv)
268 int ret = 0;
269 ret |= test_hash();
270 ret |= test_log();
271 ret |= test_math();
272 return ret;