1 // Copyright 2014 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.
12 "golang.org/x/text/internal/testtext"
16 digSec = defaultSecondary
17 digTert = defaultTertiary
20 var tPlus3 = e(0, 50, digTert+3)
22 // numWeighter is a testWeighter used for testing numericWeighter.
23 var numWeighter = testWeighter{
25 "0": []Elem{e(100, digSec, digTert+1)}, // U+FF10 FULLWIDTH DIGIT ZERO
26 "₀": []Elem{e(100, digSec, digTert+5)}, // U+2080 SUBSCRIPT ZERO
29 // Allow non-primary collation elements to be inserted.
30 "١": append(p(101), tPlus3), // U+0661 ARABIC-INDIC DIGIT ONE
31 // Allow varying tertiary weight if the number is Nd.
32 "1": []Elem{e(101, digSec, digTert+1)}, // U+FF11 FULLWIDTH DIGIT ONE
34 // Allow non-primary collation elements to be inserted.
35 "٢": append(p(102), tPlus3), // U+0662 ARABIC-INDIC DIGIT TWO
36 // Varying tertiary weights should be ignored.
37 "2": []Elem{e(102, digSec, digTert+3)}, // U+FF12 FULLWIDTH DIGIT TWO
43 // Weights must be strictly monotonically increasing, but do not need to be
47 // Allow non-primary collation elements to be inserted.
48 "٩": append(p(119), tPlus3), // U+0669 ARABIC-INDIC DIGIT NINE
49 // Varying tertiary weights should be ignored.
50 "9": []Elem{e(119, digSec, digTert+1)}, // U+FF19 FULLWIDTH DIGIT NINE
51 "₉": []Elem{e(119, digSec, digTert+5)}, // U+2089 SUBSCRIPT NINE
65 func p(w ...int) (elems []Elem) {
67 e, _ := MakeElem(x, digSec, digTert, 0)
68 elems = append(elems, e)
73 func TestNumericAppendNext(t *testing.T) {
74 for _, tt := range []struct {
81 {"1", p(120, 1, 101)},
83 {"01", p(120, 1, 101)},
84 {"0001", p(120, 1, 101)},
85 {"10", p(120, 2, 101, 100)},
86 {"99", p(120, 2, 119, 119)},
87 {"9999", p(120, 4, 119, 119, 119, 119)},
88 {"1a", p(120, 1, 101, 5)},
90 {"01c", p(120, 1, 101, 8, 2)},
91 {"10x", p(120, 2, 101, 100, 200)},
92 {"99y", p(120, 2, 119, 119, 201)},
93 {"9999nop", p(120, 4, 119, 119, 119, 119, 121)},
95 // Allow follow-up collation elements if they have a zero non-primary.
96 {"١٢٩", []Elem{e(120), e(3), e(101), tPlus3, e(102), tPlus3, e(119), tPlus3}},
101 e(101, digSec, digTert+1),
102 e(102, digSec, digTert+3),
103 e(119, digSec, digTert+1),
107 // Ensure AppendNext* adds to the given buffer.
108 {"a10", p(5, 120, 2, 101, 100)},
110 nw := NewNumericWeighter(numWeighter)
114 for n, sz := 0, 0; n < len(b); {
115 got, sz = nw.AppendNext(got, b[n:])
118 if !reflect.DeepEqual(got, tt.w) {
119 t.Errorf("AppendNext(%q) =\n%v; want\n%v", tt.in, got, tt.w)
123 for n, sz := 0, 0; n < len(tt.in); {
124 got, sz = nw.AppendNextString(got, tt.in[n:])
127 if !reflect.DeepEqual(got, tt.w) {
128 t.Errorf("AppendNextString(%q) =\n%v; want\n%v", tt.in, got, tt.w)
133 func TestNumericOverflow(t *testing.T) {
134 manyDigits := strings.Repeat("9", maxDigits+1) + "a"
136 nw := NewNumericWeighter(numWeighter)
138 got, n := nw.AppendNextString(nil, manyDigits)
141 t.Errorf("n: got %d; want %d", n, maxDigits)
144 if got[1].Primary() != maxDigits {
145 t.Errorf("primary(e[1]): got %d; want %d", n, maxDigits)
149 func TestNumericWeighterAlloc(t *testing.T) {
150 buf := make([]Elem, 100)
151 w := NewNumericWeighter(numWeighter)
154 nNormal := testtext.AllocsPerRun(3, func() { numWeighter.AppendNextString(buf, s) })
155 nNumeric := testtext.AllocsPerRun(3, func() { w.AppendNextString(buf, s) })
156 if n := nNumeric - nNormal; n > 0 {
157 t.Errorf("got %f; want 0", n)