github.com/thiagoyeds/go-cloud@v0.26.0/docstore/internal/fields/fold_test.go (about) 1 // Copyright 2019 The Go Cloud Development Kit Authors 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 15 package fields 16 17 // This file was copied from https://go.googlesource.com/go/+/go1.7.3/src/encoding/json/fold_test.go. 18 // Only the license and package were changed. 19 20 import ( 21 "bytes" 22 "strings" 23 "testing" 24 "unicode/utf8" 25 ) 26 27 var foldTests = []struct { 28 fn func(s, t []byte) bool 29 s, t string 30 want bool 31 }{ 32 {equalFoldRight, "", "", true}, 33 {equalFoldRight, "a", "a", true}, 34 {equalFoldRight, "", "a", false}, 35 {equalFoldRight, "a", "", false}, 36 {equalFoldRight, "a", "A", true}, 37 {equalFoldRight, "AB", "ab", true}, 38 {equalFoldRight, "AB", "ac", false}, 39 {equalFoldRight, "sbkKc", "ſbKKc", true}, 40 {equalFoldRight, "SbKkc", "ſbKKc", true}, 41 {equalFoldRight, "SbKkc", "ſbKK", false}, 42 {equalFoldRight, "e", "é", false}, 43 {equalFoldRight, "s", "S", true}, 44 45 {simpleLetterEqualFold, "", "", true}, 46 {simpleLetterEqualFold, "abc", "abc", true}, 47 {simpleLetterEqualFold, "abc", "ABC", true}, 48 {simpleLetterEqualFold, "abc", "ABCD", false}, 49 {simpleLetterEqualFold, "abc", "xxx", false}, 50 51 {asciiEqualFold, "a_B", "A_b", true}, 52 {asciiEqualFold, "aa@", "aa`", false}, // verify 0x40 and 0x60 aren't case-equivalent 53 } 54 55 func TestFold(t *testing.T) { 56 for i, tt := range foldTests { 57 if got := tt.fn([]byte(tt.s), []byte(tt.t)); got != tt.want { 58 t.Errorf("%d. %q, %q = %v; want %v", i, tt.s, tt.t, got, tt.want) 59 } 60 truth := strings.EqualFold(tt.s, tt.t) 61 if truth != tt.want { 62 t.Errorf("strings.EqualFold doesn't agree with case %d", i) 63 } 64 } 65 } 66 67 func TestFoldAgainstUnicode(t *testing.T) { 68 const bufSize = 5 69 buf1 := make([]byte, 0, bufSize) 70 buf2 := make([]byte, 0, bufSize) 71 var runes []rune 72 for i := 0x20; i <= 0x7f; i++ { 73 runes = append(runes, rune(i)) 74 } 75 runes = append(runes, kelvin, smallLongEss) 76 77 funcs := []struct { 78 name string 79 fold func(s, t []byte) bool 80 letter bool // must be ASCII letter 81 simple bool // must be simple ASCII letter (not 'S' or 'K') 82 }{ 83 { 84 name: "equalFoldRight", 85 fold: equalFoldRight, 86 }, 87 { 88 name: "asciiEqualFold", 89 fold: asciiEqualFold, 90 simple: true, 91 }, 92 { 93 name: "simpleLetterEqualFold", 94 fold: simpleLetterEqualFold, 95 simple: true, 96 letter: true, 97 }, 98 } 99 100 for _, ff := range funcs { 101 for _, r := range runes { 102 if r >= utf8.RuneSelf { 103 continue 104 } 105 if ff.letter && !isASCIILetter(byte(r)) { 106 continue 107 } 108 if ff.simple && (r == 's' || r == 'S' || r == 'k' || r == 'K') { 109 continue 110 } 111 for _, r2 := range runes { 112 buf1 := append(buf1[:0], 'x') 113 buf2 := append(buf2[:0], 'x') 114 buf1 = buf1[:1+utf8.EncodeRune(buf1[1:bufSize], r)] 115 buf2 = buf2[:1+utf8.EncodeRune(buf2[1:bufSize], r2)] 116 buf1 = append(buf1, 'x') 117 buf2 = append(buf2, 'x') 118 want := bytes.EqualFold(buf1, buf2) 119 if got := ff.fold(buf1, buf2); got != want { 120 t.Errorf("%s(%q, %q) = %v; want %v", ff.name, buf1, buf2, got, want) 121 } 122 } 123 } 124 } 125 } 126 127 func isASCIILetter(b byte) bool { 128 return ('A' <= b && b <= 'Z') || ('a' <= b && b <= 'z') 129 }