| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2013 Apple Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | |
| 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
| 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | |
| 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
| 23 * THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | |
| 26 #include "wtf/StringHasher.h" | |
| 27 | |
| 28 #include "testing/gtest/include/gtest/gtest.h" | |
| 29 | |
| 30 namespace WTF { | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 const LChar nullLChars[2] = {0, 0}; | |
| 35 const UChar nullUChars[2] = {0, 0}; | |
| 36 | |
| 37 const unsigned emptyStringHash = 0x4EC889EU; | |
| 38 const unsigned singleNullCharacterHash = 0x3D3ABF44U; | |
| 39 | |
| 40 const LChar testALChars[6] = {0x41, 0x95, 0xFF, 0x50, 0x01, 0}; | |
| 41 const UChar testAUChars[6] = {0x41, 0x95, 0xFF, 0x50, 0x01, 0}; | |
| 42 const UChar testBUChars[6] = {0x41, 0x95, 0xFFFF, 0x1080, 0x01, 0}; | |
| 43 | |
| 44 const unsigned testAHash1 = 0xEA32B004; | |
| 45 const unsigned testAHash2 = 0x93F0F71E; | |
| 46 const unsigned testAHash3 = 0xCB609EB1; | |
| 47 const unsigned testAHash4 = 0x7984A706; | |
| 48 const unsigned testAHash5 = 0x0427561F; | |
| 49 | |
| 50 const unsigned testBHash1 = 0xEA32B004; | |
| 51 const unsigned testBHash2 = 0x93F0F71E; | |
| 52 const unsigned testBHash3 = 0x59EB1B2C; | |
| 53 const unsigned testBHash4 = 0xA7BCCC0A; | |
| 54 const unsigned testBHash5 = 0x79201649; | |
| 55 | |
| 56 } // anonymous namespace | |
| 57 | |
| 58 TEST(StringHasherTest, StringHasher) { | |
| 59 StringHasher hasher; | |
| 60 | |
| 61 // The initial state of the hasher. | |
| 62 EXPECT_EQ(emptyStringHash, hasher.hash()); | |
| 63 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 64 } | |
| 65 | |
| 66 TEST(StringHasherTest, StringHasher_addCharacter) { | |
| 67 StringHasher hasher; | |
| 68 | |
| 69 // Hashing a single character. | |
| 70 hasher = StringHasher(); | |
| 71 hasher.addCharacter(0); | |
| 72 EXPECT_EQ(singleNullCharacterHash, hasher.hash()); | |
| 73 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, | |
| 74 hasher.hashWithTop8BitsMasked()); | |
| 75 | |
| 76 // Hashing five characters, checking the intermediate state after each is | |
| 77 // added. | |
| 78 hasher = StringHasher(); | |
| 79 hasher.addCharacter(testAUChars[0]); | |
| 80 EXPECT_EQ(testAHash1, hasher.hash()); | |
| 81 EXPECT_EQ(testAHash1 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 82 hasher.addCharacter(testAUChars[1]); | |
| 83 EXPECT_EQ(testAHash2, hasher.hash()); | |
| 84 EXPECT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 85 hasher.addCharacter(testAUChars[2]); | |
| 86 EXPECT_EQ(testAHash3, hasher.hash()); | |
| 87 EXPECT_EQ(testAHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 88 hasher.addCharacter(testAUChars[3]); | |
| 89 EXPECT_EQ(testAHash4, hasher.hash()); | |
| 90 EXPECT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 91 hasher.addCharacter(testAUChars[4]); | |
| 92 EXPECT_EQ(testAHash5, hasher.hash()); | |
| 93 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 94 | |
| 95 // Hashing a second set of five characters, including non-Latin-1 characters. | |
| 96 hasher = StringHasher(); | |
| 97 hasher.addCharacter(testBUChars[0]); | |
| 98 EXPECT_EQ(testBHash1, hasher.hash()); | |
| 99 EXPECT_EQ(testBHash1 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 100 hasher.addCharacter(testBUChars[1]); | |
| 101 EXPECT_EQ(testBHash2, hasher.hash()); | |
| 102 EXPECT_EQ(testBHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 103 hasher.addCharacter(testBUChars[2]); | |
| 104 EXPECT_EQ(testBHash3, hasher.hash()); | |
| 105 EXPECT_EQ(testBHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 106 hasher.addCharacter(testBUChars[3]); | |
| 107 EXPECT_EQ(testBHash4, hasher.hash()); | |
| 108 EXPECT_EQ(testBHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 109 hasher.addCharacter(testBUChars[4]); | |
| 110 EXPECT_EQ(testBHash5, hasher.hash()); | |
| 111 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 112 } | |
| 113 | |
| 114 TEST(StringHasherTest, StringHasher_addCharacters) { | |
| 115 StringHasher hasher; | |
| 116 | |
| 117 // Hashing zero characters. | |
| 118 hasher = StringHasher(); | |
| 119 hasher.addCharacters(static_cast<LChar*>(0), 0); | |
| 120 EXPECT_EQ(emptyStringHash, hasher.hash()); | |
| 121 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 122 hasher = StringHasher(); | |
| 123 hasher.addCharacters(nullLChars, 0); | |
| 124 EXPECT_EQ(emptyStringHash, hasher.hash()); | |
| 125 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 126 hasher = StringHasher(); | |
| 127 hasher.addCharacters(static_cast<UChar*>(0), 0); | |
| 128 EXPECT_EQ(emptyStringHash, hasher.hash()); | |
| 129 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 130 hasher = StringHasher(); | |
| 131 hasher.addCharacters(nullUChars, 0); | |
| 132 EXPECT_EQ(emptyStringHash, hasher.hash()); | |
| 133 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 134 | |
| 135 // Hashing one character. | |
| 136 hasher = StringHasher(); | |
| 137 hasher.addCharacters(nullLChars, 1); | |
| 138 EXPECT_EQ(singleNullCharacterHash, hasher.hash()); | |
| 139 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, | |
| 140 hasher.hashWithTop8BitsMasked()); | |
| 141 hasher = StringHasher(); | |
| 142 hasher.addCharacters(nullUChars, 1); | |
| 143 EXPECT_EQ(singleNullCharacterHash, hasher.hash()); | |
| 144 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, | |
| 145 hasher.hashWithTop8BitsMasked()); | |
| 146 | |
| 147 // Hashing five characters, all at once. | |
| 148 hasher = StringHasher(); | |
| 149 hasher.addCharacters(testALChars, 5); | |
| 150 EXPECT_EQ(testAHash5, hasher.hash()); | |
| 151 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 152 hasher = StringHasher(); | |
| 153 hasher.addCharacters(testAUChars, 5); | |
| 154 EXPECT_EQ(testAHash5, hasher.hash()); | |
| 155 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 156 hasher = StringHasher(); | |
| 157 hasher.addCharacters(testBUChars, 5); | |
| 158 EXPECT_EQ(testBHash5, hasher.hash()); | |
| 159 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 160 | |
| 161 // Hashing five characters, in groups of two, then the last one. | |
| 162 hasher = StringHasher(); | |
| 163 hasher.addCharacters(testALChars, 2); | |
| 164 EXPECT_EQ(testAHash2, hasher.hash()); | |
| 165 EXPECT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 166 hasher.addCharacters(testALChars + 2, 2); | |
| 167 EXPECT_EQ(testAHash4, hasher.hash()); | |
| 168 EXPECT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 169 hasher.addCharacters(testALChars + 4, 1); | |
| 170 EXPECT_EQ(testAHash5, hasher.hash()); | |
| 171 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 172 hasher = StringHasher(); | |
| 173 hasher.addCharacters(testALChars, 2); | |
| 174 hasher.addCharacters(testALChars + 2, 2); | |
| 175 hasher.addCharacters(testALChars + 4, 1); | |
| 176 EXPECT_EQ(testAHash5, hasher.hash()); | |
| 177 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 178 hasher = StringHasher(); | |
| 179 hasher.addCharacters(testAUChars, 2); | |
| 180 EXPECT_EQ(testAHash2, hasher.hash()); | |
| 181 EXPECT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 182 hasher.addCharacters(testAUChars + 2, 2); | |
| 183 EXPECT_EQ(testAHash4, hasher.hash()); | |
| 184 EXPECT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 185 hasher.addCharacters(testAUChars + 4, 1); | |
| 186 EXPECT_EQ(testAHash5, hasher.hash()); | |
| 187 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 188 hasher = StringHasher(); | |
| 189 hasher.addCharacters(testAUChars, 2); | |
| 190 hasher.addCharacters(testAUChars + 2, 2); | |
| 191 hasher.addCharacters(testAUChars + 4, 1); | |
| 192 EXPECT_EQ(testAHash5, hasher.hash()); | |
| 193 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 194 hasher = StringHasher(); | |
| 195 hasher.addCharacters(testBUChars, 2); | |
| 196 EXPECT_EQ(testBHash2, hasher.hash()); | |
| 197 EXPECT_EQ(testBHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 198 hasher.addCharacters(testBUChars + 2, 2); | |
| 199 EXPECT_EQ(testBHash4, hasher.hash()); | |
| 200 EXPECT_EQ(testBHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 201 hasher.addCharacters(testBUChars + 4, 1); | |
| 202 EXPECT_EQ(testBHash5, hasher.hash()); | |
| 203 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 204 hasher = StringHasher(); | |
| 205 hasher.addCharacters(testBUChars, 2); | |
| 206 hasher.addCharacters(testBUChars + 2, 2); | |
| 207 hasher.addCharacters(testBUChars + 4, 1); | |
| 208 EXPECT_EQ(testBHash5, hasher.hash()); | |
| 209 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 210 | |
| 211 // Hashing five characters, the first three, then the last two. | |
| 212 hasher = StringHasher(); | |
| 213 hasher.addCharacters(testALChars, 3); | |
| 214 EXPECT_EQ(testAHash3, hasher.hash()); | |
| 215 EXPECT_EQ(testAHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 216 hasher.addCharacters(testALChars + 3, 2); | |
| 217 EXPECT_EQ(testAHash5, hasher.hash()); | |
| 218 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 219 hasher = StringHasher(); | |
| 220 hasher.addCharacters(testALChars, 3); | |
| 221 EXPECT_EQ(testAHash3, hasher.hash()); | |
| 222 EXPECT_EQ(testAHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 223 hasher.addCharacters(testALChars + 3, 2); | |
| 224 EXPECT_EQ(testAHash5, hasher.hash()); | |
| 225 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 226 hasher = StringHasher(); | |
| 227 hasher.addCharacters(testAUChars, 3); | |
| 228 EXPECT_EQ(testAHash3, hasher.hash()); | |
| 229 EXPECT_EQ(testAHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 230 hasher.addCharacters(testAUChars + 3, 2); | |
| 231 EXPECT_EQ(testAHash5, hasher.hash()); | |
| 232 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 233 hasher = StringHasher(); | |
| 234 hasher.addCharacters(testAUChars, 3); | |
| 235 EXPECT_EQ(testAHash3, hasher.hash()); | |
| 236 EXPECT_EQ(testAHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 237 hasher.addCharacters(testAUChars + 3, 2); | |
| 238 EXPECT_EQ(testAHash5, hasher.hash()); | |
| 239 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 240 hasher = StringHasher(); | |
| 241 hasher.addCharacters(testBUChars, 3); | |
| 242 EXPECT_EQ(testBHash3, hasher.hash()); | |
| 243 EXPECT_EQ(testBHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 244 hasher.addCharacters(testBUChars + 3, 2); | |
| 245 EXPECT_EQ(testBHash5, hasher.hash()); | |
| 246 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 247 hasher = StringHasher(); | |
| 248 hasher.addCharacters(testBUChars, 3); | |
| 249 hasher.addCharacters(testBUChars + 3, 2); | |
| 250 EXPECT_EQ(testBHash5, hasher.hash()); | |
| 251 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 252 } | |
| 253 | |
| 254 TEST(StringHasherTest, StringHasher_addCharactersAssumingAligned) { | |
| 255 StringHasher hasher; | |
| 256 | |
| 257 // Hashing zero characters. | |
| 258 hasher = StringHasher(); | |
| 259 hasher.addCharactersAssumingAligned(static_cast<LChar*>(0), 0); | |
| 260 EXPECT_EQ(emptyStringHash, hasher.hash()); | |
| 261 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 262 hasher = StringHasher(); | |
| 263 hasher.addCharactersAssumingAligned(nullLChars, 0); | |
| 264 EXPECT_EQ(emptyStringHash, hasher.hash()); | |
| 265 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 266 hasher = StringHasher(); | |
| 267 hasher.addCharactersAssumingAligned(static_cast<UChar*>(0), 0); | |
| 268 EXPECT_EQ(emptyStringHash, hasher.hash()); | |
| 269 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 270 hasher = StringHasher(); | |
| 271 hasher.addCharactersAssumingAligned(nullUChars, 0); | |
| 272 EXPECT_EQ(emptyStringHash, hasher.hash()); | |
| 273 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 274 | |
| 275 // Hashing one character. | |
| 276 hasher = StringHasher(); | |
| 277 hasher.addCharactersAssumingAligned(nullLChars, 1); | |
| 278 EXPECT_EQ(singleNullCharacterHash, hasher.hash()); | |
| 279 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, | |
| 280 hasher.hashWithTop8BitsMasked()); | |
| 281 hasher = StringHasher(); | |
| 282 hasher.addCharactersAssumingAligned(nullUChars, 1); | |
| 283 EXPECT_EQ(singleNullCharacterHash, hasher.hash()); | |
| 284 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, | |
| 285 hasher.hashWithTop8BitsMasked()); | |
| 286 | |
| 287 // Hashing five characters, all at once. | |
| 288 hasher = StringHasher(); | |
| 289 hasher.addCharactersAssumingAligned(testALChars, 5); | |
| 290 EXPECT_EQ(testAHash5, hasher.hash()); | |
| 291 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 292 hasher = StringHasher(); | |
| 293 hasher.addCharactersAssumingAligned(testAUChars, 5); | |
| 294 EXPECT_EQ(testAHash5, hasher.hash()); | |
| 295 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 296 hasher = StringHasher(); | |
| 297 hasher.addCharactersAssumingAligned(testBUChars, 5); | |
| 298 EXPECT_EQ(testBHash5, hasher.hash()); | |
| 299 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 300 | |
| 301 // Hashing five characters, in groups of two, then the last one. | |
| 302 hasher = StringHasher(); | |
| 303 hasher.addCharactersAssumingAligned(testALChars, 2); | |
| 304 EXPECT_EQ(testAHash2, hasher.hash()); | |
| 305 EXPECT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 306 hasher.addCharactersAssumingAligned(testALChars + 2, 2); | |
| 307 EXPECT_EQ(testAHash4, hasher.hash()); | |
| 308 EXPECT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 309 hasher.addCharactersAssumingAligned(testALChars + 4, 1); | |
| 310 EXPECT_EQ(testAHash5, hasher.hash()); | |
| 311 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 312 hasher = StringHasher(); | |
| 313 hasher.addCharactersAssumingAligned(testALChars, 2); | |
| 314 hasher.addCharactersAssumingAligned(testALChars + 2, 2); | |
| 315 hasher.addCharactersAssumingAligned(testALChars + 4, 1); | |
| 316 EXPECT_EQ(testAHash5, hasher.hash()); | |
| 317 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 318 hasher = StringHasher(); | |
| 319 hasher.addCharactersAssumingAligned(testAUChars, 2); | |
| 320 EXPECT_EQ(testAHash2, hasher.hash()); | |
| 321 EXPECT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 322 hasher.addCharactersAssumingAligned(testAUChars + 2, 2); | |
| 323 EXPECT_EQ(testAHash4, hasher.hash()); | |
| 324 EXPECT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 325 hasher.addCharactersAssumingAligned(testAUChars + 4, 1); | |
| 326 EXPECT_EQ(testAHash5, hasher.hash()); | |
| 327 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 328 hasher = StringHasher(); | |
| 329 hasher.addCharactersAssumingAligned(testAUChars, 2); | |
| 330 hasher.addCharactersAssumingAligned(testAUChars + 2, 2); | |
| 331 hasher.addCharactersAssumingAligned(testAUChars + 4, 1); | |
| 332 EXPECT_EQ(testAHash5, hasher.hash()); | |
| 333 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 334 hasher = StringHasher(); | |
| 335 hasher.addCharactersAssumingAligned(testBUChars, 2); | |
| 336 EXPECT_EQ(testBHash2, hasher.hash()); | |
| 337 EXPECT_EQ(testBHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 338 hasher.addCharactersAssumingAligned(testBUChars + 2, 2); | |
| 339 EXPECT_EQ(testBHash4, hasher.hash()); | |
| 340 EXPECT_EQ(testBHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 341 hasher.addCharactersAssumingAligned(testBUChars + 4, 1); | |
| 342 EXPECT_EQ(testBHash5, hasher.hash()); | |
| 343 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 344 hasher = StringHasher(); | |
| 345 hasher.addCharactersAssumingAligned(testBUChars, 2); | |
| 346 hasher.addCharactersAssumingAligned(testBUChars + 2, 2); | |
| 347 hasher.addCharactersAssumingAligned(testBUChars + 4, 1); | |
| 348 EXPECT_EQ(testBHash5, hasher.hash()); | |
| 349 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 350 | |
| 351 // Hashing five characters, first two characters one at a time, | |
| 352 // then two more, then the last one. | |
| 353 hasher = StringHasher(); | |
| 354 hasher.addCharacter(testBUChars[0]); | |
| 355 EXPECT_EQ(testBHash1, hasher.hash()); | |
| 356 EXPECT_EQ(testBHash1 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 357 hasher.addCharacter(testBUChars[1]); | |
| 358 EXPECT_EQ(testBHash2, hasher.hash()); | |
| 359 EXPECT_EQ(testBHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 360 hasher.addCharactersAssumingAligned(testBUChars[2], testBUChars[3]); | |
| 361 EXPECT_EQ(testBHash4, hasher.hash()); | |
| 362 EXPECT_EQ(testBHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 363 hasher.addCharactersAssumingAligned(testBUChars + 4, 1); | |
| 364 EXPECT_EQ(testBHash5, hasher.hash()); | |
| 365 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); | |
| 366 } | |
| 367 | |
| 368 TEST(StringHasherTest, StringHasher_computeHash) { | |
| 369 EXPECT_EQ(emptyStringHash, | |
| 370 StringHasher::computeHash(static_cast<LChar*>(0), 0)); | |
| 371 EXPECT_EQ(emptyStringHash, StringHasher::computeHash(nullLChars, 0)); | |
| 372 EXPECT_EQ(emptyStringHash, | |
| 373 StringHasher::computeHash(static_cast<UChar*>(0), 0)); | |
| 374 EXPECT_EQ(emptyStringHash, StringHasher::computeHash(nullUChars, 0)); | |
| 375 | |
| 376 EXPECT_EQ(singleNullCharacterHash, StringHasher::computeHash(nullLChars, 1)); | |
| 377 EXPECT_EQ(singleNullCharacterHash, StringHasher::computeHash(nullUChars, 1)); | |
| 378 | |
| 379 EXPECT_EQ(testAHash5, StringHasher::computeHash(testALChars, 5)); | |
| 380 EXPECT_EQ(testAHash5, StringHasher::computeHash(testAUChars, 5)); | |
| 381 EXPECT_EQ(testBHash5, StringHasher::computeHash(testBUChars, 5)); | |
| 382 } | |
| 383 | |
| 384 TEST(StringHasherTest, StringHasher_computeHashAndMaskTop8Bits) { | |
| 385 EXPECT_EQ( | |
| 386 emptyStringHash & 0xFFFFFF, | |
| 387 StringHasher::computeHashAndMaskTop8Bits(static_cast<LChar*>(0), 0)); | |
| 388 EXPECT_EQ(emptyStringHash & 0xFFFFFF, | |
| 389 StringHasher::computeHashAndMaskTop8Bits(nullLChars, 0)); | |
| 390 EXPECT_EQ( | |
| 391 emptyStringHash & 0xFFFFFF, | |
| 392 StringHasher::computeHashAndMaskTop8Bits(static_cast<UChar*>(0), 0)); | |
| 393 EXPECT_EQ(emptyStringHash & 0xFFFFFF, | |
| 394 StringHasher::computeHashAndMaskTop8Bits(nullUChars, 0)); | |
| 395 | |
| 396 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, | |
| 397 StringHasher::computeHashAndMaskTop8Bits(nullLChars, 1)); | |
| 398 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, | |
| 399 StringHasher::computeHashAndMaskTop8Bits(nullUChars, 1)); | |
| 400 | |
| 401 EXPECT_EQ(testAHash5 & 0xFFFFFF, | |
| 402 StringHasher::computeHashAndMaskTop8Bits(testALChars, 5)); | |
| 403 EXPECT_EQ(testAHash5 & 0xFFFFFF, | |
| 404 StringHasher::computeHashAndMaskTop8Bits(testAUChars, 5)); | |
| 405 EXPECT_EQ(testBHash5 & 0xFFFFFF, | |
| 406 StringHasher::computeHashAndMaskTop8Bits(testBUChars, 5)); | |
| 407 } | |
| 408 | |
| 409 TEST(StringHasherTest, StringHasher_hashMemory) { | |
| 410 EXPECT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::hashMemory(0, 0)); | |
| 411 EXPECT_EQ(emptyStringHash & 0xFFFFFF, | |
| 412 StringHasher::hashMemory(nullUChars, 0)); | |
| 413 EXPECT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::hashMemory<0>(0)); | |
| 414 EXPECT_EQ(emptyStringHash & 0xFFFFFF, | |
| 415 StringHasher::hashMemory<0>(nullUChars)); | |
| 416 | |
| 417 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, | |
| 418 StringHasher::hashMemory(nullUChars, 2)); | |
| 419 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, | |
| 420 StringHasher::hashMemory<2>(nullUChars)); | |
| 421 | |
| 422 EXPECT_EQ(testAHash5 & 0xFFFFFF, StringHasher::hashMemory(testAUChars, 10)); | |
| 423 EXPECT_EQ(testAHash5 & 0xFFFFFF, StringHasher::hashMemory<10>(testAUChars)); | |
| 424 EXPECT_EQ(testBHash5 & 0xFFFFFF, StringHasher::hashMemory(testBUChars, 10)); | |
| 425 EXPECT_EQ(testBHash5 & 0xFFFFFF, StringHasher::hashMemory<10>(testBUChars)); | |
| 426 } | |
| 427 | |
| 428 } // namespace WTF | |
| OLD | NEW |