| 1 |
// Copyright 2012 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 build |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"fmt" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
// We take the smallest, largest and an arbitrary value for each |
| 14 |
// of the UTF-8 sequence lengths. |
| 15 |
var testRunes = []rune{ |
| 16 |
0x01, 0x0C, 0x7F, // 1-byte sequences |
| 17 |
0x80, 0x100, 0x7FF, // 2-byte sequences |
| 18 |
0x800, 0x999, 0xFFFF, // 3-byte sequences |
| 19 |
0x10000, 0x10101, 0x10FFFF, // 4-byte sequences |
| 20 |
0x200, 0x201, 0x202, 0x210, 0x215, // five entries in one sparse block |
| 21 |
} |
| 22 |
|
| 23 |
func makeTestTrie(t *testing.T) trie { |
| 24 |
n := newNode() |
| 25 |
for i, r := range testRunes { |
| 26 |
n.insert(r, uint32(i)) |
| 27 |
} |
| 28 |
idx := newTrieBuilder() |
| 29 |
idx.addTrie(n) |
| 30 |
tr, err := idx.generate() |
| 31 |
if err != nil { |
| 32 |
t.Error(err) |
| 33 |
} |
| 34 |
return *tr |
| 35 |
} |
| 36 |
|
| 37 |
func TestGenerateTrie(t *testing.T) { |
| 38 |
testdata := makeTestTrie(t) |
| 39 |
buf := &bytes.Buffer{} |
| 40 |
testdata.printArrays(buf, "test") |
| 41 |
fmt.Fprintf(buf, "var testTrie = ") |
| 42 |
testdata.printStruct(buf, &trieHandle{19, 0}, "test") |
| 43 |
if output != buf.String() { |
| 44 |
t.Error("output differs") |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
var output = `// testValues: 832 entries, 3328 bytes |
| 49 |
// Block 2 is the null block. |
| 50 |
var testValues = [832]uint32 { |
| 51 |
// Block 0x0, offset 0x0 |
| 52 |
0x000c:0x00000001, |
| 53 |
// Block 0x1, offset 0x40 |
| 54 |
0x007f:0x00000002, |
| 55 |
// Block 0x2, offset 0x80 |
| 56 |
// Block 0x3, offset 0xc0 |
| 57 |
0x00c0:0x00000003, |
| 58 |
// Block 0x4, offset 0x100 |
| 59 |
0x0100:0x00000004, |
| 60 |
// Block 0x5, offset 0x140 |
| 61 |
0x0140:0x0000000c, 0x0141:0x0000000d, 0x0142:0x0000000e, |
| 62 |
0x0150:0x0000000f, |
| 63 |
0x0155:0x00000010, |
| 64 |
// Block 0x6, offset 0x180 |
| 65 |
0x01bf:0x00000005, |
| 66 |
// Block 0x7, offset 0x1c0 |
| 67 |
0x01c0:0x00000006, |
| 68 |
// Block 0x8, offset 0x200 |
| 69 |
0x0219:0x00000007, |
| 70 |
// Block 0x9, offset 0x240 |
| 71 |
0x027f:0x00000008, |
| 72 |
// Block 0xa, offset 0x280 |
| 73 |
0x0280:0x00000009, |
| 74 |
// Block 0xb, offset 0x2c0 |
| 75 |
0x02c1:0x0000000a, |
| 76 |
// Block 0xc, offset 0x300 |
| 77 |
0x033f:0x0000000b, |
| 78 |
} |
| 79 |
|
| 80 |
// testLookup: 640 entries, 1280 bytes |
| 81 |
// Block 0 is the null block. |
| 82 |
var testLookup = [640]uint16 { |
| 83 |
// Block 0x0, offset 0x0 |
| 84 |
// Block 0x1, offset 0x40 |
| 85 |
// Block 0x2, offset 0x80 |
| 86 |
// Block 0x3, offset 0xc0 |
| 87 |
0x0e0:0x05, 0x0e6:0x06, |
| 88 |
// Block 0x4, offset 0x100 |
| 89 |
0x13f:0x07, |
| 90 |
// Block 0x5, offset 0x140 |
| 91 |
0x140:0x08, 0x144:0x09, |
| 92 |
// Block 0x6, offset 0x180 |
| 93 |
0x190:0x03, |
| 94 |
// Block 0x7, offset 0x1c0 |
| 95 |
0x1ff:0x0a, |
| 96 |
// Block 0x8, offset 0x200 |
| 97 |
0x20f:0x05, |
| 98 |
// Block 0x9, offset 0x240 |
| 99 |
0x242:0x01, 0x244:0x02, |
| 100 |
0x248:0x03, |
| 101 |
0x25f:0x04, |
| 102 |
0x260:0x01, |
| 103 |
0x26f:0x02, |
| 104 |
0x270:0x04, 0x274:0x06, |
| 105 |
} |
| 106 |
|
| 107 |
var testTrie = trie{ testLookup[1216:], testValues[0:], testLookup[:], testValues[:]}` |