From 59f1ce0f44613793be54925730fac93006d028de Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 18 Apr 2011 00:29:13 -0700 Subject: [PATCH] Macro styling for ASSERT and FATAL. closes #8. closes #6. --- test/echo-server.c | 10 +++++----- test/test-callback-stack.c | 12 ++++++------ test/test-fail-always.c | 4 ++-- test/test-ping-pong.c | 16 ++++++++-------- test/test-runner-unix.c | 1 + test/test-runner.c | 4 ++-- test/test-timeout.c | 21 ++++++++++++--------- test/test.h | 22 ++++++++++++---------- 8 files changed, 48 insertions(+), 42 deletions(-) diff --git a/test/echo-server.c b/test/echo-server.c index fdd91ee7..524bbf29 100644 --- a/test/echo-server.c +++ b/test/echo-server.c @@ -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"); } } } @@ -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"); } } @@ -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; @@ -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); } diff --git a/test/test-callback-stack.c b/test/test-callback-stack.c index d0a0939f..89867b29 100644 --- a/test/test-callback-stack.c +++ b/test/test-callback-stack.c @@ -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++; } @@ -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; } diff --git a/test/test-fail-always.c b/test/test-fail-always.c index 02a4758a..f1539848 100644 --- a/test/test-fail-always.c +++ b/test/test-fail-always.c @@ -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; -} \ No newline at end of file +} diff --git a/test/test-ping-pong.c b/test/test-ping-pong.c index f87af56a..c88c3960 100644 --- a/test/test-ping-pong.c +++ b/test/test-ping-pong.c @@ -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); @@ -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"); @@ -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); @@ -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); @@ -119,7 +119,7 @@ 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. */ @@ -127,7 +127,7 @@ void pinger_new() { oio_bind(&pinger->handle, (struct sockaddr*)&client_addr); r = oio_connect(&pinger->connect_req, (struct sockaddr*)&server_addr); - ASSERT(!r) + ASSERT(!r); } @@ -137,7 +137,7 @@ TEST_IMPL(ping_pong) { pinger_new(); oio_run(); - ASSERT(completed_pingers == 1) + ASSERT(completed_pingers == 1); return 0; } diff --git a/test/test-runner-unix.c b/test/test-runner-unix.c index 75d87c07..ee45ff45 100644 --- a/test/test-runner-unix.c +++ b/test/test-runner-unix.c @@ -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; } diff --git a/test/test-runner.c b/test/test-runner.c index 94197a9e..6f7284d9 100644 --- a/test/test-runner.c +++ b/test/test-runner.c @@ -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; @@ -78,7 +78,7 @@ finalize: /* 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) { diff --git a/test/test-timeout.c b/test/test-timeout.c index 0da89a28..ee4e2f6a 100644 --- a/test/test-timeout.c +++ b/test/test-timeout.c @@ -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"); } @@ -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; } diff --git a/test/test.h b/test/test.h index 332e8bc6..0d42ce96 100644 --- a/test/test.h +++ b/test/test.h @@ -19,9 +19,9 @@ "Fatal error in %s on line %d: %s\n", \ __FILE__, \ __LINE__, \ - #msg); \ + msg); \ abort(); \ - } while (0); + } while (0) /* @@ -29,14 +29,16 @@ * 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. */