You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

132 lines
2.2 KiB

  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. type lookupStrings struct {
  9. str string
  10. offset int
  11. n int // bytes consumed from input
  12. }
  13. type LookupTest struct {
  14. lookup []lookupStrings
  15. n int
  16. tries ContractTrieSet
  17. }
  18. var lookupTests = []LookupTest{{
  19. []lookupStrings{
  20. {"abc", 1, 3},
  21. {"a", 0, 0},
  22. {"b", 0, 0},
  23. {"c", 0, 0},
  24. {"d", 0, 0},
  25. },
  26. 1,
  27. ContractTrieSet{
  28. {'a', 0, 1, 0xFF},
  29. {'b', 0, 1, 0xFF},
  30. {'c', 'c', 0, 1},
  31. },
  32. }, {
  33. []lookupStrings{
  34. {"abc", 1, 3},
  35. {"abd", 2, 3},
  36. {"abe", 3, 3},
  37. {"a", 0, 0},
  38. {"ab", 0, 0},
  39. {"d", 0, 0},
  40. {"f", 0, 0},
  41. },
  42. 1,
  43. ContractTrieSet{
  44. {'a', 0, 1, 0xFF},
  45. {'b', 0, 1, 0xFF},
  46. {'c', 'e', 0, 1},
  47. },
  48. }, {
  49. []lookupStrings{
  50. {"abc", 1, 3},
  51. {"ab", 2, 2},
  52. {"a", 3, 1},
  53. {"abcd", 1, 3},
  54. {"abe", 2, 2},
  55. },
  56. 1,
  57. ContractTrieSet{
  58. {'a', 0, 1, 3},
  59. {'b', 0, 1, 2},
  60. {'c', 'c', 0, 1},
  61. },
  62. }, {
  63. []lookupStrings{
  64. {"abc", 1, 3},
  65. {"abd", 2, 3},
  66. {"ab", 3, 2},
  67. {"ac", 4, 2},
  68. {"a", 5, 1},
  69. {"b", 6, 1},
  70. {"ba", 6, 1},
  71. },
  72. 2,
  73. ContractTrieSet{
  74. {'b', 'b', 0, 6},
  75. {'a', 0, 2, 5},
  76. {'c', 'c', 0, 4},
  77. {'b', 0, 1, 3},
  78. {'c', 'd', 0, 1},
  79. },
  80. }, {
  81. []lookupStrings{
  82. {"bcde", 2, 4},
  83. {"bc", 7, 2},
  84. {"ab", 6, 2},
  85. {"bcd", 5, 3},
  86. {"abcd", 1, 4},
  87. {"abc", 4, 3},
  88. {"bcdf", 3, 4},
  89. },
  90. 2,
  91. ContractTrieSet{
  92. {'b', 3, 1, 0xFF},
  93. {'a', 0, 1, 0xFF},
  94. {'b', 0, 1, 6},
  95. {'c', 0, 1, 4},
  96. {'d', 'd', 0, 1},
  97. {'c', 0, 1, 7},
  98. {'d', 0, 1, 5},
  99. {'e', 'f', 0, 2},
  100. },
  101. }}
  102. func lookup(c *ContractTrieSet, nnode int, s []uint8) (i, n int) {
  103. scan := c.scanner(0, nnode, s)
  104. scan.scan(0)
  105. return scan.result()
  106. }
  107. func TestLookupContraction(t *testing.T) {
  108. for i, tt := range lookupTests {
  109. cts := ContractTrieSet(tt.tries)
  110. for j, lu := range tt.lookup {
  111. str := lu.str
  112. for _, s := range []string{str, str + "X"} {
  113. const msg = `%d:%d: %s of "%s" %v; want %v`
  114. offset, n := lookup(&cts, tt.n, []byte(s))
  115. if offset != lu.offset {
  116. t.Errorf(msg, i, j, "offset", s, offset, lu.offset)
  117. }
  118. if n != lu.n {
  119. t.Errorf(msg, i, j, "bytes consumed", s, n, len(str))
  120. }
  121. }
  122. }
  123. }
  124. }