-
Notifications
You must be signed in to change notification settings - Fork 2
/
bitfield.c
74 lines (52 loc) · 2.04 KB
/
bitfield.c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "bitfield.h"
struct s {
EMBEDDED_BITFIELD(bf, 10);
};
BITFIELD(bf, 1);
int main(int argc, char **argv)
{
struct s s;
EMBEDDED_BITFIELD_INITIALIZE(&s, bf, 10);
BITFIELD(bf0, 1);
BITFIELD(bf1, 128);
BITFIELD(bf2, 24);
bitfield_t bf3 = bitfield_alloc(0);
bitfield_t bf4 = bitfield_alloc(300);
BITFIELD_ASSERT(bf3, "unable to allocate bitfield on heap");
BITFIELD_ASSERT(bf4, "unable to allocate bitfield on heap");
bitfield_set_one(bf, 0);
BITFIELD_ASSERT(bitfield_get(bf, 0), "bit.0 is low");
bitfield_set_one(s.bf, 0);
BITFIELD_ASSERT(bitfield_get(s.bf, 0), "bit.0 is low");
bitfield_set_one(bf0, 0);
BITFIELD_ASSERT(bitfield_get(bf0, 0), "bit.0 is low");
bitfield_set_one(bf1, 10);
BITFIELD_ASSERT(bitfield_get(bf1, 10), "bit.10 is low");
bitfield_set_zero(bf1, 10);
BITFIELD_ASSERT(!bitfield_get(bf1, 10), "bit.10 is high");
bitfield_toggle(bf1, 10);
BITFIELD_ASSERT(bitfield_get(bf1, 10), "bit.10 not toggled");
bitfield_set_one(bf2, 1);
BITFIELD_ASSERT(bitfield_get(bf2, 1), "bit.1 is low");
bitfield_set_all_zero(bf1);
BITFIELD_ASSERT(bitfield_get(bf2, 1), "bit.1 is low");
//bitfield_set_one(bf3, 0);
bitfield_set_one(bf4, 100);
BITFIELD_ASSERT(bitfield_get(bf4, 100), "bit.100 is low");
bitfield_set_one(bf4, 200);
BITFIELD_ASSERT(bitfield_count_ones(bf4) == 2, "wrong number of bits set high");
BITFIELD_ASSERT(bitfield_count_zeros(bf4) == 298, "wrong number of bits set low");
bitfield_set_all_one(bf4);
BITFIELD_ASSERT(bitfield_count_ones(bf4) == 300, "wrong number of bits set high");
BITFIELD_ASSERT(bitfield_count_zeros(bf4) == 0, "wrong number of bits set low");
bitfield_set_all_zero(bf4);
BITFIELD_ASSERT(bitfield_count_ones(bf4) == 0, "wrong number of bits set high");
BITFIELD_ASSERT(bitfield_count_zeros(bf4) == 300, "wrong number of bits set low");
bitfield_free(bf3);
bitfield_free(bf4);
return 0;
}