4 * BabelTrace - bitfield test program
6 * Copyright 2010-2019 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <babeltrace/bitfield-internal.h>
32 * This function is only declared to show the size of a bitfield write in
37 bt_bitfield_write(&glob
, unsigned int, 12, 15, 0x12345678);
40 /* Test array size, in bytes */
43 #define SIGNED_INT_READ_TEST_DESC_FMT_STR "Writing and reading back 0x%X, signed int dest, varying read unit size"
44 #define SIGNED_INT_WRITE_TEST_DESC_FMT_STR "Writing and reading back 0x%X, signed int source, varying write unit size"
45 #define SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR "Writing and reading back 0x%llX, signed long long dest, varying read unit size"
46 #define SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR "Writing and reading back 0x%llX, signed long long source, varying write unit size"
47 #define UNSIGNED_INT_READ_TEST_DESC_FMT_STR "Writing and reading back 0x%X, unsigned int dest, varying read unit size"
48 #define UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR "Writing and reading back 0x%X, unsigned int source, varying write unit size"
49 #define UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR "Writing and reading back 0x%llX, unsigned long long dest, varying read unit size"
50 #define UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR "Writing and reading back 0x%llX, unsigned long long source, varying write unit size"
51 #define DIAG_FMT_STR "Failed reading value written \"%s\"-wise, with start=%i" \
52 " and length=%i. Read %llX"
55 unsigned int fls_u64(uint64_t x
)
62 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
66 if (!(x
& 0xFFFF000000000000ULL
)) {
70 if (!(x
& 0xFF00000000000000ULL
)) {
74 if (!(x
& 0xF000000000000000ULL
)) {
78 if (!(x
& 0xC000000000000000ULL
)) {
82 if (!(x
& 0x8000000000000000ULL
)) {
90 unsigned int fls_u32(uint32_t x
)
96 if (!(x
& 0xFFFF0000U
)) {
100 if (!(x
& 0xFF000000U
)) {
104 if (!(x
& 0xF0000000U
)) {
108 if (!(x
& 0xC0000000U
)) {
112 if (!(x
& 0x80000000U
)) {
119 #define print_byte_array(c, len) \
123 for (i = 0; i < (len); i++) { \
124 printf("0x%X", (c)[i]); \
125 if (i != (len) - 1) \
131 #define init_byte_array(c, len, val) \
135 for (i = 0; i < (len); i++) \
139 #define check_result(ref, val, buffer, typename, start, len, \
142 if ((val) != (ref)) { \
143 fail(desc_fmt_str, ref); \
144 diag(DIAG_FMT_STR, #typename, start, len, val); \
146 print_byte_array(buffer, TEST_LEN); \
151 void run_test_unsigned_write(unsigned int src_ui
, unsigned long long src_ull
)
153 unsigned int nrbits_ui
, nrbits_ull
;
155 unsigned char c
[TEST_LEN
];
156 unsigned short s
[TEST_LEN
/sizeof(unsigned short)];
157 unsigned int i
[TEST_LEN
/sizeof(unsigned int)];
158 unsigned long l
[TEST_LEN
/sizeof(unsigned long)];
159 unsigned long long ll
[TEST_LEN
/sizeof(unsigned long long)];
161 unsigned long long readval
;
164 /* The number of bits needed to represent 0 is 0. */
165 nrbits_ui
= fls_u32(src_ui
);
167 /* Write from unsigned integer src input. */
168 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
169 for (l
= nrbits_ui
; l
<= (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
170 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
171 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ui
);
172 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
173 if (check_result(src_ui
, readval
, target
.c
, unsigned char,
174 s
, l
, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
178 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
179 bt_bitfield_write(target
.s
, unsigned short, s
, l
, src_ui
);
180 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
181 if (check_result(src_ui
, readval
, target
.c
, unsigned short,
182 s
, l
, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
186 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
187 bt_bitfield_write(target
.i
, unsigned int, 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 int,
190 s
, l
, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
194 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
195 bt_bitfield_write(target
.l
, unsigned long, s
, l
, src_ui
);
196 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
197 if (check_result(src_ui
, readval
, target
.c
, unsigned long,
198 s
, l
, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
202 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
203 bt_bitfield_write(target
.ll
, unsigned long long, s
, l
, src_ui
);
204 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
205 if (check_result(src_ui
, readval
, target
.c
, unsigned long long,
206 s
, l
, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
211 pass(UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
, src_ui
);
213 /* The number of bits needed to represent 0 is 0. */
214 nrbits_ull
= fls_u64(src_ull
);
216 /* Write from unsigned long long src input. */
217 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
218 for (l
= nrbits_ull
; l
<= (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
219 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
220 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ull
);
221 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
222 if (check_result(src_ull
, readval
, target
.c
, unsigned char,
223 s
, l
, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
227 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
228 bt_bitfield_write(target
.s
, unsigned short, s
, l
, src_ull
);
229 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
230 if (check_result(src_ull
, readval
, target
.c
, unsigned short,
231 s
, l
, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
235 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
236 bt_bitfield_write(target
.i
, unsigned int, s
, l
, src_ull
);
237 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
238 if (check_result(src_ull
, readval
, target
.c
, unsigned int,
239 s
, l
, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
243 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
244 bt_bitfield_write(target
.l
, unsigned long, s
, l
, src_ull
);
245 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
246 if (check_result(src_ull
, readval
, target
.c
, unsigned long,
247 s
, l
, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
251 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
252 bt_bitfield_write(target
.ll
, unsigned long long, s
, l
, src_ull
);
253 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
254 if (check_result(src_ull
, readval
, target
.c
, unsigned long long,
255 s
, l
, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
260 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
)) {
290 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
291 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ui
);
292 bt_bitfield_read(target
.s
, unsigned short, s
, l
, &readval_ui
);
293 if (check_result(src_ui
, readval_ui
, target
.c
, unsigned short,
294 s
, l
, UNSIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
298 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
299 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ui
);
300 bt_bitfield_read(target
.i
, unsigned int, s
, l
, &readval_ui
);
301 if (check_result(src_ui
, readval_ui
, target
.c
, unsigned int,
302 s
, l
, UNSIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
306 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
307 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ui
);
308 bt_bitfield_read(target
.l
, unsigned long, s
, l
, &readval_ui
);
309 if (check_result(src_ui
, readval_ui
, target
.c
, unsigned long,
310 s
, l
, UNSIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
314 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
315 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ui
);
316 bt_bitfield_read(target
.ll
, unsigned long long, s
, l
, &readval_ui
);
317 if (check_result(src_ui
, readval_ui
, target
.c
, unsigned long long,
318 s
, l
, UNSIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
323 pass(UNSIGNED_INT_READ_TEST_DESC_FMT_STR
, src_ui
);
325 /* The number of bits needed to represent 0 is 0. */
326 nrbits_ull
= fls_u64(src_ull
);
328 /* Read to unsigned long long readval output. */
329 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
330 for (l
= nrbits_ull
; l
<= (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
331 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
332 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ull
);
333 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval_ull
);
334 if (check_result(src_ull
, readval_ull
, target
.c
, unsigned char,
335 s
, l
, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
339 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
340 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ull
);
341 bt_bitfield_read(target
.s
, unsigned short, s
, l
, &readval_ull
);
342 if (check_result(src_ull
, readval_ull
, target
.c
, unsigned short,
343 s
, l
, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
347 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
348 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ull
);
349 bt_bitfield_read(target
.i
, unsigned int, s
, l
, &readval_ull
);
350 if (check_result(src_ull
, readval_ull
, target
.c
, unsigned int,
351 s
, l
, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
355 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
356 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ull
);
357 bt_bitfield_read(target
.l
, unsigned long, s
, l
, &readval_ull
);
358 if (check_result(src_ull
, readval_ull
, target
.c
, unsigned long,
359 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
.ll
, unsigned long long, s
, l
, &readval_ull
);
366 if (check_result(src_ull
, readval_ull
, target
.c
, unsigned long long,
367 s
, l
, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
372 pass(UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
, src_ull
);
375 void run_test_unsigned(unsigned int src_ui
, unsigned long long src_ull
)
377 run_test_unsigned_write(src_ui
, src_ull
);
378 run_test_unsigned_read(src_ui
, src_ull
);
381 void run_test_signed_write(int src_i
, long long src_ll
)
383 int nrbits_i
, nrbits_ll
;
385 signed char c
[TEST_LEN
];
386 short s
[TEST_LEN
/sizeof(short)];
387 int i
[TEST_LEN
/sizeof(int)];
388 long l
[TEST_LEN
/sizeof(long)];
389 long long ll
[TEST_LEN
/sizeof(long long)];
395 nrbits_i
= 0; /* The number of bits needed to represent 0 is 0. */
396 else if (src_i
& 0x80000000U
)
397 nrbits_i
= fls_u32(~src_i
) + 1; /* Find least significant bit conveying sign */
399 nrbits_i
= fls_u32(src_i
) + 1; /* Keep sign at 0 */
401 /* Write from signed integer src input. */
402 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
403 for (l
= nrbits_i
; l
<= (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
404 init_byte_array(target
.c
, TEST_LEN
, 0x0);
405 bt_bitfield_write(target
.c
, signed char, s
, l
, src_i
);
406 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
407 if (check_result(src_i
, readval
, target
.c
, signed char,
408 s
, l
, SIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
412 init_byte_array(target
.c
, TEST_LEN
, 0x0);
413 bt_bitfield_write(target
.s
, short, s
, l
, src_i
);
414 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
415 if (check_result(src_i
, readval
, target
.c
, short,
416 s
, l
, SIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
420 init_byte_array(target
.c
, TEST_LEN
, 0x0);
421 bt_bitfield_write(target
.i
, int, s
, l
, src_i
);
422 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
423 if (check_result(src_i
, readval
, target
.c
, int,
424 s
, l
, SIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
428 init_byte_array(target
.c
, TEST_LEN
, 0x0);
429 bt_bitfield_write(target
.l
, long, s
, l
, src_i
);
430 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
431 if (check_result(src_i
, readval
, target
.c
, long,
432 s
, l
, SIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
436 init_byte_array(target
.c
, TEST_LEN
, 0x0);
437 bt_bitfield_write(target
.ll
, long long, s
, l
, src_i
);
438 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
439 if (check_result(src_i
, readval
, target
.c
, long long,
440 s
, l
, SIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
445 pass(SIGNED_INT_WRITE_TEST_DESC_FMT_STR
, src_i
);
448 nrbits_ll
= 0; /* The number of bits needed to represent 0 is 0. */
449 else if (src_ll
& 0x8000000000000000ULL
)
450 nrbits_ll
= fls_u64(~src_ll
) + 1; /* Find least significant bit conveying sign */
452 nrbits_ll
= fls_u64(src_ll
) + 1; /* Keep sign at 0 */
454 /* Write from signed long long src input. */
455 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
456 for (l
= nrbits_ll
; l
<= (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
457 init_byte_array(target
.c
, TEST_LEN
, 0x0);
458 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
459 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
460 if (check_result(src_ll
, readval
, target
.c
, signed char,
461 s
, l
, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
465 init_byte_array(target
.c
, TEST_LEN
, 0x0);
466 bt_bitfield_write(target
.s
, short, s
, l
, src_ll
);
467 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
468 if (check_result(src_ll
, readval
, target
.c
, short,
469 s
, l
, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
473 init_byte_array(target
.c
, TEST_LEN
, 0x0);
474 bt_bitfield_write(target
.i
, int, s
, l
, src_ll
);
475 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
476 if (check_result(src_ll
, readval
, target
.c
, int,
477 s
, l
, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
481 init_byte_array(target
.c
, TEST_LEN
, 0x0);
482 bt_bitfield_write(target
.l
, long, s
, l
, src_ll
);
483 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
484 if (check_result(src_ll
, readval
, target
.c
, long,
485 s
, l
, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
489 init_byte_array(target
.c
, TEST_LEN
, 0x0);
490 bt_bitfield_write(target
.ll
, long long, s
, l
, src_ll
);
491 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
492 if (check_result(src_ll
, readval
, target
.c
, long long,
493 s
, l
, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
498 pass(SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
, src_ll
);
501 void run_test_signed_read(int src_i
, long long src_ll
)
503 int nrbits_i
, nrbits_ll
, readval_i
;
505 unsigned char c
[TEST_LEN
];
506 unsigned short s
[TEST_LEN
/sizeof(unsigned short)];
507 unsigned int i
[TEST_LEN
/sizeof(unsigned int)];
508 unsigned long l
[TEST_LEN
/sizeof(unsigned long)];
509 unsigned long long ll
[TEST_LEN
/sizeof(unsigned long long)];
511 long long readval_ll
;
515 nrbits_i
= 0; /* The number of bits needed to represent 0 is 0. */
516 else if (src_i
& 0x80000000U
)
517 nrbits_i
= fls_u32(~src_i
) + 1; /* Find least significant bit conveying sign */
519 nrbits_i
= fls_u32(src_i
) + 1; /* Keep sign at 0 */
521 /* Read to signed integer readval output. */
522 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
523 for (l
= nrbits_i
; l
<= (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
524 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
525 bt_bitfield_write(target
.c
, signed char, s
, l
, src_i
);
526 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval_i
);
527 if (check_result(src_i
, readval_i
, target
.c
, signed char,
528 s
, l
, SIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
532 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
533 bt_bitfield_write(target
.c
, signed char, s
, l
, src_i
);
534 bt_bitfield_read(target
.s
, short, s
, l
, &readval_i
);
535 if (check_result(src_i
, readval_i
, target
.c
, short,
536 s
, l
, SIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
540 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
541 bt_bitfield_write(target
.c
, signed char, s
, l
, src_i
);
542 bt_bitfield_read(target
.i
, int, s
, l
, &readval_i
);
543 if (check_result(src_i
, readval_i
, target
.c
, int,
544 s
, l
, SIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
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
.l
, long, s
, l
, &readval_i
);
551 if (check_result(src_i
, readval_i
, target
.c
, long,
552 s
, l
, SIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
556 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
557 bt_bitfield_write(target
.c
, signed char, s
, l
, src_i
);
558 bt_bitfield_read(target
.ll
, long long, s
, l
, &readval_i
);
559 if (check_result(src_i
, readval_i
, target
.c
, long long,
560 s
, l
, SIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
565 pass(SIGNED_INT_READ_TEST_DESC_FMT_STR
, src_i
);
568 nrbits_ll
= 0; /* The number of bits needed to represent 0 is 0. */
569 if (src_ll
& 0x8000000000000000ULL
)
570 nrbits_ll
= fls_u64(~src_ll
) + 1; /* Find least significant bit conveying sign */
572 nrbits_ll
= fls_u64(src_ll
) + 1; /* Keep sign at 0 */
574 /* Read to signed long long readval output. */
575 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
576 for (l
= nrbits_ll
; l
<= (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
577 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
578 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
579 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval_ll
);
580 if (check_result(src_ll
, readval_ll
, target
.c
, signed char,
581 s
, l
, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
585 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
586 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
587 bt_bitfield_read(target
.s
, short, s
, l
, &readval_ll
);
588 if (check_result(src_ll
, readval_ll
, target
.c
, short,
589 s
, l
, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
593 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
594 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
595 bt_bitfield_read(target
.i
, int, s
, l
, &readval_ll
);
596 if (check_result(src_ll
, readval_ll
, target
.c
, int,
597 s
, l
, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
601 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
602 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
603 bt_bitfield_read(target
.l
, long, s
, l
, &readval_ll
);
604 if (check_result(src_ll
, readval_ll
, target
.c
, long,
605 s
, l
, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
609 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
610 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
611 bt_bitfield_read(target
.ll
, long long, s
, l
, &readval_ll
);
612 if (check_result(src_ll
, readval_ll
, target
.c
, long long,
613 s
, l
, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
618 pass(SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
, src_ll
);
621 void run_test_signed(int src_i
, long long src_ll
)
623 run_test_signed_write(src_i
, src_ll
);
624 run_test_signed_read(src_i
, src_ll
);
631 plan_tests(NR_TESTS
* 8 + 24);
635 run_test_unsigned(0, 0);
636 run_test_signed(0, 0);
637 run_test_unsigned(1, 1);
638 run_test_unsigned(~0U, ~0ULL);
639 run_test_signed(-1U, -1ULL);
640 run_test_signed(0x80000000U
, 0x8000000000000000ULL
);
642 for (i
= 0; i
< NR_TESTS
; i
++) {
643 unsigned int src_ui
= rand();
644 unsigned long long src_ull
= ((unsigned long long) (unsigned int) rand() << 32) |
645 (unsigned long long) (unsigned int) rand();
647 run_test_unsigned(src_ui
, src_ull
);
648 run_test_signed((int) src_ui
, (long long) src_ull
);
653 int print_encodings(unsigned long src
, unsigned int shift
, unsigned int len
)
660 unsigned long long ll
[1];
662 unsigned long long readval
;
664 init_byte_array(target
.c
, 8, 0xFF);
665 bt_bitfield_write(target
.c
, unsigned char, shift
, len
, src
);
666 printf("bytewise\n");
667 print_byte_array(target
.c
, 8);
669 init_byte_array(target
.c
, 8, 0xFF);
670 bt_bitfield_write(target
.s
, unsigned short, shift
, len
, src
);
671 printf("shortwise\n");
672 print_byte_array(target
.c
, 8);
674 init_byte_array(target
.c
, 8, 0xFF);
675 bt_bitfield_write(target
.i
, unsigned int, shift
, len
, src
);
677 print_byte_array(target
.c
, 8);
679 init_byte_array(target
.c
, 8, 0xFF);
680 bt_bitfield_write(target
.l
, unsigned long, shift
, len
, src
);
681 printf("longwise\n");
682 print_byte_array(target
.c
, 8);
684 init_byte_array(target
.c
, 8, 0xFF);
685 bt_bitfield_write(target
.ll
, unsigned long long, shift
, len
, src
);
687 print_byte_array(target
.c
, 8);
689 bt_bitfield_read(target
.c
, unsigned char, shift
, len
, &readval
);
690 printf("read: %llX\n", readval
);
691 print_byte_array(target
.c
, 8);
696 int main(int argc
, char **argv
)
699 /* Print encodings */
701 unsigned int shift
, len
;
705 shift
= atoi(argv
[2]);
712 return print_encodings(src
, shift
, len
);
715 /* Run tap-formated tests */
717 return exit_status();