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