Skip to content

Commit

Permalink
deplace TestGenericTrieBuilding to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
Villaquiranm committed Oct 2, 2024
1 parent 5b456ec commit b31ccc2
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 105 deletions.
102 changes: 102 additions & 0 deletions gnovm/stdlibs/strings/printtrie_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package strings

import (
"testing"
)

func (r *Replacer) PrintTrie() string {
r.buildOnce()
gen := r.r.(*genericReplacer)
return gen.printNode(&gen.root, 0)
}

func (r *genericReplacer) printNode(t *trieNode, depth int) (s string) {
if t.priority > 0 {
s += "+"
} else {
s += "-"
}
s += "\n"

if t.prefix != "" {
s += Repeat(".", depth) + t.prefix
s += r.printNode(t.next, depth+len(t.prefix))
} else if t.table != nil {
for b, m := range r.mapping {
if int(m) != r.tableSize && t.table[m] != nil {
s += Repeat(".", depth) + string([]byte{byte(b)})
s += r.printNode(t.table[m], depth+1)
}
}
}
return
}

func TestGenericTrieBuilding(t *testing.T) {
testCases := []struct{ in, out string }{
{"abc;abdef;abdefgh;xx;xy;z", `-
a-
.b-
..c+
..d-
...ef+
.....gh+
x-
.x+
.y+
z+
`},
{"abracadabra;abracadabrakazam;abraham;abrasion", `-
a-
.bra-
....c-
.....adabra+
...........kazam+
....h-
.....am+
....s-
.....ion+
`},
{"aaa;aa;a;i;longerst;longer;long;xx;x;X;Y", `-
X+
Y+
a+
.a+
..a+
i+
l-
.ong+
....er+
......st+
x+
.x+
`},
{"foo;;foo;foo1", `+
f-
.oo+
...1+
`},
}

for _, tc := range testCases {
keys := Split(tc.in, ";")
args := make([]string, len(keys)*2)
for i, key := range keys {
args[i*2] = key
}

got := NewReplacer(args...).PrintTrie()
// Remove tabs from tc.out
wantbuf := make([]byte, 0, len(tc.out))
for i := 0; i < len(tc.out); i++ {
if tc.out[i] != '\t' {
wantbuf = append(wantbuf, tc.out[i])
}
}
want := string(wantbuf)

if got != want {
t.Errorf("PrintTrie(%q)\ngot\n%swant\n%s", tc.in, got, want)
}
}
}
33 changes: 0 additions & 33 deletions gnovm/stdlibs/strings/replace.gno
Original file line number Diff line number Diff line change
Expand Up @@ -585,36 +585,3 @@ func (r *byteStringReplacer) WriteString(w io.Writer, s string) (n int, err erro
}
return
}

// Custom code: these test functions are normally placed on export_test.go file but it seems like we do not recognize
// methods declared on test files as part of the structure. For example:
// Replacer.printTrie() was not recognized so I added here directly
func (r *Replacer) PrintTrie() string {
r.buildOnce()
gen := r.r.(*genericReplacer)
return gen.printNode(&gen.root, 0)
}

func (r *genericReplacer) printNode(t *trieNode, depth int) (s string) {
if t.priority > 0 {
s += "+"
} else {
s += "-"
}
s += "\n"

if t.prefix != "" {
s += Repeat(".", depth) + t.prefix
s += r.printNode(t.next, depth+len(t.prefix))
} else if t.table != nil {
for b, m := range r.mapping {
if int(m) != r.tableSize && t.table[m] != nil {
s += Repeat(".", depth) + string([]byte{byte(b)})
s += r.printNode(t.table[m], depth+1)
}
}
}
return
}

// End of custom code
72 changes: 0 additions & 72 deletions gnovm/stdlibs/strings/replace_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -347,78 +347,6 @@ func TestWriteStringError(t *testing.T) {
}
}

// TestGenericTrieBuilding verifies the structure of the generated trie. There
// is one node per line, and the key ending with the current line is in the
// trie if it ends with a "+".
func TestGenericTrieBuilding(t *testing.T) {
testCases := []struct{ in, out string }{
{"abc;abdef;abdefgh;xx;xy;z", `-
a-
.b-
..c+
..d-
...ef+
.....gh+
x-
.x+
.y+
z+
`},
{"abracadabra;abracadabrakazam;abraham;abrasion", `-
a-
.bra-
....c-
.....adabra+
...........kazam+
....h-
.....am+
....s-
.....ion+
`},
{"aaa;aa;a;i;longerst;longer;long;xx;x;X;Y", `-
X+
Y+
a+
.a+
..a+
i+
l-
.ong+
....er+
......st+
x+
.x+
`},
{"foo;;foo;foo1", `+
f-
.oo+
...1+
`},
}

for _, tc := range testCases {
keys := strings.Split(tc.in, ";")
args := make([]string, len(keys)*2)
for i, key := range keys {
args[i*2] = key
}

got := strings.NewReplacer(args...).PrintTrie()
// Remove tabs from tc.out
wantbuf := make([]byte, 0, len(tc.out))
for i := 0; i < len(tc.out); i++ {
if tc.out[i] != '\t' {
wantbuf = append(wantbuf, tc.out[i])
}
}
want := string(wantbuf)

if got != want {
t.Errorf("PrintTrie(%q)\ngot\n%swant\n%s", tc.in, got, want)
}
}
}

func BenchmarkGenericNoMatch(b *testing.B) {
str := strings.Repeat("A", 100) + strings.Repeat("B", 100)
generic := strings.NewReplacer("a", "A", "b", "B", "12", "123") // varying lengths forces generic
Expand Down

0 comments on commit b31ccc2

Please sign in to comment.