github.com/go-xe2/third@v1.0.3/golang.org/x/text/internal/colltab/trie_test.go (about) 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 colltab 6 7 import ( 8 "testing" 9 ) 10 11 // We take the smallest, largest and an arbitrary value for each 12 // of the UTF-8 sequence lengths. 13 var testRunes = []rune{ 14 0x01, 0x0C, 0x7F, // 1-byte sequences 15 0x80, 0x100, 0x7FF, // 2-byte sequences 16 0x800, 0x999, 0xFFFF, // 3-byte sequences 17 0x10000, 0x10101, 0x10FFFF, // 4-byte sequences 18 0x200, 0x201, 0x202, 0x210, 0x215, // five entries in one sparse block 19 } 20 21 // Test cases for illegal runes. 22 type trietest struct { 23 size int 24 bytes []byte 25 } 26 27 var tests = []trietest{ 28 // illegal runes 29 {1, []byte{0x80}}, 30 {1, []byte{0xFF}}, 31 {1, []byte{t2, tx - 1}}, 32 {1, []byte{t2, t2}}, 33 {2, []byte{t3, tx, tx - 1}}, 34 {2, []byte{t3, tx, t2}}, 35 {1, []byte{t3, tx - 1, tx}}, 36 {3, []byte{t4, tx, tx, tx - 1}}, 37 {3, []byte{t4, tx, tx, t2}}, 38 {1, []byte{t4, t2, tx, tx - 1}}, 39 {2, []byte{t4, tx, t2, tx - 1}}, 40 41 // short runes 42 {0, []byte{t2}}, 43 {0, []byte{t3, tx}}, 44 {0, []byte{t4, tx, tx}}, 45 46 // we only support UTF-8 up to utf8.UTFMax bytes (4 bytes) 47 {1, []byte{t5, tx, tx, tx, tx}}, 48 {1, []byte{t6, tx, tx, tx, tx, tx}}, 49 } 50 51 func TestLookupTrie(t *testing.T) { 52 for i, r := range testRunes { 53 b := []byte(string(r)) 54 v, sz := testTrie.lookup(b) 55 if int(v) != i { 56 t.Errorf("lookup(%U): found value %#x, expected %#x", r, v, i) 57 } 58 if sz != len(b) { 59 t.Errorf("lookup(%U): found size %d, expected %d", r, sz, len(b)) 60 } 61 } 62 for i, tt := range tests { 63 v, sz := testTrie.lookup(tt.bytes) 64 if int(v) != 0 { 65 t.Errorf("lookup of illegal rune, case %d: found value %#x, expected 0", i, v) 66 } 67 if sz != tt.size { 68 t.Errorf("lookup of illegal rune, case %d: found size %d, expected %d", i, sz, tt.size) 69 } 70 } 71 } 72 73 // test data is taken from exp/collate/locale/build/trie_test.go 74 var testValues = [832]uint32{ 75 0x000c: 0x00000001, 76 0x007f: 0x00000002, 77 0x00c0: 0x00000003, 78 0x0100: 0x00000004, 79 0x0140: 0x0000000c, 0x0141: 0x0000000d, 0x0142: 0x0000000e, 80 0x0150: 0x0000000f, 81 0x0155: 0x00000010, 82 0x01bf: 0x00000005, 83 0x01c0: 0x00000006, 84 0x0219: 0x00000007, 85 0x027f: 0x00000008, 86 0x0280: 0x00000009, 87 0x02c1: 0x0000000a, 88 0x033f: 0x0000000b, 89 } 90 91 var testLookup = [640]uint16{ 92 0x0e0: 0x05, 0x0e6: 0x06, 93 0x13f: 0x07, 94 0x140: 0x08, 0x144: 0x09, 95 0x190: 0x03, 96 0x1ff: 0x0a, 97 0x20f: 0x05, 98 0x242: 0x01, 0x244: 0x02, 99 0x248: 0x03, 100 0x25f: 0x04, 101 0x260: 0x01, 102 0x26f: 0x02, 103 0x270: 0x04, 0x274: 0x06, 104 } 105 106 var testTrie = Trie{testLookup[6*blockSize:], testValues[:], testLookup[:], testValues[:]}