Files
strans/tests/trie_test.c
Hojun-Cho 795f11be22 test: one engine pump and one preedit check for the white-box suites
server_test, ibus_test and xim_adapter_test each ran their own copy of
the same alt loop that stands in for imthread, and two of them had
private checkstr/checkenginepreedit variants. test_util.c now provides
Pump (trace, optional hold on a chosen op, stop) in its own proc so a
test may block in socket I/O while the engine runs, and test.h declares
the engine hooks once; every .c includes dat.h and fn.h itself as the
rest of the tree does. server_test's three copies of fixture setup and
teardown became serverbegin/serverend.
2026-08-16 16:30:32 +09:00

133 lines
2.8 KiB
C

#include "dat.h"
#include "fn.h"
#include "test.h"
void
trie_exact_prefix_and_duplicate(struct ct *t)
{
static const struct {
char *key;
int match;
char *want;
} cases[] = {
{ "a", TrieExact, "alpha" },
{ "ab", TrieExact, "beta" },
{ "", TrieExact, "" },
{ "dupli", TriePrefix, nil },
{ "duplicate", TrieExact, "second" },
{ "missing", TrieMiss, nil },
{ "", TriePrefix, nil },
};
char *v;
Trie *trie;
Str key;
int i, match, n;
trie = trieopen("data/trie.map");
for(i = 0; i < nelem(cases); i++){
key = mkstr(cases[i].key);
match = trielookup(trie, &key, &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_put_and_unloaded(struct ct *t)
{
char *v;
Trie *trie;
Str key;
int n;
key = mkstr("k");
CT_EQ_INT(t, TrieMiss, trielookup(nil, &key, &v, &n));
trie = trienew();
CT_EQ_INT(t, TrieMiss, trielookup(trie, &key, &v, &n));
trieput(trie, "k", 1, "one two", 7);
trieput(trie, "ka", 2, "", 0);
if(CT_EQ_INT(t, TrieExact, trielookup(trie, &key, &v, &n)))
CT_EQ_MEM(t, "one two", v, 7);
key = mkstr("ka");
if(CT_EQ_INT(t, TrieExact, trielookup(trie, &key, &v, &n)))
CT_EQ_INT(t, 0, n);
trieput(trie, "k", 1, "three", 5);
key = mkstr("k");
if(CT_EQ_INT(t, TrieExact, trielookup(trie, &key, &v, &n)))
CT_EQ_MEM(t, "three", v, 5);
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;
} 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);
}
trieclose(fixture);
state.l->map = saved;
}