package crypto import ( "testing" "git.mleku.dev/mleku/dendrite/pkg/axiom" "git.mleku.dev/mleku/dendrite/pkg/lattice" "git.mleku.dev/mleku/dendrite/pkg/ratio" "git.mleku.dev/mleku/dendrite/pkg/spore" ) type tagConstraint struct{ tag string } func (c tagConstraint) Tag() string { return c.tag } func (c tagConstraint) Admits(e axiom.Element) bool { return e.Type() == c.tag } func buildTestLattice(wordNodes, punctNodes int) *lattice.Lattice { l := lattice.New() var words, puncts []*lattice.Node for range wordNodes { n := l.AddNode([]axiom.Constraint{tagConstraint{"word"}}) words = append(words, n) } for range punctNodes { n := l.AddNode([]axiom.Constraint{tagConstraint{"punct"}}) puncts = append(puncts, n) } // Ring within each type. for i := range words { l.Connect(words[i], words[(i+1)%len(words)]) } for i := range puncts { l.Connect(puncts[i], puncts[(i+1)%len(puncts)]) } // Cross-connect. if len(words) > 0 && len(puncts) > 0 { l.Connect(words[0], puncts[0]) } return l } func TestFromLattice(t *testing.T) { l := buildTestLattice(10, 5) b := FromLattice(l, ratio.FromInt(127)) if b.Dimension != 15 { t.Errorf("Dimension = %d, want 15", b.Dimension) } if len(b.Tags) != 2 { t.Errorf("Tags count = %d, want 2", len(b.Tags)) } if b.TagCount("word") != 10 { t.Errorf("word count = %d, want 10", b.TagCount("word")) } if b.TagCount("punct") != 5 { t.Errorf("punct count = %d, want 5", b.TagCount("punct")) } } func TestFromSporeRoundTrip(t *testing.T) { l := buildTestLattice(8, 4) s := spore.Extract(l) b1 := FromLattice(l, ratio.FromInt(127)) b2 := FromSpore(s, ratio.FromInt(127)) if !b1.Equal(b2) { t.Error("FromLattice and FromSpore should produce equal bases") } } func TestBasisEqual(t *testing.T) { l := buildTestLattice(6, 3) b1 := FromLattice(l, ratio.FromInt(127)) b2 := FromLattice(l, ratio.FromInt(127)) if !b1.Equal(b2) { t.Error("identical lattice should produce equal bases") } // Different modulus. b3 := FromLattice(l, ratio.FromInt(251)) if b1.Equal(b3) { t.Error("different modulus should produce unequal bases") } } func TestBasisTagsSorted(t *testing.T) { l := buildTestLattice(5, 5) b := FromLattice(l, ratio.FromInt(127)) for i := 1; i < len(b.Tags); i++ { if b.Tags[i] < b.Tags[i-1] { t.Errorf("tags not sorted: %q comes after %q", b.Tags[i], b.Tags[i-1]) } } }