Move to kernel style SPDX license identifiers
[babeltrace.git] / tests / bitfield / test_bitfield.c
1 /*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Copyright (C) 2010-2019 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * BabelTrace - bitfield test program
7 */
8
9 #include "compat/bitfield.h"
10 #include <time.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13
14 #include <tap/tap.h>
15
16 unsigned int glob;
17
18 /*
19 * This function is only declared to show the size of a bitfield write in
20 * objdump. The declaration is there to avoid a -Wmissing-prototypes warning.
21 */
22 void fct(void);
23 void fct(void)
24 {
25 bt_bitfield_write(&glob, unsigned int, 12, 15, 0x12345678);
26 }
27
28 /* Test array size, in bytes */
29 #define TEST_LEN 128
30 #define NR_TESTS 10
31 #define SIGNED_INT_READ_TEST_DESC_FMT_STR "Writing and reading back 0x%X, signed int dest, varying read unit size"
32 #define SIGNED_INT_WRITE_TEST_DESC_FMT_STR "Writing and reading back 0x%X, signed int source, varying write unit size"
33 #define SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR "Writing and reading back 0x%llX, signed long long dest, varying read unit size"
34 #define SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR "Writing and reading back 0x%llX, signed long long source, varying write unit size"
35 #define UNSIGNED_INT_READ_TEST_DESC_FMT_STR "Writing and reading back 0x%X, unsigned int dest, varying read unit size"
36 #define UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR "Writing and reading back 0x%X, unsigned int source, varying write unit size"
37 #define UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR "Writing and reading back 0x%llX, unsigned long long dest, varying read unit size"
38 #define UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR "Writing and reading back 0x%llX, unsigned long long source, varying write unit size"
39 #define DIAG_FMT_STR(val_type_fmt) "Failed reading value written \"%s\"-wise, with start=%i" \
40 " and length=%i. Read 0x" val_type_fmt
41
42 static
43 unsigned int fls_u64(uint64_t x)
44 {
45 unsigned int r = 64;
46
47 if (!x)
48 return 0;
49
50 if (!(x & 0xFFFFFFFF00000000ULL)) {
51 x <<= 32;
52 r -= 32;
53 }
54 if (!(x & 0xFFFF000000000000ULL)) {
55 x <<= 16;
56 r -= 16;
57 }
58 if (!(x & 0xFF00000000000000ULL)) {
59 x <<= 8;
60 r -= 8;
61 }
62 if (!(x & 0xF000000000000000ULL)) {
63 x <<= 4;
64 r -= 4;
65 }
66 if (!(x & 0xC000000000000000ULL)) {
67 x <<= 2;
68 r -= 2;
69 }
70 if (!(x & 0x8000000000000000ULL)) {
71 x <<= 1;
72 r -= 1;
73 }
74 return r;
75 }
76
77 static
78 unsigned int fls_u32(uint32_t x)
79 {
80 unsigned int r = 32;
81
82 if (!x)
83 return 0;
84 if (!(x & 0xFFFF0000U)) {
85 x <<= 16;
86 r -= 16;
87 }
88 if (!(x & 0xFF000000U)) {
89 x <<= 8;
90 r -= 8;
91 }
92 if (!(x & 0xF0000000U)) {
93 x <<= 4;
94 r -= 4;
95 }
96 if (!(x & 0xC0000000U)) {
97 x <<= 2;
98 r -= 2;
99 }
100 if (!(x & 0x80000000U)) {
101 x <<= 1;
102 r -= 1;
103 }
104 return r;
105 }
106
107 #define print_byte_array(c, len) \
108 do { \
109 unsigned long i; \
110 \
111 for (i = 0; i < (len); i++) { \
112 printf("0x%X", (c)[i]); \
113 if (i != (len) - 1) \
114 printf(" "); \
115 } \
116 printf("\n"); \
117 } while (0)
118
119 #define init_byte_array(c, len, val) \
120 do { \
121 unsigned long i; \
122 \
123 for (i = 0; i < (len); i++) \
124 (c)[i] = (val); \
125 } while (0)
126
127 #define check_result(ref, val, buffer, typename, start, len, \
128 desc_fmt_str, val_type_fmt) \
129 ({ \
130 if ((val) != (ref)) { \
131 fail(desc_fmt_str, ref); \
132 diag(DIAG_FMT_STR(val_type_fmt), #typename, start, len, val); \
133 printf("# "); \
134 print_byte_array(buffer, TEST_LEN); \
135 } \
136 (val) != (ref); \
137 })
138
139 static
140 void run_test_unsigned_write(unsigned int src_ui, unsigned long long src_ull)
141 {
142 unsigned int nrbits_ui, nrbits_ull;
143 union {
144 unsigned char c[TEST_LEN];
145 unsigned short s[TEST_LEN/sizeof(unsigned short)];
146 unsigned int i[TEST_LEN/sizeof(unsigned int)];
147 unsigned long l[TEST_LEN/sizeof(unsigned long)];
148 unsigned long long ll[TEST_LEN/sizeof(unsigned long long)];
149 } target;
150 unsigned long long readval;
151 unsigned int s, l;
152
153 /* The number of bits needed to represent 0 is 0. */
154 nrbits_ui = fls_u32(src_ui);
155
156 /* Write from unsigned integer src input. */
157 for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
158 for (l = nrbits_ui; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
159 init_byte_array(target.c, TEST_LEN, 0xFF);
160 bt_bitfield_write(target.c, unsigned char, s, l, src_ui);
161 bt_bitfield_read(target.c, unsigned char, s, l, &readval);
162 if (check_result(src_ui, readval, target.c, unsigned char,
163 s, l, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR,
164 "%llX")) {
165 return;
166 }
167
168 init_byte_array(target.c, TEST_LEN, 0xFF);
169 bt_bitfield_write(target.s, unsigned short, s, l, src_ui);
170 bt_bitfield_read(target.c, unsigned char, s, l, &readval);
171 if (check_result(src_ui, readval, target.c, unsigned short,
172 s, l, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR,
173 "%llX")) {
174 return;
175 }
176
177 init_byte_array(target.c, TEST_LEN, 0xFF);
178 bt_bitfield_write(target.i, unsigned int, s, l, src_ui);
179 bt_bitfield_read(target.c, unsigned char, s, l, &readval);
180 if (check_result(src_ui, readval, target.c, unsigned int,
181 s, l, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR,
182 "%llX")) {
183 return;
184 }
185
186 init_byte_array(target.c, TEST_LEN, 0xFF);
187 bt_bitfield_write(target.l, unsigned long, s, l, src_ui);
188 bt_bitfield_read(target.c, unsigned char, s, l, &readval);
189 if (check_result(src_ui, readval, target.c, unsigned long,
190 s, l, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR,
191 "%llX")) {
192 return;
193 }
194
195 init_byte_array(target.c, TEST_LEN, 0xFF);
196 bt_bitfield_write(target.ll, unsigned long long, s, l, src_ui);
197 bt_bitfield_read(target.c, unsigned char, s, l, &readval);
198 if (check_result(src_ui, readval, target.c, unsigned long long,
199 s, l, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR,
200 "%llX")) {
201 return;
202 }
203 }
204 }
205 pass(UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR, src_ui);
206
207 /* The number of bits needed to represent 0 is 0. */
208 nrbits_ull = fls_u64(src_ull);
209
210 /* Write from unsigned long long src input. */
211 for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
212 for (l = nrbits_ull; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
213 init_byte_array(target.c, TEST_LEN, 0xFF);
214 bt_bitfield_write(target.c, unsigned char, s, l, src_ull);
215 bt_bitfield_read(target.c, unsigned char, s, l, &readval);
216 if (check_result(src_ull, readval, target.c, unsigned char,
217 s, l, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR,
218 "%llX")) {
219 return;
220 }
221
222 init_byte_array(target.c, TEST_LEN, 0xFF);
223 bt_bitfield_write(target.s, unsigned short, s, l, src_ull);
224 bt_bitfield_read(target.c, unsigned char, s, l, &readval);
225 if (check_result(src_ull, readval, target.c, unsigned short,
226 s, l, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR,
227 "%llX")) {
228 return;
229 }
230
231 init_byte_array(target.c, TEST_LEN, 0xFF);
232 bt_bitfield_write(target.i, unsigned int, s, l, src_ull);
233 bt_bitfield_read(target.c, unsigned char, s, l, &readval);
234 if (check_result(src_ull, readval, target.c, unsigned int,
235 s, l, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR,
236 "%llX")) {
237 return;
238 }
239
240 init_byte_array(target.c, TEST_LEN, 0xFF);
241 bt_bitfield_write(target.l, unsigned long, s, l, src_ull);
242 bt_bitfield_read(target.c, unsigned char, s, l, &readval);
243 if (check_result(src_ull, readval, target.c, unsigned long,
244 s, l, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR,
245 "%llX")) {
246 return;
247 }
248
249 init_byte_array(target.c, TEST_LEN, 0xFF);
250 bt_bitfield_write(target.ll, unsigned long long, s, l, src_ull);
251 bt_bitfield_read(target.c, unsigned char, s, l, &readval);
252 if (check_result(src_ull, readval, target.c, unsigned long long,
253 s, l, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR,
254 "%llX")) {
255 return;
256 }
257 }
258 }
259 pass(UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR, src_ull);
260 }
261
262 static
263 void run_test_unsigned_read(unsigned int src_ui, unsigned long long src_ull)
264 {
265 unsigned int nrbits_ui, nrbits_ull, readval_ui;
266 union {
267 unsigned char c[TEST_LEN];
268 unsigned short s[TEST_LEN/sizeof(unsigned short)];
269 unsigned int i[TEST_LEN/sizeof(unsigned int)];
270 unsigned long l[TEST_LEN/sizeof(unsigned long)];
271 unsigned long long ll[TEST_LEN/sizeof(unsigned long long)];
272 } target;
273 unsigned long long readval_ull;
274 unsigned int s, l;
275
276 /* The number of bits needed to represent 0 is 0. */
277 nrbits_ui = fls_u32(src_ui);
278
279 /* Read to unsigned integer readval output. */
280 for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
281 for (l = nrbits_ui; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
282 init_byte_array(target.c, TEST_LEN, 0xFF);
283 bt_bitfield_write(target.c, unsigned char, s, l, src_ui);
284 bt_bitfield_read(target.c, unsigned char, s, l, &readval_ui);
285 if (check_result(src_ui, readval_ui, target.c, unsigned char,
286 s, l, UNSIGNED_INT_READ_TEST_DESC_FMT_STR,
287 "%X")) {
288 return;
289 }
290
291 init_byte_array(target.c, TEST_LEN, 0xFF);
292 bt_bitfield_write(target.c, unsigned char, s, l, src_ui);
293 bt_bitfield_read(target.s, unsigned short, s, l, &readval_ui);
294 if (check_result(src_ui, readval_ui, target.c, unsigned short,
295 s, l, UNSIGNED_INT_READ_TEST_DESC_FMT_STR,
296 "%X")) {
297 return;
298 }
299
300 init_byte_array(target.c, TEST_LEN, 0xFF);
301 bt_bitfield_write(target.c, unsigned char, s, l, src_ui);
302 bt_bitfield_read(target.i, unsigned int, s, l, &readval_ui);
303 if (check_result(src_ui, readval_ui, target.c, unsigned int,
304 s, l, UNSIGNED_INT_READ_TEST_DESC_FMT_STR,
305 "%X")) {
306 return;
307 }
308
309 init_byte_array(target.c, TEST_LEN, 0xFF);
310 bt_bitfield_write(target.c, unsigned char, s, l, src_ui);
311 bt_bitfield_read(target.l, unsigned long, s, l, &readval_ui);
312 if (check_result(src_ui, readval_ui, target.c, unsigned long,
313 s, l, UNSIGNED_INT_READ_TEST_DESC_FMT_STR,
314 "%X")) {
315 return;
316 }
317
318 init_byte_array(target.c, TEST_LEN, 0xFF);
319 bt_bitfield_write(target.c, unsigned char, s, l, src_ui);
320 bt_bitfield_read(target.ll, unsigned long long, s, l, &readval_ui);
321 if (check_result(src_ui, readval_ui, target.c, unsigned long long,
322 s, l, UNSIGNED_INT_READ_TEST_DESC_FMT_STR,
323 "%X")) {
324 return;
325 }
326 }
327 }
328 pass(UNSIGNED_INT_READ_TEST_DESC_FMT_STR, src_ui);
329
330 /* The number of bits needed to represent 0 is 0. */
331 nrbits_ull = fls_u64(src_ull);
332
333 /* Read to unsigned long long readval output. */
334 for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
335 for (l = nrbits_ull; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
336 init_byte_array(target.c, TEST_LEN, 0xFF);
337 bt_bitfield_write(target.c, unsigned char, s, l, src_ull);
338 bt_bitfield_read(target.c, unsigned char, s, l, &readval_ull);
339 if (check_result(src_ull, readval_ull, target.c, unsigned char,
340 s, l, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR,
341 "%llX")) {
342 return;
343 }
344
345 init_byte_array(target.c, TEST_LEN, 0xFF);
346 bt_bitfield_write(target.c, unsigned char, s, l, src_ull);
347 bt_bitfield_read(target.s, unsigned short, s, l, &readval_ull);
348 if (check_result(src_ull, readval_ull, target.c, unsigned short,
349 s, l, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR,
350 "%llX")) {
351 return;
352 }
353
354 init_byte_array(target.c, TEST_LEN, 0xFF);
355 bt_bitfield_write(target.c, unsigned char, s, l, src_ull);
356 bt_bitfield_read(target.i, unsigned int, s, l, &readval_ull);
357 if (check_result(src_ull, readval_ull, target.c, unsigned int,
358 s, l, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR,
359 "%llX")) {
360 return;
361 }
362
363 init_byte_array(target.c, TEST_LEN, 0xFF);
364 bt_bitfield_write(target.c, unsigned char, s, l, src_ull);
365 bt_bitfield_read(target.l, unsigned long, s, l, &readval_ull);
366 if (check_result(src_ull, readval_ull, target.c, unsigned long,
367 s, l, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR,
368 "%llX")) {
369 return;
370 }
371
372 init_byte_array(target.c, TEST_LEN, 0xFF);
373 bt_bitfield_write(target.c, unsigned char, s, l, src_ull);
374 bt_bitfield_read(target.ll, unsigned long long, s, l, &readval_ull);
375 if (check_result(src_ull, readval_ull, target.c, unsigned long long,
376 s, l, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR,
377 "%llX")) {
378 return;
379 }
380 }
381 }
382 pass(UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR, src_ull);
383 }
384
385 static
386 void run_test_unsigned(unsigned int src_ui, unsigned long long src_ull)
387 {
388 run_test_unsigned_write(src_ui, src_ull);
389 run_test_unsigned_read(src_ui, src_ull);
390 }
391
392 static
393 void run_test_signed_write(int src_i, long long src_ll)
394 {
395 unsigned int nrbits_i, nrbits_ll;
396 union {
397 signed char c[TEST_LEN];
398 short s[TEST_LEN/sizeof(short)];
399 int i[TEST_LEN/sizeof(int)];
400 long l[TEST_LEN/sizeof(long)];
401 long long ll[TEST_LEN/sizeof(long long)];
402 } target;
403 long long readval;
404 unsigned int s, l;
405
406 if (!src_i)
407 nrbits_i = 0; /* The number of bits needed to represent 0 is 0. */
408 else if (src_i & 0x80000000U)
409 nrbits_i = fls_u32(~src_i) + 1; /* Find least significant bit conveying sign */
410 else
411 nrbits_i = fls_u32(src_i) + 1; /* Keep sign at 0 */
412
413 /* Write from signed integer src input. */
414 for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
415 for (l = nrbits_i; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
416 init_byte_array(target.c, TEST_LEN, 0x0);
417 bt_bitfield_write(target.c, signed char, s, l, src_i);
418 bt_bitfield_read(target.c, signed char, s, l, &readval);
419 if (check_result(src_i, readval, target.c, signed char,
420 s, l, SIGNED_INT_WRITE_TEST_DESC_FMT_STR,
421 "%llX")) {
422 return;
423 }
424
425 init_byte_array(target.c, TEST_LEN, 0x0);
426 bt_bitfield_write(target.s, short, s, l, src_i);
427 bt_bitfield_read(target.c, signed char, s, l, &readval);
428 if (check_result(src_i, readval, target.c, short,
429 s, l, SIGNED_INT_WRITE_TEST_DESC_FMT_STR,
430 "%llX")) {
431 return;
432 }
433
434 init_byte_array(target.c, TEST_LEN, 0x0);
435 bt_bitfield_write(target.i, int, s, l, src_i);
436 bt_bitfield_read(target.c, signed char, s, l, &readval);
437 if (check_result(src_i, readval, target.c, int,
438 s, l, SIGNED_INT_WRITE_TEST_DESC_FMT_STR,
439 "%llX")) {
440 return;
441 }
442
443 init_byte_array(target.c, TEST_LEN, 0x0);
444 bt_bitfield_write(target.l, long, s, l, src_i);
445 bt_bitfield_read(target.c, signed char, s, l, &readval);
446 if (check_result(src_i, readval, target.c, long,
447 s, l, SIGNED_INT_WRITE_TEST_DESC_FMT_STR,
448 "%llX")) {
449 return;
450 }
451
452 init_byte_array(target.c, TEST_LEN, 0x0);
453 bt_bitfield_write(target.ll, long long, s, l, src_i);
454 bt_bitfield_read(target.c, signed char, s, l, &readval);
455 if (check_result(src_i, readval, target.c, long long,
456 s, l, SIGNED_INT_WRITE_TEST_DESC_FMT_STR,
457 "%llX")) {
458 return;
459 }
460 }
461 }
462 pass(SIGNED_INT_WRITE_TEST_DESC_FMT_STR, src_i);
463
464 if (!src_ll)
465 nrbits_ll = 0; /* The number of bits needed to represent 0 is 0. */
466 else if (src_ll & 0x8000000000000000ULL)
467 nrbits_ll = fls_u64(~src_ll) + 1; /* Find least significant bit conveying sign */
468 else
469 nrbits_ll = fls_u64(src_ll) + 1; /* Keep sign at 0 */
470
471 /* Write from signed long long src input. */
472 for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
473 for (l = nrbits_ll; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
474 init_byte_array(target.c, TEST_LEN, 0x0);
475 bt_bitfield_write(target.c, signed char, s, l, src_ll);
476 bt_bitfield_read(target.c, signed char, s, l, &readval);
477 if (check_result(src_ll, readval, target.c, signed char,
478 s, l, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR,
479 "%llX")) {
480 return;
481 }
482
483 init_byte_array(target.c, TEST_LEN, 0x0);
484 bt_bitfield_write(target.s, short, s, l, src_ll);
485 bt_bitfield_read(target.c, signed char, s, l, &readval);
486 if (check_result(src_ll, readval, target.c, short,
487 s, l, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR,
488 "%llX")) {
489 return;
490 }
491
492 init_byte_array(target.c, TEST_LEN, 0x0);
493 bt_bitfield_write(target.i, int, s, l, src_ll);
494 bt_bitfield_read(target.c, signed char, s, l, &readval);
495 if (check_result(src_ll, readval, target.c, int,
496 s, l, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR,
497 "%llX")) {
498 return;
499 }
500
501 init_byte_array(target.c, TEST_LEN, 0x0);
502 bt_bitfield_write(target.l, long, s, l, src_ll);
503 bt_bitfield_read(target.c, signed char, s, l, &readval);
504 if (check_result(src_ll, readval, target.c, long,
505 s, l, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR,
506 "%llX")) {
507 return;
508 }
509
510 init_byte_array(target.c, TEST_LEN, 0x0);
511 bt_bitfield_write(target.ll, long long, s, l, src_ll);
512 bt_bitfield_read(target.c, signed char, s, l, &readval);
513 if (check_result(src_ll, readval, target.c, long long,
514 s, l, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR,
515 "%llX")) {
516 return;
517 }
518 }
519 }
520 pass(SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR, src_ll);
521 }
522
523 static
524 void run_test_signed_read(int src_i, long long src_ll)
525 {
526 unsigned int nrbits_i, nrbits_ll;
527 int readval_i;
528 union {
529 unsigned char c[TEST_LEN];
530 unsigned short s[TEST_LEN/sizeof(unsigned short)];
531 unsigned int i[TEST_LEN/sizeof(unsigned int)];
532 unsigned long l[TEST_LEN/sizeof(unsigned long)];
533 unsigned long long ll[TEST_LEN/sizeof(unsigned long long)];
534 } target;
535 long long readval_ll;
536 unsigned int s, l;
537
538 if (!src_i)
539 nrbits_i = 0; /* The number of bits needed to represent 0 is 0. */
540 else if (src_i & 0x80000000U)
541 nrbits_i = fls_u32(~src_i) + 1; /* Find least significant bit conveying sign */
542 else
543 nrbits_i = fls_u32(src_i) + 1; /* Keep sign at 0 */
544
545 /* Read to signed integer readval output. */
546 for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
547 for (l = nrbits_i; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
548 init_byte_array(target.c, TEST_LEN, 0xFF);
549 bt_bitfield_write(target.c, signed char, s, l, src_i);
550 bt_bitfield_read(target.c, signed char, s, l, &readval_i);
551 if (check_result(src_i, readval_i, target.c, signed char,
552 s, l, SIGNED_INT_READ_TEST_DESC_FMT_STR,
553 "%X")) {
554 return;
555 }
556
557 init_byte_array(target.c, TEST_LEN, 0xFF);
558 bt_bitfield_write(target.c, signed char, s, l, src_i);
559 bt_bitfield_read(target.s, short, s, l, &readval_i);
560 if (check_result(src_i, readval_i, target.c, short,
561 s, l, SIGNED_INT_READ_TEST_DESC_FMT_STR,
562 "%X")) {
563 return;
564 }
565
566 init_byte_array(target.c, TEST_LEN, 0xFF);
567 bt_bitfield_write(target.c, signed char, s, l, src_i);
568 bt_bitfield_read(target.i, int, s, l, &readval_i);
569 if (check_result(src_i, readval_i, target.c, int,
570 s, l, SIGNED_INT_READ_TEST_DESC_FMT_STR,
571 "%X")) {
572 return;
573 }
574
575 init_byte_array(target.c, TEST_LEN, 0xFF);
576 bt_bitfield_write(target.c, signed char, s, l, src_i);
577 bt_bitfield_read(target.l, long, s, l, &readval_i);
578 if (check_result(src_i, readval_i, target.c, long,
579 s, l, SIGNED_INT_READ_TEST_DESC_FMT_STR,
580 "%X")) {
581 return;
582 }
583
584 init_byte_array(target.c, TEST_LEN, 0xFF);
585 bt_bitfield_write(target.c, signed char, s, l, src_i);
586 bt_bitfield_read(target.ll, long long, s, l, &readval_i);
587 if (check_result(src_i, readval_i, target.c, long long,
588 s, l, SIGNED_INT_READ_TEST_DESC_FMT_STR,
589 "%X")) {
590 return;
591 }
592 }
593 }
594 pass(SIGNED_INT_READ_TEST_DESC_FMT_STR, src_i);
595
596 if (!src_ll)
597 nrbits_ll = 0; /* The number of bits needed to represent 0 is 0. */
598 if (src_ll & 0x8000000000000000ULL)
599 nrbits_ll = fls_u64(~src_ll) + 1; /* Find least significant bit conveying sign */
600 else
601 nrbits_ll = fls_u64(src_ll) + 1; /* Keep sign at 0 */
602
603 /* Read to signed long long readval output. */
604 for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
605 for (l = nrbits_ll; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
606 init_byte_array(target.c, TEST_LEN, 0xFF);
607 bt_bitfield_write(target.c, signed char, s, l, src_ll);
608 bt_bitfield_read(target.c, signed char, s, l, &readval_ll);
609 if (check_result(src_ll, readval_ll, target.c, signed char,
610 s, l, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR,
611 "%llX")) {
612 return;
613 }
614
615 init_byte_array(target.c, TEST_LEN, 0xFF);
616 bt_bitfield_write(target.c, signed char, s, l, src_ll);
617 bt_bitfield_read(target.s, short, s, l, &readval_ll);
618 if (check_result(src_ll, readval_ll, target.c, short,
619 s, l, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR,
620 "%llX")) {
621 return;
622 }
623
624 init_byte_array(target.c, TEST_LEN, 0xFF);
625 bt_bitfield_write(target.c, signed char, s, l, src_ll);
626 bt_bitfield_read(target.i, int, s, l, &readval_ll);
627 if (check_result(src_ll, readval_ll, target.c, int,
628 s, l, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR,
629 "%llX")) {
630 return;
631 }
632
633 init_byte_array(target.c, TEST_LEN, 0xFF);
634 bt_bitfield_write(target.c, signed char, s, l, src_ll);
635 bt_bitfield_read(target.l, long, s, l, &readval_ll);
636 if (check_result(src_ll, readval_ll, target.c, long,
637 s, l, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR,
638 "%llX")) {
639 return;
640 }
641
642 init_byte_array(target.c, TEST_LEN, 0xFF);
643 bt_bitfield_write(target.c, signed char, s, l, src_ll);
644 bt_bitfield_read(target.ll, long long, s, l, &readval_ll);
645 if (check_result(src_ll, readval_ll, target.c, long long,
646 s, l, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR,
647 "%llX")) {
648 return;
649 }
650 }
651 }
652 pass(SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR, src_ll);
653 }
654
655 static
656 void run_test_signed(int src_i, long long src_ll)
657 {
658 run_test_signed_write(src_i, src_ll);
659 run_test_signed_read(src_i, src_ll);
660 }
661
662 static
663 void run_test(void)
664 {
665 int i;
666
667 plan_tests(NR_TESTS * 8 + 24);
668
669 srand(time(NULL));
670
671 run_test_unsigned(0, 0);
672 run_test_signed(0, 0);
673 run_test_unsigned(1, 1);
674 run_test_unsigned(~0U, ~0ULL);
675 run_test_signed(-1U, -1ULL);
676 run_test_signed(0x80000000U, 0x8000000000000000ULL);
677
678 for (i = 0; i < NR_TESTS; i++) {
679 unsigned int src_ui = rand();
680 unsigned long long src_ull = ((unsigned long long) (unsigned int) rand() << 32) |
681 (unsigned long long) (unsigned int) rand();
682
683 run_test_unsigned(src_ui, src_ull);
684 run_test_signed((int) src_ui, (long long) src_ull);
685 }
686 }
687
688 static
689 int print_encodings(unsigned long src, unsigned int shift, unsigned int len)
690 {
691 union {
692 unsigned char c[8];
693 unsigned short s[4];
694 unsigned int i[2];
695 unsigned long l[2];
696 unsigned long long ll[1];
697 } target;
698 unsigned long long readval;
699
700 init_byte_array(target.c, 8, 0xFF);
701 bt_bitfield_write(target.c, unsigned char, shift, len, src);
702 printf("bytewise\n");
703 print_byte_array(target.c, 8);
704
705 init_byte_array(target.c, 8, 0xFF);
706 bt_bitfield_write(target.s, unsigned short, shift, len, src);
707 printf("shortwise\n");
708 print_byte_array(target.c, 8);
709
710 init_byte_array(target.c, 8, 0xFF);
711 bt_bitfield_write(target.i, unsigned int, shift, len, src);
712 printf("intwise\n");
713 print_byte_array(target.c, 8);
714
715 init_byte_array(target.c, 8, 0xFF);
716 bt_bitfield_write(target.l, unsigned long, shift, len, src);
717 printf("longwise\n");
718 print_byte_array(target.c, 8);
719
720 init_byte_array(target.c, 8, 0xFF);
721 bt_bitfield_write(target.ll, unsigned long long, shift, len, src);
722 printf("lluwise\n");
723 print_byte_array(target.c, 8);
724
725 bt_bitfield_read(target.c, unsigned char, shift, len, &readval);
726 printf("read: %llX\n", readval);
727 print_byte_array(target.c, 8);
728
729 return 0;
730 }
731
732 int main(int argc, char **argv)
733 {
734 if (argc > 1) {
735 /* Print encodings */
736 unsigned long src;
737 unsigned int shift, len;
738
739 src = atoi(argv[1]);
740 if (argc > 2)
741 shift = atoi(argv[2]);
742 else
743 shift = 12;
744 if (argc > 3)
745 len = atoi(argv[3]);
746 else
747 len = 40;
748 return print_encodings(src, shift, len);
749 }
750
751 /* Run tap-formated tests */
752 run_test();
753 return exit_status();
754 }
This page took 0.046019 seconds and 4 git commands to generate.