package meshio import ( "bytes" "testing" "git.smesh.lol/go-iskra/lattice" ) // meshTree builds a tree in the shape MESH can carry: one key space in block // 0, record indices from 1, meta and lex pools starting at 1. func TestRoundTrip(t *testing.T) { tree := lattice.NewRecordPoolTree(0) // Insert some entries. words := []struct { word string lang uint8 }{ {"water", lattice.LangEN}, {"вода", lattice.LangBG}, {"book", lattice.LangEN}, {"книга", lattice.LangBG}, } for _, w := range words { norm := lattice.Normalize(w.word) key := lattice.MakeKey(w.lang, lattice.HashWord(norm)) metaIdx := tree.AllocMeta() lexIdx := tree.AllocLex() meta := tree.MetaAt(metaIdx) meta.LangTag = w.lang lex := tree.LexAt(lexIdx) lex.SetForm(w.word, tree.PoolPtr()) if _, err := tree.Insert(lattice.Bgrammatical, key, lattice.Record{DataLen: 0}); err != nil { t.Fatal(err) } tree.RegisterKey(key, metaIdx, lexIdx) } // Add some adj links. tree.AddAdj(1, 2) // water → вода tree.AddAdj(2, 1) // вода → water tree.AddAdj(3, 4) // book → книга tree.AddAdj(4, 3) // книга → book tree.FinalizeAdj() // Save. var buf bytes.Buffer if err := Save(&buf, tree, lattice.LangBG, lattice.LangEN); err != nil { t.Fatalf("Save: %v", err) } // Load. loaded, srcLang, tgtLang, err := Load(&buf) if err != nil { t.Fatalf("Load: %v", err) } // Verify header. if srcLang != lattice.LangBG { t.Errorf("srcLang = %d, want %d", srcLang, lattice.LangBG) } if tgtLang != lattice.LangEN { t.Errorf("tgtLang = %d, want %d", tgtLang, lattice.LangEN) } // Verify counts. if loaded.NodeCount() != tree.NodeCount() { t.Errorf("NodeCount = %d, want %d", loaded.NodeCount(), tree.NodeCount()) } // Every meta slot is the null sentinel, so the entry count is the pool // length minus one on both sides. if loaded.MetaLen != uint32(len(tree.Meta)) || loaded.EntryCount() != len(tree.Meta)-1 { t.Errorf("meta pool after round trip: len %d count %d, want len %d count %d", loaded.MetaLen, loaded.EntryCount(), len(tree.Meta), len(tree.Meta)-1) } if len(loaded.Adj) != len(tree.Adj) { t.Errorf("AdjCount = %d, want %d", len(loaded.Adj), len(tree.Adj)) } // Verify key lookups still work. for _, w := range words { norm := lattice.Normalize(w.word) key := lattice.MakeKey(w.lang, lattice.HashWord(norm)) metaIdx, lexIdx, ok := loaded.LookupMetaByKey(key) if !ok { t.Errorf("LookupMetaByKey(%q) not found after round-trip", w.word) continue } lex := loaded.LexAt(lexIdx) form := lex.GetForm(loaded.StringPool) if form != w.word { t.Errorf("GetForm after round-trip = %q, want %q", form, w.word) } meta := loaded.MetaAt(metaIdx) if meta.LangTag != w.lang { t.Errorf("LangTag after round-trip = %d, want %d", meta.LangTag, w.lang) } } // Verify Walk still works. for _, w := range words { norm := lattice.Normalize(w.word) key := lattice.MakeKey(w.lang, lattice.HashWord(norm)) if _, _, found, err := loaded.Walk(lattice.Bgrammatical, key); err != nil || !found { t.Errorf("Walk(%q) not found after round-trip (%v)", w.word, err) } } } func TestSingleEntry(t *testing.T) { tree := lattice.NewRecordPoolTree(0) // One entry, so the key map is not empty: MESH carries no keys in the tree, // only this map, so a tree without one has nothing to save. var rec lattice.Record if _, err := tree.Insert(lattice.Bgrammatical, lattice.MakeKey(lattice.LangEN, 1), rec); err != nil { t.Fatal(err) } tree.RegisterKey(lattice.MakeKey(lattice.LangEN, 1), tree.AllocMeta(), tree.AllocLex()) var buf bytes.Buffer if err := Save(&buf, tree, lattice.LangEN, lattice.LangBG); err != nil { t.Fatalf("Save empty: %v", err) } loaded, _, _, err := Load(&buf) if err != nil { t.Fatalf("Load empty: %v", err) } if loaded.EntryCount() != 1 { t.Errorf("EntryCount = %d, want 1", loaded.EntryCount()) } }