You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

130 lines
3.5 KiB

  1. // Copyright 2016 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fields
  15. // This file was copied from https://go.googlesource.com/go/+/go1.7.3/src/encoding/json/fold_test.go.
  16. // Only the license and package were changed.
  17. import (
  18. "bytes"
  19. "strings"
  20. "testing"
  21. "unicode/utf8"
  22. )
  23. var foldTests = []struct {
  24. fn func(s, t []byte) bool
  25. s, t string
  26. want bool
  27. }{
  28. {equalFoldRight, "", "", true},
  29. {equalFoldRight, "a", "a", true},
  30. {equalFoldRight, "", "a", false},
  31. {equalFoldRight, "a", "", false},
  32. {equalFoldRight, "a", "A", true},
  33. {equalFoldRight, "AB", "ab", true},
  34. {equalFoldRight, "AB", "ac", false},
  35. {equalFoldRight, "sbkKc", "ſbKKc", true},
  36. {equalFoldRight, "SbKkc", "ſbKKc", true},
  37. {equalFoldRight, "SbKkc", "ſbKK", false},
  38. {equalFoldRight, "e", "é", false},
  39. {equalFoldRight, "s", "S", true},
  40. {simpleLetterEqualFold, "", "", true},
  41. {simpleLetterEqualFold, "abc", "abc", true},
  42. {simpleLetterEqualFold, "abc", "ABC", true},
  43. {simpleLetterEqualFold, "abc", "ABCD", false},
  44. {simpleLetterEqualFold, "abc", "xxx", false},
  45. {asciiEqualFold, "a_B", "A_b", true},
  46. {asciiEqualFold, "aa@", "aa`", false}, // verify 0x40 and 0x60 aren't case-equivalent
  47. }
  48. func TestFold(t *testing.T) {
  49. for i, tt := range foldTests {
  50. if got := tt.fn([]byte(tt.s), []byte(tt.t)); got != tt.want {
  51. t.Errorf("%d. %q, %q = %v; want %v", i, tt.s, tt.t, got, tt.want)
  52. }
  53. truth := strings.EqualFold(tt.s, tt.t)
  54. if truth != tt.want {
  55. t.Errorf("strings.EqualFold doesn't agree with case %d", i)
  56. }
  57. }
  58. }
  59. func TestFoldAgainstUnicode(t *testing.T) {
  60. const bufSize = 5
  61. buf1 := make([]byte, 0, bufSize)
  62. buf2 := make([]byte, 0, bufSize)
  63. var runes []rune
  64. for i := 0x20; i <= 0x7f; i++ {
  65. runes = append(runes, rune(i))
  66. }
  67. runes = append(runes, kelvin, smallLongEss)
  68. funcs := []struct {
  69. name string
  70. fold func(s, t []byte) bool
  71. letter bool // must be ASCII letter
  72. simple bool // must be simple ASCII letter (not 'S' or 'K')
  73. }{
  74. {
  75. name: "equalFoldRight",
  76. fold: equalFoldRight,
  77. },
  78. {
  79. name: "asciiEqualFold",
  80. fold: asciiEqualFold,
  81. simple: true,
  82. },
  83. {
  84. name: "simpleLetterEqualFold",
  85. fold: simpleLetterEqualFold,
  86. simple: true,
  87. letter: true,
  88. },
  89. }
  90. for _, ff := range funcs {
  91. for _, r := range runes {
  92. if r >= utf8.RuneSelf {
  93. continue
  94. }
  95. if ff.letter && !isASCIILetter(byte(r)) {
  96. continue
  97. }
  98. if ff.simple && (r == 's' || r == 'S' || r == 'k' || r == 'K') {
  99. continue
  100. }
  101. for _, r2 := range runes {
  102. buf1 := append(buf1[:0], 'x')
  103. buf2 := append(buf2[:0], 'x')
  104. buf1 = buf1[:1+utf8.EncodeRune(buf1[1:bufSize], r)]
  105. buf2 = buf2[:1+utf8.EncodeRune(buf2[1:bufSize], r2)]
  106. buf1 = append(buf1, 'x')
  107. buf2 = append(buf2, 'x')
  108. want := bytes.EqualFold(buf1, buf2)
  109. if got := ff.fold(buf1, buf2); got != want {
  110. t.Errorf("%s(%q, %q) = %v; want %v", ff.name, buf1, buf2, got, want)
  111. }
  112. }
  113. }
  114. }
  115. }
  116. func isASCIILetter(b byte) bool {
  117. return ('A' <= b && b <= 'Z') || ('a' <= b && b <= 'z')
  118. }