blob: b44c4bfe163977fa34b0158b8ae0be8d6c5211ac [file] [log] [blame]
njn221666f2009-02-20 06:37:52 +00001// This module does unit testing of m_libcbase.
2
3#include <assert.h>
4#include <stdio.h>
5#include <stdlib.h>
6
njn132fdb02009-02-26 03:52:35 +00007#include "pub_tool_vki.h"
8#include "m_libcbase.c"
njn221666f2009-02-20 06:37:52 +00009
njn2a5a76b2009-02-23 04:16:56 +000010#define CHECK(x) \
11 if (!x) { fprintf(stderr, "failure: %s:%d\n", __FILE__, __LINE__); }
njn221666f2009-02-20 06:37:52 +000012
njn132fdb02009-02-26 03:52:35 +000013void test_VG_STREQ(void)
14{
15 CHECK( ! VG_STREQ(NULL, NULL) ); // Nb: strcmp() considers these equal
16 CHECK( ! VG_STREQ(NULL, "ab") ); // Nb: strcmp() seg faults on this
17 CHECK( ! VG_STREQ("ab", NULL) ); // Nb: strcmp() seg faults on this
18 CHECK( ! VG_STREQ("", "a") );
19 CHECK( ! VG_STREQ("a", "") );
20 CHECK( ! VG_STREQ("abc", "abcd"));
21 CHECK( ! VG_STREQ("abcd", "abc") );
22 CHECK( ! VG_STREQ("Abcd", "abcd"));
23 CHECK( ! VG_STREQ("abcd", "Abcd"));
24
25 CHECK( VG_STREQ("", "") );
26 CHECK( VG_STREQ("a", "a") );
27 CHECK( VG_STREQ("abcd", "abcd") );
28}
29
30void test_VG_STREQN(void)
31{
32 CHECK( ! VG_STREQN(0, NULL, NULL) );
33 CHECK( ! VG_STREQN(5, NULL, NULL) );
34 CHECK( ! VG_STREQN(0, NULL, "ab") );
35 CHECK( ! VG_STREQN(5, NULL, "ab") );
36 CHECK( ! VG_STREQN(0, "ab", NULL) );
37 CHECK( ! VG_STREQN(1, "", "a") );
38 CHECK( ! VG_STREQN(1, "a", "") );
39 CHECK( ! VG_STREQN(4, "abc", "abcd"));
40 CHECK( ! VG_STREQN(4, "abcd", "abc") );
41 CHECK( ! VG_STREQN(1, "Abcd", "abcd"));
42 CHECK( ! VG_STREQN(4, "Abcd", "abcd"));
43 CHECK( ! VG_STREQN(4, "abcd", "abce"));
44 CHECK( ! VG_STREQN(9, "abcd", "abce"));
45
46 CHECK( VG_STREQN(0, "", "") );
47 CHECK( VG_STREQN(1, "", "") );
48 CHECK( VG_STREQN(0, "a", "a") );
49 CHECK( VG_STREQN(1, "a", "a") );
50 CHECK( VG_STREQN(2, "a", "a") );
51 CHECK( VG_STREQN(9, "a", "a") );
52 CHECK( VG_STREQN(1, "ab", "ac"));
53 CHECK( VG_STREQN(3, "abcd", "abce"));
54}
55
56void test_VG_IS_XXX_ALIGNED(void)
57{
58 CHECK( VG_IS_2_ALIGNED(0x0) );
59 CHECK( ! VG_IS_2_ALIGNED(0x1) );
60 CHECK( VG_IS_2_ALIGNED(0x2) );
61 CHECK( ! VG_IS_2_ALIGNED(0x3) );
62 CHECK( VG_IS_2_ALIGNED(0x4) );
63 CHECK( ! VG_IS_2_ALIGNED(0x5) );
64 CHECK( VG_IS_2_ALIGNED(0x6) );
65 CHECK( ! VG_IS_2_ALIGNED(0x7) );
66 CHECK( VG_IS_2_ALIGNED(0x8) );
67 CHECK( ! VG_IS_2_ALIGNED(0x9) );
68 CHECK( VG_IS_2_ALIGNED(0xa) );
69 CHECK( ! VG_IS_2_ALIGNED(0xb) );
70 CHECK( VG_IS_2_ALIGNED(0xc) );
71 CHECK( ! VG_IS_2_ALIGNED(0xd) );
72 CHECK( VG_IS_2_ALIGNED(0xe) );
73 CHECK( ! VG_IS_2_ALIGNED(0xf) );
74
75 CHECK( VG_IS_4_ALIGNED(0x0) );
76 CHECK( ! VG_IS_4_ALIGNED(0x1) );
77 CHECK( ! VG_IS_4_ALIGNED(0x2) );
78 CHECK( ! VG_IS_4_ALIGNED(0x3) );
79 CHECK( VG_IS_4_ALIGNED(0x4) );
80 CHECK( ! VG_IS_4_ALIGNED(0x5) );
81 CHECK( ! VG_IS_4_ALIGNED(0x6) );
82 CHECK( ! VG_IS_4_ALIGNED(0x7) );
83 CHECK( VG_IS_4_ALIGNED(0x8) );
84 CHECK( ! VG_IS_4_ALIGNED(0x9) );
85 CHECK( ! VG_IS_4_ALIGNED(0xa) );
86 CHECK( ! VG_IS_4_ALIGNED(0xb) );
87 CHECK( VG_IS_4_ALIGNED(0xc) );
88 CHECK( ! VG_IS_4_ALIGNED(0xd) );
89 CHECK( ! VG_IS_4_ALIGNED(0xe) );
90 CHECK( ! VG_IS_4_ALIGNED(0xf) );
91
92 CHECK( VG_IS_8_ALIGNED(0x0) );
93 CHECK( ! VG_IS_8_ALIGNED(0x1) );
94 CHECK( ! VG_IS_8_ALIGNED(0x2) );
95 CHECK( ! VG_IS_8_ALIGNED(0x3) );
96 CHECK( ! VG_IS_8_ALIGNED(0x4) );
97 CHECK( ! VG_IS_8_ALIGNED(0x5) );
98 CHECK( ! VG_IS_8_ALIGNED(0x6) );
99 CHECK( ! VG_IS_8_ALIGNED(0x7) );
100 CHECK( VG_IS_8_ALIGNED(0x8) );
101 CHECK( ! VG_IS_8_ALIGNED(0x9) );
102 CHECK( ! VG_IS_8_ALIGNED(0xa) );
103 CHECK( ! VG_IS_8_ALIGNED(0xb) );
104 CHECK( ! VG_IS_8_ALIGNED(0xc) );
105 CHECK( ! VG_IS_8_ALIGNED(0xd) );
106 CHECK( ! VG_IS_8_ALIGNED(0xe) );
107 CHECK( ! VG_IS_8_ALIGNED(0xf) );
108
109 CHECK( VG_IS_16_ALIGNED(0x0) );
110 CHECK( ! VG_IS_16_ALIGNED(0x1) );
111 CHECK( ! VG_IS_16_ALIGNED(0x2) );
112 CHECK( ! VG_IS_16_ALIGNED(0x3) );
113 CHECK( ! VG_IS_16_ALIGNED(0x4) );
114 CHECK( ! VG_IS_16_ALIGNED(0x5) );
115 CHECK( ! VG_IS_16_ALIGNED(0x6) );
116 CHECK( ! VG_IS_16_ALIGNED(0x7) );
117 CHECK( ! VG_IS_16_ALIGNED(0x8) );
118 CHECK( ! VG_IS_16_ALIGNED(0x9) );
119 CHECK( ! VG_IS_16_ALIGNED(0xa) );
120 CHECK( ! VG_IS_16_ALIGNED(0xb) );
121 CHECK( ! VG_IS_16_ALIGNED(0xc) );
122 CHECK( ! VG_IS_16_ALIGNED(0xd) );
123 CHECK( ! VG_IS_16_ALIGNED(0xe) );
124 CHECK( ! VG_IS_16_ALIGNED(0xf) );
125
126 CHECK( VG_IS_WORD_ALIGNED(0x0) );
127 CHECK( ! VG_IS_WORD_ALIGNED(0x1) );
128 CHECK( ! VG_IS_WORD_ALIGNED(0x2) );
129 CHECK( ! VG_IS_WORD_ALIGNED(0x3) );
130 // 0x4 case below
131 CHECK( ! VG_IS_WORD_ALIGNED(0x5) );
132 CHECK( ! VG_IS_WORD_ALIGNED(0x6) );
133 CHECK( ! VG_IS_WORD_ALIGNED(0x7) );
134 CHECK( VG_IS_WORD_ALIGNED(0x8) );
135 CHECK( ! VG_IS_WORD_ALIGNED(0x9) );
136 CHECK( ! VG_IS_WORD_ALIGNED(0xa) );
137 CHECK( ! VG_IS_WORD_ALIGNED(0xb) );
138 // 0xc case below
139 CHECK( ! VG_IS_WORD_ALIGNED(0xd) );
140 CHECK( ! VG_IS_WORD_ALIGNED(0xe) );
141 CHECK( ! VG_IS_WORD_ALIGNED(0xf) );
142 if (4 == sizeof(void*)) {
143 CHECK( VG_IS_WORD_ALIGNED(0x4) );
144 CHECK( VG_IS_WORD_ALIGNED(0xc) );
145 } else if (8 == sizeof(void*)) {
146 CHECK( ! VG_IS_WORD_ALIGNED(0x4) );
147 CHECK( ! VG_IS_WORD_ALIGNED(0xc) );
148 } else {
149 assert(0);
150 }
151
152 CHECK( VG_IS_PAGE_ALIGNED(0x0) );
153 CHECK( ! VG_IS_PAGE_ALIGNED(0x1) );
154 CHECK( ! VG_IS_PAGE_ALIGNED(0x2) );
155 CHECK( ! VG_IS_PAGE_ALIGNED(0x3) );
156 CHECK( ! VG_IS_PAGE_ALIGNED(0x4) );
157 CHECK( ! VG_IS_PAGE_ALIGNED(VKI_PAGE_SIZE-1) );
158 CHECK( VG_IS_PAGE_ALIGNED(VKI_PAGE_SIZE ) );
159 CHECK( ! VG_IS_PAGE_ALIGNED(VKI_PAGE_SIZE+1) );
160}
161
162void test_VG_ROUND_et_al()
163{
164 CHECK( 0 == VG_ROUNDDN(0, 1) );
165 CHECK( 1 == VG_ROUNDDN(1, 1) );
166 CHECK( 2 == VG_ROUNDDN(2, 1) );
167 CHECK( 3 == VG_ROUNDDN(3, 1) );
168 CHECK( 4 == VG_ROUNDDN(4, 1) );
169 CHECK( 5 == VG_ROUNDDN(5, 1) );
170 CHECK( 6 == VG_ROUNDDN(6, 1) );
171 CHECK( 7 == VG_ROUNDDN(7, 1) );
172
173 CHECK( 0 == VG_ROUNDUP(0, 1) );
174 CHECK( 1 == VG_ROUNDUP(1, 1) );
175 CHECK( 2 == VG_ROUNDUP(2, 1) );
176 CHECK( 3 == VG_ROUNDUP(3, 1) );
177 CHECK( 4 == VG_ROUNDUP(4, 1) );
178 CHECK( 5 == VG_ROUNDUP(5, 1) );
179 CHECK( 6 == VG_ROUNDUP(6, 1) );
180 CHECK( 7 == VG_ROUNDUP(7, 1) );
181
182 CHECK( 0 == VG_ROUNDDN(0, 2) );
183 CHECK( 0 == VG_ROUNDDN(1, 2) );
184 CHECK( 2 == VG_ROUNDDN(2, 2) );
185 CHECK( 2 == VG_ROUNDDN(3, 2) );
186 CHECK( 4 == VG_ROUNDDN(4, 2) );
187 CHECK( 4 == VG_ROUNDDN(5, 2) );
188 CHECK( 6 == VG_ROUNDDN(6, 2) );
189 CHECK( 6 == VG_ROUNDDN(7, 2) );
190
191 CHECK( 0 == VG_ROUNDUP(0, 2) );
192 CHECK( 2 == VG_ROUNDUP(1, 2) );
193 CHECK( 2 == VG_ROUNDUP(2, 2) );
194 CHECK( 4 == VG_ROUNDUP(3, 2) );
195 CHECK( 4 == VG_ROUNDUP(4, 2) );
196 CHECK( 6 == VG_ROUNDUP(5, 2) );
197 CHECK( 6 == VG_ROUNDUP(6, 2) );
198 CHECK( 8 == VG_ROUNDUP(7, 2) );
199
200 CHECK( 0 == VG_ROUNDDN(0, 4) );
201 CHECK( 0 == VG_ROUNDDN(1, 4) );
202 CHECK( 0 == VG_ROUNDDN(2, 4) );
203 CHECK( 0 == VG_ROUNDDN(3, 4) );
204 CHECK( 4 == VG_ROUNDDN(4, 4) );
205 CHECK( 4 == VG_ROUNDDN(5, 4) );
206 CHECK( 4 == VG_ROUNDDN(6, 4) );
207 CHECK( 4 == VG_ROUNDDN(7, 4) );
208
209 CHECK( 0 == VG_ROUNDUP(0, 4) );
210 CHECK( 4 == VG_ROUNDUP(1, 4) );
211 CHECK( 4 == VG_ROUNDUP(2, 4) );
212 CHECK( 4 == VG_ROUNDUP(3, 4) );
213 CHECK( 4 == VG_ROUNDUP(4, 4) );
214 CHECK( 8 == VG_ROUNDUP(5, 4) );
215 CHECK( 8 == VG_ROUNDUP(6, 4) );
216 CHECK( 8 == VG_ROUNDUP(7, 4) );
217
218 CHECK( 0 == VG_ROUNDDN(0, 8) );
219 CHECK( 0 == VG_ROUNDDN(1, 8) );
220 CHECK( 0 == VG_ROUNDDN(2, 8) );
221 CHECK( 0 == VG_ROUNDDN(3, 8) );
222 CHECK( 0 == VG_ROUNDDN(4, 8) );
223 CHECK( 0 == VG_ROUNDDN(5, 8) );
224 CHECK( 0 == VG_ROUNDDN(6, 8) );
225 CHECK( 0 == VG_ROUNDDN(7, 8) );
226
227 CHECK( 0 == VG_ROUNDUP(0, 8) );
228 CHECK( 8 == VG_ROUNDUP(1, 8) );
229 CHECK( 8 == VG_ROUNDUP(2, 8) );
230 CHECK( 8 == VG_ROUNDUP(3, 8) );
231 CHECK( 8 == VG_ROUNDUP(4, 8) );
232 CHECK( 8 == VG_ROUNDUP(5, 8) );
233 CHECK( 8 == VG_ROUNDUP(6, 8) );
234 CHECK( 8 == VG_ROUNDUP(7, 8) );
235
236 CHECK( 0 == VG_PGROUNDDN(0) );
237 CHECK( 0 == VG_PGROUNDDN(1) );
238 CHECK( 0 == VG_PGROUNDDN(2) );
239 CHECK( 0 == VG_PGROUNDDN(3) );
240 CHECK( 0 == VG_PGROUNDDN(4) );
241 CHECK( 0 == VG_PGROUNDDN(VKI_PAGE_SIZE-1) );
242 CHECK( VKI_PAGE_SIZE == VG_PGROUNDDN(VKI_PAGE_SIZE ) );
243 CHECK( VKI_PAGE_SIZE == VG_PGROUNDDN(VKI_PAGE_SIZE+1) );
244
245 CHECK( 0 == VG_PGROUNDUP(0) );
246 CHECK( VKI_PAGE_SIZE == VG_PGROUNDUP(1) );
247 CHECK( VKI_PAGE_SIZE == VG_PGROUNDUP(2) );
248 CHECK( VKI_PAGE_SIZE == VG_PGROUNDUP(3) );
249 CHECK( VKI_PAGE_SIZE == VG_PGROUNDUP(4) );
250 CHECK( VKI_PAGE_SIZE == VG_PGROUNDUP(VKI_PAGE_SIZE-1) );
251 CHECK( VKI_PAGE_SIZE == VG_PGROUNDUP(VKI_PAGE_SIZE ) );
252 CHECK( VKI_PAGE_SIZE*2 == VG_PGROUNDUP(VKI_PAGE_SIZE+1) );
253}
254
255void test_isspace(void)
njn221666f2009-02-20 06:37:52 +0000256{
njn2a5a76b2009-02-23 04:16:56 +0000257 CHECK( VG_(isspace)(' ') );
258 CHECK( VG_(isspace)('\n') );
259 CHECK( VG_(isspace)('\t') );
260 CHECK( ! VG_(isspace)('3') );
261 CHECK( ! VG_(isspace)('x') );
njn132fdb02009-02-26 03:52:35 +0000262}
njn221666f2009-02-20 06:37:52 +0000263
njn132fdb02009-02-26 03:52:35 +0000264void test_isdigit(void)
265{
njn2a5a76b2009-02-23 04:16:56 +0000266 CHECK( VG_(isdigit)('0') );
267 CHECK( VG_(isdigit)('1') );
268 CHECK( VG_(isdigit)('5') );
269 CHECK( VG_(isdigit)('9') );
270 CHECK( ! VG_(isdigit)('a') );
271 CHECK( ! VG_(isdigit)('!') );
njn221666f2009-02-20 06:37:52 +0000272}
273
njn132fdb02009-02-26 03:52:35 +0000274void test_is_dec_digit()
njn221666f2009-02-20 06:37:52 +0000275{
276 Long x;
njn2a5a76b2009-02-23 04:16:56 +0000277 CHECK( is_dec_digit('0', &x) && 0 == x );
278 CHECK( is_dec_digit('1', &x) && 1 == x );
279 CHECK( is_dec_digit('9', &x) && 9 == x );
njn132fdb02009-02-26 03:52:35 +0000280}
njn221666f2009-02-20 06:37:52 +0000281
njn132fdb02009-02-26 03:52:35 +0000282void test_is_hex_digit()
283{
284 Long x;
njn2a5a76b2009-02-23 04:16:56 +0000285 CHECK( is_hex_digit('0', &x) && 0 == x );
286 CHECK( is_hex_digit('1', &x) && 1 == x );
287 CHECK( is_hex_digit('9', &x) && 9 == x );
288 CHECK( is_hex_digit('a', &x) && 10 == x );
289 CHECK( is_hex_digit('f', &x) && 15 == x );
290 CHECK( is_hex_digit('A', &x) && 10 == x );
291 CHECK( is_hex_digit('F', &x) && 15 == x );
njn221666f2009-02-20 06:37:52 +0000292}
293
njn132fdb02009-02-26 03:52:35 +0000294void test_strtoll_and_strtod(void)
njn221666f2009-02-20 06:37:52 +0000295{
296 // For VG_(strtoll*)()
297 typedef struct {
298 Char* str; // The string to convert.
299 Long res; // The result.
300 Char endptr_val; // The char one past the end of the converted text.
301 } StrtollInputs;
302
303 // VG_(strtoll10)()
304 {
305 StrtollInputs a[] = {
306 // If there's no number at the head of the string, return 0, and
307 // make 'endptr' point to the start of the string.
308 { str : "", res : 0, endptr_val : '\0' },
309 { str : " \n\t", res : 0, endptr_val : ' ' },
310 { str : "one", res : 0, endptr_val : 'o' },
311 { str : "\ntwo", res : 0, endptr_val : '\n' },
312
313 // Successful conversion. Leading whitespace is ignored. A single
314 // '-' or '+' is accepted.
315 { str : "0", res : 0, endptr_val : '\0' },
316 { str : "+0", res : 0, endptr_val : '\0' },
317 { str : "-0", res : 0, endptr_val : '\0' },
318 { str : "1", res : 1, endptr_val : '\0' },
319 { str : "+1", res : 1, endptr_val : '\0' },
320 { str : "-1", res : -1, endptr_val : '\0' },
321 { str : "12", res : 12, endptr_val : '\0' },
322 { str : "-567", res : -567, endptr_val : '\0' },
323 { str : "1234567", res : 1234567, endptr_val : '\0' },
324 { str : "007", res : 7, endptr_val : '\0' },
325 { str : " +42", res : 42, endptr_val : '\0' },
326 { str : "\n\t\r\v -56", res : -56, endptr_val : '\0' },
327 { str : "123xyz", res : 123, endptr_val : 'x' },
328 { str : " -123abc", res : -123, endptr_val : 'a' },
329
330 // Whitespace after the +/- is not allowed; conversion fails.
331 { str : "+ 1", res : 0, endptr_val : '+' },
332 { str : "-\n1", res : 0, endptr_val : '-' },
333 };
334
335 // Nb: We test the results against strtoll() as well.
336 int i;
337 for (i = 0; i < (sizeof(a) / sizeof(StrtollInputs)); i++) {
338 Char* endptr1;
339 char* endptr2;
340 Long res1 = VG_(strtoll10)(a[i].str, &endptr1);
341 long long res2 = strtoll (a[i].str, &endptr2, 10);
342 //printf("res1 = %lld, *endptr1 = '%c'\n", res1, *endptr1);
343 //printf("res2 = %lld, *endptr2 = '%c'\n", res2, *endptr2);
njn2a5a76b2009-02-23 04:16:56 +0000344 CHECK(a[i].res == res1 && a[i].endptr_val == *endptr1);
345 CHECK(res2 == res1 && *endptr2 == *endptr1);
njn221666f2009-02-20 06:37:52 +0000346 }
347 }
348
349 // VG_(strtoll16)()
350 {
351 StrtollInputs a[] = {
352 // If there's no number at the head of the string, return 0, and
353 // make 'endptr' point to the start of the string.
354 { str : "", res : 0, endptr_val : '\0' },
355 { str : " \n\t", res : 0, endptr_val : ' ' },
356 { str : "one", res : 0, endptr_val : 'o' },
357 { str : "\ntwo", res : 0, endptr_val : '\n' },
358
359 // Successful conversion. Leading whitespace is ignored. A single
360 // '-' or '+' is accepted. "0X" and "0x" are also allowed at the
361 // front, but if no digits follow, just the "0" is converted.
362 { str : "0", res : 0, endptr_val : '\0' },
363 { str : "0x0", res : 0, endptr_val : '\0' },
364 { str : "0X0", res : 0, endptr_val : '\0' },
365 { str : "0x", res : 0, endptr_val : 'x' },
366 { str : "0Xg", res : 0, endptr_val : 'X' },
367 { str : "0", res : 0, endptr_val : '\0' },
368 { str : "+0", res : 0, endptr_val : '\0' },
369 { str : "-0", res : 0, endptr_val : '\0' },
370 { str : "1", res : 1, endptr_val : '\0' },
371 { str : "+1", res : 1, endptr_val : '\0' },
372 { str : "-1", res : -1, endptr_val : '\0' },
373 { str : "1a", res : 26, endptr_val : '\0' },
374 { str : "-5F7", res : -1527, endptr_val : '\0' },
375 { str : "0x1234567", res : 19088743, endptr_val : '\0' },
376 { str : "007", res : 7, endptr_val : '\0' },
377 { str : "0X00ABCD", res : 43981, endptr_val : '\0' },
378 { str : " +AbC", res : 2748, endptr_val : '\0' },
379 { str : " -0xAbC", res : -2748, endptr_val : '\0' },
380 { str : " -0xxx", res : 0, endptr_val : 'x' },
381 { str : "\n\t\r\v -56", res : -86, endptr_val : '\0' },
382 { str : "123xyz", res : 291, endptr_val : 'x' },
383 { str : " -123defghi", res : -1195503, endptr_val : 'g' },
384
385 // Whitespace after the +/- is not allowed; conversion fails.
386 { str : "+ 1", res : 0, endptr_val : '+' },
387 { str : "-\n0x1", res : 0, endptr_val : '-' },
388 };
389
390 // Nb: We test the results against strtoll() as well.
391 int i;
392 for (i = 0; i < (sizeof(a) / sizeof(StrtollInputs)); i++) {
393 Char* endptr1;
394 char* endptr2;
395 Long res1 = VG_(strtoll16)(a[i].str, &endptr1);
396 long long res2 = strtoll (a[i].str, &endptr2, 16);
397 //printf(" res1 = %lld, *endptr1 = '%c'\n", res1, *endptr1);
398 //printf(" res2 = %lld, *endptr2 = '%c'\n", res2, *endptr2);
njn2a5a76b2009-02-23 04:16:56 +0000399 CHECK(a[i].res == res1 && a[i].endptr_val == *endptr1);
400 CHECK(res2 == res1 && *endptr2 == *endptr1);
njn221666f2009-02-20 06:37:52 +0000401 }
402 }
njn2a5a76b2009-02-23 04:16:56 +0000403
njn221666f2009-02-20 06:37:52 +0000404 // VG_(strtod)()
njn2a5a76b2009-02-23 04:16:56 +0000405 // XXX: todo
njn221666f2009-02-20 06:37:52 +0000406}
407
njn132fdb02009-02-26 03:52:35 +0000408void test_log2(void)
409{
410 CHECK( -1 == VG_(log2)(0) );
411 CHECK( 0 == VG_(log2)(1) );
412 CHECK( 1 == VG_(log2)(2) );
413 CHECK( -1 == VG_(log2)(3) );
414 CHECK( 2 == VG_(log2)(4) );
415 CHECK( -1 == VG_(log2)(5) );
416 CHECK( -1 == VG_(log2)(6) );
417 CHECK( -1 == VG_(log2)(7) );
418 CHECK( 3 == VG_(log2)(8) );
419
420 CHECK( -1 == VG_(log2)( 15) );
421 CHECK( 4 == VG_(log2)( 16) );
422 CHECK( -1 == VG_(log2)( 17) );
423
424 CHECK( -1 == VG_(log2)( 63) );
425 CHECK( 6 == VG_(log2)( 64) );
426 CHECK( -1 == VG_(log2)( 65) );
427
428 CHECK( -1 == VG_(log2)(255) );
429 CHECK( 8 == VG_(log2)(256) );
430 CHECK( -1 == VG_(log2)(257) );
431
432 CHECK( -1 == VG_(log2)(65535) );
433 CHECK( 16 == VG_(log2)(65536) );
434 CHECK( -1 == VG_(log2)(65537) );
435
436 CHECK( -1 == VG_(log2)(16777215) );
437 CHECK( 24 == VG_(log2)(16777216) );
438 CHECK( -1 == VG_(log2)(16777217) );
439
440 CHECK( -1 == VG_(log2)(2147483647U) );
441 CHECK( 31 == VG_(log2)(2147483648U) );
442 CHECK( -1 == VG_(log2)(2147483649U) );
443
444 CHECK( -1 == VG_(log2)(4294967295U) ); // Max UInt
njn132fdb02009-02-26 03:52:35 +0000445}
446
447void test_random(void)
448{
449 // Hmm, it's really hard to unit test a pseudo-random number generator.
450 // So no testing here, sorry.
451}
452
453//-----------------------------------------------------------------------
454// main
455//-----------------------------------------------------------------------
456
njn221666f2009-02-20 06:37:52 +0000457int main(void)
458{
njn132fdb02009-02-26 03:52:35 +0000459 // Nb: the order of the tests is based on the order of the code in
460 // m_libcbase.c, except that macros defined in pub_tool_libcbase.h are
461 // tested first.
462
njn221666f2009-02-20 06:37:52 +0000463 //--------------------------------------------------------------------
njn132fdb02009-02-26 03:52:35 +0000464 // pub_tool_libcbase.h macros
njn221666f2009-02-20 06:37:52 +0000465 //--------------------------------------------------------------------
njn132fdb02009-02-26 03:52:35 +0000466 test_VG_STREQ();
467 test_VG_STREQN();
468 test_VG_IS_XXX_ALIGNED();
469 test_VG_ROUND_et_al();
njn221666f2009-02-20 06:37:52 +0000470
471 //--------------------------------------------------------------------
472 // Char functions
473 //--------------------------------------------------------------------
njn132fdb02009-02-26 03:52:35 +0000474 test_isspace();
475 test_isdigit();
njn221666f2009-02-20 06:37:52 +0000476
477 //--------------------------------------------------------------------
478 // String-to-number functions
479 //--------------------------------------------------------------------
njn132fdb02009-02-26 03:52:35 +0000480 test_is_dec_digit();
481 test_is_hex_digit();
482 test_strtoll_and_strtod();
njn221666f2009-02-20 06:37:52 +0000483
484 //--------------------------------------------------------------------
485 // String functions
486 //--------------------------------------------------------------------
njn132fdb02009-02-26 03:52:35 +0000487 // XXX: more todo: VG_(str_*)
njn221666f2009-02-20 06:37:52 +0000488
489 //--------------------------------------------------------------------
490 // Mem functions
491 //--------------------------------------------------------------------
njn132fdb02009-02-26 03:52:35 +0000492 // XXX: todo: VG_(mem*)
njn221666f2009-02-20 06:37:52 +0000493
494 //--------------------------------------------------------------------
495 // Miscellaneous functions
496 //--------------------------------------------------------------------
njn132fdb02009-02-26 03:52:35 +0000497 // XXX: todo: VG_(ssort)
498 test_log2();
499 test_random();
njn221666f2009-02-20 06:37:52 +0000500
501 return 0;
502}
503