Skip to content

Instantly share code, notes, and snippets.

@acdimalev
Created August 19, 2010 05:49
Show Gist options
  • Save acdimalev/537148 to your computer and use it in GitHub Desktop.
Save acdimalev/537148 to your computer and use it in GitHub Desktop.
#include "buffer.h"
/* statically allocate tests */
#define TEST_MAX (2 << 8)
struct test {
struct test *next;
char *desc;
int pass;
} tests[TEST_MAX];
struct test *test = tests;
void describe(char *it) {
test->it = it;
test->should = NULL;
test->failure = NULL;
}
void it_should(char *should) {
if (test->should) {
if (test->failure) {
printf(".");
} else {
printf("F");
}
}
test->should = should;
test->failure = NULL;
}
void it_failed_because(char *error_condition) {
struct test *next = &test[1];
test->failure = error_condition;
next->it = test->it;
next->should = test->should;
test = next;
}
void init_tests(void) {
int i;
for (i = 0; i < TEST_MAX; i = i + 1) {
tests[i]->it = NULL;
tests[i]->should = NULL;
tests[i]->failure = NULL;
}
}
void print_failures(void) {
struct test *test = tests;
while (test->failure) {
printf("%s should %s, but %s.\n", test->it, test->should, test->failure);
}
}
int main(int argc, char **argv) {
init_tests();
describe("dsp_b_new"); do {
struct dsp_bc *cursor = dsp_b_new();
it_should("return a cursor"); do {
if (!cursor) { it_failed_because("no cursor was returned"); break; }
}
it_should("create a single, empty page"); do {
if (!cursor) { it_failed_because("pre-conditions are not met"); break; }
if (!cursor->page) { it_failed_because("no page was created"); break; }
if (cursor->page->next) { it_failed_because("more than one page was created"); break; }
if (cursor->page->size) { it_failed_because("the created page is not empty"); break; }
}
it_should("set the cursor at the end of the page"); do {
if (!cursor || !cursor->page) { it_failed_because("pre-conditions are not met"); break; }
if (cursor->page_offset != page->size) {
it_failed_because("the cursor is not at the end of the page"); break;
}
}
}
printf("\n");
print_failures();
return (NULL != tests[0]->failure);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment