1 //! Tests for `__BindgenBitfieldUnit`.
2 //!
3 //! Note that bit-fields are allocated right to left (least to most significant
4 //! bits).
5 //!
6 //! From the x86 PS ABI:
7 //!
8 //! ```c
9 //! struct {
10 //!     int j : 5;
11 //!     int k : 6;
12 //!     int m : 7;
13 //! };
14 //! ```
15 //!
16 //! ```ignore
17 //! +------------------------------------------------------------+
18 //! |                     |              |            |          |
19 //! |       padding       |       m      |     k      |    j     |
20 //! |31                 18|17          11|10         5|4        0|
21 //! +------------------------------------------------------------+
22 //! ```
23 
24 use super::bitfield_unit::__BindgenBitfieldUnit;
25 
26 #[test]
bitfield_unit_get_bit()27 fn bitfield_unit_get_bit() {
28     let unit = __BindgenBitfieldUnit::<[u8; 2]>::new([0b10011101, 0b00011101]);
29 
30     let mut bits = vec![];
31     for i in 0..16 {
32         bits.push(unit.get_bit(i));
33     }
34 
35     println!();
36     println!("bits = {:?}", bits);
37     assert_eq!(
38         bits,
39         &[
40             // 0b10011101
41             true, false, true, true, true, false, false, true,
42             // 0b00011101
43             true, false, true, true, true, false, false, false
44         ]
45     );
46 }
47 
48 #[test]
bitfield_unit_set_bit()49 fn bitfield_unit_set_bit() {
50     let mut unit =
51         __BindgenBitfieldUnit::<[u8; 2]>::new([0b00000000, 0b00000000]);
52 
53     for i in 0..16 {
54         if i % 3 == 0 {
55             unit.set_bit(i, true);
56         }
57     }
58 
59     for i in 0..16 {
60         assert_eq!(unit.get_bit(i), i % 3 == 0);
61     }
62 
63     let mut unit =
64         __BindgenBitfieldUnit::<[u8; 2]>::new([0b11111111, 0b11111111]);
65 
66     for i in 0..16 {
67         if i % 3 == 0 {
68             unit.set_bit(i, false);
69         }
70     }
71 
72     for i in 0..16 {
73         assert_eq!(unit.get_bit(i), i % 3 != 0);
74     }
75 }
76 
77 macro_rules! bitfield_unit_get {
78     (
79         $(
80             With $storage:expr , then get($start:expr, $len:expr) is $expected:expr;
81         )*
82     ) => {
83         #[test]
84         fn bitfield_unit_get() {
85             $({
86                 let expected = $expected;
87                 let unit = __BindgenBitfieldUnit::<_>::new($storage);
88                 let actual = unit.get($start, $len);
89 
90                 println!();
91                 println!("expected = {:064b}", expected);
92                 println!("actual   = {:064b}", actual);
93 
94                 assert_eq!(expected, actual);
95             })*
96         }
97    }
98 }
99 
100 bitfield_unit_get! {
101     // Let's just exhaustively test getting the bits from a single byte, since
102     // there are few enough combinations...
103 
104     With [0b11100010], then get(0, 1) is 0;
105     With [0b11100010], then get(1, 1) is 1;
106     With [0b11100010], then get(2, 1) is 0;
107     With [0b11100010], then get(3, 1) is 0;
108     With [0b11100010], then get(4, 1) is 0;
109     With [0b11100010], then get(5, 1) is 1;
110     With [0b11100010], then get(6, 1) is 1;
111     With [0b11100010], then get(7, 1) is 1;
112 
113     With [0b11100010], then get(0, 2) is 0b10;
114     With [0b11100010], then get(1, 2) is 0b01;
115     With [0b11100010], then get(2, 2) is 0b00;
116     With [0b11100010], then get(3, 2) is 0b00;
117     With [0b11100010], then get(4, 2) is 0b10;
118     With [0b11100010], then get(5, 2) is 0b11;
119     With [0b11100010], then get(6, 2) is 0b11;
120 
121     With [0b11100010], then get(0, 3) is 0b010;
122     With [0b11100010], then get(1, 3) is 0b001;
123     With [0b11100010], then get(2, 3) is 0b000;
124     With [0b11100010], then get(3, 3) is 0b100;
125     With [0b11100010], then get(4, 3) is 0b110;
126     With [0b11100010], then get(5, 3) is 0b111;
127 
128     With [0b11100010], then get(0, 4) is 0b0010;
129     With [0b11100010], then get(1, 4) is 0b0001;
130     With [0b11100010], then get(2, 4) is 0b1000;
131     With [0b11100010], then get(3, 4) is 0b1100;
132     With [0b11100010], then get(4, 4) is 0b1110;
133 
134     With [0b11100010], then get(0, 5) is 0b00010;
135     With [0b11100010], then get(1, 5) is 0b10001;
136     With [0b11100010], then get(2, 5) is 0b11000;
137     With [0b11100010], then get(3, 5) is 0b11100;
138 
139     With [0b11100010], then get(0, 6) is 0b100010;
140     With [0b11100010], then get(1, 6) is 0b110001;
141     With [0b11100010], then get(2, 6) is 0b111000;
142 
143     With [0b11100010], then get(0, 7) is 0b1100010;
144     With [0b11100010], then get(1, 7) is 0b1110001;
145 
146     With [0b11100010], then get(0, 8) is 0b11100010;
147 
148     // OK. Now let's test getting bits from across byte boundaries.
149 
150     With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
151     then get(0, 16) is 0b1111111101010101;
152 
153     With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
154     then get(1, 16) is 0b0111111110101010;
155 
156     With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
157     then get(2, 16) is 0b0011111111010101;
158 
159     With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
160     then get(3, 16) is 0b0001111111101010;
161 
162     With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
163     then get(4, 16) is 0b0000111111110101;
164 
165     With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
166     then get(5, 16) is 0b0000011111111010;
167 
168     With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
169     then get(6, 16) is 0b0000001111111101;
170 
171     With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
172     then get(7, 16) is 0b0000000111111110;
173 
174     With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
175     then get(8, 16) is 0b0000000011111111;
176 }
177 
178 macro_rules! bitfield_unit_set {
179     (
180         $(
181             set($start:expr, $len:expr, $val:expr) is $expected:expr;
182         )*
183     ) => {
184         #[test]
185         fn bitfield_unit_set() {
186             $(
187                 let mut unit = __BindgenBitfieldUnit::<[u8; 4]>::new([0, 0, 0, 0]);
188                 unit.set($start, $len, $val);
189                 let actual = unit.get(0, 32);
190 
191                 println!();
192                 println!("set({}, {}, {:032b}", $start, $len, $val);
193                 println!("expected = {:064b}", $expected);
194                 println!("actual   = {:064b}", actual);
195 
196                 assert_eq!($expected, actual);
197             )*
198         }
199     }
200 }
201 
202 bitfield_unit_set! {
203     // Once again, let's exhaustively test single byte combinations.
204 
205     set(0, 1, 0b11111111) is 0b00000001;
206     set(1, 1, 0b11111111) is 0b00000010;
207     set(2, 1, 0b11111111) is 0b00000100;
208     set(3, 1, 0b11111111) is 0b00001000;
209     set(4, 1, 0b11111111) is 0b00010000;
210     set(5, 1, 0b11111111) is 0b00100000;
211     set(6, 1, 0b11111111) is 0b01000000;
212     set(7, 1, 0b11111111) is 0b10000000;
213 
214     set(0, 2, 0b11111111) is 0b00000011;
215     set(1, 2, 0b11111111) is 0b00000110;
216     set(2, 2, 0b11111111) is 0b00001100;
217     set(3, 2, 0b11111111) is 0b00011000;
218     set(4, 2, 0b11111111) is 0b00110000;
219     set(5, 2, 0b11111111) is 0b01100000;
220     set(6, 2, 0b11111111) is 0b11000000;
221 
222     set(0, 3, 0b11111111) is 0b00000111;
223     set(1, 3, 0b11111111) is 0b00001110;
224     set(2, 3, 0b11111111) is 0b00011100;
225     set(3, 3, 0b11111111) is 0b00111000;
226     set(4, 3, 0b11111111) is 0b01110000;
227     set(5, 3, 0b11111111) is 0b11100000;
228 
229     set(0, 4, 0b11111111) is 0b00001111;
230     set(1, 4, 0b11111111) is 0b00011110;
231     set(2, 4, 0b11111111) is 0b00111100;
232     set(3, 4, 0b11111111) is 0b01111000;
233     set(4, 4, 0b11111111) is 0b11110000;
234 
235     set(0, 5, 0b11111111) is 0b00011111;
236     set(1, 5, 0b11111111) is 0b00111110;
237     set(2, 5, 0b11111111) is 0b01111100;
238     set(3, 5, 0b11111111) is 0b11111000;
239 
240     set(0, 6, 0b11111111) is 0b00111111;
241     set(1, 6, 0b11111111) is 0b01111110;
242     set(2, 6, 0b11111111) is 0b11111100;
243 
244     set(0, 7, 0b11111111) is 0b01111111;
245     set(1, 7, 0b11111111) is 0b11111110;
246 
247     set(0, 8, 0b11111111) is 0b11111111;
248 
249     // And, now let's cross byte boundaries.
250 
251     set(0, 16, 0b1111111111111111) is 0b00000000000000001111111111111111;
252     set(1, 16, 0b1111111111111111) is 0b00000000000000011111111111111110;
253     set(2, 16, 0b1111111111111111) is 0b00000000000000111111111111111100;
254     set(3, 16, 0b1111111111111111) is 0b00000000000001111111111111111000;
255     set(4, 16, 0b1111111111111111) is 0b00000000000011111111111111110000;
256     set(5, 16, 0b1111111111111111) is 0b00000000000111111111111111100000;
257     set(6, 16, 0b1111111111111111) is 0b00000000001111111111111111000000;
258     set(7, 16, 0b1111111111111111) is 0b00000000011111111111111110000000;
259     set(8, 16, 0b1111111111111111) is 0b00000000111111111111111100000000;
260 }
261