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