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
58
| #include <stdio.h>
#include "bit_ops.h"
void test_get_bit(unsigned x, unsigned n, unsigned expected) {
unsigned a = get_bit(x, n);
if(a!=expected) {
printf("get_bit(0x%08x,%u) returned 0x%08x, but we expected 0x%08x\n",x,n,a,expected);
} else {
printf("get_bit(0x%08x,%u)returned 0x%08x, correct\n",x,n,a);
}
}
void test_set_bit(unsigned x, unsigned n, unsigned v, unsigned expected) {
unsigned o = x;
set_bit(&x, n, v);
if(x!=expected) {
printf("set_bit(0x%08x,%u,%u) returned 0x%08x but we expected 0x%08x\n",o,n,v,x,expected);
} else {
printf("set_bit(0x%08x,%u,%u) returned 0x%08x, correct\n",o,n,v,x);
}
}
void test_flip_bit(unsigned x, unsigned n, unsigned expected) {
unsigned o = x;
flip_bit(&x, n);
if(x!=expected) {
printf("flip_bit(0x%08x,%u) returned 0x%08x, but we expected 0x%08x\n",o,n,x,expected);
} else {
printf("flip_bit(0x%08x,%u) returned 0x%08x, correct\n",o,n,x);
}
}
int main(int argc, const char * argv[]) {
printf("\nTesting get_bit()\n\n");
test_get_bit(0b1001110,0,0);
test_get_bit(0b1001110,1,1);
test_get_bit(0b1001110,5,0);
test_get_bit(0b11011,3,1);
test_get_bit(0b11011,2,0);
test_get_bit(0b11011,9,0);
printf("\nTesting set_bit()\n\n");
test_set_bit(0b1001110,2,0,0b1001010);
test_set_bit(0b1101101,0,0,0b1101100);
test_set_bit(0b1001110,2,1,0b1001110);
test_set_bit(0b1101101,0,1,0b1101101);
test_set_bit(0b1001110,9,0,0b1001110);
test_set_bit(0b1101101,4,0,0b1101101);
test_set_bit(0b1001110,9,1,0b1001001110);
test_set_bit(0b1101101,7,1,0b11101101);
printf("\nTesting flip_bit()\n\n");
test_flip_bit(0b1001110,0,0b1001111);
test_flip_bit(0b1001110,1,0b1001100);
test_flip_bit(0b1001110,2,0b1001010);
test_flip_bit(0b1001110,5,0b1101110);
test_flip_bit(0b1001110,9,0b1001001110);
printf("\n");
return 0;
}
|