quakeforge/libs/util/test/test-bitop.c
Bill Currie cfaf158ebc [math] Add some bit-op functions
Just 32-bit rounding to next higher power of two, and base 2 logarithm.
Most importantly, they are suitable for use in initializers as they are
constant in, constant out.
2022-01-23 13:47:14 +09:00

55 lines
1.1 KiB
C

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include "QF/math/bitop.h"
struct {
uint32_t value;
uint32_t expect;
} tests [] = {
{BITOP_RUP(1), 1},
{BITOP_RUP(2), 2},
{BITOP_RUP(3), 4},
{BITOP_RUP(4), 4},
{BITOP_RUP(5), 8},
{BITOP_RUP(7), 8},
{BITOP_RUP(8), 8},
{BITOP_RUP(0x40000000), 0x40000000},
{BITOP_RUP(0x40000001), 0x80000000},
{BITOP_LOG2(1), 0},
{BITOP_LOG2(2), 1},
{BITOP_LOG2(3), 2},
{BITOP_LOG2(4), 2},
{BITOP_LOG2(5), 3},
{BITOP_LOG2(7), 3},
{BITOP_LOG2(8), 3},
{BITOP_LOG2(9), 4},
{BITOP_LOG2(15), 4},
{BITOP_LOG2(16), 4},
{BITOP_LOG2(17), 5},
{BITOP_LOG2(31), 5},
{BITOP_LOG2(32), 5},
{BITOP_LOG2(33), 6},
{BITOP_LOG2(0x40000000), 30},
{BITOP_LOG2(0x40000001), 31},
};
#define num_tests (sizeof (tests) / sizeof (tests[0]))
int
main (int argc, const char **argv)
{
size_t i;
int res = 0;
for (i = 0; i < num_tests; i++) {
if (tests[i].value != tests[i].expect) {
res |= 1;
printf ("test %d failed\n", (int) i);
printf ("expect: %8x\n", tests[i].expect);
printf ("got : %8x\n", tests[i].value);
}
}
return res;
}