github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/soliton/collate/collate_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package defCauslate 15 16 import ( 17 "testing" 18 19 . "github.com/whtcorpsinc/check" 20 "github.com/whtcorpsinc/milevadb/soliton/testleak" 21 ) 22 23 func TestT(t *testing.T) { 24 TestingT(t) 25 } 26 27 var ( 28 _ = SerialSuites(&testDefCauslateSuite{}) 29 ) 30 31 type testDefCauslateSuite struct { 32 } 33 34 type compareBlock struct { 35 Left string 36 Right string 37 Expect int 38 } 39 40 type keyBlock struct { 41 Str string 42 Expect []byte 43 } 44 45 func testCompareBlock(causet []compareBlock, defCauslate string, c *C) { 46 for i, t := range causet { 47 comment := Commentf("%d %v %v", i, t.Left, t.Right) 48 c.Assert(GetDefCauslator(defCauslate).Compare(t.Left, t.Right), Equals, t.Expect, comment) 49 } 50 } 51 52 func testKeyBlock(causet []keyBlock, defCauslate string, c *C) { 53 for i, t := range causet { 54 comment := Commentf("%d %s", i, t.Str) 55 c.Assert(GetDefCauslator(defCauslate).Key(t.Str), DeepEquals, t.Expect, comment) 56 } 57 } 58 59 func (s *testDefCauslateSuite) TestBinDefCauslator(c *C) { 60 defer testleak.AfterTest(c)() 61 SetNewDefCauslationEnabledForTest(false) 62 compareBlock := []compareBlock{ 63 {"a", "b", -1}, 64 {"a", "A", 1}, 65 {"abc", "abc", 0}, 66 {"abc", "ab", 1}, 67 {"a", "a ", -1}, 68 {"a ", "a ", -1}, 69 {"a\t", "a", 1}, 70 } 71 keyBlock := []keyBlock{ 72 {"a", []byte{0x61}}, 73 {"A", []byte{0x41}}, 74 {"Foo © bar 𝌆 baz ☃ qux", []byte{0x46, 0x6f, 0x6f, 0x20, 0xc2, 0xa9, 0x20, 0x62, 0x61, 0x72, 0x20, 0xf0, 75 0x9d, 0x8c, 0x86, 0x20, 0x62, 0x61, 0x7a, 0x20, 0xe2, 0x98, 0x83, 0x20, 0x71, 0x75, 0x78}}, 76 {"a ", []byte{0x61, 0x20}}, 77 {"a", []byte{0x61}}, 78 } 79 testCompareBlock(compareBlock, "utf8mb4_bin", c) 80 testKeyBlock(keyBlock, "utf8mb4_bin", c) 81 } 82 83 func (s *testDefCauslateSuite) TestBinPaddingDefCauslator(c *C) { 84 defer testleak.AfterTest(c)() 85 SetNewDefCauslationEnabledForTest(true) 86 defer SetNewDefCauslationEnabledForTest(false) 87 compareBlock := []compareBlock{ 88 {"a", "b", -1}, 89 {"a", "A", 1}, 90 {"abc", "abc", 0}, 91 {"abc", "ab", 1}, 92 {"a", "a ", 0}, 93 {"a ", "a ", 0}, 94 {"a\t", "a", 1}, 95 } 96 keyBlock := []keyBlock{ 97 {"a", []byte{0x61}}, 98 {"A", []byte{0x41}}, 99 {"Foo © bar 𝌆 baz ☃ qux", []byte{0x46, 0x6f, 0x6f, 0x20, 0xc2, 0xa9, 0x20, 0x62, 0x61, 100 0x72, 0x20, 0xf0, 0x9d, 0x8c, 0x86, 0x20, 0x62, 0x61, 0x7a, 0x20, 0xe2, 0x98, 0x83, 0x20, 0x71, 0x75, 0x78}}, 101 {"a ", []byte{0x61}}, 102 {"a", []byte{0x61}}, 103 } 104 testCompareBlock(compareBlock, "utf8mb4_bin", c) 105 testKeyBlock(keyBlock, "utf8mb4_bin", c) 106 } 107 108 func (s *testDefCauslateSuite) TestGeneralCIDefCauslator(c *C) { 109 defer testleak.AfterTest(c)() 110 SetNewDefCauslationEnabledForTest(true) 111 defer SetNewDefCauslationEnabledForTest(false) 112 compareBlock := []compareBlock{ 113 {"a", "b", -1}, 114 {"a", "A", 0}, 115 {"À", "A", 0}, 116 {"abc", "abc", 0}, 117 {"abc", "ab", 1}, 118 {"😜", "😃", 0}, 119 {"a ", "a ", 0}, 120 {"a\t", "a", 1}, 121 } 122 keyBlock := []keyBlock{ 123 {"a", []byte{0x0, 0x41}}, 124 {"A", []byte{0x0, 0x41}}, 125 {"😃", []byte{0xff, 0xfd}}, 126 {"Foo © bar 𝌆 baz ☃ qux", []byte{0x0, 0x46, 0x0, 0x4f, 0x0, 0x4f, 0x0, 0x20, 0x0, 0xa9, 0x0, 0x20, 0x0, 127 0x42, 0x0, 0x41, 0x0, 0x52, 0x0, 0x20, 0xff, 0xfd, 0x0, 0x20, 0x0, 0x42, 0x0, 0x41, 0x0, 0x5a, 0x0, 0x20, 0x26, 128 0x3, 0x0, 0x20, 0x0, 0x51, 0x0, 0x55, 0x0, 0x58}}, 129 {string([]byte{0x88, 0xe6}), []byte{0xff, 0xfd, 0xff, 0xfd}}, 130 {"a ", []byte{0x0, 0x41}}, 131 {"a", []byte{0x0, 0x41}}, 132 } 133 testCompareBlock(compareBlock, "utf8mb4_general_ci", c) 134 testKeyBlock(keyBlock, "utf8mb4_general_ci", c) 135 } 136 137 func (s *testDefCauslateSuite) TestUnicodeCIDefCauslator(c *C) { 138 defer testleak.AfterTest(c)() 139 SetNewDefCauslationEnabledForTest(true) 140 defer SetNewDefCauslationEnabledForTest(false) 141 142 compareBlock := []compareBlock{ 143 {"a", "b", -1}, 144 {"a", "A", 0}, 145 {"abc", "abc", 0}, 146 {"abc", "ab", 1}, 147 {"a", "a ", 0}, 148 {"a ", "a ", 0}, 149 {"😜", "😃", 0}, 150 {"a\t", "a", 1}, 151 {"ß", "s", 1}, 152 {"ß", "ss", 0}, 153 } 154 keyBlock := []keyBlock{ 155 {"a", []byte{0x0E, 0x33}}, 156 {"A", []byte{0x0E, 0x33}}, 157 {"ß", []byte{0x0F, 0xEA, 0x0F, 0xEA}}, 158 {"Foo © bar 𝌆 baz ☃ qux", []byte{0x0E, 0xB9, 0x0F, 0x82, 0x0F, 0x82, 0x02, 0x09, 0x02, 159 0xC5, 0x02, 0x09, 0x0E, 0x4A, 0x0E, 0x33, 0x0F, 0xC0, 0x02, 0x09, 0xFF, 0xFD, 0x02, 160 0x09, 0x0E, 0x4A, 0x0E, 0x33, 0x10, 0x6A, 0x02, 0x09, 0x06, 0xFF, 0x02, 0x09, 0x0F, 161 0xB4, 0x10, 0x1F, 0x10, 0x5A}}, 162 {"a ", []byte{0x0E, 0x33}}, 163 {"ﷻ", []byte{0x13, 0x5E, 0x13, 0xAB, 0x02, 0x09, 0x13, 0x5E, 0x13, 0xAB, 0x13, 0x50, 0x13, 0xAB, 0x13, 0xB7}}, 164 } 165 166 testCompareBlock(compareBlock, "utf8mb4_unicode_ci", c) 167 testKeyBlock(keyBlock, "utf8mb4_unicode_ci", c) 168 } 169 170 func (s *testDefCauslateSuite) TestSetNewDefCauslateEnabled(c *C) { 171 defer SetNewDefCauslationEnabledForTest(false) 172 173 SetNewDefCauslationEnabledForTest(true) 174 c.Assert(NewDefCauslationEnabled(), Equals, true) 175 } 176 177 func (s *testDefCauslateSuite) TestRewriteAndRestoreDefCauslationID(c *C) { 178 SetNewDefCauslationEnabledForTest(true) 179 c.Assert(RewriteNewDefCauslationIDIfNeeded(5), Equals, int32(-5)) 180 c.Assert(RewriteNewDefCauslationIDIfNeeded(-5), Equals, int32(-5)) 181 c.Assert(RestoreDefCauslationIDIfNeeded(-5), Equals, int32(5)) 182 c.Assert(RestoreDefCauslationIDIfNeeded(5), Equals, int32(5)) 183 184 SetNewDefCauslationEnabledForTest(false) 185 c.Assert(RewriteNewDefCauslationIDIfNeeded(5), Equals, int32(5)) 186 c.Assert(RewriteNewDefCauslationIDIfNeeded(-5), Equals, int32(-5)) 187 c.Assert(RestoreDefCauslationIDIfNeeded(5), Equals, int32(5)) 188 c.Assert(RestoreDefCauslationIDIfNeeded(-5), Equals, int32(-5)) 189 } 190 191 func (s *testDefCauslateSuite) TestGetDefCauslator(c *C) { 192 defer testleak.AfterTest(c)() 193 SetNewDefCauslationEnabledForTest(true) 194 defer SetNewDefCauslationEnabledForTest(false) 195 c.Assert(GetDefCauslator("binary"), FitsTypeOf, &binDefCauslator{}) 196 c.Assert(GetDefCauslator("utf8mb4_bin"), FitsTypeOf, &binPaddingDefCauslator{}) 197 c.Assert(GetDefCauslator("utf8_bin"), FitsTypeOf, &binPaddingDefCauslator{}) 198 c.Assert(GetDefCauslator("utf8mb4_general_ci"), FitsTypeOf, &generalCIDefCauslator{}) 199 c.Assert(GetDefCauslator("utf8_general_ci"), FitsTypeOf, &generalCIDefCauslator{}) 200 c.Assert(GetDefCauslator("utf8mb4_unicode_ci"), FitsTypeOf, &unicodeCIDefCauslator{}) 201 c.Assert(GetDefCauslator("utf8_unicode_ci"), FitsTypeOf, &unicodeCIDefCauslator{}) 202 c.Assert(GetDefCauslator("default_test"), FitsTypeOf, &binPaddingDefCauslator{}) 203 c.Assert(GetDefCauslatorByID(63), FitsTypeOf, &binDefCauslator{}) 204 c.Assert(GetDefCauslatorByID(46), FitsTypeOf, &binPaddingDefCauslator{}) 205 c.Assert(GetDefCauslatorByID(83), FitsTypeOf, &binPaddingDefCauslator{}) 206 c.Assert(GetDefCauslatorByID(45), FitsTypeOf, &generalCIDefCauslator{}) 207 c.Assert(GetDefCauslatorByID(33), FitsTypeOf, &generalCIDefCauslator{}) 208 c.Assert(GetDefCauslatorByID(224), FitsTypeOf, &unicodeCIDefCauslator{}) 209 c.Assert(GetDefCauslatorByID(192), FitsTypeOf, &unicodeCIDefCauslator{}) 210 c.Assert(GetDefCauslatorByID(9999), FitsTypeOf, &binPaddingDefCauslator{}) 211 212 SetNewDefCauslationEnabledForTest(false) 213 c.Assert(GetDefCauslator("binary"), FitsTypeOf, &binDefCauslator{}) 214 c.Assert(GetDefCauslator("utf8mb4_bin"), FitsTypeOf, &binDefCauslator{}) 215 c.Assert(GetDefCauslator("utf8_bin"), FitsTypeOf, &binDefCauslator{}) 216 c.Assert(GetDefCauslator("utf8mb4_general_ci"), FitsTypeOf, &binDefCauslator{}) 217 c.Assert(GetDefCauslator("utf8_general_ci"), FitsTypeOf, &binDefCauslator{}) 218 c.Assert(GetDefCauslator("utf8mb4_unicode_ci"), FitsTypeOf, &binDefCauslator{}) 219 c.Assert(GetDefCauslator("utf8_unicode_ci"), FitsTypeOf, &binDefCauslator{}) 220 c.Assert(GetDefCauslator("default_test"), FitsTypeOf, &binDefCauslator{}) 221 c.Assert(GetDefCauslatorByID(63), FitsTypeOf, &binDefCauslator{}) 222 c.Assert(GetDefCauslatorByID(46), FitsTypeOf, &binDefCauslator{}) 223 c.Assert(GetDefCauslatorByID(83), FitsTypeOf, &binDefCauslator{}) 224 c.Assert(GetDefCauslatorByID(45), FitsTypeOf, &binDefCauslator{}) 225 c.Assert(GetDefCauslatorByID(33), FitsTypeOf, &binDefCauslator{}) 226 c.Assert(GetDefCauslatorByID(224), FitsTypeOf, &binDefCauslator{}) 227 c.Assert(GetDefCauslatorByID(192), FitsTypeOf, &binDefCauslator{}) 228 c.Assert(GetDefCauslatorByID(9999), FitsTypeOf, &binDefCauslator{}) 229 }