contract_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. "sort"
  8. "testing"
  9. "golang.org/x/text/internal/colltab"
  10. )
  11. var largetosmall = []stridx{
  12. {"a", 5},
  13. {"ab", 4},
  14. {"abc", 3},
  15. {"abcd", 2},
  16. {"abcde", 1},
  17. {"abcdef", 0},
  18. }
  19. var offsetSortTests = [][]stridx{
  20. {
  21. {"bcde", 1},
  22. {"bc", 5},
  23. {"ab", 4},
  24. {"bcd", 3},
  25. {"abcd", 0},
  26. {"abc", 2},
  27. },
  28. largetosmall,
  29. }
  30. func TestOffsetSort(t *testing.T) {
  31. for i, st := range offsetSortTests {
  32. sort.Sort(offsetSort(st))
  33. for j, si := range st {
  34. if j != si.index {
  35. t.Errorf("%d: failed: %v", i, st)
  36. }
  37. }
  38. }
  39. for i, tt := range genStateTests {
  40. // ensure input is well-formed
  41. sort.Sort(offsetSort(tt.in))
  42. for j, si := range tt.in {
  43. if si.index != j+1 {
  44. t.Errorf("%dth sort failed: %v", i, tt.in)
  45. }
  46. }
  47. }
  48. }
  49. var genidxtest1 = []stridx{
  50. {"bcde", 3},
  51. {"bc", 6},
  52. {"ab", 2},
  53. {"bcd", 5},
  54. {"abcd", 0},
  55. {"abc", 1},
  56. {"bcdf", 4},
  57. }
  58. var genidxSortTests = [][]stridx{
  59. genidxtest1,
  60. largetosmall,
  61. }
  62. func TestGenIdxSort(t *testing.T) {
  63. for i, st := range genidxSortTests {
  64. sort.Sort(genidxSort(st))
  65. for j, si := range st {
  66. if j != si.index {
  67. t.Errorf("%dth sort failed %v", i, st)
  68. break
  69. }
  70. }
  71. }
  72. }
  73. var entrySortTests = []colltab.ContractTrieSet{
  74. {
  75. {10, 0, 1, 3},
  76. {99, 0, 1, 0},
  77. {20, 50, 0, 2},
  78. {30, 0, 1, 1},
  79. },
  80. }
  81. func TestEntrySort(t *testing.T) {
  82. for i, et := range entrySortTests {
  83. sort.Sort(entrySort(et))
  84. for j, fe := range et {
  85. if j != int(fe.I) {
  86. t.Errorf("%dth sort failed %v", i, et)
  87. break
  88. }
  89. }
  90. }
  91. }
  92. type GenStateTest struct {
  93. in []stridx
  94. firstBlockLen int
  95. out colltab.ContractTrieSet
  96. }
  97. var genStateTests = []GenStateTest{
  98. {[]stridx{
  99. {"abc", 1},
  100. },
  101. 1,
  102. colltab.ContractTrieSet{
  103. {'a', 0, 1, noIndex},
  104. {'b', 0, 1, noIndex},
  105. {'c', 'c', final, 1},
  106. },
  107. },
  108. {[]stridx{
  109. {"abc", 1},
  110. {"abd", 2},
  111. {"abe", 3},
  112. },
  113. 1,
  114. colltab.ContractTrieSet{
  115. {'a', 0, 1, noIndex},
  116. {'b', 0, 1, noIndex},
  117. {'c', 'e', final, 1},
  118. },
  119. },
  120. {[]stridx{
  121. {"abc", 1},
  122. {"ab", 2},
  123. {"a", 3},
  124. },
  125. 1,
  126. colltab.ContractTrieSet{
  127. {'a', 0, 1, 3},
  128. {'b', 0, 1, 2},
  129. {'c', 'c', final, 1},
  130. },
  131. },
  132. {[]stridx{
  133. {"abc", 1},
  134. {"abd", 2},
  135. {"ab", 3},
  136. {"ac", 4},
  137. {"a", 5},
  138. {"b", 6},
  139. },
  140. 2,
  141. colltab.ContractTrieSet{
  142. {'b', 'b', final, 6},
  143. {'a', 0, 2, 5},
  144. {'c', 'c', final, 4},
  145. {'b', 0, 1, 3},
  146. {'c', 'd', final, 1},
  147. },
  148. },
  149. {[]stridx{
  150. {"bcde", 2},
  151. {"bc", 7},
  152. {"ab", 6},
  153. {"bcd", 5},
  154. {"abcd", 1},
  155. {"abc", 4},
  156. {"bcdf", 3},
  157. },
  158. 2,
  159. colltab.ContractTrieSet{
  160. {'b', 3, 1, noIndex},
  161. {'a', 0, 1, noIndex},
  162. {'b', 0, 1, 6},
  163. {'c', 0, 1, 4},
  164. {'d', 'd', final, 1},
  165. {'c', 0, 1, 7},
  166. {'d', 0, 1, 5},
  167. {'e', 'f', final, 2},
  168. },
  169. },
  170. }
  171. func TestGenStates(t *testing.T) {
  172. for i, tt := range genStateTests {
  173. si := []stridx{}
  174. for _, e := range tt.in {
  175. si = append(si, e)
  176. }
  177. // ensure input is well-formed
  178. sort.Sort(genidxSort(si))
  179. ct := colltab.ContractTrieSet{}
  180. n, _ := genStates(&ct, si)
  181. if nn := tt.firstBlockLen; nn != n {
  182. t.Errorf("%d: block len %v; want %v", i, n, nn)
  183. }
  184. if lv, lw := len(ct), len(tt.out); lv != lw {
  185. t.Errorf("%d: len %v; want %v", i, lv, lw)
  186. continue
  187. }
  188. for j, fe := range tt.out {
  189. const msg = "%d:%d: value %s=%v; want %v"
  190. if fe.L != ct[j].L {
  191. t.Errorf(msg, i, j, "l", ct[j].L, fe.L)
  192. }
  193. if fe.H != ct[j].H {
  194. t.Errorf(msg, i, j, "h", ct[j].H, fe.H)
  195. }
  196. if fe.N != ct[j].N {
  197. t.Errorf(msg, i, j, "n", ct[j].N, fe.N)
  198. }
  199. if fe.I != ct[j].I {
  200. t.Errorf(msg, i, j, "i", ct[j].I, fe.I)
  201. }
  202. }
  203. }
  204. }
  205. func TestLookupContraction(t *testing.T) {
  206. for i, tt := range genStateTests {
  207. input := []string{}
  208. for _, e := range tt.in {
  209. input = append(input, e.str)
  210. }
  211. cts := colltab.ContractTrieSet{}
  212. h, _ := appendTrie(&cts, input)
  213. for j, si := range tt.in {
  214. str := si.str
  215. for _, s := range []string{str, str + "X"} {
  216. msg := "%d:%d: %s(%s) %v; want %v"
  217. idx, sn := lookup(&cts, h, []byte(s))
  218. if idx != si.index {
  219. t.Errorf(msg, i, j, "index", s, idx, si.index)
  220. }
  221. if sn != len(str) {
  222. t.Errorf(msg, i, j, "sn", s, sn, len(str))
  223. }
  224. }
  225. }
  226. }
  227. }
  228. func TestPrintContractionTrieSet(t *testing.T) {
  229. testdata := colltab.ContractTrieSet(genStateTests[4].out)
  230. buf := &bytes.Buffer{}
  231. print(&testdata, buf, "test")
  232. if contractTrieOutput != buf.String() {
  233. t.Errorf("output differs; found\n%s", buf.String())
  234. println(string(buf.Bytes()))
  235. }
  236. }
  237. const contractTrieOutput = `// testCTEntries: 8 entries, 32 bytes
  238. var testCTEntries = [8]struct{L,H,N,I uint8}{
  239. {0x62, 0x3, 1, 255},
  240. {0x61, 0x0, 1, 255},
  241. {0x62, 0x0, 1, 6},
  242. {0x63, 0x0, 1, 4},
  243. {0x64, 0x64, 0, 1},
  244. {0x63, 0x0, 1, 7},
  245. {0x64, 0x0, 1, 5},
  246. {0x65, 0x66, 0, 2},
  247. }
  248. var testContractTrieSet = colltab.ContractTrieSet( testCTEntries[:] )
  249. `