Add missing runall.sh to dist packaging
[babeltrace.git] / tests / 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 #define _GNU_SOURCE
23 #include <babeltrace/bitfield.h>
24 #include <time.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27
28 unsigned int glob;
29
30 /*
31 * This function is only declared to show the size of a bitfield write in
32 * objdump.
33 */
34 void fct(void)
35 {
36 bt_bitfield_write(&glob, unsigned int, 12, 15, 0x12345678);
37 }
38
39 /* Test array size, in bytes */
40 #define TEST_LEN 128
41 #define NR_TESTS 10
42
43 unsigned int srcrand;
44
45 #if defined(__i386) || defined(__x86_64)
46
47 static inline int fls(int x)
48 {
49 int r;
50 asm("bsrl %1,%0\n\t"
51 "cmovzl %2,%0"
52 : "=&r" (r) : "rm" (x), "rm" (-1));
53 return r + 1;
54 }
55
56 #elif defined(__PPC__)
57
58 static __inline__ int fls(unsigned int x)
59 {
60 int lz;
61
62 asm ("cntlzw %0,%1" : "=r" (lz) : "r" (x));
63 return 32 - lz;
64 }
65
66 #else
67
68 static int fls(unsigned int x)
69 {
70 int r = 32;
71
72 if (!x)
73 return 0;
74 if (!(x & 0xFFFF0000U)) {
75 x <<= 16;
76 r -= 16;
77 }
78 if (!(x & 0xFF000000U)) {
79 x <<= 8;
80 r -= 8;
81 }
82 if (!(x & 0xF0000000U)) {
83 x <<= 4;
84 r -= 4;
85 }
86 if (!(x & 0xC0000000U)) {
87 x <<= 2;
88 r -= 2;
89 }
90 if (!(x & 0x80000000U)) {
91 x <<= 1;
92 r -= 1;
93 }
94 return r;
95 }
96
97 #endif
98
99 #define print_byte_array(c, len) \
100 do { \
101 unsigned long i; \
102 \
103 for (i = 0; i < (len); i++) { \
104 printf("0x%X", (c)[i]); \
105 if (i != (len) - 1) \
106 printf(" "); \
107 } \
108 printf("\n"); \
109 } while (0)
110
111 #define init_byte_array(c, len, val) \
112 do { \
113 unsigned long i; \
114 \
115 for (i = 0; i < (len); i++) \
116 (c)[i] = (val); \
117 } while (0)
118
119 int run_test_unsigned(void)
120 {
121 unsigned int src, nrbits;
122 union {
123 unsigned char c[TEST_LEN];
124 unsigned short s[TEST_LEN/sizeof(unsigned short)];
125 unsigned int i[TEST_LEN/sizeof(unsigned int)];
126 unsigned long l[TEST_LEN/sizeof(unsigned long)];
127 unsigned long long ll[TEST_LEN/sizeof(unsigned long long)];
128 } target;
129 unsigned long long readval;
130 unsigned int s, l;
131 int err = 0;
132
133 printf("Running unsigned test with 0x%X\n", srcrand);
134
135 src = srcrand;
136 nrbits = fls(src);
137
138 for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
139 for (l = nrbits; l < (CHAR_BIT * TEST_LEN) - s; l++) {
140 init_byte_array(target.c, TEST_LEN, 0xFF);
141 bt_bitfield_write(target.c, unsigned char, s, l, src);
142 bt_bitfield_read(target.c, unsigned char, s, l, &readval);
143 if (readval != src) {
144 printf("Error (bytewise) src %X read %llX shift %d len %d\n",
145 src, readval, s, l);
146 print_byte_array(target.c, TEST_LEN);
147 err = 1;
148 }
149
150 init_byte_array(target.c, TEST_LEN, 0xFF);
151 bt_bitfield_write(target.s, unsigned short, s, l, src);
152 bt_bitfield_read(target.c, unsigned char, s, l, &readval);
153 if (readval != src) {
154 printf("Error (shortwise) src %X read %llX shift %d len %d\n",
155 src, readval, s, l);
156 print_byte_array(target.c, TEST_LEN);
157 err = 1;
158 }
159
160 init_byte_array(target.c, TEST_LEN, 0xFF);
161 bt_bitfield_write(target.i, unsigned int, s, l, src);
162 bt_bitfield_read(target.c, unsigned char, s, l, &readval);
163 if (readval != src) {
164 printf("Error (intwise) src %X read %llX shift %d len %d\n",
165 src, readval, s, l);
166 print_byte_array(target.c, TEST_LEN);
167 err = 1;
168 }
169
170 init_byte_array(target.c, TEST_LEN, 0xFF);
171 bt_bitfield_write(target.l, unsigned long, s, l, src);
172 bt_bitfield_read(target.c, unsigned char, s, l, &readval);
173 if (readval != src) {
174 printf("Error (longwise) src %X read %llX shift %d len %d\n",
175 src, readval, s, l);
176 print_byte_array(target.c, TEST_LEN);
177 err = 1;
178 }
179
180 init_byte_array(target.c, TEST_LEN, 0xFF);
181 bt_bitfield_write(target.ll, unsigned long long, s, l, src);
182 bt_bitfield_read(target.c, unsigned char, s, l, &readval);
183 if (readval != src) {
184 printf("Error (longlongwise) src %X read %llX shift %d len %d\n",
185 src, readval, s, l);
186 print_byte_array(target.c, TEST_LEN);
187 err = 1;
188 }
189 }
190 }
191 if (!err)
192 printf("Success!\n");
193 else
194 printf("Failed!\n");
195 return err;
196 }
197
198 int run_test_signed(void)
199 {
200 int src, nrbits;
201 union {
202 signed char c[TEST_LEN];
203 short s[TEST_LEN/sizeof(short)];
204 int i[TEST_LEN/sizeof(int)];
205 long l[TEST_LEN/sizeof(long)];
206 long long ll[TEST_LEN/sizeof(long long)];
207 } target;
208 long long readval;
209 unsigned int s, l;
210 int err = 0;
211
212 printf("Running signed test with 0x%X\n", srcrand);
213
214 src = srcrand;
215 if (src & 0x80000000U)
216 nrbits = fls(~src) + 1; /* Find least significant bit conveying sign */
217 else
218 nrbits = fls(src) + 1; /* Keep sign at 0 */
219
220 for (s = 0; s < 8 * TEST_LEN; s++) {
221 for (l = nrbits; l < (8 * TEST_LEN) - s; l++) {
222 init_byte_array(target.c, TEST_LEN, 0x0);
223 bt_bitfield_write(target.c, signed char, s, l, src);
224 bt_bitfield_read(target.c, signed char, s, l, &readval);
225 if (readval != src) {
226 printf("Error (bytewise) src %X read %llX shift %d len %d\n",
227 src, readval, s, l);
228 print_byte_array(target.c, TEST_LEN);
229 err = 1;
230 }
231
232 init_byte_array(target.c, TEST_LEN, 0x0);
233 bt_bitfield_write(target.s, short, s, l, src);
234 bt_bitfield_read(target.c, signed char, s, l, &readval);
235 if (readval != src) {
236 printf("Error (shortwise) src %X read %llX shift %d len %d\n",
237 src, readval, s, l);
238 print_byte_array(target.c, TEST_LEN);
239 err = 1;
240 }
241
242 init_byte_array(target.c, TEST_LEN, 0x0);
243 bt_bitfield_write(target.i, int, s, l, src);
244 bt_bitfield_read(target.c, signed char, s, l, &readval);
245 if (readval != src) {
246 printf("Error (intwise) src %X read %llX shift %d len %d\n",
247 src, readval, s, l);
248 print_byte_array(target.c, TEST_LEN);
249 err = 1;
250 }
251
252 init_byte_array(target.c, TEST_LEN, 0x0);
253 bt_bitfield_write(target.l, long, s, l, src);
254 bt_bitfield_read(target.c, signed char, s, l, &readval);
255 if (readval != src) {
256 printf("Error (longwise) src %X read %llX shift %d len %d\n",
257 src, readval, s, l);
258 print_byte_array(target.c, TEST_LEN);
259 err = 1;
260 }
261
262 init_byte_array(target.c, TEST_LEN, 0x0);
263 bt_bitfield_write(target.ll, long long, s, l, src);
264 bt_bitfield_read(target.c, signed char, s, l, &readval);
265 if (readval != src) {
266 printf("Error (longlongwise) src %X read %llX shift %d len %d\n",
267 src, readval, s, l);
268 print_byte_array(target.c, TEST_LEN);
269 err = 1;
270 }
271 }
272 }
273 if (!err)
274 printf("Success!\n");
275 else
276 printf("Failed!\n");
277 return err;
278 }
279
280 int run_test(void)
281 {
282 int err = 0;
283 int i;
284
285 srand(time(NULL));
286
287 srcrand = 0;
288 err |= run_test_unsigned();
289 srcrand = 0;
290 err |= run_test_signed();
291 srcrand = 1;
292 err |= run_test_unsigned();
293 srcrand = ~0U;
294 err |= run_test_unsigned();
295 srcrand = -1;
296 err |= run_test_signed();
297 srcrand = (int)0x80000000U;
298 err |= run_test_signed();
299
300 for (i = 0; i < NR_TESTS; i++) {
301 srcrand = rand();
302 err |= run_test_unsigned();
303 err |= run_test_signed();
304 }
305 return err;
306 }
307
308 int main(int argc, char **argv)
309 {
310 unsigned long src;
311 unsigned int shift, len;
312 int ret;
313 union {
314 unsigned char c[8];
315 unsigned short s[4];
316 unsigned int i[2];
317 unsigned long l[2];
318 unsigned long long ll[1];
319 } target;
320 unsigned long long readval;
321
322 if (argc > 1)
323 src = atoi(argv[1]);
324 else
325 src = 0x12345678;
326 if (argc > 2)
327 shift = atoi(argv[2]);
328 else
329 shift = 12;
330 if (argc > 3)
331 len = atoi(argv[3]);
332 else
333 len = 40;
334
335 target.i[0] = 0xFFFFFFFF;
336 target.i[1] = 0xFFFFFFFF;
337 bt_bitfield_write(target.c, unsigned char, shift, len, src);
338 printf("bytewise\n");
339 print_byte_array(target.c, 8);
340
341 target.i[0] = 0xFFFFFFFF;
342 target.i[1] = 0xFFFFFFFF;
343 bt_bitfield_write(target.s, unsigned short, shift, len, src);
344 printf("shortwise\n");
345 print_byte_array(target.c, 8);
346
347 target.i[0] = 0xFFFFFFFF;
348 target.i[1] = 0xFFFFFFFF;
349 bt_bitfield_write(target.i, unsigned int, shift, len, src);
350 printf("intwise\n");
351 print_byte_array(target.c, 8);
352
353 target.i[0] = 0xFFFFFFFF;
354 target.i[1] = 0xFFFFFFFF;
355 bt_bitfield_write(target.l, unsigned long, shift, len, src);
356 printf("longwise\n");
357 print_byte_array(target.c, 8);
358
359 target.i[0] = 0xFFFFFFFF;
360 target.i[1] = 0xFFFFFFFF;
361 bt_bitfield_write(target.ll, unsigned long long, shift, len, src);
362 printf("lluwise\n");
363 print_byte_array(target.c, 8);
364
365 bt_bitfield_read(target.c, unsigned char, shift, len, &readval);
366 printf("read: %llX\n", readval);
367
368 ret = run_test();
369
370 return ret;
371 }
This page took 0.036824 seconds and 4 git commands to generate.