diff options
author | Matt Stancliff <matt@genges.com> | 2014-11-12 21:58:57 -0500 |
---|---|---|
committer | Matt Stancliff <matt@genges.com> | 2014-12-23 09:31:03 -0500 |
commit | 8febcffdc597566f1e307c0534014b2bdf687c02 (patch) | |
tree | 74cf78291298cce2dfc1abf5fe1c25aacd86aa4f | |
parent | 8380655e85f0afd1f0afc99b464717cb97002b7a (diff) | |
download | redis-8febcffdc597566f1e307c0534014b2bdf687c02.tar.gz |
Allow all code tests to run using Redis args
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.
-rw-r--r-- | src/crc64.c | 8 | ||||
-rw-r--r-- | src/crc64.h | 4 | ||||
-rw-r--r-- | src/endianconv.c | 8 | ||||
-rw-r--r-- | src/endianconv.h | 4 | ||||
-rw-r--r-- | src/intset.c | 42 | ||||
-rw-r--r-- | src/intset.h | 4 | ||||
-rw-r--r-- | src/redis.c | 24 | ||||
-rw-r--r-- | src/redis.h | 6 | ||||
-rw-r--r-- | src/sds.c | 15 | ||||
-rw-r--r-- | src/sds.h | 4 | ||||
-rw-r--r-- | src/sha1.c | 11 | ||||
-rw-r--r-- | src/sha1.h | 7 | ||||
-rw-r--r-- | src/util.c | 12 | ||||
-rw-r--r-- | src/util.h | 4 | ||||
-rw-r--r-- | src/ziplist.c | 19 | ||||
-rw-r--r-- | src/ziplist.h | 4 | ||||
-rw-r--r-- | src/zipmap.c | 10 | ||||
-rw-r--r-- | src/zipmap.h | 4 |
18 files changed, 143 insertions, 47 deletions
diff --git a/src/crc64.c b/src/crc64.c index ecdba90e0..f1f764922 100644 --- a/src/crc64.c +++ b/src/crc64.c @@ -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; diff --git a/src/crc64.h b/src/crc64.h index ab375d3f4..c9fca519d 100644 --- a/src/crc64.h +++ b/src/crc64.h @@ -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 diff --git a/src/endianconv.c b/src/endianconv.c index 9adf09c1f..f3b0b4730 100644 --- a/src/endianconv.c +++ b/src/endianconv.c @@ -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); diff --git a/src/endianconv.h b/src/endianconv.h index d93cd99ba..08f553136 100644 --- a/src/endianconv.h +++ b/src/endianconv.h @@ -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 diff --git a/src/intset.c b/src/intset.c index 92c4ecd6e..762bd48c8 100644 --- a/src/intset.c +++ b/src/intset.c @@ -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 { @@ -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) { @@ -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); @@ -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); @@ -566,5 +570,7 @@ int main(int argc, char **argv) { checkConsistency(is); ok(); } + + return 0; } #endif diff --git a/src/intset.h b/src/intset.h index 51a512753..7550df303 100644 --- a/src/intset.h +++ b/src/intset.h @@ -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 diff --git a/src/redis.c b/src/redis.c index 9077dd5e6..01912b79b 100644 --- a/src/redis.c +++ b/src/redis.c @@ -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); diff --git a/src/redis.h b/src/redis.h index 87415967d..1720c24bd 100644 --- a/src/redis.h +++ b/src/redis.h @@ -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 @@ -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; @@ -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"); @@ -1113,3 +1116,9 @@ int main(void) { return 0; } #endif + +#ifdef SDS_TEST_MAIN +int main(void) { + return sdsTest(); +} +#endif @@ -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 diff --git a/src/sha1.c b/src/sha1.c index 59e6f461d..199545df4 100644 --- a/src/sha1.c +++ b/src/sha1.c @@ -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; @@ -223,6 +226,4 @@ main(int argc, char **argv) printf("\n"); return 0; } - #endif - diff --git a/src/sha1.h b/src/sha1.h index 9d6f12965..4c76d19da 100644 --- a/src/sha1.h +++ b/src/sha1.h @@ -1,3 +1,5 @@ +#ifndef SHA1_H +#define SHA1_H /* ================ sha1.h ================ */ /* SHA-1 in C @@ -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 diff --git a/src/util.c b/src/util.c index 80242ff71..bd158ae90 100644 --- a/src/util.c +++ b/src/util.c @@ -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; @@ -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; @@ -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; diff --git a/src/util.h b/src/util.h index b3667cd6f..666042c9b 100644 --- a/src/util.h +++ b/src/util.h @@ -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 diff --git a/src/ziplist.c b/src/ziplist.c index 41d9cb20c..3ff44c5b8 100644 --- a/src/ziplist.c +++ b/src/ziplist.c @@ -952,14 +952,14 @@ void ziplistRepr(unsigned char *zl) { printf("{end}\n\n"); } -#ifdef ZIPLIST_TEST_MAIN +#ifdef REDIS_TEST #include <sys/time.h> #include "adlist.h" #include "sds.h" #define debug(f, ...) { if (DEBUG) printf(f, __VA_ARGS__); } -unsigned char *createList() { +static unsigned char *createList() { unsigned char *zl = ziplistNew(); zl = ziplistPush(zl, (unsigned char*)"foo", 3, ZIPLIST_TAIL); zl = ziplistPush(zl, (unsigned char*)"quux", 4, ZIPLIST_TAIL); @@ -968,7 +968,7 @@ unsigned char *createList() { return zl; } -unsigned char *createIntList() { +static unsigned char *createIntList() { unsigned char *zl = ziplistNew(); char buf[32]; @@ -987,13 +987,13 @@ unsigned char *createIntList() { return zl; } -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; } -void stress(int pos, int num, int maxsize, int dnum) { +static void stress(int pos, int num, int maxsize, int dnum) { int i,j,k; unsigned char *zl; char posstr[2][5] = { "HEAD", "TAIL" }; @@ -1016,7 +1016,7 @@ void stress(int pos, int num, int maxsize, int dnum) { } } -void pop(unsigned char *zl, int where) { +static void pop(unsigned char *zl, int where) { unsigned char *p, *vstr; unsigned int vlen; long long vlong; @@ -1043,7 +1043,7 @@ void pop(unsigned char *zl, int where) { } } -int randstring(char *target, unsigned int min, unsigned int max) { +static int randstring(char *target, unsigned int min, unsigned int max) { int p = 0; int len = min+rand()%(max-min+1); int minval, maxval; @@ -1069,7 +1069,7 @@ int randstring(char *target, unsigned int min, unsigned int max) { return len; } -void verify(unsigned char *zl, zlentry *e) { +static void verify(unsigned char *zl, zlentry *e) { int i; int len = ziplistLen(zl); zlentry _e; @@ -1085,7 +1085,7 @@ void verify(unsigned char *zl, zlentry *e) { } } -int main(int argc, char **argv) { +int ziplistTest(int argc, char **argv) { unsigned char *zl, *p; unsigned char *entry; unsigned int elen; @@ -1534,5 +1534,4 @@ int main(int argc, char **argv) { return 0; } - #endif diff --git a/src/ziplist.h b/src/ziplist.h index b29c34167..bc27006aa 100644 --- a/src/ziplist.h +++ b/src/ziplist.h @@ -44,3 +44,7 @@ unsigned int ziplistCompare(unsigned char *p, unsigned char *s, unsigned int sle unsigned char *ziplistFind(unsigned char *p, unsigned char *vstr, unsigned int vlen, unsigned int skip); unsigned int ziplistLen(unsigned char *zl); size_t ziplistBlobLen(unsigned char *zl); + +#ifdef REDIS_TEST +int ziplistTest(int argc, char *argv[]); +#endif diff --git a/src/zipmap.c b/src/zipmap.c index 384b76bba..22bfa1a46 100644 --- a/src/zipmap.c +++ b/src/zipmap.c @@ -370,8 +370,8 @@ size_t zipmapBlobLen(unsigned char *zm) { return totlen; } -#ifdef ZIPMAP_TEST_MAIN -void zipmapRepr(unsigned char *p) { +#ifdef REDIS_TEST +static void zipmapRepr(unsigned char *p) { unsigned int l; printf("{status %u}",*p++); @@ -404,9 +404,13 @@ void zipmapRepr(unsigned char *p) { printf("\n"); } -int main(void) { +#define UNUSED(x) (void)(x) +int zipmapTest(int argc, char *argv[]) { unsigned char *zm; + UNUSED(argc); + UNUSED(argv); + zm = zipmapNew(); zm = zipmapSet(zm,(unsigned char*) "name",4, (unsigned char*) "foo",3,NULL); diff --git a/src/zipmap.h b/src/zipmap.h index 9cf1b2484..ac588f05a 100644 --- a/src/zipmap.h +++ b/src/zipmap.h @@ -46,4 +46,8 @@ unsigned int zipmapLen(unsigned char *zm); size_t zipmapBlobLen(unsigned char *zm); void zipmapRepr(unsigned char *p); +#ifdef REDIS_TEST +int zipmapTest(int argc, char *argv[]); +#endif + #endif |