This commit is contained in:
2020-06-09 22:42:32 +02:00
commit 50a26664e3
9 changed files with 578 additions and 0 deletions
+159
View File
@@ -0,0 +1,159 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "prefix_match.h"
#include "test_framework.h"
// --- test cases ---
bool test_prefix_match() {
const char *options[] = {
"ahoj",//0
"ahojahoj",//1
"ble",//2
"citron",//3
"foo",//4
"foo bar",//5
"foo baz",//6
"foo bcz",//7
"eps",//8
"eps set",//9
"eps set config",//10
"eps get config",//11
"eps hk2",//12
"eps hk2 vi",//13
"eps hk2 out",//14
"delete from table users",//15
"delete,from;,,,,table sessions",//16
NULL
};
check_eq(prefix_match("", options, 0), -1);
check_eq(prefix_match("b", options, 0), 2);
check_eq(prefix_match("c", options, 0), 3);
check_eq(prefix_match("citron", options, 0), 3);
check_eq(prefix_match("foo", options, 0), 4);
check_eq(prefix_match("foo ", options, 0), -1);
check_eq(prefix_match("foo bc", options, 0), 7);
check_eq(prefix_match("foo bcz", options, 0), 7);
check_eq(prefix_match("fOO bcz", options, PREFIXMATCH_CASE_SENSITIVE), -1); // CS
check_eq(prefix_match("fOO bcz", options, 0), 7); // CI
check_eq(prefix_match("ahoj", options, 0), 0);
check_eq(prefix_match("ahoja", options, 0), 1);
return true;
}
bool test_multipart_test() {
check_eq(pm_multipart_test("ahoj", "ahoj", " ", 0), 1);
check_eq(pm_multipart_test("ah", "ahoj", " ", 0), 1);
check_eq(pm_multipart_test("xxxx", "ahoj", " ", 0), 0);
check_eq(pm_multipart_test("", "ahoj", " ", 0), 0);
check_eq(pm_multipart_test("", "", " ", 0), 1);
check_eq(pm_multipart_test("multi part", "multi part", " ", 0), 1);
check_eq(pm_multipart_test("multi", "multi part", " ", 0), 0);
check_eq(pm_multipart_test("multi part", "multi", " ", 0), 0);
check_eq(pm_multipart_test("multi part", "multi dog", " ", 0), 0);
check_eq(pm_multipart_test("multi part", "multi part", " ", 0), 1);
check_eq(pm_multipart_test(" multi part ", "multi part", " ", 0), 1);
check_eq(pm_multipart_test("multi ,; part", "multi,part", ",; ", 0), 1);
check_eq(pm_multipart_test("m p", "multi part", " ", 0), 1);
check_eq(pm_multipart_test("mu pa", "multi part", " ", 0), 1);
check_eq(pm_multipart_test("mu pp", "multi part", " ", 0), 0);
check_eq(pm_multipart_test("m ", "multi part", " ", PREFIXMATCH_MULTI_PARTIAL), 2);
check_eq(pm_multipart_test("v l s h", "very long sentence here", " ", 0), 1);
check_eq(pm_multipart_test("v l s h", "very long sentence here too", " ", 0), 0);
check_eq(pm_multipart_test("v l", "very long sentence here too", " ", 0), 0);
check_eq(pm_multipart_test("v l", "very long sentence here too", " ", PREFIXMATCH_MULTI_PARTIAL), 2);
check_eq(pm_multipart_test("v l s h", "very long sentence here", " ", PREFIXMATCH_MULTI_PARTIAL | PREFIXMATCH_CASE_SENSITIVE), 1);
check_eq(pm_multipart_test("v l s h", "very long sentence here too", " ", PREFIXMATCH_MULTI_PARTIAL), 2);
return true;
}
bool test_multipart_match() {
const char *options[] = {
"ahoj",//0
"foo",//1
"foo bar",//2
"foo baz",//3
"foo bcz",//4
"eps", // 5
"eps set",//6
"eps set config",//7
"eps get config",//8
"eps hk2",//9
"eps hk2 vi", //10
"eps hk2 out",//11
"delete from table users",//12
"delete,from;,,,,table sessions",//13
NULL
};
check_eq(prefix_multipart_match("", options, " ", 0), -1);
check_eq(prefix_multipart_match("x", options, " ", 0), -1);
check_eq(prefix_multipart_match("ahoj", options, " ", 0), 0);
check_eq(prefix_multipart_match("a", options, " ", 0), 0);
check_eq(prefix_multipart_match("ah", options, " ", 0), 0);
check_eq(prefix_multipart_match("foo", options, " ", 0), 1);
check_eq(prefix_multipart_match("f", options, " ", 0), 1);
check_eq(prefix_multipart_match("f b", options, " ", 0), -1);
check_eq(prefix_multipart_match("f b", options, " ", PREFIXMATCH_MULTI_PARTIAL), -1);
check_eq(prefix_multipart_match("f bc", options, " ", 0), 4);
check_eq(prefix_multipart_match("e", options, " ", 0), 5);
check_eq(prefix_multipart_match("eps", options, " ", 0), 5);
check_eq(prefix_multipart_match("eps banana", options, " ", 0), -1);
check_eq(prefix_multipart_match("eps banana", options, " ", PREFIXMATCH_MULTI_PARTIAL), -1);
check_eq(prefix_multipart_match("eps set", options, " ", 0), 6);
check_eq(prefix_multipart_match("e s", options, " ", 0), 6);
check_eq(prefix_multipart_match("e set", options, " ", 0), 6);
check_eq(prefix_multipart_match("eps s", options, " ", 0), 6);
check_eq(prefix_multipart_match("eps s", options, " ", 0), 6);
check_eq(prefix_multipart_match("e s c", options, " ", 0), 7);
check_eq(prefix_multipart_match("e g c", options, " ", 0), 8);
// there is only one get command
check_eq(prefix_multipart_match("eps get", options, " ", 0), -1);
check_eq(prefix_multipart_match("eps get", options, " ", PREFIXMATCH_MULTI_PARTIAL), 8);
check_eq(prefix_multipart_match("e g", options, " ", 0), -1);
check_eq(prefix_multipart_match("e g", options, " ", PREFIXMATCH_MULTI_PARTIAL), 8);
check_eq(prefix_multipart_match("epx get", options, " ", 0), -1);
check_eq(prefix_multipart_match("epx get", options, " ", PREFIXMATCH_MULTI_PARTIAL), -1);
check_eq(prefix_multipart_match("d f t u", options, " ", 0), 12);
check_eq(prefix_multipart_match("d f t s", options, ",; ", 0), 13);
return true;
}
// --- test launcher ---
static struct Test tests[] = {
{"prefix_match", test_prefix_match},
{"prefix_multipart_test", test_multipart_test},
{"prefix_multipart_match", test_multipart_match},
{NULL, NULL}
};
int main() {
run_tests(tests, "main");
return 0;
}
+22
View File
@@ -0,0 +1,22 @@
#include <stdio.h>
#include "test_framework.h"
void run_tests(struct Test *tests, const char *module_name) {
printf("Running tests module \"%s\"...\n\n", module_name);
struct Test *t = &tests[0];
int passed = 0, failed = 0;
do {
printf("Running test \"%s\"...\n", t->name);
if (t->func()) {
printf("Test %s \x1b[32mPASSED\x1b[m\n", t->name);
passed++;
} else {
printf("Test %s \x1b[31mFAILED\x1b[m\n", t->name);
failed++;
}
} while((++t)->name != NULL);
printf("\nTests module \"%s\" done. %d passed, %d failed.\n\n", module_name, passed, failed);
}
+100
View File
@@ -0,0 +1,100 @@
/**
* shared defines and utils for tests
*
* Created on 2020/05/12.
*/
#ifndef TESTS_TEST_FRAMEWORK_H
#define TESTS_TEST_FRAMEWORK_H
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#define XSTR(s) STR(s)
#define STR(s) #s
typedef bool(*testfn_t)(void);
struct Test {
const char *name;
testfn_t func;
};
void run_tests(struct Test *tests, const char *module_name);
// _good must be an array, not a pointer
#define check_array(_var, _good) \
do { \
for (int i = 0; i < (int)sizeof(_good); i++) { \
if (_var[i] != _good[i]) { \
printf("%s:%d \x1b[31mAssert failed\x1b[m: arrays differ\n\x1b[37;1m n# exp act\x1b[m\n", __func__, __LINE__); \
for (i = 0; i < (int)sizeof(_good); i++) { \
if (_var[i] == _good[i]) { \
printf(" %2d - 0x%02x -\n", i, _var[i]); \
} else { \
printf(" %2d \x1b[32m0x%02x \x1b[31m0x%02x\x1b[m\n", i, _good[i], _var[i]); \
} \
} \
return false; \
} \
} \
} while(0)
#define check(_cond) \
do { \
if (!(_cond)) { \
printf("%s:%d \x1b[31mAssert failed\x1b[m: %s\n", __func__, __LINE__, STR(_cond)); \
return false; \
} \
} while(0)
#define check_eq(_var, _good) \
do { \
if ((_var) != (_good)) { \
printf("%s:%d \x1b[31mAssert failed\x1b[m: %s == %s\n", __func__, __LINE__, STR(_var), STR(_good)); \
printf(" \x1b[31m%d (0x%02x)\x1b[m != \x1b[32m%d (0x%02x)\x1b[m\n", _var, _var, _good, _good); \
return false; \
} \
} while(0)
#define check_eq_ul(_var, _good) \
do { \
if ((_var) != (_good)) { \
printf("%s:%d \x1b[31mAssert failed\x1b[m: %s == %s\n", __func__, __LINE__, STR(_var), STR(_good)); \
printf(" \x1b[31m%ld (0x%02lx)\x1b[m != \x1b[32m%ld (0x%02lx)\x1b[m\n", _var, _var, _good, _good); \
return false; \
} \
} while(0)
#define check_eq_l(_var, _good) \
do { \
if ((_var) != (_good)) { \
printf("%s:%d \x1b[31mAssert failed\x1b[m: %s == %s\n", __func__, __LINE__, STR(_var), STR(_good)); \
printf(" \x1b[31m%ld\x1b[m != \x1b[32m%ld\x1b[m\n", _var, _good); \
return false; \
} \
} while(0)
#define check_eq_f(_var, _good) \
do { \
if ((_var) != (_good)) { \
printf("%s:%d \x1b[31mAssert failed\x1b[m: %s == %s\n", __func__, __LINE__, STR(_var), STR(_good)); \
printf(" \x1b[31m%f\x1b[m != \x1b[32m%f\x1b[m\n", _var, _good); \
return false; \
} \
} while(0)
#define check_eq_f_safe(_var, _good, thr) \
do { \
if (fabs((double)((_var) - (_good))) > thr) { \
printf("%s:%d \x1b[31mAssert failed\x1b[m: %s == %s\n", __func__, __LINE__, STR(_var), STR(_good)); \
printf(" \x1b[31m%f\x1b[m != \x1b[32m%f\x1b[m\n", _var, _good); \
return false; \
} \
} while(0)
#endif //TESTS_TEST_FRAMEWORK_H