| 1 |
// Copyright 2014 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 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 |
// Only set our random rune if it is a valid Unicode code point. |
| 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 |
// Example_build shows how to build a simple trie. It assigns the value 1 to |
| 33 |
// 100 random runes generated by randomRunes. |
| 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 |
// Output: |
| 46 |
// Trie size: 9280 bytes |
| 47 |
// Error: <nil> |
| 48 |
} |
| 49 |
|
| 50 |
// Example_lookup demonstrates how to use the trie generated by Example_build. |
| 51 |
func Example_lookup() { |
| 52 |
trie := newRandTrie(0) |
| 53 |
|
| 54 |
// The same set of runes used by Example_build. |
| 55 |
runes := randomRunes() |
| 56 |
|
| 57 |
// Verify the right value is returned for all runes. |
| 58 |
for r := rune(0); r <= unicode.MaxRune; r++ { |
| 59 |
// Note that the return type of lookup is uint8. |
| 60 |
if v, _ := trie.lookupString(string(r)); v != runes[r] { |
| 61 |
fmt.Println("FAILURE") |
| 62 |
return |
| 63 |
} |
| 64 |
} |
| 65 |
fmt.Println("SUCCESS") |
| 66 |
|
| 67 |
// Output: |
| 68 |
// SUCCESS |
| 69 |
} |
| 70 |
|
| 71 |
// runeValues generates some random values for a set of interesting runes. |
| 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 |
// ExampleGen_build demonstrates the creation of multiple tries sharing common |
| 84 |
// blocks. ExampleGen_lookup demonstrates how to use the generated tries. |
| 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 |
// Output: |
| 114 |
// Trie size: 18250 bytes |
| 115 |
// Error: <nil> |
| 116 |
} |
| 117 |
|
| 118 |
// ExampleGen_lookup shows how to look up values in the trie generated by |
| 119 |
// ExampleGen_build. |
| 120 |
func ExampleGen_lookup() { |
| 121 |
rv := runeValues() |
| 122 |
for i, include := range []func(rune) bool{ |
| 123 |
func(r rune) bool { return true }, // all |
| 124 |
func(r rune) bool { return r < 0x80 }, // ASCII only |
| 125 |
func(r rune) bool { return r < 0x80 }, // ASCII only 2 |
| 126 |
func(r rune) bool { return r <= 0xFFFF }, // BMP only |
| 127 |
func(r rune) bool { return r > 0xFFFF }, // No BMP |
| 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 |
// As we convert from a valid rune, we know it is safe to use |
| 137 |
// lookupStringUnsafe. |
| 138 |
if v := t.lookupStringUnsafe(string(r)); x != v { |
| 139 |
fmt.Println("FAILURE") |
| 140 |
return |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
fmt.Println("SUCCESS") |
| 145 |
|
| 146 |
// Output: |
| 147 |
// SUCCESS |
| 148 |
} |