Commit 7e773ee2 authored by Pieter Noordhuis's avatar Pieter Noordhuis
Browse files

Use macro for test definition

This makes sure that tests are declared static, so the compiler
complains when they are not used. Declaring static is easy to forget,
and we want to make sure all tests are run!
parent 47f23f0a
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
#include "../handle.h" #include "../handle.h"
#include "test-helper.h" #include "test-helper.h"
int test_connect_in_refused(void) { TEST(connect_in_refused) {
redis_handle h; redis_handle h;
int rv; int rv;
...@@ -39,10 +39,9 @@ int test_connect_in_refused(void) { ...@@ -39,10 +39,9 @@ int test_connect_in_refused(void) {
assert_equal_int(errno, ECONNREFUSED); assert_equal_int(errno, ECONNREFUSED);
redis_handle_destroy(&h); redis_handle_destroy(&h);
return 0;
} }
int test_connect_in6_refused(void) { TEST(connect_in6_refused) {
redis_handle h; redis_handle h;
int rv; int rv;
...@@ -62,10 +61,9 @@ int test_connect_in6_refused(void) { ...@@ -62,10 +61,9 @@ int test_connect_in6_refused(void) {
assert_equal_int(errno, ECONNREFUSED); assert_equal_int(errno, ECONNREFUSED);
redis_handle_destroy(&h); redis_handle_destroy(&h);
return 0;
} }
int test_connect_un_noent(void) { TEST(connect_un_noent) {
redis_handle h; redis_handle h;
int rv; int rv;
...@@ -81,10 +79,9 @@ int test_connect_un_noent(void) { ...@@ -81,10 +79,9 @@ int test_connect_un_noent(void) {
assert_equal_int(errno, ENOENT); assert_equal_int(errno, ENOENT);
redis_handle_destroy(&h); redis_handle_destroy(&h);
return 0;
} }
int test_connect_timeout(void) { TEST(connect_timeout) {
redis_handle h; redis_handle h;
long long t1, t2; long long t1, t2;
int rv; int rv;
...@@ -112,10 +109,9 @@ int test_connect_timeout(void) { ...@@ -112,10 +109,9 @@ int test_connect_timeout(void) {
assert((t2 - t1) < 15000); /* 5ms of slack should be enough */ assert((t2 - t1) < 15000); /* 5ms of slack should be enough */
redis_handle_destroy(&h); redis_handle_destroy(&h);
return 0;
} }
int test_connect_gai_unknown_host(void) { TEST(connect_gai_unknown_host) {
redis_handle h; redis_handle h;
int rv; int rv;
...@@ -127,10 +123,9 @@ int test_connect_gai_unknown_host(void) { ...@@ -127,10 +123,9 @@ int test_connect_gai_unknown_host(void) {
/* Don't care about the specific error for now. */ /* Don't care about the specific error for now. */
redis_handle_destroy(&h); redis_handle_destroy(&h);
return 0;
} }
int test_connect_gai_success(void) { TEST(connect_gai_success) {
redis_handle h; redis_handle h;
int rv; int rv;
...@@ -144,7 +139,6 @@ int test_connect_gai_success(void) { ...@@ -144,7 +139,6 @@ int test_connect_gai_success(void) {
assert_equal_int(rv, REDIS_OK); assert_equal_int(rv, REDIS_OK);
redis_handle_destroy(&h); redis_handle_destroy(&h);
return 0;
} }
redis_handle *setup(void) { redis_handle *setup(void) {
...@@ -168,7 +162,7 @@ void teardown(redis_handle *h) { ...@@ -168,7 +162,7 @@ void teardown(redis_handle *h) {
free(h); free(h);
} }
void test_eof_after_quit(void) { TEST(eof_after_quit) {
redis_handle *h = setup(); redis_handle *h = setup();
int rv, drained; int rv, drained;
...@@ -202,7 +196,7 @@ void test_eof_after_quit(void) { ...@@ -202,7 +196,7 @@ void test_eof_after_quit(void) {
teardown(h); teardown(h);
} }
void test_read_timeout(void) { TEST(read_timeout) {
redis_handle *h = setup(); redis_handle *h = setup();
int rv; int rv;
...@@ -213,7 +207,7 @@ void test_read_timeout(void) { ...@@ -213,7 +207,7 @@ void test_read_timeout(void) {
teardown(h); teardown(h);
} }
void test_einval_against_closed_handle(void) { TEST(einval_against_closed_handle) {
redis_handle *h = setup(); redis_handle *h = setup();
int rv; int rv;
......
...@@ -48,6 +48,20 @@ ...@@ -48,6 +48,20 @@
} \ } \
} while(0) } while(0)
/******************************************************************************/
/* TEST DEFINITION ************************************************************/
/******************************************************************************/
static const char *current_test = NULL;
#define TEST(name) \
static void test__##name(void); \
static void test_##name(void) { \
current_test = #name; \
test__##name(); \
} \
static void test__##name(void)
/******************************************************************************/ /******************************************************************************/
/* TIMING *********************************************************************/ /* TIMING *********************************************************************/
/******************************************************************************/ /******************************************************************************/
......
...@@ -139,6 +139,7 @@ static redis_parser_callbacks callbacks = { ...@@ -139,6 +139,7 @@ static redis_parser_callbacks callbacks = {
#define RESET_PARSER(__parser) do { \ #define RESET_PARSER(__parser) do { \
reset_cb_log(); \ reset_cb_log(); \
redis_parser_init((__parser), &callbacks); \ redis_parser_init((__parser), &callbacks); \
destroy_called = 0; \
} while(0) } while(0)
void test_char_by_char(const char *buf, size_t len) { void test_char_by_char(const char *buf, size_t len) {
...@@ -194,7 +195,19 @@ void test_char_by_char(const char *buf, size_t len) { ...@@ -194,7 +195,19 @@ void test_char_by_char(const char *buf, size_t len) {
free(ref); free(ref);
} }
void test_string(redis_parser *p) { #define SETUP(var) \
redis_parser _parser, *(var) = &_parser; \
redis_parser_init((var), &callbacks);
/* Not a real test, just print sizeof's */
TEST(struct_size) {
printf("redis_protocol: %lu bytes\n", sizeof(redis_protocol));
printf("redis_parser: %lu bytes\n", sizeof(redis_parser));
}
TEST(string) {
SETUP(p);
const char *buf = "$5\r\nhello\r\n"; const char *buf = "$5\r\nhello\r\n";
size_t len = 11; size_t len = 11;
redis_protocol *res; redis_protocol *res;
...@@ -218,7 +231,9 @@ void test_string(redis_parser *p) { ...@@ -218,7 +231,9 @@ void test_string(redis_parser *p) {
test_char_by_char(buf, len); test_char_by_char(buf, len);
} }
void test_empty_string(redis_parser *p) { TEST(empty_string) {
SETUP(p);
const char *buf = "$0\r\n\r\n"; const char *buf = "$0\r\n\r\n";
size_t len = 6; size_t len = 6;
redis_protocol *res; redis_protocol *res;
...@@ -242,7 +257,9 @@ void test_empty_string(redis_parser *p) { ...@@ -242,7 +257,9 @@ void test_empty_string(redis_parser *p) {
test_char_by_char(buf, len); test_char_by_char(buf, len);
} }
void test_nil_string(redis_parser *p) { TEST(nil_string) {
SETUP(p);
const char *buf = "$-1\r\n"; const char *buf = "$-1\r\n";
size_t len = 5; size_t len = 5;
redis_protocol *res; redis_protocol *res;
...@@ -261,7 +278,9 @@ void test_nil_string(redis_parser *p) { ...@@ -261,7 +278,9 @@ void test_nil_string(redis_parser *p) {
test_char_by_char(buf, len); test_char_by_char(buf, len);
} }
void test_array(redis_parser *p) { TEST(array) {
SETUP(p);
const char *buf = const char *buf =
"*2\r\n" "*2\r\n"
"$5\r\nhello\r\n" "$5\r\nhello\r\n"
...@@ -304,7 +323,9 @@ void test_array(redis_parser *p) { ...@@ -304,7 +323,9 @@ void test_array(redis_parser *p) {
test_char_by_char(buf, len); test_char_by_char(buf, len);
} }
void test_empty_array(redis_parser *p) { TEST(empty_array) {
SETUP(p);
const char *buf = "*0\r\n"; const char *buf = "*0\r\n";
size_t len = 4; size_t len = 4;
redis_protocol *res; redis_protocol *res;
...@@ -325,7 +346,9 @@ void test_empty_array(redis_parser *p) { ...@@ -325,7 +346,9 @@ void test_empty_array(redis_parser *p) {
test_char_by_char(buf, len); test_char_by_char(buf, len);
} }
void test_nil_array(redis_parser *p) { TEST(nil_array) {
SETUP(p);
const char *buf = "*-1\r\n"; const char *buf = "*-1\r\n";
size_t len = 5; size_t len = 5;
redis_protocol *res; redis_protocol *res;
...@@ -342,7 +365,9 @@ void test_nil_array(redis_parser *p) { ...@@ -342,7 +365,9 @@ void test_nil_array(redis_parser *p) {
test_char_by_char(buf, len); test_char_by_char(buf, len);
} }
void test_integer(redis_parser *p) { TEST(integer) {
SETUP(p);
const char *buf = ":1234\r\n"; const char *buf = ":1234\r\n";
size_t len = 7; size_t len = 7;
redis_protocol *res; redis_protocol *res;
...@@ -466,30 +491,33 @@ void test_integer(redis_parser *p) { ...@@ -466,30 +491,33 @@ void test_integer(redis_parser *p) {
assert(redis_parser_err(p) == RPE_OVERFLOW); assert(redis_parser_err(p) == RPE_OVERFLOW);
} }
void test_nil(redis_parser *p) { TEST(nil) {
SETUP(p);
const char *buf = "$-1\r\n"; const char *buf = "$-1\r\n";
size_t len = 7; size_t len = 5;
redis_protocol *res; redis_protocol *res;
/* Parse and check resulting protocol_t */ /* Parse and check resulting protocol_t */
RESET_PARSER(p); RESET_PARSER(p);
assert_equal_size_t(redis_parser_execute(p, &res, buf, len), len); assert_equal_size_t(redis_parser_execute(p, &res, buf, len), len);
assert(res != NULL); assert(res != NULL);
assert_equal_size_t(res->type, REDIS_INTEGER); assert_equal_size_t(res->type, REDIS_NIL);
assert_equal_size_t(res->poff, 0); assert_equal_size_t(res->poff, 0);
assert_equal_size_t(res->plen, 7); assert_equal_size_t(res->plen, 5);
assert_equal_size_t(res->coff, 1); assert_equal_size_t(res->coff, 0);
assert_equal_size_t(res->clen, 4); assert_equal_size_t(res->clen, 0);
/* Check callbacks */ /* Check callbacks */
assert_equal_size_t(cb_log_idx, 1); assert_equal_size_t(cb_log_idx, 1);
assert_equal_size_t(cb_log[0].integer_value, 1234);
/* Chunked check */ /* Chunked check */
test_char_by_char(buf, len); test_char_by_char(buf, len);
} }
void test_status(redis_parser *p) { TEST(status) {
SETUP(p);
const char *buf = "+status\r\n"; const char *buf = "+status\r\n";
size_t len = 9; size_t len = 9;
redis_protocol *res; redis_protocol *res;
...@@ -509,7 +537,9 @@ void test_status(redis_parser *p) { ...@@ -509,7 +537,9 @@ void test_status(redis_parser *p) {
test_char_by_char(buf, len); test_char_by_char(buf, len);
} }
void test_error(redis_parser *p) { TEST(error) {
SETUP(p);
const char *buf = "-error\r\n"; const char *buf = "-error\r\n";
size_t len = 8; size_t len = 8;
redis_protocol *res; redis_protocol *res;
...@@ -529,7 +559,9 @@ void test_error(redis_parser *p) { ...@@ -529,7 +559,9 @@ void test_error(redis_parser *p) {
test_char_by_char(buf, len); test_char_by_char(buf, len);
} }
void test_abort_after_error(redis_parser *p) { TEST(abort_after_error) {
SETUP(p);
redis_protocol *res; redis_protocol *res;
enum redis_parser_errno err; enum redis_parser_errno err;
...@@ -548,7 +580,9 @@ void test_abort_after_error(redis_parser *p) { ...@@ -548,7 +580,9 @@ void test_abort_after_error(redis_parser *p) {
assert(res == NULL); assert(res == NULL);
} }
void test_destroy_callback_after_success(redis_parser *p) { TEST(destroy_callback_after_success) {
SETUP(p);
redis_protocol *res; redis_protocol *res;
RESET_PARSER(p); RESET_PARSER(p);
...@@ -562,7 +596,9 @@ void test_destroy_callback_after_success(redis_parser *p) { ...@@ -562,7 +596,9 @@ void test_destroy_callback_after_success(redis_parser *p) {
assert_equal_int(destroy_called, 0); assert_equal_int(destroy_called, 0);
} }
void test_destroy_callback_after_error(redis_parser *p) { TEST(destroy_callback_after_error) {
SETUP(p);
redis_protocol *res; redis_protocol *res;
RESET_PARSER(p); RESET_PARSER(p);
...@@ -575,7 +611,9 @@ void test_destroy_callback_after_error(redis_parser *p) { ...@@ -575,7 +611,9 @@ void test_destroy_callback_after_error(redis_parser *p) {
assert_equal_int(destroy_called, 1); assert_equal_int(destroy_called, 1);
} }
void test_destroy_callback_in_flight(redis_parser *p) { TEST(destroy_callback_in_flight) {
SETUP(p);
redis_protocol *res; redis_protocol *res;
RESET_PARSER(p); RESET_PARSER(p);
...@@ -592,21 +630,25 @@ int main(int argc, char **argv) { ...@@ -592,21 +630,25 @@ int main(int argc, char **argv) {
((void)argc); ((void)argc);
((void)argv); ((void)argv);
redis_parser *parser = malloc(sizeof(redis_parser)); test_struct_size();
redis_parser_init(parser, &callbacks);
printf("redis_protocol: %lu bytes\n", sizeof(redis_protocol)); test_string();
printf("redis_parser: %lu bytes\n", sizeof(redis_parser)); test_empty_string();
test_nil_string();
test_array();
test_empty_array();
test_nil_array();
test_integer();
test_nil();
test_status();
test_error();
test_string(parser); test_abort_after_error();
test_empty_string(parser); test_destroy_callback_after_success();
test_array(parser); test_destroy_callback_after_error();
test_empty_array(parser); test_destroy_callback_in_flight();
test_integer(parser);
test_status(parser);
test_error(parser);
test_abort_after_error(parser);
free(parser);
return 0; return 0;
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment