Skip to content

Commit

Permalink
Macro styling for ASSERT and FATAL.
Browse files Browse the repository at this point in the history
closes nodejs#8.
closes nodejs#6.
  • Loading branch information
ry committed Apr 18, 2011
1 parent d546a31 commit 59f1ce0
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 42 deletions.
10 changes: 5 additions & 5 deletions test/echo-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void after_read(oio_req* req, size_t nread) {
oio_req_init(&peer->req, &peer->handle, after_write);
peer->req.data = peer;
if (oio_write(&peer->req, &peer->buf, 1)) {
FATAL(oio_write failed)
FATAL("oio_write failed");
}
}
}
Expand All @@ -49,7 +49,7 @@ void try_read(peer_t* peer) {
oio_req_init(&peer->req, &peer->handle, after_read);
peer->req.data = peer;
if (oio_read(&peer->req, &peer->buf, 1)) {
FATAL(oio_read failed)
FATAL("oio_read failed");
}
}

Expand All @@ -65,10 +65,10 @@ void on_accept(oio_handle* server) {
peer_t* p = (peer_t*)calloc(sizeof(peer_t), 1);

int r = oio_tcp_handle_init(&p->handle, on_close, (void*)p);
ASSERT(!r)
ASSERT(!r);

if (oio_tcp_handle_accept(server, &p->handle)) {
FATAL(oio_tcp_handle_accept failed)
FATAL("oio_tcp_handle_accept failed");
}

p->buf.base = (char*)&p->read_buffer;
Expand All @@ -79,7 +79,7 @@ void on_accept(oio_handle* server) {

void on_server_close(oio_handle* handle, oio_err err) {
ASSERT(handle == &server);
ASSERT(!err)
ASSERT(!err);
}


Expand Down
12 changes: 6 additions & 6 deletions test/test-callback-stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ int close_cb_called = 0;


void close_cb(oio_handle *handle, oio_err err) {
ASSERT(!err)
ASSERT(nested == 0 && "oio_close_cb must be called from a fresh stack")
ASSERT(!err);
ASSERT(nested == 0 && "oio_close_cb must be called from a fresh stack");
close_cb_called++;
}

Expand All @@ -22,21 +22,21 @@ TEST_IMPL(close_cb_stack) {
oio_init();

if (oio_tcp_handle_init(&handle, &close_cb, NULL)) {
FATAL(oio_tcp_handle_init failed)
FATAL("oio_tcp_handle_init failed");
}

nested++;

if (oio_close(&handle)) {
FATAL(oio_close failed)
FATAL("oio_close failed");
}

nested--;

oio_run();

ASSERT(nested == 0)
ASSERT(close_cb_called == 1 && "oio_close_cb must be called exactly once")
ASSERT(nested == 0);
ASSERT(close_cb_called == 1 && "oio_close_cb must be called exactly once");

return 0;
}
4 changes: 2 additions & 2 deletions test/test-fail-always.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

TEST_IMPL(fail_always) {
/* This test always fails. It is used to test the test runner. */
FATAL("Yes, it always fails")
FATAL("Yes, it always fails");
return 2;
}
}
16 changes: 8 additions & 8 deletions test/test-ping-pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ void pinger_try_read(pinger_t* pinger);
void pinger_on_close(oio_handle* handle, oio_err err) {
pinger_t* pinger = (pinger_t*)handle->data;

ASSERT(!err)
ASSERT(NUM_PINGS == pinger->pongs)
ASSERT(!err);
ASSERT(NUM_PINGS == pinger->pongs);

free(pinger);

Expand All @@ -50,7 +50,7 @@ static void pinger_write_ping(pinger_t* pinger) {
oio_req_init(req, &pinger->handle, pinger_after_write);

if (oio_write2(req, (char*)&PING)) {
FATAL(oio_write2 failed)
FATAL("oio_write2 failed");
}

puts("PING");
Expand All @@ -71,7 +71,7 @@ static void pinger_after_read(oio_req* req, size_t nread) {

/* Now we count the pings */
for (i = 0; i < nread; i++) {
ASSERT(pinger->buf.base[i] == PING[pinger->state])
ASSERT(pinger->buf.base[i] == PING[pinger->state]);
pinger->state = (pinger->state + 1) % (sizeof(PING) - 1);
if (pinger->state == 0) {
printf("PONG %d\n", pinger->pongs);
Expand All @@ -98,7 +98,7 @@ void pinger_try_read(pinger_t* pinger) {
void pinger_on_connect(oio_req *req, oio_err err) {
pinger_t *pinger = (pinger_t*)req->handle->data;

ASSERT(!err)
ASSERT(!err);

pinger_try_read(pinger);
pinger_write_ping(pinger);
Expand All @@ -119,15 +119,15 @@ void pinger_new() {

/* Try to connec to the server and do NUM_PINGS ping-pongs. */
r = oio_tcp_handle_init(&pinger->handle, pinger_on_close, (void*)pinger);
ASSERT(!r)
ASSERT(!r);

/* We are never doing multiple reads/connects at a time anyway. */
/* so these handles can be pre-initialized. */
oio_req_init(&pinger->connect_req, &pinger->handle, pinger_on_connect);

oio_bind(&pinger->handle, (struct sockaddr*)&client_addr);
r = oio_connect(&pinger->connect_req, (struct sockaddr*)&server_addr);
ASSERT(!r)
ASSERT(!r);
}


Expand All @@ -137,7 +137,7 @@ TEST_IMPL(ping_pong) {
pinger_new();
oio_run();

ASSERT(completed_pingers == 1)
ASSERT(completed_pingers == 1);

return 0;
}
1 change: 1 addition & 0 deletions test/test-runner-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,5 @@ void process_cleanup(process_info_t *p) {
/* Move the console cursor one line up and back to the first column. */
int rewind_cursor() {
printf("\033[1A\033[80D");
return 0;
}
4 changes: 2 additions & 2 deletions test/test-runner.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int run_test(test_entry_t *test) {
/* Wait for the main process to terminate. */
result = process_wait(main_process, 1, TEST_TIMEOUT);
if (result == -1) {
FATAL(process_wait failed)
FATAL("process_wait failed");
} else if (result == -2) {
snprintf((char*)&errmsg, sizeof(errmsg), "timeout.");
goto finalize;
Expand All @@ -78,7 +78,7 @@ int run_test(test_entry_t *test) {

/* Wait until all processes have really terminated. */
if (process_wait((process_info_t*)&processes, process_count, -1) < 0)
FATAL(process_wait failed)
FATAL("process_wait failed");

/* Show error and output from processes if the test failed. */
if (!success) {
Expand Down
21 changes: 12 additions & 9 deletions test/test-timeout.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static void exit_timeout_cb(oio_req *req) {

static void dummy_timeout_cb(oio_req *req) {
/* Should never be called */
FATAL(dummy_timer_cb should never be called)
FATAL("dummy_timer_cb should never be called");
}


Expand All @@ -44,28 +44,31 @@ TEST_IMPL(timeout) {
/* Let 10 timers time out in 500 ms total. */
for (i = 0; i < 10; i++) {
req = (oio_req*)malloc(sizeof(*req));
ASSERT(req != NULL)
ASSERT(req != NULL);

oio_req_init(req, NULL, timeout_cb);

if (oio_timeout(req, i * 50) < 0)
FATAL(oio_timeout failed)
if (oio_timeout(req, i * 50) < 0) {
FATAL("oio_timeout failed");
}

expected++;
}

/* The 11th timer exits the test and runs after 1 s. */
oio_req_init(&exit_req, NULL, exit_timeout_cb);
if (oio_timeout(&exit_req, 1000) < 0)
FATAL(oio_timeout failed)
if (oio_timeout(&exit_req, 1000) < 0) {
FATAL("oio_timeout failed");
}

/* The 12th timer should never run. */
oio_req_init(&dummy_req, NULL, dummy_timeout_cb);
if (oio_timeout(&dummy_req, 2000))
FATAL(oio_timeout failed)
if (oio_timeout(&dummy_req, 2000)) {
FATAL("oio_timeout failed");
}

oio_run();

FATAL(should never get here)
FATAL("should never get here");
return 2;
}
22 changes: 12 additions & 10 deletions test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,26 @@
"Fatal error in %s on line %d: %s\n", \
__FILE__, \
__LINE__, \
#msg); \
msg); \
abort(); \
} while (0);
} while (0)


/*
* Have our own assert, so we are sure it does not get optimized away in
* a release build.
*/
#define ASSERT(expr) \
if (!(expr)) { \
fprintf(stderr, \
"Assertion failed in %s on line %d: %s\n", \
__FILE__, \
__LINE__, \
#expr); \
abort(); \
}
do { \
if (!(expr)) { \
fprintf(stderr, \
"Assertion failed in %s on line %d: %s\n", \
__FILE__, \
__LINE__, \
#expr); \
abort(); \
} \
} while (0)


/* Just sugar for wrapping the main() for a test. */
Expand Down

0 comments on commit 59f1ce0

Please sign in to comment.