popcount_u32_tests.c (768B)
1 #include "../test.h" 2 3 int popcount_u32(uint32_t x); 4 5 int 6 popcount_u32_simple(uint32_t x) 7 { 8 int ret; 9 10 for (ret = 0; x != 0; x >>= 1) 11 ret += x & 1; 12 13 return ret; 14 } 15 16 bool 17 correct(uint32_t x) 18 { 19 int expected = popcount_u32_simple(x); 20 int actual = popcount_u32(x); 21 if (actual != expected) { 22 printf("Error at %" PRIu32 ": expected %d bits, found %d\n", 23 x, expected, actual); 24 return false; 25 } 26 return true; 27 } 28 29 void run(void) { 30 uint32_t i; 31 32 /* Test all numbers up to 2^16, and other ranges of 2^16 numbers */ 33 for (i = 0; i < 0xFFFF; i++) { 34 if (!correct(i) || 35 !correct(i + UINT32_C(0xFFFF0000)) || 36 !correct(i + UINT32_C(1000000)) || 37 !correct(i + UINT32_C(1)) || 38 !correct(i + UINT32_C(1234567))) 39 return; 40 } 41 42 printf("Ok\n"); 43 }