Abbreviated multi-part command recognition C library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
prefix_match/test/main.c

159 lines
5.8 KiB

#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;
}