Files
strans/tests/trie_test.c

127 lines
2.7 KiB
C

#include "test.h"
void
trie_exact_prefix_and_duplicate(struct ct *t)
{
static const struct {
char *key;
int klen;
int match;
char *want;
} cases[] = {
{ "a", 1, TrieExact, "alpha" },
{ "dupli", 5, TriePrefix, nil },
{ "duplicate", 9, TrieExact, "second" },
{ "missing", 7, TrieMiss, nil },
{ nil, 1, TrieMiss, nil },
{ "a", -1, TrieMiss, nil },
};
char *v;
Trie *trie;
int i, match, n;
trie = trieopen("data/trie.map");
for(i = 0; i < nelem(cases); i++){
v = "unchanged";
n = 77;
match = trielookup(trie, cases[i].key, cases[i].klen, &v, &n);
if(match != cases[i].match){
CT_ERRORF(t, "case %d: want match %d, got %d",
i, cases[i].match, match);
continue;
}
if(cases[i].want == nil){
CT_EQ_PTR(t, nil, v);
CT_EQ_INT(t, 0, n);
}else{
CT_EQ_INT(t, strlen(cases[i].want), n);
CT_EQ_MEM(t, cases[i].want, v, n);
}
}
trieclose(trie);
}
void
trie_optional_outputs_and_invalid_lengths(struct ct *t)
{
char *v;
Trie *trie;
int n;
trie = trieopen("data/trie.map");
CT_EQ_INT(t, TrieExact, trielookup(trie, "a", 1, &v, nil));
CT_CHECK(t, v != nil);
CT_EQ_INT(t, TrieExact, trielookup(trie, "a", 1, nil, &n));
CT_EQ_INT(t, 5, n);
CT_EQ_INT(t, TriePrefix,
trielookup(trie, "dupli", 5, nil, nil));
CT_EQ_INT(t, TriePrefix, trielookup(trie, nil, 0, &v, &n));
CT_EQ_PTR(t, nil, v);
CT_EQ_INT(t, 0, n);
trieclose(trie);
}
void
production_maps_load(struct ct *t)
{
char path[256];
Trie *map;
int i, loaded, registered;
loaded = registered = 0;
for(i = 0; i < nlang; i++){
if(langs[i].mapname == nil)
continue;
registered++;
snprint(path, sizeof path, "../map/%s.map", langs[i].mapname);
map = trieopen(path);
if(!CT_CHECK(t, map != nil))
continue;
trieclose(map);
loaded++;
}
CT_CHECK(t, registered > 0);
CT_EQ_INT(t, registered, loaded);
}
void
transmap_states(struct ct *t)
{
static const struct {
char *pre;
Rune key;
int eat;
char *emit;
char *next;
char *mapped;
} cases[] = {
{ "", 'k', 1, "", "k", "" },
{ "k", 'a', 1, "", "ka", "" },
{ "ka", 's', 1, "", "s", "" },
{ "ka", 'q', 0, "", "", "" },
{ "k", 'q', 0, "k", "", "" },
};
Emit e;
Im state;
Trie *fixture, *saved;
int i;
fixture = trieopen("data/hira.map");
memset(&state, 0, sizeof state);
state.l = getlang(LangJP);
saved = state.l->map;
state.l->map = fixture;
for(i = 0; i < nelem(cases); i++){
state.pre = mkstr(cases[i].pre);
e = transmap(&state, cases[i].key);
if(e.eat != cases[i].eat)
CT_ERRORF(t, "case %d: want eat %d, got %d",
i, cases[i].eat, e.eat);
checkstr(t, "emit", cases[i].emit, &e.s);
checkstr(t, "next", cases[i].next, &e.next);
checkstr(t, "mapped", cases[i].mapped, &e.dict);
}
trieclose(fixture);
state.l->map = saved;
}