trie_test.go 2.5 KB

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