collate_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.
  4. package colltab_test
  5. // This file contains tests which need to import package collate, which causes
  6. // an import cycle when done within package colltab itself.
  7. import (
  8. "bytes"
  9. "testing"
  10. "unicode"
  11. "golang.org/x/text/collate"
  12. "golang.org/x/text/language"
  13. "golang.org/x/text/unicode/rangetable"
  14. )
  15. // assigned is used to only test runes that are inside the scope of the Unicode
  16. // version used to generation the collation table.
  17. var assigned = rangetable.Assigned(collate.UnicodeVersion)
  18. func TestNonDigits(t *testing.T) {
  19. c := collate.New(language.English, collate.Loose, collate.Numeric)
  20. // Verify that all non-digit numbers sort outside of the number range.
  21. for r, hi := rune(unicode.N.R16[0].Lo), rune(unicode.N.R32[0].Hi); r <= hi; r++ {
  22. if unicode.In(r, unicode.Nd) || !unicode.In(r, assigned) {
  23. continue
  24. }
  25. if a := string(r); c.CompareString(a, "0") != -1 && c.CompareString(a, "999999") != 1 {
  26. t.Errorf("%+q non-digit number is collated as digit", a)
  27. }
  28. }
  29. }
  30. func TestNumericCompare(t *testing.T) {
  31. c := collate.New(language.English, collate.Loose, collate.Numeric)
  32. // Iterate over all digits.
  33. for _, r16 := range unicode.Nd.R16 {
  34. testDigitCompare(t, c, rune(r16.Lo), rune(r16.Hi))
  35. }
  36. for _, r32 := range unicode.Nd.R32 {
  37. testDigitCompare(t, c, rune(r32.Lo), rune(r32.Hi))
  38. }
  39. }
  40. func testDigitCompare(t *testing.T, c *collate.Collator, zero, nine rune) {
  41. if !unicode.In(zero, assigned) {
  42. return
  43. }
  44. n := int(nine - zero + 1)
  45. if n%10 != 0 {
  46. t.Fatalf("len([%+q, %+q]) = %d; want a multiple of 10", zero, nine, n)
  47. }
  48. for _, tt := range []struct {
  49. prefix string
  50. b [11]string
  51. }{
  52. {
  53. prefix: "",
  54. b: [11]string{
  55. "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
  56. },
  57. },
  58. {
  59. prefix: "1",
  60. b: [11]string{
  61. "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
  62. },
  63. },
  64. {
  65. prefix: "0",
  66. b: [11]string{
  67. "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10",
  68. },
  69. },
  70. {
  71. prefix: "00",
  72. b: [11]string{
  73. "000", "001", "002", "003", "004", "005", "006", "007", "008", "009", "010",
  74. },
  75. },
  76. {
  77. prefix: "9",
  78. b: [11]string{
  79. "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100",
  80. },
  81. },
  82. } {
  83. for k := 0; k <= n; k++ {
  84. i := k % 10
  85. a := tt.prefix + string(zero+rune(i))
  86. for j, b := range tt.b {
  87. want := 0
  88. switch {
  89. case i < j:
  90. want = -1
  91. case i > j:
  92. want = 1
  93. }
  94. got := c.CompareString(a, b)
  95. if got != want {
  96. t.Errorf("Compare(%+q, %+q) = %d; want %d", a, b, got, want)
  97. return
  98. }
  99. }
  100. }
  101. }
  102. }
  103. func BenchmarkNumericWeighter(b *testing.B) {
  104. c := collate.New(language.English, collate.Numeric)
  105. input := bytes.Repeat([]byte("Testing, testing 123..."), 100)
  106. b.SetBytes(int64(2 * len(input)))
  107. for i := 0; i < b.N; i++ {
  108. c.Compare(input, input)
  109. }
  110. }