4 * BabelTrace - bitfield test program
6 * Copyright 2010 - 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.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_TEST_DESC_FMT_STR "Writing and reading back 0x%X, signed"
44 #define UNSIGNED_TEST_DESC_FMT_STR "Writing and reading back 0x%X, unsigned"
45 #define DIAG_FMT_STR "Failed reading value written \"%s\"-wise, with start=%i" \
46 " and length=%i. Read %llX"
50 #if defined(__i386) || defined(__x86_64)
52 static inline int fls(int x
)
57 : "=&r" (r
) : "rm" (x
), "rm" (-1));
61 #elif defined(__PPC__)
63 static __inline__
int fls(unsigned int x
)
67 asm ("cntlzw %0,%1" : "=r" (lz
) : "r" (x
));
73 static int fls(unsigned int x
)
79 if (!(x
& 0xFFFF0000U
)) {
83 if (!(x
& 0xFF000000U
)) {
87 if (!(x
& 0xF0000000U
)) {
91 if (!(x
& 0xC0000000U
)) {
95 if (!(x
& 0x80000000U
)) {
104 #define print_byte_array(c, len) \
108 for (i = 0; i < (len); i++) { \
109 printf("0x%X", (c)[i]); \
110 if (i != (len) - 1) \
116 #define init_byte_array(c, len, val) \
120 for (i = 0; i < (len); i++) \
124 #define check_result(ref, val, buffer, typename, start, len, \
127 if ((val) != (ref)) { \
128 fail(desc_fmt_str, ref); \
129 diag(DIAG_FMT_STR, #typename, start, len, val); \
131 print_byte_array(buffer, TEST_LEN); \
136 void run_test_unsigned(void)
138 unsigned int src
, nrbits
;
140 unsigned char c
[TEST_LEN
];
141 unsigned short s
[TEST_LEN
/sizeof(unsigned short)];
142 unsigned int i
[TEST_LEN
/sizeof(unsigned int)];
143 unsigned long l
[TEST_LEN
/sizeof(unsigned long)];
144 unsigned long long ll
[TEST_LEN
/sizeof(unsigned long long)];
146 unsigned long long readval
;
152 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
153 for (l
= nrbits
; l
< (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
154 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
155 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src
);
156 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
157 if (check_result(src
, readval
, target
.c
, unsigned char,
158 s
, l
, UNSIGNED_TEST_DESC_FMT_STR
)) {
162 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
163 bt_bitfield_write(target
.s
, unsigned short, s
, l
, src
);
164 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
165 if (check_result(src
, readval
, target
.c
, unsigned short,
166 s
, l
, UNSIGNED_TEST_DESC_FMT_STR
)) {
170 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
171 bt_bitfield_write(target
.i
, unsigned int, s
, l
, src
);
172 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
173 if (check_result(src
, readval
, target
.c
, unsigned int,
174 s
, l
, UNSIGNED_TEST_DESC_FMT_STR
)) {
178 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
179 bt_bitfield_write(target
.l
, unsigned long, s
, l
, src
);
180 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
181 if (check_result(src
, readval
, target
.c
, unsigned long,
182 s
, l
, UNSIGNED_TEST_DESC_FMT_STR
)) {
186 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
187 bt_bitfield_write(target
.ll
, unsigned long long, s
, l
, src
);
188 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
189 if (check_result(src
, readval
, target
.c
, unsigned long long,
190 s
, l
, UNSIGNED_TEST_DESC_FMT_STR
)) {
196 pass(UNSIGNED_TEST_DESC_FMT_STR
, src
);
199 void run_test_signed(void)
203 signed char c
[TEST_LEN
];
204 short s
[TEST_LEN
/sizeof(short)];
205 int i
[TEST_LEN
/sizeof(int)];
206 long l
[TEST_LEN
/sizeof(long)];
207 long long ll
[TEST_LEN
/sizeof(long long)];
213 if (src
& 0x80000000U
)
214 nrbits
= fls(~src
) + 1; /* Find least significant bit conveying sign */
216 nrbits
= fls(src
) + 1; /* Keep sign at 0 */
218 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
219 for (l
= nrbits
; l
< (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
220 init_byte_array(target
.c
, TEST_LEN
, 0x0);
221 bt_bitfield_write(target
.c
, signed char, s
, l
, src
);
222 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
223 if (check_result(src
, readval
, target
.c
, signed char,
224 s
, l
, SIGNED_TEST_DESC_FMT_STR
)) {
228 init_byte_array(target
.c
, TEST_LEN
, 0x0);
229 bt_bitfield_write(target
.s
, short, s
, l
, src
);
230 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
231 if (check_result(src
, readval
, target
.c
, short,
232 s
, l
, SIGNED_TEST_DESC_FMT_STR
)) {
236 init_byte_array(target
.c
, TEST_LEN
, 0x0);
237 bt_bitfield_write(target
.i
, int, s
, l
, src
);
238 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
239 if (check_result(src
, readval
, target
.c
, int,
240 s
, l
, SIGNED_TEST_DESC_FMT_STR
)) {
244 init_byte_array(target
.c
, TEST_LEN
, 0x0);
245 bt_bitfield_write(target
.l
, long, s
, l
, src
);
246 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
247 if (check_result(src
, readval
, target
.c
, long,
248 s
, l
, SIGNED_TEST_DESC_FMT_STR
)) {
252 init_byte_array(target
.c
, TEST_LEN
, 0x0);
253 bt_bitfield_write(target
.ll
, long long, s
, l
, src
);
254 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
255 if (check_result(src
, readval
, target
.c
, long long,
256 s
, l
, SIGNED_TEST_DESC_FMT_STR
)) {
262 pass(SIGNED_TEST_DESC_FMT_STR
, src
);
268 plan_tests(NR_TESTS
* 2 + 6);
286 srcrand
= (int)0x80000000U
;
289 for (i
= 0; i
< NR_TESTS
; i
++) {
297 int print_encodings(unsigned long src
, unsigned int shift
, unsigned int len
)
304 unsigned long long ll
[1];
306 unsigned long long readval
;
308 init_byte_array(target
.c
, 8, 0xFF);
309 bt_bitfield_write(target
.c
, unsigned char, shift
, len
, src
);
310 printf("bytewise\n");
311 print_byte_array(target
.c
, 8);
313 init_byte_array(target
.c
, 8, 0xFF);
314 bt_bitfield_write(target
.s
, unsigned short, shift
, len
, src
);
315 printf("shortwise\n");
316 print_byte_array(target
.c
, 8);
318 init_byte_array(target
.c
, 8, 0xFF);
319 bt_bitfield_write(target
.i
, unsigned int, shift
, len
, src
);
321 print_byte_array(target
.c
, 8);
323 init_byte_array(target
.c
, 8, 0xFF);
324 bt_bitfield_write(target
.l
, unsigned long, shift
, len
, src
);
325 printf("longwise\n");
326 print_byte_array(target
.c
, 8);
328 init_byte_array(target
.c
, 8, 0xFF);
329 bt_bitfield_write(target
.ll
, unsigned long long, shift
, len
, src
);
331 print_byte_array(target
.c
, 8);
333 bt_bitfield_read(target
.c
, unsigned char, shift
, len
, &readval
);
334 printf("read: %llX\n", readval
);
335 print_byte_array(target
.c
, 8);
340 int main(int argc
, char **argv
)
343 /* Print encodings */
345 unsigned int shift
, len
;
349 shift
= atoi(argv
[2]);
356 return print_encodings(src
, shift
, len
);
359 /* Run tap-formated tests */
361 return exit_status();