...
1
2
3
4
5 package triegen_test
6
7 import (
8 "fmt"
9 "io"
10 "math/rand"
11 "unicode"
12
13 "golang.org/x/text/internal/triegen"
14 )
15
16 const seed = 0x12345
17
18 var genWriter = io.Discard
19
20 func randomRunes() map[rune]uint8 {
21 rnd := rand.New(rand.NewSource(seed))
22 m := map[rune]uint8{}
23 for len(m) < 100 {
24
25 if r := rune(rnd.Int31n(unicode.MaxRune + 1)); []rune(string(r))[0] == r {
26 m[r] = 1
27 }
28 }
29 return m
30 }
31
32
33
34 func Example_build() {
35 t := triegen.NewTrie("rand")
36
37 for r, _ := range randomRunes() {
38 t.Insert(r, 1)
39 }
40 sz, err := t.Gen(genWriter)
41
42 fmt.Printf("Trie size: %d bytes\n", sz)
43 fmt.Printf("Error: %v\n", err)
44
45
46
47
48 }
49
50
51 func Example_lookup() {
52 trie := newRandTrie(0)
53
54
55 runes := randomRunes()
56
57
58 for r := rune(0); r <= unicode.MaxRune; r++ {
59
60 if v, _ := trie.lookupString(string(r)); v != runes[r] {
61 fmt.Println("FAILURE")
62 return
63 }
64 }
65 fmt.Println("SUCCESS")
66
67
68
69 }
70
71
72 func runeValues() map[rune]uint64 {
73 rnd := rand.New(rand.NewSource(seed))
74 m := map[rune]uint64{}
75 for p := 4; p <= unicode.MaxRune; p <<= 1 {
76 for d := -1; d <= 1; d++ {
77 m[rune(p+d)] = uint64(rnd.Int63())
78 }
79 }
80 return m
81 }
82
83
84
85 func ExampleGen_build() {
86 var tries []*triegen.Trie
87
88 rv := runeValues()
89 for _, c := range []struct {
90 include func(rune) bool
91 name string
92 }{
93 {func(r rune) bool { return true }, "all"},
94 {func(r rune) bool { return r < 0x80 }, "ASCII only"},
95 {func(r rune) bool { return r < 0x80 }, "ASCII only 2"},
96 {func(r rune) bool { return r <= 0xFFFF }, "BMP only"},
97 {func(r rune) bool { return r > 0xFFFF }, "No BMP"},
98 } {
99 t := triegen.NewTrie(c.name)
100 tries = append(tries, t)
101
102 for r, v := range rv {
103 if c.include(r) {
104 t.Insert(r, v)
105 }
106 }
107 }
108 sz, err := triegen.Gen(genWriter, "multi", tries)
109
110 fmt.Printf("Trie size: %d bytes\n", sz)
111 fmt.Printf("Error: %v\n", err)
112
113
114
115
116 }
117
118
119
120 func ExampleGen_lookup() {
121 rv := runeValues()
122 for i, include := range []func(rune) bool{
123 func(r rune) bool { return true },
124 func(r rune) bool { return r < 0x80 },
125 func(r rune) bool { return r < 0x80 },
126 func(r rune) bool { return r <= 0xFFFF },
127 func(r rune) bool { return r > 0xFFFF },
128 } {
129 t := newMultiTrie(i)
130
131 for r := rune(0); r <= unicode.MaxRune; r++ {
132 x := uint64(0)
133 if include(r) {
134 x = rv[r]
135 }
136
137
138 if v := t.lookupStringUnsafe(string(r)); x != v {
139 fmt.Println("FAILURE")
140 return
141 }
142 }
143 }
144 fmt.Println("SUCCESS")
145
146
147
148 }
149
View as plain text