Refactor tests.

Refactor tests to use explicit testing assertions, rather than diff'ing
test output.  This makes the test code a bit shorter, more explicitly
encodes testing intent, and makes test failure diagnosis more
straightforward.
This commit is contained in:
Jason Evans
2013-12-08 20:52:21 -08:00
parent 9f35a71a81
commit 2a83ed0284
31 changed files with 880 additions and 708 deletions

View File

@@ -6,21 +6,21 @@
# define MAXBITS (1U << LG_BITMAP_MAXBITS)
#endif
static void
test_bitmap_size(void)
TEST_BEGIN(test_bitmap_size)
{
size_t i, prev_size;
prev_size = 0;
for (i = 1; i <= MAXBITS; i++) {
size_t size = bitmap_size(i);
assert(size >= prev_size);
assert_true(size >= prev_size,
"Bitmap size is smaller than expected");
prev_size = size;
}
}
TEST_END
static void
test_bitmap_init(void)
TEST_BEGIN(test_bitmap_init)
{
size_t i;
@@ -33,15 +33,17 @@ test_bitmap_init(void)
bitmap_info_ngroups(&binfo));
bitmap_init(bitmap, &binfo);
for (j = 0; j < i; j++)
assert(bitmap_get(bitmap, &binfo, j) == false);
for (j = 0; j < i; j++) {
assert_false(bitmap_get(bitmap, &binfo, j),
"Bit should be unset");
}
free(bitmap);
}
}
}
TEST_END
static void
test_bitmap_set(void)
TEST_BEGIN(test_bitmap_set)
{
size_t i;
@@ -56,14 +58,15 @@ test_bitmap_set(void)
for (j = 0; j < i; j++)
bitmap_set(bitmap, &binfo, j);
assert(bitmap_full(bitmap, &binfo));
assert_true(bitmap_full(bitmap, &binfo),
"All bits should be set");
free(bitmap);
}
}
}
TEST_END
static void
test_bitmap_unset(void)
TEST_BEGIN(test_bitmap_unset)
{
size_t i;
@@ -78,19 +81,21 @@ test_bitmap_unset(void)
for (j = 0; j < i; j++)
bitmap_set(bitmap, &binfo, j);
assert(bitmap_full(bitmap, &binfo));
assert_true(bitmap_full(bitmap, &binfo),
"All bits should be set");
for (j = 0; j < i; j++)
bitmap_unset(bitmap, &binfo, j);
for (j = 0; j < i; j++)
bitmap_set(bitmap, &binfo, j);
assert(bitmap_full(bitmap, &binfo));
assert_true(bitmap_full(bitmap, &binfo),
"All bits should be set");
free(bitmap);
}
}
}
TEST_END
static void
test_bitmap_sfu(void)
TEST_BEGIN(test_bitmap_sfu)
{
size_t i;
@@ -104,9 +109,13 @@ test_bitmap_sfu(void)
bitmap_init(bitmap, &binfo);
/* Iteratively set bits starting at the beginning. */
for (j = 0; j < i; j++)
assert(bitmap_sfu(bitmap, &binfo) == j);
assert(bitmap_full(bitmap, &binfo));
for (j = 0; j < i; j++) {
assert_zd_eq(bitmap_sfu(bitmap, &binfo), j,
"First unset bit should be just after "
"previous first unset bit");
}
assert_true(bitmap_full(bitmap, &binfo),
"All bits should be set");
/*
* Iteratively unset bits starting at the end, and
@@ -114,10 +123,13 @@ test_bitmap_sfu(void)
*/
for (j = i - 1; j >= 0; j--) {
bitmap_unset(bitmap, &binfo, j);
assert(bitmap_sfu(bitmap, &binfo) == j);
assert_zd_eq(bitmap_sfu(bitmap, &binfo), j,
"First unset bit should the bit previously "
"unset");
bitmap_unset(bitmap, &binfo, j);
}
assert(bitmap_get(bitmap, &binfo, 0) == false);
assert_false(bitmap_get(bitmap, &binfo, 0),
"Bit should be unset");
/*
* Iteratively set bits starting at the beginning, and
@@ -125,27 +137,29 @@ test_bitmap_sfu(void)
*/
for (j = 1; j < i; j++) {
bitmap_set(bitmap, &binfo, j - 1);
assert(bitmap_sfu(bitmap, &binfo) == j);
assert_zd_eq(bitmap_sfu(bitmap, &binfo), j,
"First unset bit should be just after the "
"bit previously set");
bitmap_unset(bitmap, &binfo, j);
}
assert(bitmap_sfu(bitmap, &binfo) == i - 1);
assert(bitmap_full(bitmap, &binfo));
assert_zd_eq(bitmap_sfu(bitmap, &binfo), i - 1,
"First unset bit should be the last bit");
assert_true(bitmap_full(bitmap, &binfo),
"All bits should be set");
free(bitmap);
}
}
}
TEST_END
int
main(void)
{
malloc_printf("Test begin\n");
test_bitmap_size();
test_bitmap_init();
test_bitmap_set();
test_bitmap_unset();
test_bitmap_sfu();
malloc_printf("Test end\n");
return (0);
return (test(
test_bitmap_size,
test_bitmap_init,
test_bitmap_set,
test_bitmap_unset,
test_bitmap_sfu));
}

