meshio_test.go raw
1 package meshio
2
3 import (
4 "bytes"
5 "testing"
6
7 "git.smesh.lol/go-iskra/lattice"
8 )
9
10 // meshTree builds a tree in the shape MESH can carry: one key space in block
11 // 0, record indices from 1, meta and lex pools starting at 1.
12 func TestRoundTrip(t *testing.T) {
13 tree := lattice.NewRecordPoolTree(0)
14
15 // Insert some entries.
16 words := []struct {
17 word string
18 lang uint8
19 }{
20 {"water", lattice.LangEN},
21 {"вода", lattice.LangBG},
22 {"book", lattice.LangEN},
23 {"книга", lattice.LangBG},
24 }
25
26 for _, w := range words {
27 norm := lattice.Normalize(w.word)
28 key := lattice.MakeKey(w.lang, lattice.HashWord(norm))
29 metaIdx := tree.AllocMeta()
30 lexIdx := tree.AllocLex()
31 meta := tree.MetaAt(metaIdx)
32 meta.LangTag = w.lang
33 lex := tree.LexAt(lexIdx)
34 lex.SetForm(w.word, tree.PoolPtr())
35 if _, err := tree.Insert(lattice.Bgrammatical, key, lattice.Record{DataLen: 0}); err != nil {
36 t.Fatal(err)
37 }
38 tree.RegisterKey(key, metaIdx, lexIdx)
39 }
40
41 // Add some adj links.
42 tree.AddAdj(1, 2) // water → вода
43 tree.AddAdj(2, 1) // вода → water
44 tree.AddAdj(3, 4) // book → книга
45 tree.AddAdj(4, 3) // книга → book
46 tree.FinalizeAdj()
47
48 // Save.
49 var buf bytes.Buffer
50 if err := Save(&buf, tree, lattice.LangBG, lattice.LangEN); err != nil {
51 t.Fatalf("Save: %v", err)
52 }
53
54 // Load.
55 loaded, srcLang, tgtLang, err := Load(&buf)
56 if err != nil {
57 t.Fatalf("Load: %v", err)
58 }
59
60 // Verify header.
61 if srcLang != lattice.LangBG {
62 t.Errorf("srcLang = %d, want %d", srcLang, lattice.LangBG)
63 }
64 if tgtLang != lattice.LangEN {
65 t.Errorf("tgtLang = %d, want %d", tgtLang, lattice.LangEN)
66 }
67
68 // Verify counts.
69 if loaded.NodeCount() != tree.NodeCount() {
70 t.Errorf("NodeCount = %d, want %d", loaded.NodeCount(), tree.NodeCount())
71 }
72 // Every meta slot is the null sentinel, so the entry count is the pool
73 // length minus one on both sides.
74 if loaded.MetaLen != uint32(len(tree.Meta)) || loaded.EntryCount() != len(tree.Meta)-1 {
75 t.Errorf("meta pool after round trip: len %d count %d, want len %d count %d",
76 loaded.MetaLen, loaded.EntryCount(), len(tree.Meta), len(tree.Meta)-1)
77 }
78 if len(loaded.Adj) != len(tree.Adj) {
79 t.Errorf("AdjCount = %d, want %d", len(loaded.Adj), len(tree.Adj))
80 }
81
82 // Verify key lookups still work.
83 for _, w := range words {
84 norm := lattice.Normalize(w.word)
85 key := lattice.MakeKey(w.lang, lattice.HashWord(norm))
86 metaIdx, lexIdx, ok := loaded.LookupMetaByKey(key)
87 if !ok {
88 t.Errorf("LookupMetaByKey(%q) not found after round-trip", w.word)
89 continue
90 }
91 lex := loaded.LexAt(lexIdx)
92 form := lex.GetForm(loaded.StringPool)
93 if form != w.word {
94 t.Errorf("GetForm after round-trip = %q, want %q", form, w.word)
95 }
96 meta := loaded.MetaAt(metaIdx)
97 if meta.LangTag != w.lang {
98 t.Errorf("LangTag after round-trip = %d, want %d", meta.LangTag, w.lang)
99 }
100 }
101
102 // Verify Walk still works.
103 for _, w := range words {
104 norm := lattice.Normalize(w.word)
105 key := lattice.MakeKey(w.lang, lattice.HashWord(norm))
106 if _, _, found, err := loaded.Walk(lattice.Bgrammatical, key); err != nil || !found {
107 t.Errorf("Walk(%q) not found after round-trip (%v)", w.word, err)
108 }
109 }
110 }
111
112 func TestSingleEntry(t *testing.T) {
113 tree := lattice.NewRecordPoolTree(0)
114 // One entry, so the key map is not empty: MESH carries no keys in the tree,
115 // only this map, so a tree without one has nothing to save.
116 var rec lattice.Record
117 if _, err := tree.Insert(lattice.Bgrammatical, lattice.MakeKey(lattice.LangEN, 1), rec); err != nil {
118 t.Fatal(err)
119 }
120 tree.RegisterKey(lattice.MakeKey(lattice.LangEN, 1), tree.AllocMeta(), tree.AllocLex())
121
122 var buf bytes.Buffer
123 if err := Save(&buf, tree, lattice.LangEN, lattice.LangBG); err != nil {
124 t.Fatalf("Save empty: %v", err)
125 }
126
127 loaded, _, _, err := Load(&buf)
128 if err != nil {
129 t.Fatalf("Load empty: %v", err)
130 }
131 if loaded.EntryCount() != 1 {
132 t.Errorf("EntryCount = %d, want 1", loaded.EntryCount())
133 }
134 }
135