trie_test.go 2.5 KB

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