z_0_filetest.gno

2.15 Kb ยท 91 lines
 1// PKGPATH: gno.land/r/test
 2package test
 3
 4import (
 5	"encoding/hex"
 6
 7	"gno.land/p/demo/avl"
 8	"gno.land/p/demo/avlhelpers"
 9	"gno.land/p/demo/ufmt"
10)
11
12func main() {
13	tree := avl.NewTree()
14
15	{
16		// Empty tree.
17		matches := avlhelpers.ListByteStringKeysByPrefix(tree, "", 10)
18		println(ufmt.Sprintf("# matches: %d", len(matches)))
19	}
20
21	tree.Set("alice", "")
22	tree.Set("andy", "")
23	tree.Set("bob", "")
24
25	{
26		// Match only alice.
27		matches := avlhelpers.ListByteStringKeysByPrefix(tree, "al", 10)
28		println(ufmt.Sprintf("# matches: %d", len(matches)))
29		println("match: " + matches[0])
30	}
31
32	{
33		// Match alice and andy.
34		matches := avlhelpers.ListByteStringKeysByPrefix(tree, "a", 10)
35		println(ufmt.Sprintf("# matches: %d", len(matches)))
36		println("match: " + matches[0])
37		println("match: " + matches[1])
38	}
39
40	{
41		// Match alice and andy limited to 1.
42		matches := avlhelpers.ListByteStringKeysByPrefix(tree, "a", 1)
43		println(ufmt.Sprintf("# matches: %d", len(matches)))
44		println("match: " + matches[0])
45	}
46
47	tree = avl.NewTree()
48	tree.Set("a\xff", "")
49	tree.Set("a\xff\xff", "")
50	tree.Set("b", "")
51	tree.Set("\xff\xff\x00", "")
52
53	{
54		// Match only "a\xff\xff".
55		matches := avlhelpers.ListByteStringKeysByPrefix(tree, "a\xff\xff", 10)
56		println(ufmt.Sprintf("# matches: %d", len(matches)))
57		println(ufmt.Sprintf("match: %s", hex.EncodeToString([]byte(matches[0]))))
58	}
59
60	{
61		// Match "a\xff" and "a\xff\xff".
62		matches := avlhelpers.ListByteStringKeysByPrefix(tree, "a\xff", 10)
63		println(ufmt.Sprintf("# matches: %d", len(matches)))
64		println(ufmt.Sprintf("match: %s", hex.EncodeToString([]byte(matches[0]))))
65		println(ufmt.Sprintf("match: %s", hex.EncodeToString([]byte(matches[1]))))
66	}
67
68	{
69		// Edge case: Match only "\xff\xff\x00".
70		matches := avlhelpers.ListByteStringKeysByPrefix(tree, "\xff\xff", 10)
71		println(ufmt.Sprintf("# matches: %d", len(matches)))
72		println(ufmt.Sprintf("match: %s", hex.EncodeToString([]byte(matches[0]))))
73	}
74}
75
76// Output:
77// # matches: 0
78// # matches: 1
79// match: alice
80// # matches: 2
81// match: alice
82// match: andy
83// # matches: 1
84// match: alice
85// # matches: 1
86// match: 61ffff
87// # matches: 2
88// match: 61ff
89// match: 61ffff
90// # matches: 1
91// match: ffff00