Re-organize sources
[babeltrace.git] / tests / lib / test_bitfield.c
1 /*
2 * test-bitfield.c
3 *
4 * BabelTrace - bitfield test program
5 *
6 * Copyright 2010-2019 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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.
11 *
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.
16 *
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.
20 */
21
22 #include "compat/bitfield.h"
23 #include <time.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26
27 #include <tap/tap.h>
28
29 unsigned int glob;
30
31 /*
32 * This function is only declared to show the size of a bitfield write in
33 * objdump.
34 */
35 void fct(void)
36 {
37 bt_bitfield_write(&glob, unsigned int, 12, 15, 0x12345678);
38 }
39
40 /* Test array size, in bytes */
41 #define TEST_LEN 128
42 #define NR_TESTS 10
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"
53
54 static
55 unsigned int fls_u64(uint64_t x)
56 {
57 unsigned int r = 64;
58
59 if (!x)
60 return 0;
61
62 if (!(x & 0xFFFFFFFF00000000ULL)) {
63 x <<= 32;
64 r -= 32;
65 }
66 if (!(x & 0xFFFF000000000000ULL)) {
67 x <<= 16;
68 r -= 16;
69 }
70 if (!(x & 0xFF00000000000000ULL)) {
71 x <<= 8;
72 r -= 8;
73 }
74 if (!(x & 0xF000000000000000ULL)) {
75 x <<= 4;
76 r -= 4;
77 }
78 if (!(x & 0xC000000000000000ULL)) {
79 x <<= 2;
80 r -= 2;
81 }
82 if (!(x & 0x8000000000000000ULL)) {
83 x <<= 1;
84 r -= 1;
85 }
86 return r;
87 }
88
89 static
90 unsigned int fls_u32(uint32_t x)
91 {
92 unsigned int r = 32;
93
94 if (!x)
95 return 0;
96 if (!(x & 0xFFFF0000U)) {
97 x <<= 16;
98 r -= 16;
99 }
100 if (!(x & 0xFF000000U)) {
101 x <<= 8;
102 r -= 8;
103 }
104 if (!(x & 0xF0000000U)) {
105 x <<= 4;
106 r -= 4;
107 }
108 if (!(x & 0xC0000000U)) {
109 x <<= 2;
110 r -= 2;
111 }
112 if (!(x & 0x80000000U)) {
113 x <<= 1;
114 r -= 1;
115 }
116 return r;
117 }
118
119 #define print_byte_array(c, len) \
120 do { \
121 unsigned long i; \
122 \
123 for (i = 0; i < (len); i++) { \
124 printf("0x%X", (c)[i]); \
125 if (i != (len) - 1) \
126 printf(" "); \
127 } \
128 printf("\n"); \
129 } while (0)
130
131 #define init_byte_array(c, len, val) \
132 do { \
133 unsigned long i; \
134 \
135 for (i = 0; i < (len); i++) \
136 (c)[i] = (val); \
137 } while (0)
138
139 #define check_result(ref, val, buffer, typename, start, len, \
140 desc_fmt_str) \
141 ({ \
142 if ((val) != (ref)) { \
143 fail(desc_fmt_str, ref); \
144 diag(DIAG_FMT_STR, #typename, start, len, val); \
145 printf("# "); \
146 print_byte_array(buffer, TEST_LEN); \
147 } \
148 (val) != (ref); \
149 })
150
151 void run_test_unsigned_write(unsigned int src_ui, unsigned long long src_ull)
152 {
153 unsigned int nrbits_ui, nrbits_ull;
154 union {
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)];
160 } target;
161 unsigned long long readval;
162 unsigned int s, l;
163
164 /* The number of bits needed to represent 0 is 0. */
165 nrbits_ui = fls_u32(src_ui);
166
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)) {
175 return;
176 }
177
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)) {
183 return;
184 }
185
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)) {
191 return;
192 }
193
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)) {
199 return;
200 }
201
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)) {
207 return;
208 }
209 }
210 }
211 pass(UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR, src_ui);
212
213 /* The number of bits needed to represent 0 is 0. */
214 nrbits_ull = fls_u64(src_ull);
215
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)) {
224 return;
225 }
226
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)) {
232 return;
233 }
234
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)) {
240 return;
241 }
242
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)) {
248 return;
249 }
250
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)) {
256 return;
257 }
258 }
259 }
260 pass(UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR, src_ull);
261 }
262
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 return;
288 }
289
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)) {
295 return;
296 }
297
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)) {
303 return;
304 }
305
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)) {
311 return;
312 }
313
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)) {
319 return;
320 }
321 }
322 }
323 pass(UNSIGNED_INT_READ_TEST_DESC_FMT_STR, src_ui);
324
325 /* The number of bits needed to represent 0 is 0. */
326 nrbits_ull = fls_u64(src_ull);
327
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)) {
336 return;
337 }
338
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)) {
344 return;
345 }
346
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)) {
352 return;
353 }
354
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)) {
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.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)) {
368 return;
369 }
370 }
371 }
372 pass(UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR, src_ull);
373 }
374
375 void run_test_unsigned(unsigned int src_ui, unsigned long long src_ull)
376 {
377 run_test_unsigned_write(src_ui, src_ull);
378 run_test_unsigned_read(src_ui, src_ull);
379 }
380
381 void run_test_signed_write(int src_i, long long src_ll)
382 {
383 unsigned int nrbits_i, nrbits_ll;
384 union {
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)];
390 } target;
391 long long readval;
392 unsigned int s, l;
393
394 if (!src_i)
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 */
398 else
399 nrbits_i = fls_u32(src_i) + 1; /* Keep sign at 0 */
400
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)) {
409 return;
410 }
411
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)) {
417 return;
418 }
419
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)) {
425 return;
426 }
427
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)) {
433 return;
434 }
435
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)) {
441 return;
442 }
443 }
444 }
445 pass(SIGNED_INT_WRITE_TEST_DESC_FMT_STR, src_i);
446
447 if (!src_ll)
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 */
451 else
452 nrbits_ll = fls_u64(src_ll) + 1; /* Keep sign at 0 */
453
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)) {
462 return;
463 }
464
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)) {
470 return;
471 }
472
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)) {
478 return;
479 }
480
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)) {
486 return;
487 }
488
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)) {
494 return;
495 }
496 }
497 }
498 pass(SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR, src_ll);
499 }
500
501 void run_test_signed_read(int src_i, long long src_ll)
502 {
503 unsigned int nrbits_i, nrbits_ll;
504 int readval_i;
505 union {
506 unsigned char c[TEST_LEN];
507 unsigned short s[TEST_LEN/sizeof(unsigned short)];
508 unsigned int i[TEST_LEN/sizeof(unsigned int)];
509 unsigned long l[TEST_LEN/sizeof(unsigned long)];
510 unsigned long long ll[TEST_LEN/sizeof(unsigned long long)];
511 } target;
512 long long readval_ll;
513 unsigned int s, l;
514
515 if (!src_i)
516 nrbits_i = 0; /* The number of bits needed to represent 0 is 0. */
517 else if (src_i & 0x80000000U)
518 nrbits_i = fls_u32(~src_i) + 1; /* Find least significant bit conveying sign */
519 else
520 nrbits_i = fls_u32(src_i) + 1; /* Keep sign at 0 */
521
522 /* Read to signed integer readval output. */
523 for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
524 for (l = nrbits_i; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
525 init_byte_array(target.c, TEST_LEN, 0xFF);
526 bt_bitfield_write(target.c, signed char, s, l, src_i);
527 bt_bitfield_read(target.c, signed char, s, l, &readval_i);
528 if (check_result(src_i, readval_i, target.c, signed char,
529 s, l, SIGNED_INT_READ_TEST_DESC_FMT_STR)) {
530 return;
531 }
532
533 init_byte_array(target.c, TEST_LEN, 0xFF);
534 bt_bitfield_write(target.c, signed char, s, l, src_i);
535 bt_bitfield_read(target.s, short, s, l, &readval_i);
536 if (check_result(src_i, readval_i, target.c, short,
537 s, l, SIGNED_INT_READ_TEST_DESC_FMT_STR)) {
538 return;
539 }
540
541 init_byte_array(target.c, TEST_LEN, 0xFF);
542 bt_bitfield_write(target.c, signed char, s, l, src_i);
543 bt_bitfield_read(target.i, int, s, l, &readval_i);
544 if (check_result(src_i, readval_i, target.c, int,
545 s, l, SIGNED_INT_READ_TEST_DESC_FMT_STR)) {
546 return;
547 }
548
549 init_byte_array(target.c, TEST_LEN, 0xFF);
550 bt_bitfield_write(target.c, signed char, s, l, src_i);
551 bt_bitfield_read(target.l, long, s, l, &readval_i);
552 if (check_result(src_i, readval_i, target.c, long,
553 s, l, SIGNED_INT_READ_TEST_DESC_FMT_STR)) {
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.ll, long long, s, l, &readval_i);
560 if (check_result(src_i, readval_i, target.c, long long,
561 s, l, SIGNED_INT_READ_TEST_DESC_FMT_STR)) {
562 return;
563 }
564 }
565 }
566 pass(SIGNED_INT_READ_TEST_DESC_FMT_STR, src_i);
567
568 if (!src_ll)
569 nrbits_ll = 0; /* The number of bits needed to represent 0 is 0. */
570 if (src_ll & 0x8000000000000000ULL)
571 nrbits_ll = fls_u64(~src_ll) + 1; /* Find least significant bit conveying sign */
572 else
573 nrbits_ll = fls_u64(src_ll) + 1; /* Keep sign at 0 */
574
575 /* Read to signed long long readval output. */
576 for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
577 for (l = nrbits_ll; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
578 init_byte_array(target.c, TEST_LEN, 0xFF);
579 bt_bitfield_write(target.c, signed char, s, l, src_ll);
580 bt_bitfield_read(target.c, signed char, s, l, &readval_ll);
581 if (check_result(src_ll, readval_ll, target.c, signed char,
582 s, l, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR)) {
583 return;
584 }
585
586 init_byte_array(target.c, TEST_LEN, 0xFF);
587 bt_bitfield_write(target.c, signed char, s, l, src_ll);
588 bt_bitfield_read(target.s, short, s, l, &readval_ll);
589 if (check_result(src_ll, readval_ll, target.c, short,
590 s, l, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR)) {
591 return;
592 }
593
594 init_byte_array(target.c, TEST_LEN, 0xFF);
595 bt_bitfield_write(target.c, signed char, s, l, src_ll);
596 bt_bitfield_read(target.i, int, s, l, &readval_ll);
597 if (check_result(src_ll, readval_ll, target.c, int,
598 s, l, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR)) {
599 return;
600 }
601
602 init_byte_array(target.c, TEST_LEN, 0xFF);
603 bt_bitfield_write(target.c, signed char, s, l, src_ll);
604 bt_bitfield_read(target.l, long, s, l, &readval_ll);
605 if (check_result(src_ll, readval_ll, target.c, long,
606 s, l, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR)) {
607 return;
608 }
609
610 init_byte_array(target.c, TEST_LEN, 0xFF);
611 bt_bitfield_write(target.c, signed char, s, l, src_ll);
612 bt_bitfield_read(target.ll, long long, s, l, &readval_ll);
613 if (check_result(src_ll, readval_ll, target.c, long long,
614 s, l, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR)) {
615 return;
616 }
617 }
618 }
619 pass(SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR, src_ll);
620 }
621
622 void run_test_signed(int src_i, long long src_ll)
623 {
624 run_test_signed_write(src_i, src_ll);
625 run_test_signed_read(src_i, src_ll);
626 }
627
628 void run_test(void)
629 {
630 int i;
631
632 plan_tests(NR_TESTS * 8 + 24);
633
634 srand(time(NULL));
635
636 run_test_unsigned(0, 0);
637 run_test_signed(0, 0);
638 run_test_unsigned(1, 1);
639 run_test_unsigned(~0U, ~0ULL);
640 run_test_signed(-1U, -1ULL);
641 run_test_signed(0x80000000U, 0x8000000000000000ULL);
642
643 for (i = 0; i < NR_TESTS; i++) {
644 unsigned int src_ui = rand();
645 unsigned long long src_ull = ((unsigned long long) (unsigned int) rand() << 32) |
646 (unsigned long long) (unsigned int) rand();
647
648 run_test_unsigned(src_ui, src_ull);
649 run_test_signed((int) src_ui, (long long) src_ull);
650 }
651 }
652
653 static
654 int print_encodings(unsigned long src, unsigned int shift, unsigned int len)
655 {
656 union {
657 unsigned char c[8];
658 unsigned short s[4];
659 unsigned int i[2];
660 unsigned long l[2];
661 unsigned long long ll[1];
662 } target;
663 unsigned long long readval;
664
665 init_byte_array(target.c, 8, 0xFF);
666 bt_bitfield_write(target.c, unsigned char, shift, len, src);
667 printf("bytewise\n");
668 print_byte_array(target.c, 8);
669
670 init_byte_array(target.c, 8, 0xFF);
671 bt_bitfield_write(target.s, unsigned short, shift, len, src);
672 printf("shortwise\n");
673 print_byte_array(target.c, 8);
674
675 init_byte_array(target.c, 8, 0xFF);
676 bt_bitfield_write(target.i, unsigned int, shift, len, src);
677 printf("intwise\n");
678 print_byte_array(target.c, 8);
679
680 init_byte_array(target.c, 8, 0xFF);
681 bt_bitfield_write(target.l, unsigned long, shift, len, src);
682 printf("longwise\n");
683 print_byte_array(target.c, 8);
684
685 init_byte_array(target.c, 8, 0xFF);
686 bt_bitfield_write(target.ll, unsigned long long, shift, len, src);
687 printf("lluwise\n");
688 print_byte_array(target.c, 8);
689
690 bt_bitfield_read(target.c, unsigned char, shift, len, &readval);
691 printf("read: %llX\n", readval);
692 print_byte_array(target.c, 8);
693
694 return 0;
695 }
696
697 int main(int argc, char **argv)
698 {
699 if (argc > 1) {
700 /* Print encodings */
701 unsigned long src;
702 unsigned int shift, len;
703
704 src = atoi(argv[1]);
705 if (argc > 2)
706 shift = atoi(argv[2]);
707 else
708 shift = 12;
709 if (argc > 3)
710 len = atoi(argv[3]);
711 else
712 len = 40;
713 return print_encodings(src, shift, len);
714 }
715
716 /* Run tap-formated tests */
717 run_test();
718 return exit_status();
719 }
This page took 0.045246 seconds and 4 git commands to generate.