From 1fa209bdb78afe5c796123d529d001b24024d877 Mon Sep 17 00:00:00 2001
From: Dan Hirsch <thequux@thequux.com>
Date: Fri, 27 Jul 2012 15:47:38 -0700
Subject: [PATCH] Added test harness for bitwriter

---
 src/bitwriter.c  | 30 ++++++++++++++++++++++++++++++
 src/test_suite.h |  1 +
 2 files changed, 31 insertions(+)

diff --git a/src/bitwriter.c b/src/bitwriter.c
index b51df3ee..a6d06ca5 100644
--- a/src/bitwriter.c
+++ b/src/bitwriter.c
@@ -2,6 +2,7 @@
 #include <assert.h>
 #include "hammer.h"
 #include "internal.h"
+#include "test_suite.h"
 
 // This file provides the logical inverse of bitreader.c
 struct HBitWriter_ {
@@ -104,3 +105,32 @@ void h_bit_writer_free(HBitWriter* w) {
 }
 
 // TESTS BELOW HERE
+typedef struct {
+  unsigned long long data;
+  size_t nbits;
+} bitwriter_test_elem; // should end with {0,0}
+
+void run_bitwriter_test(bitwriter_test_elem data[], char flags) {
+  size_t len;
+  const uint8_t *buf;
+  HBitWriter *w = h_bit_writer_new();
+  int i;
+  w->flags = flags;
+  for (i = 0; data[i].nbits; i++) {
+    h_bit_writer_put(w, data[i].data, data[i].nbits);
+  }
+
+  buf = h_bit_writer_get_buffer(w, &len);
+  HInputStream input = {
+    .input = buf,
+    .index = 0,
+    .length = len,
+    .bit_offset = (flags & BIT_BIG_ENDIAN) ? 8 : 0,
+    .endianness = flags,
+    .overrun = 0
+  };
+
+  for (i = 0; data[i].nbits; i++) {
+    g_check_cmpulonglong ((unsigned long long)h_read_bits(&input, data[i].nbits, FALSE), ==,  data[i].data);
+  }
+}
diff --git a/src/test_suite.h b/src/test_suite.h
index 6a14101b..68cf83a8 100644
--- a/src/test_suite.h
+++ b/src/test_suite.h
@@ -94,6 +94,7 @@
 #define g_check_cmplonglong(n1, op, n2) g_check_inttype("%lld", long long, n1, op, n2)
 #define g_check_cmpuint(n1, op, n2) g_check_inttype("%u", unsigned int, n1, op, n2)
 #define g_check_cmpulong(n1, op, n2) g_check_inttype("%lu", unsigned long, n1, op, n2)
+#define g_check_cmpulonglong(n1, op, n2) g_check_inttype("%llu", unsigned long long, n1, op, n2)
 #define g_check_cmpfloat(n1, op, n2) g_check_inttype("%g", float, n1, op, n2)
 #define g_check_cmpdouble(n1, op, n2) g_check_inttype("%g", double, n1, op, n2)
 
-- 
GitLab