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