summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/int.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/tests/int.c b/tests/int.c
index 0039862..e59efde 100644
--- a/tests/int.c
+++ b/tests/int.c
@@ -60,5 +60,62 @@ int main(void) {
assert(bswap((uint32_t)0x12345678) == 0x78563412);
assert(bswap((uint64_t)0x1234567812345678) == 0x7856341278563412);
+ assert(count_ones(0x0) == 0);
+ assert(count_ones(0x1) == 1);
+ assert(count_ones(0x2) == 1);
+ assert(count_ones(0x3) == 2);
+ assert(count_ones(0x137F) == 10);
+
+ assert(count_zeros(0) == INT_WIDTH);
+ assert(count_zeros(0L) == LONG_WIDTH);
+ assert(count_zeros(0LL) == LLONG_WIDTH);
+ assert(count_zeros((uint8_t)0) == 8);
+ assert(count_zeros((uint16_t)0) == 16);
+ assert(count_zeros((uint32_t)0) == 32);
+ assert(count_zeros((uint64_t)0) == 64);
+
+ assert(rotate_left((uint8_t)0xA1, 4) == 0x1A);
+ assert(rotate_left((uint16_t)0x1234, 12) == 0x4123);
+ assert(rotate_left((uint32_t)0x12345678, 20) == 0x67812345);
+ assert(rotate_left((uint32_t)0x12345678, 0) == 0x12345678);
+
+ assert(rotate_right((uint8_t)0xA1, 4) == 0x1A);
+ assert(rotate_right((uint16_t)0x1234, 12) == 0x2341);
+ assert(rotate_right((uint32_t)0x12345678, 20) == 0x45678123);
+ assert(rotate_right((uint32_t)0x12345678, 0) == 0x12345678);
+
+ for (int i = 0; i < 16; ++i) {
+ uint16_t n = (uint16_t)1 << i;
+ for (int j = i; j < 16; ++j) {
+ uint16_t m = (uint16_t)1 << j;
+ uint16_t nm = n | m;
+ assert(count_ones(nm) == 1 + (n != m));
+ assert(count_zeros(nm) == 15 - (n != m));
+ assert(leading_zeros(nm) == 15 - j);
+ assert(trailing_zeros(nm) == i);
+ assert(first_leading_one(nm) == j + 1);
+ assert(first_trailing_one(nm) == i + 1);
+ assert(bit_width(nm) == j + 1);
+ assert(bit_floor(nm) == m);
+ if (n == m) {
+ assert(bit_ceil(nm) == m);
+ assert(has_single_bit(nm));
+ } else {
+ if (j < 15) {
+ assert(bit_ceil(nm) == (m << 1));
+ }
+ assert(!has_single_bit(nm));
+ }
+ }
+ }
+
+ assert(leading_zeros((uint16_t)0) == 16);
+ assert(trailing_zeros((uint16_t)0) == 16);
+ assert(first_leading_one(0) == 0);
+ assert(first_trailing_one(0) == 0);
+ assert(bit_width(0) == 0);
+ assert(bit_floor(0) == 0);
+ assert(bit_ceil(0) == 1);
+
return EXIT_SUCCESS;
}