1 // Copyright 2015 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.
11 "golang.org/x/text/internal/testtext"
12 "golang.org/x/text/language"
16 cup = MustParseISO("CUP")
17 czk = MustParseISO("CZK")
18 xcd = MustParseISO("XCD")
19 zwr = MustParseISO("ZWR")
22 func TestParseISO(t *testing.T) {
23 testCases := []struct {
33 {"", XXX, false}, // not well-formed
34 {"UUU", XXX, false}, // unknown
35 {"\u22A9", XXX, false}, // non-ASCII, printable
42 {"\x00\x00\x00", XXX, false},
43 {"\xff\xff\xff", XXX, false},
45 for i, tc := range testCases {
46 if x, err := ParseISO(tc.in); x != tc.out || err == nil != tc.ok {
47 t.Errorf("%d:%s: was %s, %v; want %s, %v", i, tc.in, x, err == nil, tc.out, tc.ok)
52 func TestFromRegion(t *testing.T) {
53 testCases := []struct {
62 {"CU", cup, true}, // first of multiple
63 {"DG", USD, true}, // does not have M49 code
64 {"150", XXX, false}, // implicit false
65 {"CP", XXX, false}, // explicit false in CLDR
66 {"CS", XXX, false}, // all expired
67 {"ZZ", XXX, false}, // none match
69 for _, tc := range testCases {
70 cur, ok := FromRegion(language.MustParseRegion(tc.region))
71 if cur != tc.currency || ok != tc.ok {
72 t.Errorf("%s: got %v, %v; want %v, %v", tc.region, cur, ok, tc.currency, tc.ok)
77 func TestFromTag(t *testing.T) {
78 testCases := []struct {
81 conf language.Confidence
83 {"nl", EUR, language.Low}, // nl also spoken outside Euro land.
84 {"nl-BE", EUR, language.Exact}, // region is known
85 {"pt", BRL, language.Low},
86 {"en", USD, language.Low},
87 {"en-u-cu-eur", EUR, language.Exact},
88 {"tlh", XXX, language.No}, // Klingon has no country.
89 {"es-419", XXX, language.No},
90 {"und", USD, language.Low},
92 for _, tc := range testCases {
93 cur, conf := FromTag(language.MustParse(tc.tag))
94 if cur != tc.currency || conf != tc.conf {
95 t.Errorf("%s: got %v, %v; want %v, %v", tc.tag, cur, conf, tc.currency, tc.conf)
100 func TestTable(t *testing.T) {
101 for i := 4; i < len(currency); i += 4 {
102 if a, b := currency[i-4:i-1], currency[i:i+3]; a >= b {
103 t.Errorf("currency unordered at element %d: %s >= %s", i, a, b)
106 // First currency has index 1, last is numCurrencies.
107 if c := currency.Elem(1)[:3]; c != "ADP" {
108 t.Errorf("first was %c; want ADP", c)
110 if c := currency.Elem(numCurrencies)[:3]; c != "ZWR" {
111 t.Errorf("last was %c; want ZWR", c)
115 func TestKindRounding(t *testing.T) {
116 testCases := []struct {
122 {Standard, USD, 2, 1},
123 {Standard, CHF, 2, 1},
125 {Standard, TWD, 2, 1},
127 {Standard, czk, 2, 1},
129 {Standard, zwr, 2, 1},
131 {Standard, KRW, 0, 1},
132 {Cash, KRW, 0, 1}, // Cash defaults to standard.
134 for i, tc := range testCases {
135 if scale, inc := tc.kind.Rounding(tc.cur); scale != tc.scale && inc != tc.inc {
136 t.Errorf("%d: got %d, %d; want %d, %d", i, scale, inc, tc.scale, tc.inc)
141 const body = `package main
144 "golang.org/x/text/currency"
151 func TestLinking(t *testing.T) {
152 base := getSize(t, `fmt.Print(currency.CLDRVersion)`)
153 symbols := getSize(t, `fmt.Print(currency.Symbol(currency.USD))`)
154 if d := symbols - base; d < 2*1024 {
155 t.Errorf("size(symbols)-size(base) was %d; want > 2K", d)
159 func getSize(t *testing.T, main string) int {
160 size, err := testtext.CodeSize(fmt.Sprintf(body, main))
162 t.Skipf("skipping link size test; binary size could not be determined: %v", err)
167 func BenchmarkString(b *testing.B) {
168 for i := 0; i < b.N; i++ {