Refactored tests; make just builds library, make test builds/runs tests

This commit is contained in:
Meredith L. Patterson 2012-11-13 22:29:25 -05:00
parent 236ec733a1
commit de8db18db4
12 changed files with 613 additions and 615 deletions

View file

@ -7,18 +7,6 @@
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
// This file provides the logical inverse of bitreader.c
struct HBitWriter_ {
uint8_t* buf;
HAllocator *mm__;
size_t index;
size_t capacity;
char bit_offset; // unlike in bit_reader, this is always the number
// of used bits in the current byte. i.e., 0 always
// means that 8 bits are available for use.
char flags;
};
// h_bit_writer_
HBitWriter *h_bit_writer_new(HAllocator* mm__) {
HBitWriter *writer = h_new(HBitWriter, 1);
@ -110,112 +98,3 @@ void h_bit_writer_free(HBitWriter* w) {
h_free(w->buf);
h_free(w);
}
#ifdef INCLUDE_TESTS
#include <glib.h>
// 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(&system_allocator);
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);
}
}
static void test_bitwriter_ints(void) {
bitwriter_test_elem data[] = {
{ -0x200000000, 64 },
{ 0,0 }
};
run_bitwriter_test(data, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN);
}
static void test_bitwriter_be(void) {
bitwriter_test_elem data[] = {
{ 0x03, 3 },
{ 0x52, 8 },
{ 0x1A, 5 },
{ 0, 0 }
};
run_bitwriter_test(data, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN);
}
static void test_bitwriter_le(void) {
bitwriter_test_elem data[] = {
{ 0x02, 3 },
{ 0x4D, 8 },
{ 0x0B, 5 },
{ 0, 0 }
};
run_bitwriter_test(data, BIT_LITTLE_ENDIAN | BYTE_LITTLE_ENDIAN);
}
static void test_largebits_be(void) {
bitwriter_test_elem data[] = {
{ 0x352, 11 },
{ 0x1A, 5 },
{ 0, 0 }
};
run_bitwriter_test(data, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN);
}
static void test_largebits_le(void) {
bitwriter_test_elem data[] = {
{ 0x26A, 11 },
{ 0x0B, 5 },
{ 0, 0 }
};
run_bitwriter_test(data, BIT_LITTLE_ENDIAN | BYTE_LITTLE_ENDIAN);
}
static void test_offset_largebits_be(void) {
bitwriter_test_elem data[] = {
{ 0xD, 5 },
{ 0x25A, 11 },
{ 0, 0 }
};
run_bitwriter_test(data, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN);
}
static void test_offset_largebits_le(void) {
bitwriter_test_elem data[] = {
{ 0xA, 5 },
{ 0x2D3, 11 },
{ 0, 0 }
};
run_bitwriter_test(data, BIT_LITTLE_ENDIAN | BYTE_LITTLE_ENDIAN);
}
void register_bitwriter_tests(void) {
g_test_add_func("/core/bitwriter/be", test_bitwriter_be);
g_test_add_func("/core/bitwriter/le", test_bitwriter_le);
g_test_add_func("/core/bitwriter/largebits-be", test_largebits_be);
g_test_add_func("/core/bitwriter/largebits-le", test_largebits_le);
g_test_add_func("/core/bitwriter/offset-largebits-be", test_offset_largebits_be);
g_test_add_func("/core/bitwriter/offset-largebits-le", test_offset_largebits_le);
g_test_add_func("/core/bitwriter/ints", test_bitwriter_ints);
}
#endif // #ifdef INCLUDE_TESTS