diff --git a/src/crc64.c b/src/crc64.c index ecdba90e..f1f76492 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 -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 ab375d3f..c9fca519 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 9adf09c1..f3b0b473 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 -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 d93cd99b..08f55313 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 92c4ecd6..762bd48c 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 +#include -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< 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 51a51275..7550df30 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 9077dd5e..01912b79 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 87415967..1720c24b 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 diff --git a/src/sds.c b/src/sds.c index 1df1043e..05ee0ad5 100644 --- a/src/sds.c +++ b/src/sds.c @@ -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 #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 diff --git a/src/sds.h b/src/sds.h index 37aaf7a2..93dd4f28 100644 --- a/src/sds.h +++ b/src/sds.h @@ -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 59e6f461..199545df 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 -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 b3667cd6..666042c9 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 41d9cb20..3ff44c5b 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 #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 b29c3416..bc27006a 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 384b76bb..22bfa1a4 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 9cf1b248..ac588f05 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