Skip to content
This repository has been archived by the owner on Jul 11, 2021. It is now read-only.

Commit

Permalink
Allow all code tests to run using Redis args
Browse files Browse the repository at this point in the history
Previously, many files had individual main() functions for testing,
but each required being compiled with their own testing flags.
That gets difficult when you have 8 different flags you need
to set just to run all tests (plus, some test files required
other files to be compiled aaginst them, and it seems some didn't
build at all without including the rest of Redis).

Now all individual test main() funcions are renamed to a test
function for the file itself and one global REDIS_TEST define enables
testing across the entire codebase.

Tests can now be run with:
  - `./redis-server test <test>`

  e.g. ./redis-server test ziplist

If REDIS_TEST is not defined, then no tests get included and no
tests are included in the final redis-server binary.
  • Loading branch information
mattsta committed Dec 23, 2014
1 parent 8380655 commit 8febcff
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 47 deletions.
8 changes: 6 additions & 2 deletions src/crc64.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,13 @@ uint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l) {
}

/* Test main */
#ifdef TEST_MAIN
#ifdef REDIS_TEST
#include <stdio.h>
int main(void) {

#define UNUSED(x) (void)(x)
int crc64Test(int argc, char *argv[]) {
UNUSED(argc);
UNUSED(argv);
printf("e9c6d914c4b8d9ca == %016llx\n",
(unsigned long long) crc64(0,(unsigned char*)"123456789",9));
return 0;
Expand Down
4 changes: 4 additions & 0 deletions src/crc64.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@

uint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l);

#ifdef REDIS_TEST
int crc64Test(int argc, char *argv[]);
#endif

#endif
8 changes: 6 additions & 2 deletions src/endianconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,16 @@ uint64_t intrev64(uint64_t v) {
return v;
}

#ifdef TESTMAIN
#ifdef REDIS_TEST
#include <stdio.h>

int main(void) {
#define UNUSED(x) (void)(x)
int endianconvTest(int argc, char *argv[]) {
char buf[32];

UNUSED(argc);
UNUSED(argv);

sprintf(buf,"ciaoroma");
memrev16(buf);
printf("%s\n", buf);
Expand Down
4 changes: 4 additions & 0 deletions src/endianconv.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,8 @@ uint64_t intrev64(uint64_t v);
#define ntohu64(v) intrev64(v)
#endif

#ifdef REDIS_TEST
int endianconvTest(int argc, char *argv[]);
#endif

#endif
42 changes: 24 additions & 18 deletions src/intset.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,44 +365,46 @@ size_t intsetBlobLen(intset *is) {
return sizeof(intset)+intrev32ifbe(is->length)*intrev32ifbe(is->encoding);
}

#ifdef INTSET_TEST_MAIN
#ifdef REDIS_TEST
#include <sys/time.h>
#include <time.h>

void intsetRepr(intset *is) {
int i;
for (i = 0; i < intrev32ifbe(is->length); i++) {
#if 0
static void intsetRepr(intset *is) {
for (uint32_t i = 0; i < intrev32ifbe(is->length); i++) {
printf("%lld\n", (uint64_t)_intsetGet(is,i));
}
printf("\n");
}

void error(char *err) {
static void error(char *err) {
printf("%s\n", err);
exit(1);
}
#endif

void ok(void) {
static void ok(void) {
printf("OK\n");
}

long long usec(void) {
static long long usec(void) {
struct timeval tv;
gettimeofday(&tv,NULL);
return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
}

#define assert(_e) ((_e)?(void)0:(_assert(#_e,__FILE__,__LINE__),exit(1)))
void _assert(char *estr, char *file, int line) {
static void _assert(char *estr, char *file, int line) {
printf("\n\n=== ASSERTION FAILED ===\n");
printf("==> %s:%d '%s' is not true\n",file,line,estr);
}

intset *createSet(int bits, int size) {
static intset *createSet(int bits, int size) {
uint64_t mask = (1<<bits)-1;
uint64_t i, value;
uint64_t value;
intset *is = intsetNew();

for (i = 0; i < size; i++) {
for (int i = 0; i < size; i++) {
if (bits > 32) {
value = (rand()*rand()) & mask;
} else {
Expand All @@ -413,10 +415,8 @@ intset *createSet(int bits, int size) {
return is;
}

void checkConsistency(intset *is) {
int i;

for (i = 0; i < (intrev32ifbe(is->length)-1); i++) {
static void checkConsistency(intset *is) {
for (uint32_t i = 0; i < (intrev32ifbe(is->length)-1); i++) {
uint32_t encoding = intrev32ifbe(is->encoding);

if (encoding == INTSET_ENC_INT16) {
Expand All @@ -432,11 +432,15 @@ void checkConsistency(intset *is) {
}
}

int main(int argc, char **argv) {
#define UNUSED(x) (void)(x)
int intsetTest(int argc, char **argv) {
uint8_t success;
int i;
intset *is;
sranddev();
srand(time(NULL));

UNUSED(argc);
UNUSED(argv);

printf("Value encodings: "); {
assert(_intsetValueEncoding(-32768) == INTSET_ENC_INT16);
Expand Down Expand Up @@ -464,7 +468,7 @@ int main(int argc, char **argv) {
}

printf("Large number of random adds: "); {
int inserts = 0;
uint32_t inserts = 0;
is = intsetNew();
for (i = 0; i < 1024; i++) {
is = intsetAdd(is,rand()%0x800,&success);
Expand Down Expand Up @@ -566,5 +570,7 @@ int main(int argc, char **argv) {
checkConsistency(is);
ok();
}

return 0;
}
#endif
4 changes: 4 additions & 0 deletions src/intset.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ uint8_t intsetGet(intset *is, uint32_t pos, int64_t *value);
uint32_t intsetLen(intset *is);
size_t intsetBlobLen(intset *is);

#ifdef REDIS_TEST
int intsetTest(int argc, char *argv[]);
#endif

#endif // __INTSET_H
24 changes: 24 additions & 0 deletions src/redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -3655,6 +3655,30 @@ int redisIsSupervised(void) {
int main(int argc, char **argv) {
struct timeval tv;

#ifdef REDIS_TEST
if (argc == 3 && !strcasecmp(argv[1], "test")) {
if (!strcasecmp(argv[2], "ziplist")) {
return ziplistTest(argc, argv);
} else if (!strcasecmp(argv[2], "intset")) {
return intsetTest(argc, argv);
} else if (!strcasecmp(argv[2], "zipmap")) {
return zipmapTest(argc, argv);
} else if (!strcasecmp(argv[2], "sha1test")) {
return sha1Test(argc, argv);
} else if (!strcasecmp(argv[2], "util")) {
return utilTest(argc, argv);
} else if (!strcasecmp(argv[2], "sds")) {
return sdsTest(argc, argv);
} else if (!strcasecmp(argv[2], "endianconv")) {
return endianconvTest(argc, argv);
} else if (!strcasecmp(argv[2], "crc64")) {
return crc64Test(argc, argv);
}

return -1; /* test not found */
}
#endif

/* We need to initialize our libraries, and the server configuration. */
#ifdef INIT_SETPROCTITLE_REPLACEMENT
spt_init(argc, argv);
Expand Down
6 changes: 6 additions & 0 deletions src/redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ typedef long long mstime_t; /* millisecond time type. */
#include "latency.h" /* Latency monitor API */
#include "sparkline.h" /* ASII graphs API */

/* Following includes allow test functions to be called from Redis main() */
#include "zipmap.h"
#include "sha1.h"
#include "endianconv.h"
#include "crc64.h"

/* Error codes */
#define REDIS_OK 0
#define REDIS_ERR -1
Expand Down
15 changes: 12 additions & 3 deletions src/sds.c
Original file line number Diff line number Diff line change
Expand Up @@ -962,12 +962,15 @@ sds sdsjoin(char **argv, int argc, char *sep) {
return join;
}

#ifdef SDS_TEST_MAIN
#if defined(REDIS_TEST) || defined(SDS_TEST_MAIN)
#include <stdio.h>
#include "testhelp.h"
#include "limits.h"

int main(void) {
#define UNUSED(x) (void)(x)
int sdsTest(int argc, char *argv[]) {
UNUSED(argc);
UNUSED(argv);
{
struct sdshdr *sh;
sds x = sdsnew("foo"), y;
Expand Down Expand Up @@ -1092,7 +1095,7 @@ int main(void) {
memcmp(y,"\"\\a\\n\\x00foo\\r\"",15) == 0)

{
int oldfree;
unsigned int oldfree;

sdsfree(x);
x = sdsnew("0");
Expand All @@ -1113,3 +1116,9 @@ int main(void) {
return 0;
}
#endif

#ifdef SDS_TEST_MAIN
int main(void) {
return sdsTest();
}
#endif
4 changes: 4 additions & 0 deletions src/sds.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,8 @@ void sdsIncrLen(sds s, int incr);
sds sdsRemoveFreeSpace(sds s);
size_t sdsAllocSize(sds s);

#ifdef REDIS_TEST
int sdsTest(int argc, char *argv[]);
#endif

#endif
11 changes: 6 additions & 5 deletions src/sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,19 @@ void SHA1Final(unsigned char digest[20], SHA1_CTX* context)
}
/* ================ end of sha1.c ================ */

#if 0
#ifdef REDIS_TEST
#define BUFSIZE 4096

int
main(int argc, char **argv)
#define UNUSED(x) (void)(x)
int sha1Test(int argc, char **argv)
{
SHA1_CTX ctx;
unsigned char hash[20], buf[BUFSIZE];
int i;

UNUSED(argc);
UNUSED(argv);

for(i=0;i<BUFSIZE;i++)
buf[i] = i;

Expand All @@ -223,6 +226,4 @@ main(int argc, char **argv)
printf("\n");
return 0;
}

#endif

7 changes: 7 additions & 0 deletions src/sha1.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#ifndef SHA1_H
#define SHA1_H
/* ================ sha1.h ================ */
/*
SHA-1 in C
Expand All @@ -15,3 +17,8 @@ void SHA1Transform(u_int32_t state[5], const unsigned char buffer[64]);
void SHA1Init(SHA1_CTX* context);
void SHA1Update(SHA1_CTX* context, const unsigned char* data, u_int32_t len);
void SHA1Final(unsigned char digest[20], SHA1_CTX* context);

#ifdef REDIS_TEST
int sha1Test(int argc, char **argv);
#endif
#endif
12 changes: 8 additions & 4 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,10 @@ int pathIsBaseName(char *path) {
return strchr(path,'/') == NULL && strchr(path,'\\') == NULL;
}

#ifdef UTIL_TEST_MAIN
#ifdef REDIS_TEST
#include <assert.h>

void test_string2ll(void) {
static void test_string2ll(void) {
char buf[32];
long long v;

Expand Down Expand Up @@ -587,7 +587,7 @@ void test_string2ll(void) {
assert(string2ll(buf,strlen(buf),&v) == 0);
}

void test_string2l(void) {
static void test_string2l(void) {
char buf[32];
long v;

Expand Down Expand Up @@ -636,7 +636,11 @@ void test_string2l(void) {
#endif
}

int main(int argc, char **argv) {
#define UNUSED(x) (void)(x)
int utilTest(int argc, char **argv) {
UNUSED(argc);
UNUSED(argv);

test_string2ll();
test_string2l();
return 0;
Expand Down
4 changes: 4 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ int d2string(char *buf, size_t len, double value);
sds getAbsolutePath(char *filename);
int pathIsBaseName(char *path);

#ifdef REDIS_TEST
int utilTest(int argc, char **argv);
#endif

#endif
Loading

0 comments on commit 8febcff

Please sign in to comment.