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 "config.h" 27 28 #include "wtf/StringHasher.h" 29 #include <gtest/gtest.h> 30 31 namespace { 32 33 static const LChar nullLChars[2] = { 0, 0 }; 34 static const UChar nullUChars[2] = { 0, 0 }; 35 36 static const unsigned emptyStringHash = 0x4EC889EU; 37 static const unsigned singleNullCharacterHash = 0x3D3ABF44U; 38 39 static const LChar testALChars[6] = { 0x41, 0x95, 0xFF, 0x50, 0x01, 0 }; 40 static const UChar testAUChars[6] = { 0x41, 0x95, 0xFF, 0x50, 0x01, 0 }; 41 static const UChar testBUChars[6] = { 0x41, 0x95, 0xFFFF, 0x1080, 0x01, 0 }; 42 43 static const unsigned testAHash1 = 0xEA32B004; 44 static const unsigned testAHash2 = 0x93F0F71E; 45 static const unsigned testAHash3 = 0xCB609EB1; 46 static const unsigned testAHash4 = 0x7984A706; 47 static const unsigned testAHash5 = 0x0427561F; 48 49 static const unsigned testBHash1 = 0xEA32B004; 50 static const unsigned testBHash2 = 0x93F0F71E; 51 static const unsigned testBHash3 = 0x59EB1B2C; 52 static const unsigned testBHash4 = 0xA7BCCC0A; 53 static const unsigned testBHash5 = 0x79201649; 54 55 TEST(StringHasherTest, StringHasher) 56 { 57 StringHasher hasher; 58 59 // The initial state of the hasher. 60 EXPECT_EQ(emptyStringHash, hasher.hash()); 61 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 62 } 63 64 TEST(StringHasherTest, StringHasher_addCharacter) 65 { 66 StringHasher hasher; 67 68 // Hashing a single character. 69 hasher = StringHasher(); 70 hasher.addCharacter(0); 71 EXPECT_EQ(singleNullCharacterHash, hasher.hash()); 72 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 73 74 // Hashing five characters, checking the intermediate state after each is added. 75 hasher = StringHasher(); 76 hasher.addCharacter(testAUChars[0]); 77 EXPECT_EQ(testAHash1, hasher.hash()); 78 EXPECT_EQ(testAHash1 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 79 hasher.addCharacter(testAUChars[1]); 80 EXPECT_EQ(testAHash2, hasher.hash()); 81 EXPECT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 82 hasher.addCharacter(testAUChars[2]); 83 EXPECT_EQ(testAHash3, hasher.hash()); 84 EXPECT_EQ(testAHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 85 hasher.addCharacter(testAUChars[3]); 86 EXPECT_EQ(testAHash4, hasher.hash()); 87 EXPECT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 88 hasher.addCharacter(testAUChars[4]); 89 EXPECT_EQ(testAHash5, hasher.hash()); 90 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 91 92 // Hashing a second set of five characters, including non-Latin-1 characters. 93 hasher = StringHasher(); 94 hasher.addCharacter(testBUChars[0]); 95 EXPECT_EQ(testBHash1, hasher.hash()); 96 EXPECT_EQ(testBHash1 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 97 hasher.addCharacter(testBUChars[1]); 98 EXPECT_EQ(testBHash2, hasher.hash()); 99 EXPECT_EQ(testBHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 100 hasher.addCharacter(testBUChars[2]); 101 EXPECT_EQ(testBHash3, hasher.hash()); 102 EXPECT_EQ(testBHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 103 hasher.addCharacter(testBUChars[3]); 104 EXPECT_EQ(testBHash4, hasher.hash()); 105 EXPECT_EQ(testBHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 106 hasher.addCharacter(testBUChars[4]); 107 EXPECT_EQ(testBHash5, hasher.hash()); 108 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 109 } 110 111 TEST(StringHasherTest, StringHasher_addCharacters) 112 { 113 StringHasher hasher; 114 115 // Hashing zero characters. 116 hasher = StringHasher(); 117 hasher.addCharacters(static_cast<LChar*>(0), 0); 118 EXPECT_EQ(emptyStringHash, hasher.hash()); 119 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 120 hasher = StringHasher(); 121 hasher.addCharacters(nullLChars, 0); 122 EXPECT_EQ(emptyStringHash, hasher.hash()); 123 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 124 hasher = StringHasher(); 125 hasher.addCharacters(static_cast<UChar*>(0), 0); 126 EXPECT_EQ(emptyStringHash, hasher.hash()); 127 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 128 hasher = StringHasher(); 129 hasher.addCharacters(nullUChars, 0); 130 EXPECT_EQ(emptyStringHash, hasher.hash()); 131 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 132 133 // Hashing one character. 134 hasher = StringHasher(); 135 hasher.addCharacters(nullLChars, 1); 136 EXPECT_EQ(singleNullCharacterHash, hasher.hash()); 137 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 138 hasher = StringHasher(); 139 hasher.addCharacters(nullUChars, 1); 140 EXPECT_EQ(singleNullCharacterHash, hasher.hash()); 141 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 142 143 // Hashing five characters, all at once. 144 hasher = StringHasher(); 145 hasher.addCharacters(testALChars, 5); 146 EXPECT_EQ(testAHash5, hasher.hash()); 147 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 148 hasher = StringHasher(); 149 hasher.addCharacters(testAUChars, 5); 150 EXPECT_EQ(testAHash5, hasher.hash()); 151 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 152 hasher = StringHasher(); 153 hasher.addCharacters(testBUChars, 5); 154 EXPECT_EQ(testBHash5, hasher.hash()); 155 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 156 157 // Hashing five characters, in groups of two, then the last one. 158 hasher = StringHasher(); 159 hasher.addCharacters(testALChars, 2); 160 EXPECT_EQ(testAHash2, hasher.hash()); 161 EXPECT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 162 hasher.addCharacters(testALChars + 2, 2); 163 EXPECT_EQ(testAHash4, hasher.hash()); 164 EXPECT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 165 hasher.addCharacters(testALChars + 4, 1); 166 EXPECT_EQ(testAHash5, hasher.hash()); 167 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 168 hasher = StringHasher(); 169 hasher.addCharacters(testALChars, 2); 170 hasher.addCharacters(testALChars + 2, 2); 171 hasher.addCharacters(testALChars + 4, 1); 172 EXPECT_EQ(testAHash5, hasher.hash()); 173 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 174 hasher = StringHasher(); 175 hasher.addCharacters(testAUChars, 2); 176 EXPECT_EQ(testAHash2, hasher.hash()); 177 EXPECT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 178 hasher.addCharacters(testAUChars + 2, 2); 179 EXPECT_EQ(testAHash4, hasher.hash()); 180 EXPECT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 181 hasher.addCharacters(testAUChars + 4, 1); 182 EXPECT_EQ(testAHash5, hasher.hash()); 183 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 184 hasher = StringHasher(); 185 hasher.addCharacters(testAUChars, 2); 186 hasher.addCharacters(testAUChars + 2, 2); 187 hasher.addCharacters(testAUChars + 4, 1); 188 EXPECT_EQ(testAHash5, hasher.hash()); 189 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 190 hasher = StringHasher(); 191 hasher.addCharacters(testBUChars, 2); 192 EXPECT_EQ(testBHash2, hasher.hash()); 193 EXPECT_EQ(testBHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 194 hasher.addCharacters(testBUChars + 2, 2); 195 EXPECT_EQ(testBHash4, hasher.hash()); 196 EXPECT_EQ(testBHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 197 hasher.addCharacters(testBUChars + 4, 1); 198 EXPECT_EQ(testBHash5, hasher.hash()); 199 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 200 hasher = StringHasher(); 201 hasher.addCharacters(testBUChars, 2); 202 hasher.addCharacters(testBUChars + 2, 2); 203 hasher.addCharacters(testBUChars + 4, 1); 204 EXPECT_EQ(testBHash5, hasher.hash()); 205 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 206 207 // Hashing five characters, the first three, then the last two. 208 hasher = StringHasher(); 209 hasher.addCharacters(testALChars, 3); 210 EXPECT_EQ(testAHash3, hasher.hash()); 211 EXPECT_EQ(testAHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 212 hasher.addCharacters(testALChars + 3, 2); 213 EXPECT_EQ(testAHash5, hasher.hash()); 214 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 215 hasher = StringHasher(); 216 hasher.addCharacters(testALChars, 3); 217 EXPECT_EQ(testAHash3, hasher.hash()); 218 EXPECT_EQ(testAHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 219 hasher.addCharacters(testALChars + 3, 2); 220 EXPECT_EQ(testAHash5, hasher.hash()); 221 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 222 hasher = StringHasher(); 223 hasher.addCharacters(testAUChars, 3); 224 EXPECT_EQ(testAHash3, hasher.hash()); 225 EXPECT_EQ(testAHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 226 hasher.addCharacters(testAUChars + 3, 2); 227 EXPECT_EQ(testAHash5, hasher.hash()); 228 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 229 hasher = StringHasher(); 230 hasher.addCharacters(testAUChars, 3); 231 EXPECT_EQ(testAHash3, hasher.hash()); 232 EXPECT_EQ(testAHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 233 hasher.addCharacters(testAUChars + 3, 2); 234 EXPECT_EQ(testAHash5, hasher.hash()); 235 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 236 hasher = StringHasher(); 237 hasher.addCharacters(testBUChars, 3); 238 EXPECT_EQ(testBHash3, hasher.hash()); 239 EXPECT_EQ(testBHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 240 hasher.addCharacters(testBUChars + 3, 2); 241 EXPECT_EQ(testBHash5, hasher.hash()); 242 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 243 hasher = StringHasher(); 244 hasher.addCharacters(testBUChars, 3); 245 hasher.addCharacters(testBUChars + 3, 2); 246 EXPECT_EQ(testBHash5, hasher.hash()); 247 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 248 } 249 250 TEST(StringHasherTest, StringHasher_addCharactersAssumingAligned) 251 { 252 StringHasher hasher; 253 254 // Hashing zero characters. 255 hasher = StringHasher(); 256 hasher.addCharactersAssumingAligned(static_cast<LChar*>(0), 0); 257 EXPECT_EQ(emptyStringHash, hasher.hash()); 258 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 259 hasher = StringHasher(); 260 hasher.addCharactersAssumingAligned(nullLChars, 0); 261 EXPECT_EQ(emptyStringHash, hasher.hash()); 262 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 263 hasher = StringHasher(); 264 hasher.addCharactersAssumingAligned(static_cast<UChar*>(0), 0); 265 EXPECT_EQ(emptyStringHash, hasher.hash()); 266 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 267 hasher = StringHasher(); 268 hasher.addCharactersAssumingAligned(nullUChars, 0); 269 EXPECT_EQ(emptyStringHash, hasher.hash()); 270 EXPECT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 271 272 // Hashing one character. 273 hasher = StringHasher(); 274 hasher.addCharactersAssumingAligned(nullLChars, 1); 275 EXPECT_EQ(singleNullCharacterHash, hasher.hash()); 276 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 277 hasher = StringHasher(); 278 hasher.addCharactersAssumingAligned(nullUChars, 1); 279 EXPECT_EQ(singleNullCharacterHash, hasher.hash()); 280 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 281 282 // Hashing five characters, all at once. 283 hasher = StringHasher(); 284 hasher.addCharactersAssumingAligned(testALChars, 5); 285 EXPECT_EQ(testAHash5, hasher.hash()); 286 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 287 hasher = StringHasher(); 288 hasher.addCharactersAssumingAligned(testAUChars, 5); 289 EXPECT_EQ(testAHash5, hasher.hash()); 290 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 291 hasher = StringHasher(); 292 hasher.addCharactersAssumingAligned(testBUChars, 5); 293 EXPECT_EQ(testBHash5, hasher.hash()); 294 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 295 296 // Hashing five characters, in groups of two, then the last one. 297 hasher = StringHasher(); 298 hasher.addCharactersAssumingAligned(testALChars, 2); 299 EXPECT_EQ(testAHash2, hasher.hash()); 300 EXPECT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 301 hasher.addCharactersAssumingAligned(testALChars + 2, 2); 302 EXPECT_EQ(testAHash4, hasher.hash()); 303 EXPECT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 304 hasher.addCharactersAssumingAligned(testALChars + 4, 1); 305 EXPECT_EQ(testAHash5, hasher.hash()); 306 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 307 hasher = StringHasher(); 308 hasher.addCharactersAssumingAligned(testALChars, 2); 309 hasher.addCharactersAssumingAligned(testALChars + 2, 2); 310 hasher.addCharactersAssumingAligned(testALChars + 4, 1); 311 EXPECT_EQ(testAHash5, hasher.hash()); 312 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 313 hasher = StringHasher(); 314 hasher.addCharactersAssumingAligned(testAUChars, 2); 315 EXPECT_EQ(testAHash2, hasher.hash()); 316 EXPECT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 317 hasher.addCharactersAssumingAligned(testAUChars + 2, 2); 318 EXPECT_EQ(testAHash4, hasher.hash()); 319 EXPECT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 320 hasher.addCharactersAssumingAligned(testAUChars + 4, 1); 321 EXPECT_EQ(testAHash5, hasher.hash()); 322 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 323 hasher = StringHasher(); 324 hasher.addCharactersAssumingAligned(testAUChars, 2); 325 hasher.addCharactersAssumingAligned(testAUChars + 2, 2); 326 hasher.addCharactersAssumingAligned(testAUChars + 4, 1); 327 EXPECT_EQ(testAHash5, hasher.hash()); 328 EXPECT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 329 hasher = StringHasher(); 330 hasher.addCharactersAssumingAligned(testBUChars, 2); 331 EXPECT_EQ(testBHash2, hasher.hash()); 332 EXPECT_EQ(testBHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 333 hasher.addCharactersAssumingAligned(testBUChars + 2, 2); 334 EXPECT_EQ(testBHash4, hasher.hash()); 335 EXPECT_EQ(testBHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 336 hasher.addCharactersAssumingAligned(testBUChars + 4, 1); 337 EXPECT_EQ(testBHash5, hasher.hash()); 338 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 339 hasher = StringHasher(); 340 hasher.addCharactersAssumingAligned(testBUChars, 2); 341 hasher.addCharactersAssumingAligned(testBUChars + 2, 2); 342 hasher.addCharactersAssumingAligned(testBUChars + 4, 1); 343 EXPECT_EQ(testBHash5, hasher.hash()); 344 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 345 346 // Hashing five characters, first two characters one at a time, 347 // then two more, then the last one. 348 hasher = StringHasher(); 349 hasher.addCharacter(testBUChars[0]); 350 EXPECT_EQ(testBHash1, hasher.hash()); 351 EXPECT_EQ(testBHash1 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 352 hasher.addCharacter(testBUChars[1]); 353 EXPECT_EQ(testBHash2, hasher.hash()); 354 EXPECT_EQ(testBHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 355 hasher.addCharactersAssumingAligned(testBUChars[2], testBUChars[3]); 356 EXPECT_EQ(testBHash4, hasher.hash()); 357 EXPECT_EQ(testBHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 358 hasher.addCharactersAssumingAligned(testBUChars + 4, 1); 359 EXPECT_EQ(testBHash5, hasher.hash()); 360 EXPECT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 361 } 362 363 TEST(StringHasherTest, StringHasher_computeHash) 364 { 365 EXPECT_EQ(emptyStringHash, StringHasher::computeHash(static_cast<LChar*>(0), 0)); 366 EXPECT_EQ(emptyStringHash, StringHasher::computeHash(nullLChars, 0)); 367 EXPECT_EQ(emptyStringHash, StringHasher::computeHash(static_cast<UChar*>(0), 0)); 368 EXPECT_EQ(emptyStringHash, StringHasher::computeHash(nullUChars, 0)); 369 370 EXPECT_EQ(singleNullCharacterHash, StringHasher::computeHash(nullLChars, 1)); 371 EXPECT_EQ(singleNullCharacterHash, StringHasher::computeHash(nullUChars, 1)); 372 373 EXPECT_EQ(testAHash5, StringHasher::computeHash(testALChars, 5)); 374 EXPECT_EQ(testAHash5, StringHasher::computeHash(testAUChars, 5)); 375 EXPECT_EQ(testBHash5, StringHasher::computeHash(testBUChars, 5)); 376 } 377 378 TEST(StringHasherTest, StringHasher_computeHashAndMaskTop8Bits) 379 { 380 EXPECT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(static_cast<LChar*>(0), 0)); 381 EXPECT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(nullLChars, 0)); 382 EXPECT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(static_cast<UChar*>(0), 0)); 383 EXPECT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(nullUChars, 0)); 384 385 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(nullLChars, 1)); 386 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(nullUChars, 1)); 387 388 EXPECT_EQ(testAHash5 & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(testALChars, 5)); 389 EXPECT_EQ(testAHash5 & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(testAUChars, 5)); 390 EXPECT_EQ(testBHash5 & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(testBUChars, 5)); 391 } 392 393 TEST(StringHasherTest, StringHasher_hashMemory) 394 { 395 EXPECT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::hashMemory(0, 0)); 396 EXPECT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::hashMemory(nullUChars, 0)); 397 EXPECT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::hashMemory<0>(0)); 398 EXPECT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::hashMemory<0>(nullUChars)); 399 400 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, StringHasher::hashMemory(nullUChars, 2)); 401 EXPECT_EQ(singleNullCharacterHash & 0xFFFFFF, StringHasher::hashMemory<2>(nullUChars)); 402 403 EXPECT_EQ(testAHash5 & 0xFFFFFF, StringHasher::hashMemory(testAUChars, 10)); 404 EXPECT_EQ(testAHash5 & 0xFFFFFF, StringHasher::hashMemory<10>(testAUChars)); 405 EXPECT_EQ(testBHash5 & 0xFFFFFF, StringHasher::hashMemory(testBUChars, 10)); 406 EXPECT_EQ(testBHash5 & 0xFFFFFF, StringHasher::hashMemory<10>(testBUChars)); 407 } 408 409 } // namespace 410