Use automatic numbering in tests

This commit is contained in:
Pieter Noordhuis 2010-09-20 13:44:24 +02:00
parent 3253105d49
commit aec1fbd2ad

52
test.c
View File

@ -6,7 +6,8 @@
#include "hiredis.h" #include "hiredis.h"
/* The following line is our testing "framework" :) */ /* The following lines make up our testing "framework" :) */
#define test(_s) { printf("#%02d ", ++tests); printf(_s); }
#define test_cond(_c) if(_c) printf("PASSED\n"); else {printf("FAILED\n"); fails++;} #define test_cond(_c) if(_c) printf("PASSED\n"); else {printf("FAILED\n"); fails++;}
static long long usec(void) { static long long usec(void) {
@ -25,21 +26,19 @@ static void __connect(int *fd) {
int main(void) { int main(void) {
int fd; int fd;
int i, fails = 0; int i, tests = 0, fails = 0;
long long t1, t2; long long t1, t2;
redisReply *reply; redisReply *reply;
__connect(&fd); __connect(&fd);
/* test 0 */ test("Returns I/O error when the connection is lost: ");
printf("#0 Returns I/O error when the connection is lost: ");
reply = redisCommand(fd,"QUIT"); reply = redisCommand(fd,"QUIT");
test_cond(reply->type == REDIS_REPLY_ERROR && test_cond(reply->type == REDIS_REPLY_ERROR &&
strcasecmp(reply->reply,"i/o error") == 0); strcasecmp(reply->reply,"i/o error") == 0);
freeReplyObject(reply); freeReplyObject(reply);
__connect(&fd); /* reconnect */ __connect(&fd); /* reconnect */
/* test 1 */ test("Is able to deliver commands: ");
printf("#1 Is able to deliver commands: ");
reply = redisCommand(fd,"PING"); reply = redisCommand(fd,"PING");
test_cond(reply->type == REDIS_REPLY_STRING && test_cond(reply->type == REDIS_REPLY_STRING &&
strcasecmp(reply->reply,"pong") == 0) strcasecmp(reply->reply,"pong") == 0)
@ -60,15 +59,13 @@ int main(void) {
} }
freeReplyObject(reply); freeReplyObject(reply);
/* test 2 */ test("Is a able to send commands verbatim: ");
printf("#2 Is a able to send commands verbatim: ");
reply = redisCommand(fd,"SET foo bar"); reply = redisCommand(fd,"SET foo bar");
test_cond (reply->type == REDIS_REPLY_STRING && test_cond (reply->type == REDIS_REPLY_STRING &&
strcasecmp(reply->reply,"ok") == 0) strcasecmp(reply->reply,"ok") == 0)
freeReplyObject(reply); freeReplyObject(reply);
/* test 3 */ test("%%s String interpolation works: ");
printf("#3 %%s String interpolation works: ");
reply = redisCommand(fd,"SET %s %s","foo","hello world"); reply = redisCommand(fd,"SET %s %s","foo","hello world");
freeReplyObject(reply); freeReplyObject(reply);
reply = redisCommand(fd,"GET foo"); reply = redisCommand(fd,"GET foo");
@ -76,32 +73,29 @@ int main(void) {
strcmp(reply->reply,"hello world") == 0); strcmp(reply->reply,"hello world") == 0);
freeReplyObject(reply); freeReplyObject(reply);
/* test 4 & 5 */ test("%%b String interpolation works: ");
printf("#4 %%b String interpolation works: ");
reply = redisCommand(fd,"SET %b %b","foo",3,"hello\x00world",11); reply = redisCommand(fd,"SET %b %b","foo",3,"hello\x00world",11);
freeReplyObject(reply); freeReplyObject(reply);
reply = redisCommand(fd,"GET foo"); reply = redisCommand(fd,"GET foo");
test_cond(reply->type == REDIS_REPLY_STRING && test_cond(reply->type == REDIS_REPLY_STRING &&
memcmp(reply->reply,"hello\x00world",11) == 0) memcmp(reply->reply,"hello\x00world",11) == 0)
printf("#5 binary reply length is correct: "); test("Binary reply length is correct: ");
test_cond(sdslen(reply->reply) == 11) test_cond(sdslen(reply->reply) == 11)
freeReplyObject(reply); freeReplyObject(reply);
/* test 6 */ test("Can parse nil replies: ");
printf("#6 can parse nil replies: ");
reply = redisCommand(fd,"GET nokey"); reply = redisCommand(fd,"GET nokey");
test_cond(reply->type == REDIS_REPLY_NIL) test_cond(reply->type == REDIS_REPLY_NIL)
freeReplyObject(reply); freeReplyObject(reply);
/* test 7 */ /* test 7 */
printf("#7 can parse integer replies: "); test("Can parse integer replies: ");
reply = redisCommand(fd,"INCR mycounter"); reply = redisCommand(fd,"INCR mycounter");
test_cond(reply->type == REDIS_REPLY_INTEGER && reply->integer == 1) test_cond(reply->type == REDIS_REPLY_INTEGER && reply->integer == 1)
freeReplyObject(reply); freeReplyObject(reply);
/* test 8 */ test("Can parse multi bulk replies: ");
printf("#8 can parse multi bulk replies: ");
freeReplyObject(redisCommand(fd,"LPUSH mylist foo")); freeReplyObject(redisCommand(fd,"LPUSH mylist foo"));
freeReplyObject(redisCommand(fd,"LPUSH mylist bar")); freeReplyObject(redisCommand(fd,"LPUSH mylist bar"));
reply = redisCommand(fd,"LRANGE mylist 0 -1"); reply = redisCommand(fd,"LRANGE mylist 0 -1");
@ -111,9 +105,9 @@ int main(void) {
!memcmp(reply->element[1]->reply,"foo",3)) !memcmp(reply->element[1]->reply,"foo",3))
freeReplyObject(reply); freeReplyObject(reply);
/* test 9 (m/e with multi bulk reply *before* other reply). /* m/e with multi bulk reply *before* other reply.
* specifically test ordering of reply items to parse. */ * specifically test ordering of reply items to parse. */
printf("#10 can handle nested multi bulk replies: "); test("Can handle nested multi bulk replies: ");
freeReplyObject(redisCommand(fd,"MULTI")); freeReplyObject(redisCommand(fd,"MULTI"));
freeReplyObject(redisCommand(fd,"LRANGE mylist 0 -1")); freeReplyObject(redisCommand(fd,"LRANGE mylist 0 -1"));
freeReplyObject(redisCommand(fd,"PING")); freeReplyObject(redisCommand(fd,"PING"));
@ -128,13 +122,7 @@ int main(void) {
strcasecmp(reply->element[1]->reply,"pong") == 0); strcasecmp(reply->element[1]->reply,"pong") == 0);
freeReplyObject(reply); freeReplyObject(reply);
if (fails == 0) { test("Throughput:\n");
printf("ALL TESTS PASSED\n");
} else {
printf("*** %d TESTS FAILED ***\n", fails);
}
printf("\nSpeed tests:\n");
for (i = 0; i < 500; i++) for (i = 0; i < 500; i++)
freeReplyObject(redisCommand(fd,"LPUSH mylist foo")); freeReplyObject(redisCommand(fd,"LPUSH mylist foo"));
@ -142,17 +130,23 @@ int main(void) {
for (i = 0; i < 1000; i++) for (i = 0; i < 1000; i++)
freeReplyObject(redisCommand(fd,"PING")); freeReplyObject(redisCommand(fd,"PING"));
t2 = usec(); t2 = usec();
printf("(1000x PING: %.2fs)\n", (t2-t1)/1000000.0); printf("\t(1000x PING: %.2fs)\n", (t2-t1)/1000000.0);
t1 = usec(); t1 = usec();
for (i = 0; i < 1000; i++) for (i = 0; i < 1000; i++)
freeReplyObject(redisCommand(fd,"LRANGE mylist 0 499")); freeReplyObject(redisCommand(fd,"LRANGE mylist 0 499"));
t2 = usec(); t2 = usec();
printf("(1000x LRANGE with 500 elements: %.2fs)\n", (t2-t1)/1000000.0); printf("\t(1000x LRANGE with 500 elements: %.2fs)\n", (t2-t1)/1000000.0);
/* Clean DB 9 */ /* Clean DB 9 */
reply = redisCommand(fd,"FLUSHDB"); reply = redisCommand(fd,"FLUSHDB");
freeReplyObject(reply); freeReplyObject(reply);
if (fails == 0) {
printf("ALL TESTS PASSED\n");
} else {
printf("*** %d TESTS FAILED ***\n", fails);
}
return 0; return 0;
} }