1 /* test harness for bits.h
3 * This file is Copyright (c) 2010 by the GPSD project
4 * BSD terms apply: see the file COPYING in the distribution root for details.
12 /*@ -duplicatequals -formattype */
13 typedef unsigned long long ubig;
15 static unsigned char buf[80];
16 static union int_float i_f;
17 static union long_double l_d;
19 static unsigned char ub1, ub2;
20 static short sw1, sw2;
21 static unsigned short uw1, uw2;
23 static unsigned int ul1, ul2;
24 static long long sL1, sL2;
25 static unsigned long long uL1, uL2;
30 static char /*@ observer @*/ *hexdump(const void *binbuf, size_t len)
32 static char hexbuf[BUFSIZ];
34 const char *ibuf = (const char *)binbuf;
35 const char *hexchar = "0123456789abcdef";
37 /*@ -shiftimplementation @*/
38 for (i = 0; i < len; i++) {
39 hexbuf[j++] = hexchar[(ibuf[i] & 0xf0) >> 4];
40 hexbuf[j++] = hexchar[ibuf[i] & 0x0f];
42 /*@ +shiftimplementation @*/
47 static void bedumpall(void)
49 (void)printf("getsb: %016llx %016llx %016llx %016llx\n",
50 (ubig) sb1, (ubig) sb2,
51 (ubig) getsb(buf, 0), (ubig) getsb(buf, 8));
52 (void)printf("getub: %016llx %016llx %016llx %016llx\n",
53 (ubig) ub1, (ubig) ub2,
54 (ubig) getub(buf, 0), (ubig) getub(buf, 8));
55 (void)printf("getbesw: %016llx %016llx %016llx %016llx\n",
56 (ubig) sw1, (ubig) sw2,
57 (ubig) getbesw(buf, 0), (ubig) getbesw(buf, 8));
58 (void)printf("getbeuw: %016llx %016llx %016llx %016llx\n",
59 (ubig) uw1, (ubig) uw2,
60 (ubig) getbeuw(buf, 0), (ubig) getbeuw(buf, 8));
61 (void)printf("getbesl: %016llx %016llx %016llx %016llx\n",
62 (ubig) sl1, (ubig) sl2,
63 (ubig) getbesl(buf, 0), (ubig) getbesl(buf, 8));
64 (void)printf("getbeul: %016llx %016llx %016llx %016llx\n",
65 (ubig) ul1, (ubig) ul2,
66 (ubig) getbeul(buf, 0), (ubig) getbeul(buf, 8));
67 (void)printf("getbesL: %016llx %016llx %016llx %016llx\n",
68 (ubig) sL1, (ubig) sL2,
69 (ubig) getbesL(buf, 0), (ubig) getbesL(buf, 8));
70 (void)printf("getbeuL: %016llx %016llx %016llx %016llx\n",
71 (ubig) uL1, (ubig) uL2,
72 (ubig) getbeuL(buf, 0), (ubig) getbeuL(buf, 8));
73 (void)printf("getbef: %f %f\n", f1, getbef(buf, 24));
74 (void)printf("getbed: %.16f %.16f\n", d1, getbed(buf, 16));
77 static void ledumpall(void)
79 (void)printf("getsb: %016llx %016llx %016llx %016llx\n",
80 (ubig) sb1, (ubig) sb2,
81 (ubig) getsb(buf, 0), (ubig) getsb(buf, 8));
82 (void)printf("getub: %016llx %016llx %016llx %016llx\n",
83 (ubig) ub1, (ubig) ub2,
84 (ubig) getub(buf, 0), (ubig) getub(buf, 8));
85 (void)printf("getlesw: %016llx %016llx %016llx %016llx\n",
86 (ubig) sw1, (ubig) sw2,
87 (ubig) getlesw(buf, 0), (ubig) getlesw(buf, 8));
88 (void)printf("getleuw: %016llx %016llx %016llx %016llx\n",
89 (ubig) uw1, (ubig) uw2,
90 (ubig) getleuw(buf, 0), (ubig) getleuw(buf, 8));
91 (void)printf("getlesl: %016llx %016llx %016llx %016llx\n",
92 (ubig) sl1, (ubig) sl2,
93 (ubig) getlesl(buf, 0), (ubig) getlesl(buf, 8));
94 (void)printf("getleul: %016llx %016llx %016llx %016llx\n",
95 (ubig) ul1, (ubig) ul2,
96 (ubig) getleul(buf, 0), (ubig) getleul(buf, 8));
97 (void)printf("getlesL: %016llx %016llx %016llx %016llx\n",
98 (ubig) sL1, (ubig) sL2,
99 (ubig) getlesL(buf, 0), (ubig) getlesL(buf, 8));
100 (void)printf("getleuL: %016llx %016llx %016llx %016llx\n",
101 (ubig) uL1, (ubig) uL2,
102 (ubig) getleuL(buf, 0), (ubig) getleuL(buf, 8));
103 (void)printf("getlef: %f %f\n", f1, getlef(buf, 24));
104 (void)printf("getled: %.16f %.16f\n", d1, getled(buf, 16));
110 unsigned int start, width;
111 unsigned long long expected;
115 /*@ -duplicatequals +ignorequals @*/
118 /*@ -observertrans -usereleased @*/
119 struct unsigned_test *up, unsigned_tests[] = {
120 /* tests using the big buffer */
121 {buf, 0, 1, 0, "first bit of first byte"},
122 {buf, 0, 8, 0x01, "first 8 bits"},
123 {buf, 32, 7, 2, "first seven bits of fifth byte"},
124 {buf, 56, 12, 0x8f, "12 bits crossing 7th to 8th bytes (0x08ff)"},
125 {buf, 78, 4, 11, "2 bits crossing 8th to 9th byte (0xfefd)"},
126 /* sporadic tests based on found bugs */
127 {(unsigned char *)"\x19\x23\f6",
128 7, 2, 2, "2 bits crossing 1st to 2nd byte (0x1923)"},
133 memcpy(buf, "\x01\x02\x03\x04\x05\x06\x07\x08", 8);
134 memcpy(buf + 8, "\xff\xfe\xfd\xfc\xfb\xfa\xf9\xf8", 8);
135 memcpy(buf + 16, "\x40\x09\x21\xfb\x54\x44\x2d\x18", 8);
136 memcpy(buf + 24, "\x40\x49\x0f\xdb", 4);
137 /*@ +observertrans +usereleased @*/
139 (void)fputs("Test data:", stdout);
140 for (sp = buf; sp < buf + 28; sp++)
141 (void)printf(" %02x", *sp);
142 (void)putc('\n', stdout);
144 /* big-endian test */
146 printf("Big-endian:\n");
151 sw1 = getbesw(buf, 0);
152 sw2 = getbesw(buf, 8);
153 uw1 = getbeuw(buf, 0);
154 uw2 = getbeuw(buf, 8);
155 sl1 = getbesl(buf, 0);
156 sl2 = getbesl(buf, 8);
157 ul1 = getbeul(buf, 0);
158 ul2 = getbeul(buf, 8);
159 sL1 = getbesL(buf, 0);
160 sL2 = getbesL(buf, 8);
161 uL1 = getbeuL(buf, 0);
162 uL2 = getbeuL(buf, 8);
163 f1 = getbef(buf, 24);
164 d1 = getbed(buf, 16);
168 /* little-endian test */
169 printf("Little-endian:\n");
175 sw1 = getlesw(buf, 0);
176 sw2 = getlesw(buf, 8);
177 uw1 = getleuw(buf, 0);
178 uw2 = getleuw(buf, 8);
179 sl1 = getlesl(buf, 0);
180 sl2 = getlesl(buf, 8);
181 ul1 = getleul(buf, 0);
182 ul2 = getleul(buf, 8);
183 sL1 = getlesL(buf, 0);
184 sL2 = getlesL(buf, 8);
185 uL1 = getleuL(buf, 0);
186 uL2 = getleuL(buf, 8);
187 f1 = getlef(buf, 24);
188 d1 = getled(buf, 16);
193 (void)printf("Testing bitfield extraction:\n");
194 for (up = unsigned_tests;
196 unsigned_tests + sizeof(unsigned_tests) / sizeof(unsigned_tests[0]);
198 unsigned long long res = ubits((char *)buf, up->start, up->width);
199 (void)printf("ubits(%s, %d, %d) %s should be %llu, is %llu: %s\n",
200 hexdump(buf, strlen((char *)buf)),
201 up->start, up->width, up->description, up->expected, res,
202 res == up->expected ? "succeeded" : "FAILED");