summaryrefslogtreecommitdiff
path: root/test_bits.c
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2009-03-27 00:04:13 +0000
committerEric S. Raymond <esr@thyrsus.com>2009-03-27 00:04:13 +0000
commit42a670b9883a73ffd1f50149c9a95cabc13eccce (patch)
treef45e0b0285a31d9f50f1c3338398b764c7c51050 /test_bits.c
parentc5c2ae57fc8901e28824000d8e8cd924356a9e3d (diff)
downloadgpsd-42a670b9883a73ffd1f50149c9a95cabc13eccce.tar.gz
Improved documentation and regression testing for the bitfield extractor.
Diffstat (limited to 'test_bits.c')
-rw-r--r--test_bits.c36
1 files changed, 30 insertions, 6 deletions
diff --git a/test_bits.c b/test_bits.c
index 4d2089ff..f7c03717 100644
--- a/test_bits.c
+++ b/test_bits.c
@@ -23,6 +23,24 @@ static unsigned long long uL1,uL2;
static float f1;
static double d1;
+
+static char /*@ observer @*/ *hexdump(const void *binbuf, size_t len)
+{
+ static char hexbuf[BUFSIZ];
+ size_t i, j = 0;
+ const char *ibuf = (const char *)binbuf;
+ const char *hexchar = "0123456789abcdef";
+
+ /*@ -shiftimplementation @*/
+ for (i = 0; i < len; i++) {
+ hexbuf[j++] = hexchar[ (ibuf[i]&0xf0)>>4 ];
+ hexbuf[j++] = hexchar[ ibuf[i]&0x0f ];
+ }
+ /*@ +shiftimplementation @*/
+ hexbuf[j] ='\0';
+ return hexbuf;
+}
+
static void bedumpall(void)
{
(void)printf("getsb: %016llx %016llx %016llx %016llx\n",
@@ -84,6 +102,7 @@ static void ledumpall(void)
}
struct unsigned_test {
+ unsigned char *buf;
unsigned int start, width;
unsigned long long expected;
char *description;
@@ -93,11 +112,15 @@ struct unsigned_test {
int main(void)
{
struct unsigned_test *up, unsigned_tests[] = {
- {0, 1, 0, "first bit of first byte"},
- {0, 8, 0x01,"first 8 bits"},
- {32, 7, 2, "first seven bits of fifth byte"},
- {56, 12, 0x8f, "12 bits crossing 7th to 8th bytes (08ff)"},
- {78, 4, 11, "2 bits crossing 8th to 9th byte (fefd)"},
+ /* tests using the big buffer */
+ {buf, 0, 1, 0, "first bit of first byte"},
+ {buf, 0, 8, 0x01,"first 8 bits"},
+ {buf, 32, 7, 2, "first seven bits of fifth byte"},
+ {buf, 56, 12, 0x8f, "12 bits crossing 7th to 8th bytes (0x08ff)"},
+ {buf, 78, 4, 11, "2 bits crossing 8th to 9th byte (0xfefd)"},
+ /* sporadic tests based on found bugs */
+ {(unsigned char *)"\x19\x23\f6",
+ 7, 2, 2, "2 bits crossing 1st to 2nd byte (0x1923)"},
};
unsigned char *sp;
@@ -162,7 +185,8 @@ int main(void)
up < unsigned_tests+sizeof(unsigned_tests)/sizeof(unsigned_tests[0]);
up++) {
unsigned long long res = ubits((char *)buf, up->start, up->width);
- (void)printf("ubits(..., %d, %d) %s should be %llu, is %llu: %s\n",
+ (void)printf("ubits(%s, %d, %d) %s should be %llu, is %llu: %s\n",
+ hexdump(buf, strlen((char *)buf)),
up->start, up->width, up->description, up->expected, res,
res == up->expected ? "succeeded" : "FAILED");
}