colelem_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. "testing"
  7. "golang.org/x/text/internal/colltab"
  8. )
  9. type ceTest struct {
  10. f func(in []int) (uint32, error)
  11. arg []int
  12. val uint32
  13. }
  14. func normalCE(in []int) (ce uint32, err error) {
  15. return makeCE(rawCE{w: in[:3], ccc: uint8(in[3])})
  16. }
  17. func expandCE(in []int) (ce uint32, err error) {
  18. return makeExpandIndex(in[0])
  19. }
  20. func contractCE(in []int) (ce uint32, err error) {
  21. return makeContractIndex(ctHandle{in[0], in[1]}, in[2])
  22. }
  23. func decompCE(in []int) (ce uint32, err error) {
  24. return makeDecompose(in[0], in[1])
  25. }
  26. var ceTests = []ceTest{
  27. {normalCE, []int{0, 0, 0, 0}, 0xA0000000},
  28. {normalCE, []int{0, 0x28, 3, 0}, 0xA0002803},
  29. {normalCE, []int{0, 0x28, 3, 0xFF}, 0xAFF02803},
  30. {normalCE, []int{100, defaultSecondary, 3, 0}, 0x0000C883},
  31. // non-ignorable primary with non-default secondary
  32. {normalCE, []int{100, 0x28, defaultTertiary, 0}, 0x4000C828},
  33. {normalCE, []int{100, defaultSecondary + 8, 3, 0}, 0x0000C983},
  34. {normalCE, []int{100, 0, 3, 0}, 0xFFFF}, // non-ignorable primary with non-supported secondary
  35. {normalCE, []int{100, 1, 3, 0}, 0xFFFF},
  36. {normalCE, []int{1 << maxPrimaryBits, defaultSecondary, 0, 0}, 0xFFFF},
  37. {normalCE, []int{0, 1 << maxSecondaryBits, 0, 0}, 0xFFFF},
  38. {normalCE, []int{100, defaultSecondary, 1 << maxTertiaryBits, 0}, 0xFFFF},
  39. {normalCE, []int{0x123, defaultSecondary, 8, 0xFF}, 0x88FF0123},
  40. {normalCE, []int{0x123, defaultSecondary + 1, 8, 0xFF}, 0xFFFF},
  41. {contractCE, []int{0, 0, 0}, 0xC0000000},
  42. {contractCE, []int{1, 1, 1}, 0xC0010011},
  43. {contractCE, []int{1, (1 << maxNBits) - 1, 1}, 0xC001001F},
  44. {contractCE, []int{(1 << maxTrieIndexBits) - 1, 1, 1}, 0xC001FFF1},
  45. {contractCE, []int{1, 1, (1 << maxContractOffsetBits) - 1}, 0xDFFF0011},
  46. {contractCE, []int{1, (1 << maxNBits), 1}, 0xFFFF},
  47. {contractCE, []int{(1 << maxTrieIndexBits), 1, 1}, 0xFFFF},
  48. {contractCE, []int{1, (1 << maxContractOffsetBits), 1}, 0xFFFF},
  49. {expandCE, []int{0}, 0xE0000000},
  50. {expandCE, []int{5}, 0xE0000005},
  51. {expandCE, []int{(1 << maxExpandIndexBits) - 1}, 0xE000FFFF},
  52. {expandCE, []int{1 << maxExpandIndexBits}, 0xFFFF},
  53. {decompCE, []int{0, 0}, 0xF0000000},
  54. {decompCE, []int{1, 1}, 0xF0000101},
  55. {decompCE, []int{0x1F, 0x1F}, 0xF0001F1F},
  56. {decompCE, []int{256, 0x1F}, 0xFFFF},
  57. {decompCE, []int{0x1F, 256}, 0xFFFF},
  58. }
  59. func TestColElem(t *testing.T) {
  60. for i, tt := range ceTests {
  61. in := make([]int, len(tt.arg))
  62. copy(in, tt.arg)
  63. ce, err := tt.f(in)
  64. if tt.val == 0xFFFF {
  65. if err == nil {
  66. t.Errorf("%d: expected error for args %x", i, tt.arg)
  67. }
  68. continue
  69. }
  70. if err != nil {
  71. t.Errorf("%d: unexpected error: %v", i, err.Error())
  72. }
  73. if ce != tt.val {
  74. t.Errorf("%d: colElem=%X; want %X", i, ce, tt.val)
  75. }
  76. }
  77. }
  78. func mkRawCES(in [][]int) []rawCE {
  79. out := []rawCE{}
  80. for _, w := range in {
  81. out = append(out, rawCE{w: w})
  82. }
  83. return out
  84. }
  85. type weightsTest struct {
  86. a, b [][]int
  87. level colltab.Level
  88. result int
  89. }
  90. var nextWeightTests = []weightsTest{
  91. {
  92. a: [][]int{{100, 20, 5, 0}},
  93. b: [][]int{{101, defaultSecondary, defaultTertiary, 0}},
  94. level: colltab.Primary,
  95. },
  96. {
  97. a: [][]int{{100, 20, 5, 0}},
  98. b: [][]int{{100, 21, defaultTertiary, 0}},
  99. level: colltab.Secondary,
  100. },
  101. {
  102. a: [][]int{{100, 20, 5, 0}},
  103. b: [][]int{{100, 20, 6, 0}},
  104. level: colltab.Tertiary,
  105. },
  106. {
  107. a: [][]int{{100, 20, 5, 0}},
  108. b: [][]int{{100, 20, 5, 0}},
  109. level: colltab.Identity,
  110. },
  111. }
  112. var extra = [][]int{{200, 32, 8, 0}, {0, 32, 8, 0}, {0, 0, 8, 0}, {0, 0, 0, 0}}
  113. func TestNextWeight(t *testing.T) {
  114. for i, tt := range nextWeightTests {
  115. test := func(l colltab.Level, tt weightsTest, a, gold [][]int) {
  116. res := nextWeight(tt.level, mkRawCES(a))
  117. if !equalCEArrays(mkRawCES(gold), res) {
  118. t.Errorf("%d:%d: expected weights %d; found %d", i, l, gold, res)
  119. }
  120. }
  121. test(-1, tt, tt.a, tt.b)
  122. for l := colltab.Primary; l <= colltab.Tertiary; l++ {
  123. if tt.level <= l {
  124. test(l, tt, append(tt.a, extra[l]), tt.b)
  125. } else {
  126. test(l, tt, append(tt.a, extra[l]), append(tt.b, extra[l]))
  127. }
  128. }
  129. }
  130. }
  131. var compareTests = []weightsTest{
  132. {
  133. [][]int{{100, 20, 5, 0}},
  134. [][]int{{100, 20, 5, 0}},
  135. colltab.Identity,
  136. 0,
  137. },
  138. {
  139. [][]int{{100, 20, 5, 0}, extra[0]},
  140. [][]int{{100, 20, 5, 1}},
  141. colltab.Primary,
  142. 1,
  143. },
  144. {
  145. [][]int{{100, 20, 5, 0}},
  146. [][]int{{101, 20, 5, 0}},
  147. colltab.Primary,
  148. -1,
  149. },
  150. {
  151. [][]int{{101, 20, 5, 0}},
  152. [][]int{{100, 20, 5, 0}},
  153. colltab.Primary,
  154. 1,
  155. },
  156. {
  157. [][]int{{100, 0, 0, 0}, {0, 20, 5, 0}},
  158. [][]int{{0, 20, 5, 0}, {100, 0, 0, 0}},
  159. colltab.Identity,
  160. 0,
  161. },
  162. {
  163. [][]int{{100, 20, 5, 0}},
  164. [][]int{{100, 21, 5, 0}},
  165. colltab.Secondary,
  166. -1,
  167. },
  168. {
  169. [][]int{{100, 20, 5, 0}},
  170. [][]int{{100, 20, 2, 0}},
  171. colltab.Tertiary,
  172. 1,
  173. },
  174. {
  175. [][]int{{100, 20, 5, 1}},
  176. [][]int{{100, 20, 5, 2}},
  177. colltab.Quaternary,
  178. -1,
  179. },
  180. }
  181. func TestCompareWeights(t *testing.T) {
  182. for i, tt := range compareTests {
  183. test := func(tt weightsTest, a, b [][]int) {
  184. res, level := compareWeights(mkRawCES(a), mkRawCES(b))
  185. if res != tt.result {
  186. t.Errorf("%d: expected comparison result %d; found %d", i, tt.result, res)
  187. }
  188. if level != tt.level {
  189. t.Errorf("%d: expected level %d; found %d", i, tt.level, level)
  190. }
  191. }
  192. test(tt, tt.a, tt.b)
  193. test(tt, append(tt.a, extra[0]), append(tt.b, extra[0]))
  194. }
  195. }