summaryrefslogtreecommitdiff
path: root/vendor/golang.org/x/text/internal/colltab/collate_test.go
blob: 580c85c22daf370d2f3bcaf0a4498b1212a5c6cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package colltab_test

// This file contains tests which need to import package collate, which causes
// an import cycle when done within package colltab itself.

import (
	"bytes"
	"testing"
	"unicode"

	"golang.org/x/text/collate"
	"golang.org/x/text/language"
	"golang.org/x/text/unicode/rangetable"
)

// assigned is used to only test runes that are inside the scope of the Unicode
// version used to generation the collation table.
var assigned = rangetable.Assigned(collate.UnicodeVersion)

func TestNonDigits(t *testing.T) {
	c := collate.New(language.English, collate.Loose, collate.Numeric)

	// Verify that all non-digit numbers sort outside of the number range.
	for r, hi := rune(unicode.N.R16[0].Lo), rune(unicode.N.R32[0].Hi); r <= hi; r++ {
		if unicode.In(r, unicode.Nd) || !unicode.In(r, assigned) {
			continue
		}
		if a := string(r); c.CompareString(a, "0") != -1 && c.CompareString(a, "999999") != 1 {
			t.Errorf("%+q non-digit number is collated as digit", a)
		}
	}
}

func TestNumericCompare(t *testing.T) {
	c := collate.New(language.English, collate.Loose, collate.Numeric)

	// Iterate over all digits.
	for _, r16 := range unicode.Nd.R16 {
		testDigitCompare(t, c, rune(r16.Lo), rune(r16.Hi))
	}
	for _, r32 := range unicode.Nd.R32 {
		testDigitCompare(t, c, rune(r32.Lo), rune(r32.Hi))
	}
}

func testDigitCompare(t *testing.T, c *collate.Collator, zero, nine rune) {
	if !unicode.In(zero, assigned) {
		return
	}
	n := int(nine - zero + 1)
	if n%10 != 0 {
		t.Fatalf("len([%+q, %+q]) = %d; want a multiple of 10", zero, nine, n)
	}
	for _, tt := range []struct {
		prefix string
		b      [11]string
	}{
		{
			prefix: "",
			b: [11]string{
				"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
			},
		},
		{
			prefix: "1",
			b: [11]string{
				"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
			},
		},
		{
			prefix: "0",
			b: [11]string{
				"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10",
			},
		},
		{
			prefix: "00",
			b: [11]string{
				"000", "001", "002", "003", "004", "005", "006", "007", "008", "009", "010",
			},
		},
		{
			prefix: "9",
			b: [11]string{
				"90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100",
			},
		},
	} {
		for k := 0; k <= n; k++ {
			i := k % 10
			a := tt.prefix + string(zero+rune(i))
			for j, b := range tt.b {
				want := 0
				switch {
				case i < j:
					want = -1
				case i > j:
					want = 1
				}
				got := c.CompareString(a, b)
				if got != want {
					t.Errorf("Compare(%+q, %+q) = %d; want %d", a, b, got, want)
					return
				}
			}
		}
	}
}

func BenchmarkNumericWeighter(b *testing.B) {
	c := collate.New(language.English, collate.Numeric)
	input := bytes.Repeat([]byte("Testing, testing 123..."), 100)
	b.SetBytes(int64(2 * len(input)))
	for i := 0; i < b.N; i++ {
		c.Compare(input, input)
	}
}