synchornizing changes from RediSearch. @mnunberge please next time reflect these changes on the main repo
parent
2ce8734f03
commit
755ecf306b
@ -1,67 +1,69 @@
|
||||
#ifndef __TEST_UTIL_H__
|
||||
#define __TEST_UTIL_H__
|
||||
#ifndef __TESTUTIL_H__
|
||||
#define __TESTUTIL_H__
|
||||
|
||||
#include "util.h"
|
||||
#include <stdarg.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static int numTests = 0;
|
||||
static int numAsserts = 0;
|
||||
|
||||
#define RMUtil_Test(f) \
|
||||
if (argc < 2 || RMUtil_ArgExists(__STRING(f), argv, argc, 1)) { \
|
||||
int rc = f(ctx); \
|
||||
if (rc != REDISMODULE_OK) { \
|
||||
RedisModule_ReplyWithError(ctx, "Test " __STRING(f) " FAILED"); \
|
||||
return REDISMODULE_ERR;\
|
||||
}\
|
||||
}
|
||||
#define TESTFUNC(f) \
|
||||
printf(" Testing %s\t\t", __STRING(f)); \
|
||||
numTests++; \
|
||||
fflush(stdout); \
|
||||
if (f()) { \
|
||||
printf(" %s FAILED!\n", __STRING(f)); \
|
||||
exit(1); \
|
||||
} else \
|
||||
printf("[PASS]\n");
|
||||
|
||||
#define ASSERTM(expr, ...) \
|
||||
if (!(expr)) { \
|
||||
fprintf(stderr, "%s:%d: Assertion '%s' Failed: " __VA_ARGS__ "\n", __FILE__, __LINE__, \
|
||||
__STRING(expr)); \
|
||||
return -1; \
|
||||
} \
|
||||
numAsserts++;
|
||||
|
||||
#define RMUtil_Assert(expr) if (!(expr)) { fprintf (stderr, "Assertion '%s' Failed\n", __STRING(expr)); return REDISMODULE_ERR; }
|
||||
#define ASSERT(expr) \
|
||||
if (!(expr)) { \
|
||||
fprintf(stderr, "%s:%d Assertion '%s' Failed\n", __FILE__, __LINE__, __STRING(expr)); \
|
||||
return -1; \
|
||||
} \
|
||||
numAsserts++;
|
||||
|
||||
#define RMUtil_AssertReplyEquals(rep, cstr) RMUtil_Assert( \
|
||||
RMUtil_StringEquals(RedisModule_CreateStringFromCallReply(rep), RedisModule_CreateString(ctx, cstr, strlen(cstr))) \
|
||||
)
|
||||
#
|
||||
#define ASSERT_STRING_EQ(s1, s2) ASSERT(!strcmp(s1, s2));
|
||||
|
||||
/**
|
||||
* Create an arg list to pass to a redis command handler manually, based on the format in fmt.
|
||||
* The accepted format specifiers are:
|
||||
* c - for null terminated c strings
|
||||
* s - for RedisModuleString* objects
|
||||
* l - for longs
|
||||
*
|
||||
* Example: RMUtil_MakeArgs(ctx, &argc, "clc", "hello", 1337, "world");
|
||||
*
|
||||
* Returns an array of RedisModuleString pointers. The size of the array is store in argcp
|
||||
*/
|
||||
RedisModuleString **RMUtil_MakeArgs(RedisModuleCtx *ctx, int *argcp, const char *fmt, ...) {
|
||||
#define ASSERT_EQUAL(x, y, ...) \
|
||||
if (x != y) { \
|
||||
fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); \
|
||||
fprintf(stderr, "%g != %g: " __VA_ARGS__ "\n", (double)x, (double)y); \
|
||||
return -1; \
|
||||
} \
|
||||
numAsserts++;
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
RedisModuleString **argv = calloc(strlen(fmt), sizeof(RedisModuleString*));
|
||||
int argc = 0;
|
||||
const char *p = fmt;
|
||||
while(*p) {
|
||||
if (*p == 'c') {
|
||||
char *cstr = va_arg(ap,char*);
|
||||
argv[argc++] = RedisModule_CreateString(ctx, cstr, strlen(cstr));
|
||||
} else if (*p == 's') {
|
||||
argv[argc++] = va_arg(ap,void*);;
|
||||
} else if (*p == 'l') {
|
||||
long ll = va_arg(ap,long long);
|
||||
argv[argc++] = RedisModule_CreateStringFromLongLong(ctx, ll);
|
||||
} else {
|
||||
goto fmterr;
|
||||
#define FAIL(fmt, ...) \
|
||||
{ \
|
||||
fprintf(stderr, "%s:%d: FAIL: " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
|
||||
return -1; \
|
||||
}
|
||||
p++;
|
||||
|
||||
#define RETURN_TEST_SUCCESS return 0;
|
||||
#define TEST_CASE(x, block) \
|
||||
int x { \
|
||||
block; \
|
||||
return 0 \
|
||||
}
|
||||
*argcp = argc;
|
||||
|
||||
return argv;
|
||||
fmterr:
|
||||
free(argv);
|
||||
return NULL;
|
||||
}
|
||||
#define PRINT_TEST_SUMMARY printf("\nTotal: %d tests and %d assertions OK\n", numTests, numAsserts);
|
||||
|
||||
#define TEST_MAIN(body) \
|
||||
int main(int argc, char **argv) { \
|
||||
printf("Starting Test '%s'...\n", argv[0]); \
|
||||
body; \
|
||||
PRINT_TEST_SUMMARY; \
|
||||
printf("\n--------------------\n\n"); \
|
||||
return 0; \
|
||||
}
|
||||
#endif
|
Loading…
Reference in New Issue