python-plugin-provider: Make it build
[babeltrace.git] / tests / lib / test_bitfield.c
1 /*
2 * test-bitfield.c
3 *
4 * BabelTrace - bitfield test program
5 *
6 * Copyright 2010 - 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 <babeltrace/bitfield-internal.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_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"
47
48 unsigned int srcrand;
49
50 #if defined(__i386) || defined(__x86_64)
51
52 static inline int fls(int x)
53 {
54 int r;
55 asm("bsrl %1,%0\n\t"
56 "cmovzl %2,%0"
57 : "=&r" (r) : "rm" (x), "rm" (-1));
58 return r + 1;
59 }
60
61 #elif defined(__PPC__)
62
63 static __inline__ int fls(unsigned int x)
64 {
65 int lz;
66
67 asm ("cntlzw %0,%1" : "=r" (lz) : "r" (x));
68 return 32 - lz;
69 }
70
71 #else
72
73 static int fls(unsigned int x)
74 {
75 int r = 32;
76
77 if (!x)
78 return 0;
79 if (!(x & 0xFFFF0000U)) {
80 x <<= 16;
81 r -= 16;
82 }
83 if (!(x & 0xFF000000U)) {
84 x <<= 8;
85 r -= 8;
86 }
87 if (!(x & 0xF0000000U)) {
88 x <<= 4;
89 r -= 4;
90 }
91 if (!(x & 0xC0000000U)) {
92 x <<= 2;
93 r -= 2;
94 }
95 if (!(x & 0x80000000U)) {
96 x <<= 1;
97 r -= 1;
98 }
99 return r;
100 }
101
102 #endif
103
104 #define print_byte_array(c, len) \
105 do { \
106 unsigned long i; \
107 \
108 for (i = 0; i < (len); i++) { \
109 printf("0x%X", (c)[i]); \
110 if (i != (len) - 1) \
111 printf(" "); \
112 } \
113 printf("\n"); \
114 } while (0)
115
116 #define init_byte_array(c, len, val) \
117 do { \
118 unsigned long i; \
119 \
120 for (i = 0; i < (len); i++) \
121 (c)[i] = (val); \
122 } while (0)
123
124 #define check_result(ref, val, buffer, typename, start, len, \
125 desc_fmt_str) \
126 ({ \
127 if ((val) != (ref)) { \
128 fail(desc_fmt_str, ref); \
129 diag(DIAG_FMT_STR, #typename, start, len, val); \
130 printf("# "); \
131 print_byte_array(buffer, TEST_LEN); \
132 } \
133 (val) != (ref); \
134 })
135
136 void run_test_unsigned(void)
137 {
138 unsigned int src, nrbits;
139 union {
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)];
145 } target;
146 unsigned long long readval;
147 unsigned int s, l;
148
149 src = srcrand;
150 nrbits = fls(src);
151
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)) {
159 return;
160 }
161
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)) {
167 return;
168 }
169
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)) {
175 return;
176 }
177
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)) {
183 return;
184 }
185
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)) {
191 return;
192 }
193 }
194 }
195
196 pass(UNSIGNED_TEST_DESC_FMT_STR, src);
197 }
198
199 void run_test_signed(void)
200 {
201 int src, nrbits;
202 union {
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)];
208 } target;
209 long long readval;
210 unsigned int s, l;
211
212 src = srcrand;
213 if (src & 0x80000000U)
214 nrbits = fls(~src) + 1; /* Find least significant bit conveying sign */
215 else
216 nrbits = fls(src) + 1; /* Keep sign at 0 */
217
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)) {
225 return;
226 }
227
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)) {
233 return;
234 }
235
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)) {
241 return;
242 }
243
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)) {
249 return;
250 }
251
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)) {
257 return;
258 }
259 }
260 }
261
262 pass(SIGNED_TEST_DESC_FMT_STR, src);
263 }
264
265 void run_test(void)
266 {
267 int i;
268 plan_tests(NR_TESTS * 2 + 6);
269
270 srand(time(NULL));
271
272 srcrand = 0;
273 run_test_unsigned();
274 srcrand = 0;
275 run_test_signed();
276
277 srcrand = 1;
278 run_test_unsigned();
279
280 srcrand = ~0U;
281 run_test_unsigned();
282
283 srcrand = -1;
284 run_test_signed();
285
286 srcrand = (int)0x80000000U;
287 run_test_signed();
288
289 for (i = 0; i < NR_TESTS; i++) {
290 srcrand = rand();
291 run_test_unsigned();
292 run_test_signed();
293 }
294 }
295
296 static
297 int print_encodings(unsigned long src, unsigned int shift, unsigned int len)
298 {
299 union {
300 unsigned char c[8];
301 unsigned short s[4];
302 unsigned int i[2];
303 unsigned long l[2];
304 unsigned long long ll[1];
305 } target;
306 unsigned long long readval;
307
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);
312
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);
317
318 init_byte_array(target.c, 8, 0xFF);
319 bt_bitfield_write(target.i, unsigned int, shift, len, src);
320 printf("intwise\n");
321 print_byte_array(target.c, 8);
322
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);
327
328 init_byte_array(target.c, 8, 0xFF);
329 bt_bitfield_write(target.ll, unsigned long long, shift, len, src);
330 printf("lluwise\n");
331 print_byte_array(target.c, 8);
332
333 bt_bitfield_read(target.c, unsigned char, shift, len, &readval);
334 printf("read: %llX\n", readval);
335 print_byte_array(target.c, 8);
336
337 return 0;
338 }
339
340 int main(int argc, char **argv)
341 {
342 if (argc > 1) {
343 /* Print encodings */
344 unsigned long src;
345 unsigned int shift, len;
346
347 src = atoi(argv[1]);
348 if (argc > 2)
349 shift = atoi(argv[2]);
350 else
351 shift = 12;
352 if (argc > 3)
353 len = atoi(argv[3]);
354 else
355 len = 40;
356 return print_encodings(src, shift, len);
357 }
358
359 /* Run tap-formated tests */
360 run_test();
361 return exit_status();
362 }
This page took 0.036076 seconds and 4 git commands to generate.