-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_bits.c
More file actions
57 lines (50 loc) · 1.38 KB
/
test_bits.c
File metadata and controls
57 lines (50 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "bits.h"
void test_bit_and(unsigned int x, unsigned int y) {
unsigned int got = bit_to_int(bit_and(bit_from_int(x), bit_from_int(y)));
unsigned int expected = x&y;
if (got != expected) {
printf("Input: %u & %u\n", x, y);
printf("Got: %u\n", got);
printf("Expected: %u\n", expected);
exit(1);
}
}
void test_bit_or(unsigned int x, unsigned int y) {
unsigned int got = bit_to_int(bit_or(bit_from_int(x), bit_from_int(y)));
unsigned int expected = x|y;
if (got != expected) {
printf("Input: %u | %u\n", x, y);
printf("Got: %u\n", got);
printf("Expected: %u\n", expected);
exit(1);
}
}
void test_bit_xor(unsigned int x, unsigned int y) {
unsigned int got = bit_to_int(bit_xor(bit_from_int(x), bit_from_int(y)));
unsigned int expected = x^y;
if (got != expected) {
printf("Input: %u ^ %u\n", x, y);
printf("Got: %u\n", got);
printf("Expected: %u\n", expected);
exit(1);
}
}
int main() {
assert(bit_to_int(bit_not(bit_from_int(1))) == 0);
assert(bit_to_int(bit_not(bit_from_int(0))) == 1);
test_bit_and(1,1);
test_bit_and(1,0);
test_bit_and(0,1);
test_bit_and(0,0);
test_bit_or(1,1);
test_bit_or(1,0);
test_bit_or(0,1);
test_bit_or(0,0);
test_bit_xor(1,1);
test_bit_xor(1,0);
test_bit_xor(0,1);
test_bit_xor(0,0);
}