1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
#include <stdio.h>
#include <string.h>
#include <vector>
#include "ac.h"
using namespace std;
/////////////////////////////////////////////////////////////////////////
//
// Test using strings from input files
//
/////////////////////////////////////////////////////////////////////////
//
class BigFileTester {
public:
BigFileTester(const char* filepath);
private:
void Genector
privaete:
const char* _msg;
int _msg_len;
int _key_num; // number of strings in dictionary
int _key_len_idx;
};
/////////////////////////////////////////////////////////////////////////
//
// Simple (yet maybe tricky) testings
//
/////////////////////////////////////////////////////////////////////////
//
typedef struct {
const char* str;
const char* match;
} StrPair;
typedef struct {
const char* name;
const char** dict;
StrPair* strpairs;
int dict_len;
int strpair_num;
} TestingCase;
class Tests {
public:
Tests(const char* name,
const char* dict[], int dict_len,
StrPair strpairs[], int strpair_num) {
if (!_tests)
_tests = new vector<TestingCase>;
TestingCase tc;
tc.name = name;
tc.dict = dict;
tc.strpairs = strpairs;
tc.dict_len = dict_len;
tc.strpair_num = strpair_num;
_tests->push_back(tc);
}
static vector<TestingCase>* Get_Tests() { return _tests; }
static void Erase_Tests() { delete _tests; _tests = 0; }
private:
static vector<TestingCase> *_tests;
};
vector<TestingCase>* Tests::_tests = 0;
static void
simple_test(void) {
int total = 0;
int fail = 0;
vector<TestingCase> *tests = Tests::Get_Tests();
if (!tests)
return 0;
for (vector<TestingCase>::iterator i = tests->begin(), e = tests->end();
i != e; i++) {
TestingCase& t = *i;
fprintf(stdout, ">Testing %s\nDictionary:[ ", t.name);
for (int i = 0, e = t.dict_len, need_break=0; i < e; i++) {
fprintf(stdout, "%s, ", t.dict[i]);
if (need_break++ == 16) {
fputs("\n ", stdout);
need_break = 0;
}
}
fputs("]\n", stdout);
/* Create the dictionary */
int dict_len = t.dict_len;
ac_t* ac = ac_create(t.dict, dict_len);
for (int ii = 0, ee = t.strpair_num; ii < ee; ii++, total++) {
const StrPair& sp = t.strpairs[ii];
const char *str = sp.str; // the string to be matched
const char *match = sp.match;
fprintf(stdout, "[%3d] Testing '%s' : ", total, str);
int len = strlen(str);
ac_result_t r = ac_match(ac, str, len);
int m_b = r.match_begin;
int m_e = r.match_end;
// The return value per se is insane.
if (m_b > m_e ||
((m_b < 0 || m_e < 0) && (m_b != -1 || m_e != -1))) {
fprintf(stdout, "Insane return value (%d, %d)\n", m_b, m_e);
fail ++;
continue;
}
// If the string is not supposed to match the dictionary.
if (!match) {
if (m_b != -1 || m_e != -1) {
fail ++;
fprintf(stdout, "Not Supposed to match (%d, %d) \n",
m_b, m_e);
} else
fputs("Pass\n", stdout);
continue;
}
// The string or its substring is match the dict.
if (m_b >= len || m_b >= len) {
fail ++;
fprintf(stdout,
"Return value >= the length of the string (%d, %d)\n",
m_b, m_e);
continue;
} else {
int mlen = strlen(match);
if ((mlen != m_e - m_b + 1) ||
strncmp(str + m_b, match, mlen)) {
fail ++;
fprintf(stdout, "Fail\n");
} else
fprintf(stdout, "Pass\n");
}
}
fputs("\n", stdout);
ac_free(ac);
}
fprintf(stdout, "Total : %d, Fail %d\n", total, fail);
return fail ? -1 : 0;
}
int
main (int argc, char** argv) {
int res = simple_test();
return res;
};
/* test 1*/
const char *dict1[] = {"he", "she", "his", "her"};
StrPair strpair1[] = {
{"he", "he"}, {"she", "she"}, {"his", "his"},
{"hers", "he"}, {"ahe", "he"}, {"shhe", "he"},
{"shis2", "his"}, {"ahhe", "he"}
};
Tests test1("test 1",
dict1, sizeof(dict1)/sizeof(dict1[0]),
strpair1, sizeof(strpair1)/sizeof(strpair1[0]));
/* test 2*/
const char *dict2[] = {"poto", "poto"}; /* duplicated strings*/
StrPair strpair2[] = {{"The pot had a handle", 0}};
Tests test2("test 2", dict2, 2, strpair2, 1);
/* test 3*/
const char *dict3[] = {"The"};
StrPair strpair3[] = {{"The pot had a handle", "The"}};
Tests test3("test 3", dict3, 1, strpair3, 1);
/* test 4*/
const char *dict4[] = {"pot"};
StrPair strpair4[] = {{"The pot had a handle", "pot"}};
Tests test4("test 4", dict4, 1, strpair4, 1);
/* test 5*/
const char *dict5[] = {"pot "};
StrPair strpair5[] = {{"The pot had a handle", "pot "}};
Tests test5("test 5", dict5, 1, strpair5, 1);
/* test 6*/
const char *dict6[] = {"ot h"};
StrPair strpair6[] = {{"The pot had a handle", "ot h"}};
Tests test6("test 6", dict6, 1, strpair6, 1);
/* test 7*/
const char *dict7[] = {"andle"};
StrPair strpair7[] = {{"The pot had a handle", "andle"}};
Tests test7("test 7", dict7, 1, strpair7, 1);
|