View File

@@ -1,2 +0,0 @@
Test begin
Test end

View File

@@ -4,12 +4,16 @@
typedef unsigned int data_t;
static bool data_cleanup_executed;
void
data_cleanup(void *arg)
{
data_t *data = (data_t *)arg;
malloc_printf("Cleanup for data %x.\n", *data);
assert_x_eq(*data, THREAD_DATA,
"Argument passed into cleanup function should match tsd value");
data_cleanup_executed = true;
}
malloc_tsd_protos(, data, data_t)
@@ -21,34 +25,47 @@ malloc_tsd_funcs(, data, data_t, DATA_INIT, data_cleanup)
void *
je_thread_start(void *arg)
{
data_t d = (data_t)(uintptr_t) arg;
malloc_printf("Initial tsd_get returns %x. Expected %x.\n",
*data_tsd_get(), DATA_INIT);
data_t d = (data_t)(uintptr_t)arg;
assert_x_eq(*data_tsd_get(), DATA_INIT,
"Initial tsd get should return initialization value");
data_tsd_set(&d);
malloc_printf("After tsd_set: %x. Expected %x.\n",
*data_tsd_get(), d);
assert_x_eq(*data_tsd_get(), d,
"After tsd set, tsd get should return value that was set");
d = 0;
malloc_printf("After resetting local data: %x. Expected %x.\n",
*data_tsd_get(), (data_t)(uintptr_t) arg);
assert_x_eq(*data_tsd_get(), (data_t)(uintptr_t)arg,
"Resetting local data should have no effect on tsd");
return NULL;
}
int
main(void)
TEST_BEGIN(test_tsd_main_thread)
{
je_thread_start((void *) 0xa5f3e329);
}
TEST_END
TEST_BEGIN(test_tsd_sub_thread)
{
je_thread_t thread;
malloc_printf("Test begin\n");
data_tsd_boot();
je_thread_start((void *) 0xa5f3e329);
data_cleanup_executed = false;
je_thread_create(&thread, je_thread_start, (void *) THREAD_DATA);
je_thread_join(thread, NULL);
malloc_printf("Test end\n");
return (0);
assert_true(data_cleanup_executed,
"Cleanup function should have executed");
}
TEST_END
int
main(void)
{
data_tsd_boot();
return (test(
test_tsd_main_thread,
test_tsd_sub_thread));
}

View File

@@ -1,9 +0,0 @@
Test begin
Initial tsd_get returns 12345678. Expected 12345678.
After tsd_set: a5f3e329. Expected a5f3e329.
After resetting local data: a5f3e329. Expected a5f3e329.
Initial tsd_get returns 12345678. Expected 12345678.
After tsd_set: 72b65c10. Expected 72b65c10.
After resetting local data: 72b65c10. Expected 72b65c10.
Cleanup for data 72b65c10.
Test end