add focused unit and map checks

This commit is contained in:
2026-08-11 19:35:11 +09:00
parent 9dc116a035
commit 42823420ab
18 changed files with 1326 additions and 9 deletions

126
tests/trie_test.c Normal file
View File

@@ -0,0 +1,126 @@
#include "test.h"
static void
checkcrlf(struct ct *t)
{
static char data[] = "crlf\tvalue\r\n";
char path[80], *v;
Trie *trie;
int fd, n;
snprint(path, sizeof path, "/tmp/strans-crlf-%d.map", getpid());
remove(path);
fd = create(path, OWRITE, 0600);
if(fd < 0){
CT_ERRORF(t, "cannot create CRLF fixture");
return;
}
if(write(fd, data, sizeof data - 1) != (long)(sizeof data - 1)){
CT_ERRORF(t, "cannot write CRLF fixture");
close(fd);
remove(path);
return;
}
close(fd);
trie = trieopen(path);
remove(path);
v = trieget(trie, "crlf", 4, &n);
if(CT_CHECK(t, v != nil)){
CT_EQ_INT(t, 5, n);
CT_EQ_MEM(t, "value", v, n);
}
trieclose(trie);
}
void
trie_exact_prefix_and_duplicate(struct ct *t)
{
char *v, *sentinel;
Trie *trie;
int n;
trie = trieopen("data/trie.map");
v = trieget(trie, "a", 1, &n);
if(!CT_CHECK(t, v != nil))
goto cleanup;
CT_EQ_INT(t, 5, n);
CT_EQ_MEM(t, "alpha", v, n);
sentinel = "unchanged";
v = sentinel;
n = 77;
CT_CHECK(t, trielookup(trie, "dupli", 5, &v, &n));
CT_EQ_PTR(t, sentinel, v);
CT_EQ_INT(t, 77, n);
v = trieget(trie, "duplicate", 9, &n);
if(!CT_CHECK(t, v != nil))
goto cleanup;
CT_EQ_INT(t, 6, n);
CT_EQ_MEM(t, "second", v, n);
v = trieget(trie, "tabs", 4, &n);
if(!CT_CHECK(t, v != nil))
goto cleanup;
CT_EQ_INT(t, 7, n);
CT_EQ_MEM(t, "one\ttwo", v, n);
CT_EQ_PTR(t, nil, trieget(trie, "missing", 7, &n));
cleanup:
trieclose(trie);
checkcrlf(t);
}
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;
int i;
memset(&state, 0, sizeof state);
state.l = getlang(LangJP);
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);
}
}