2 * SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2010-2019 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * BabelTrace - bitfield test program
9 #include "compat/bitfield.h"
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.
25 bt_bitfield_write(&glob
, unsigned int, 12, 15, 0x12345678);
28 /* Test array size, in bytes */
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
43 unsigned int fls_u64(uint64_t x
)
50 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
54 if (!(x
& 0xFFFF000000000000ULL
)) {
58 if (!(x
& 0xFF00000000000000ULL
)) {
62 if (!(x
& 0xF000000000000000ULL
)) {
66 if (!(x
& 0xC000000000000000ULL
)) {
70 if (!(x
& 0x8000000000000000ULL
)) {
78 unsigned int fls_u32(uint32_t x
)
84 if (!(x
& 0xFFFF0000U
)) {
88 if (!(x
& 0xFF000000U
)) {
92 if (!(x
& 0xF0000000U
)) {
96 if (!(x
& 0xC0000000U
)) {
100 if (!(x
& 0x80000000U
)) {
107 #define print_byte_array(c, len) \
111 for (i = 0; i < (len); i++) { \
112 printf("0x%X", (c)[i]); \
113 if (i != (len) - 1) \
119 #define init_byte_array(c, len, val) \
123 for (i = 0; i < (len); i++) \
127 #define check_result(ref, val, buffer, typename, start, len, \
128 desc_fmt_str, val_type_fmt) \
130 if ((val) != (ref)) { \
131 fail(desc_fmt_str, ref); \
132 diag(DIAG_FMT_STR(val_type_fmt), #typename, start, len, val); \
134 print_byte_array(buffer, TEST_LEN); \
140 void run_test_unsigned_write(unsigned int src_ui
, unsigned long long src_ull
)
142 unsigned int nrbits_ui
, nrbits_ull
;
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)];
150 unsigned long long readval
;
153 /* The number of bits needed to represent 0 is 0. */
154 nrbits_ui
= fls_u32(src_ui
);
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
,
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
,
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
,
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
,
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
,
205 pass(UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
, src_ui
);
207 /* The number of bits needed to represent 0 is 0. */
208 nrbits_ull
= fls_u64(src_ull
);
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
,
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
,
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
,
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
,
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
,
259 pass(UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
, src_ull
);
263 void run_test_unsigned_read(unsigned int src_ui
, unsigned long long src_ull
)
265 unsigned int nrbits_ui
, nrbits_ull
, readval_ui
;
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)];
273 unsigned long long readval_ull
;
276 /* The number of bits needed to represent 0 is 0. */
277 nrbits_ui
= fls_u32(src_ui
);
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
,
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
,
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
,
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
,
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
,
328 pass(UNSIGNED_INT_READ_TEST_DESC_FMT_STR
, src_ui
);
330 /* The number of bits needed to represent 0 is 0. */
331 nrbits_ull
= fls_u64(src_ull
);
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
,
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
,
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
,
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
,
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
,
382 pass(UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
, src_ull
);
386 void run_test_unsigned(unsigned int src_ui
, unsigned long long src_ull
)
388 run_test_unsigned_write(src_ui
, src_ull
);
389 run_test_unsigned_read(src_ui
, src_ull
);
393 void run_test_signed_write(int src_i
, long long src_ll
)
395 unsigned int nrbits_i
, nrbits_ll
;
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)];
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 */
411 nrbits_i
= fls_u32(src_i
) + 1; /* Keep sign at 0 */
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
,
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
,
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
,
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
,
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
,
462 pass(SIGNED_INT_WRITE_TEST_DESC_FMT_STR
, src_i
);
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 */
469 nrbits_ll
= fls_u64(src_ll
) + 1; /* Keep sign at 0 */
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
,
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
,
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
,
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
,
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
,
520 pass(SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
, src_ll
);
524 void run_test_signed_read(int src_i
, long long src_ll
)
526 unsigned int nrbits_i
, nrbits_ll
;
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)];
535 long long readval_ll
;
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 */
543 nrbits_i
= fls_u32(src_i
) + 1; /* Keep sign at 0 */
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
,
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
,
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
,
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
,
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
,
594 pass(SIGNED_INT_READ_TEST_DESC_FMT_STR
, src_i
);
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 */
601 nrbits_ll
= fls_u64(src_ll
) + 1; /* Keep sign at 0 */
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
,
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
,
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
,
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
,
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
,
652 pass(SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
, src_ll
);
656 void run_test_signed(int src_i
, long long src_ll
)
658 run_test_signed_write(src_i
, src_ll
);
659 run_test_signed_read(src_i
, src_ll
);
667 plan_tests(NR_TESTS
* 8 + 24);
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
);
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();
683 run_test_unsigned(src_ui
, src_ull
);
684 run_test_signed((int) src_ui
, (long long) src_ull
);
689 int print_encodings(unsigned long src
, unsigned int shift
, unsigned int len
)
696 unsigned long long ll
[1];
698 unsigned long long readval
;
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);
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);
710 init_byte_array(target
.c
, 8, 0xFF);
711 bt_bitfield_write(target
.i
, unsigned int, shift
, len
, src
);
713 print_byte_array(target
.c
, 8);
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);
720 init_byte_array(target
.c
, 8, 0xFF);
721 bt_bitfield_write(target
.ll
, unsigned long long, shift
, len
, src
);
723 print_byte_array(target
.c
, 8);
725 bt_bitfield_read(target
.c
, unsigned char, shift
, len
, &readval
);
726 printf("read: %llX\n", readval
);
727 print_byte_array(target
.c
, 8);
732 int main(int argc
, char **argv
)
735 /* Print encodings */
737 unsigned int shift
, len
;
741 shift
= atoi(argv
[2]);
748 return print_encodings(src
, shift
, len
);
751 /* Run tap-formated tests */
753 return exit_status();