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.
13 func (e Elem) String() string {
15 if v := e.Quaternary(); v == MaxQuaternary {
20 return fmt.Sprintf("[%d, %d, %d, %s]",
28 f func(inout []int) (Elem, ceType)
32 func makeCE(weights []int) Elem {
33 ce, _ := MakeElem(weights[0], weights[1], weights[2], uint8(weights[3]))
37 var defaultValues = []int{0, defaultSecondary, defaultTertiary, 0}
39 func e(w ...int) Elem {
40 return makeCE(append(w, defaultValues[len(w):]...))
43 func makeContractIndex(index, n, offset int) Elem {
45 contractID = 0xC0000000
48 maxContractOffsetBits = 13
50 ce := Elem(contractID)
51 ce += Elem(offset << (maxNBits + maxTrieIndexBits))
52 ce += Elem(index << maxNBits)
57 func makeExpandIndex(index int) Elem {
58 const expandID = 0xE0000000
59 return expandID + Elem(index)
62 func makeDecompose(t1, t2 int) Elem {
63 const decompID = 0xF0000000
64 return Elem(t2<<8+t1) + decompID
67 func normalCE(inout []int) (ce Elem, t ceType) {
69 inout[0] = ce.Primary()
70 inout[1] = ce.Secondary()
71 inout[2] = int(ce.Tertiary())
72 inout[3] = int(ce.CCC())
76 func expandCE(inout []int) (ce Elem, t ceType) {
77 ce = makeExpandIndex(inout[0])
78 inout[0] = splitExpandIndex(ce)
79 return ce, ceExpansionIndex
82 func contractCE(inout []int) (ce Elem, t ceType) {
83 ce = makeContractIndex(inout[0], inout[1], inout[2])
84 i, n, o := splitContractIndex(ce)
85 inout[0], inout[1], inout[2] = i, n, o
86 return ce, ceContractionIndex
89 func decompCE(inout []int) (ce Elem, t ceType) {
90 ce = makeDecompose(inout[0], inout[1])
91 t1, t2 := splitDecompose(ce)
92 inout[0], inout[1] = int(t1), int(t2)
93 return ce, ceDecompose
96 var ceTests = []ceTest{
97 {normalCE, []int{0, 0, 0, 0}},
98 {normalCE, []int{0, 30, 3, 0}},
99 {normalCE, []int{0, 30, 3, 0xFF}},
100 {normalCE, []int{100, defaultSecondary, defaultTertiary, 0}},
101 {normalCE, []int{100, defaultSecondary, defaultTertiary, 0xFF}},
102 {normalCE, []int{100, defaultSecondary, 3, 0}},
103 {normalCE, []int{0x123, defaultSecondary, 8, 0xFF}},
105 {contractCE, []int{0, 0, 0}},
106 {contractCE, []int{1, 1, 1}},
107 {contractCE, []int{1, (1 << maxNBits) - 1, 1}},
108 {contractCE, []int{(1 << maxTrieIndexBits) - 1, 1, 1}},
109 {contractCE, []int{1, 1, (1 << maxContractOffsetBits) - 1}},
111 {expandCE, []int{0}},
112 {expandCE, []int{5}},
113 {expandCE, []int{(1 << maxExpandIndexBits) - 1}},
115 {decompCE, []int{0, 0}},
116 {decompCE, []int{1, 1}},
117 {decompCE, []int{0x1F, 0x1F}},
120 func TestColElem(t *testing.T) {
121 for i, tt := range ceTests {
122 inout := make([]int, len(tt.arg))
124 ce, typ := tt.f(inout)
125 if ce.ctype() != typ {
126 t.Errorf("%d: type is %d; want %d (ColElem: %X)", i, ce.ctype(), typ, ce)
128 for j, a := range tt.arg {
130 t.Errorf("%d: argument %d is %X; want %X (ColElem: %X)", i, j, inout[j], a, ce)
136 type implicitTest struct {
141 var implicitTests = []implicitTest{
156 {unicode.MaxRune, 0x15FFFF}, // maximum primary value
159 func TestImplicit(t *testing.T) {
160 for _, tt := range implicitTests {
161 if p := implicitPrimary(tt.r); p != tt.p {
162 t.Errorf("%U: was %X; want %X", tt.r, p, tt.p)
167 func TestUpdateTertiary(t *testing.T) {
172 {0x4000FE20, 0x0000FE8A, 0x0A},
173 {0x4000FE21, 0x0000FEAA, 0x0A},
174 {0x0000FE8B, 0x0000FE83, 0x03},
175 {0x82FF0188, 0x9BFF0188, 0x1B},
176 {0xAFF0CC02, 0xAFF0CC1B, 0x1B},
178 for i, tt := range tests {
179 if out := tt.in.updateTertiary(tt.t); out != tt.out {
180 t.Errorf("%d: was %X; want %X", i, out, tt.out)