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(WTF, StringHasher) 56 { 57 StringHasher hasher; 58 59 // The initial state of the hasher. 60 ASSERT_EQ(emptyStringHash, hasher.hash()); 61 ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 62 } 63 64 TEST(WTF, StringHasher_addCharacter) 65 { 66 StringHasher hasher; 67 68 // Hashing a single character. 69 hasher = StringHasher(); 70 hasher.addCharacter(0); 71 ASSERT_EQ(singleNullCharacterHash, hasher.hash()); 72 ASSERT_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 ASSERT_EQ(testAHash1, hasher.hash()); 78 ASSERT_EQ(testAHash1 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 79 hasher.addCharacter(testAUChars[1]); 80 ASSERT_EQ(testAHash2, hasher.hash()); 81 ASSERT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 82 hasher.addCharacter(testAUChars[2]); 83 ASSERT_EQ(testAHash3, hasher.hash()); 84 ASSERT_EQ(testAHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 85 hasher.addCharacter(testAUChars[3]); 86 ASSERT_EQ(testAHash4, hasher.hash()); 87 ASSERT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 88 hasher.addCharacter(testAUChars[4]); 89 ASSERT_EQ(testAHash5, hasher.hash()); 90 ASSERT_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 ASSERT_EQ(testBHash1, hasher.hash()); 96 ASSERT_EQ(testBHash1 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 97 hasher.addCharacter(testBUChars[1]); 98 ASSERT_EQ(testBHash2, hasher.hash()); 99 ASSERT_EQ(testBHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 100 hasher.addCharacter(testBUChars[2]); 101 ASSERT_EQ(testBHash3, hasher.hash()); 102 ASSERT_EQ(testBHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 103 hasher.addCharacter(testBUChars[3]); 104 ASSERT_EQ(testBHash4, hasher.hash()); 105 ASSERT_EQ(testBHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 106 hasher.addCharacter(testBUChars[4]); 107 ASSERT_EQ(testBHash5, hasher.hash()); 108 ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 109 } 110 111 TEST(WTF, StringHasher_addCharacters) 112 { 113 StringHasher hasher; 114 115 // Hashing zero characters. 116 hasher = StringHasher(); 117 hasher.addCharacters(static_cast<LChar*>(0), 0); 118 ASSERT_EQ(emptyStringHash, hasher.hash()); 119 ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 120 hasher = StringHasher(); 121 hasher.addCharacters(nullLChars, 0); 122 ASSERT_EQ(emptyStringHash, hasher.hash()); 123 ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 124 hasher = StringHasher(); 125 hasher.addCharacters(nullLChars); 126 ASSERT_EQ(emptyStringHash, hasher.hash()); 127 ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 128 hasher = StringHasher(); 129 hasher.addCharacters(static_cast<UChar*>(0), 0); 130 ASSERT_EQ(emptyStringHash, hasher.hash()); 131 ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 132 hasher = StringHasher(); 133 hasher.addCharacters(nullUChars, 0); 134 ASSERT_EQ(emptyStringHash, hasher.hash()); 135 ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 136 hasher = StringHasher(); 137 hasher.addCharacters(nullUChars); 138 ASSERT_EQ(emptyStringHash, hasher.hash()); 139 ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 140 141 // Hashing one character. 142 hasher = StringHasher(); 143 hasher.addCharacters(nullLChars, 1); 144 ASSERT_EQ(singleNullCharacterHash, hasher.hash()); 145 ASSERT_EQ(singleNullCharacterHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 146 hasher = StringHasher(); 147 hasher.addCharacters(nullUChars, 1); 148 ASSERT_EQ(singleNullCharacterHash, hasher.hash()); 149 ASSERT_EQ(singleNullCharacterHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 150 151 // Hashing five characters, all at once. 152 hasher = StringHasher(); 153 hasher.addCharacters(testALChars, 5); 154 ASSERT_EQ(testAHash5, hasher.hash()); 155 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 156 hasher = StringHasher(); 157 hasher.addCharacters(testALChars); 158 ASSERT_EQ(testAHash5, hasher.hash()); 159 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 160 hasher = StringHasher(); 161 hasher.addCharacters(testAUChars, 5); 162 ASSERT_EQ(testAHash5, hasher.hash()); 163 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 164 hasher = StringHasher(); 165 hasher.addCharacters(testAUChars); 166 ASSERT_EQ(testAHash5, hasher.hash()); 167 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 168 hasher = StringHasher(); 169 hasher.addCharacters(testBUChars, 5); 170 ASSERT_EQ(testBHash5, hasher.hash()); 171 ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 172 hasher = StringHasher(); 173 hasher.addCharacters(testBUChars); 174 ASSERT_EQ(testBHash5, hasher.hash()); 175 ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 176 177 // Hashing five characters, in groups of two, then the last one. 178 hasher = StringHasher(); 179 hasher.addCharacters(testALChars, 2); 180 ASSERT_EQ(testAHash2, hasher.hash()); 181 ASSERT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 182 hasher.addCharacters(testALChars + 2, 2); 183 ASSERT_EQ(testAHash4, hasher.hash()); 184 ASSERT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 185 hasher.addCharacters(testALChars + 4, 1); 186 ASSERT_EQ(testAHash5, hasher.hash()); 187 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 188 hasher = StringHasher(); 189 hasher.addCharacters(testALChars, 2); 190 hasher.addCharacters(testALChars + 2, 2); 191 hasher.addCharacters(testALChars + 4); 192 ASSERT_EQ(testAHash5, hasher.hash()); 193 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 194 hasher = StringHasher(); 195 hasher.addCharacters(testAUChars, 2); 196 ASSERT_EQ(testAHash2, hasher.hash()); 197 ASSERT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 198 hasher.addCharacters(testAUChars + 2, 2); 199 ASSERT_EQ(testAHash4, hasher.hash()); 200 ASSERT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 201 hasher.addCharacters(testAUChars + 4, 1); 202 ASSERT_EQ(testAHash5, hasher.hash()); 203 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 204 hasher = StringHasher(); 205 hasher.addCharacters(testAUChars, 2); 206 hasher.addCharacters(testAUChars + 2, 2); 207 hasher.addCharacters(testAUChars + 4); 208 ASSERT_EQ(testAHash5, hasher.hash()); 209 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 210 hasher = StringHasher(); 211 hasher.addCharacters(testBUChars, 2); 212 ASSERT_EQ(testBHash2, hasher.hash()); 213 ASSERT_EQ(testBHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 214 hasher.addCharacters(testBUChars + 2, 2); 215 ASSERT_EQ(testBHash4, hasher.hash()); 216 ASSERT_EQ(testBHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 217 hasher.addCharacters(testBUChars + 4, 1); 218 ASSERT_EQ(testBHash5, hasher.hash()); 219 ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 220 hasher = StringHasher(); 221 hasher.addCharacters(testBUChars, 2); 222 hasher.addCharacters(testBUChars + 2, 2); 223 hasher.addCharacters(testBUChars + 4); 224 ASSERT_EQ(testBHash5, hasher.hash()); 225 ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 226 227 // Hashing five characters, the first three, then the last two. 228 hasher = StringHasher(); 229 hasher.addCharacters(testALChars, 3); 230 ASSERT_EQ(testAHash3, hasher.hash()); 231 ASSERT_EQ(testAHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 232 hasher.addCharacters(testALChars + 3, 2); 233 ASSERT_EQ(testAHash5, hasher.hash()); 234 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 235 hasher = StringHasher(); 236 hasher.addCharacters(testALChars, 3); 237 ASSERT_EQ(testAHash3, hasher.hash()); 238 ASSERT_EQ(testAHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 239 hasher.addCharacters(testALChars + 3); 240 ASSERT_EQ(testAHash5, hasher.hash()); 241 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 242 hasher = StringHasher(); 243 hasher.addCharacters(testAUChars, 3); 244 ASSERT_EQ(testAHash3, hasher.hash()); 245 ASSERT_EQ(testAHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 246 hasher.addCharacters(testAUChars + 3, 2); 247 ASSERT_EQ(testAHash5, hasher.hash()); 248 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 249 hasher = StringHasher(); 250 hasher.addCharacters(testAUChars, 3); 251 ASSERT_EQ(testAHash3, hasher.hash()); 252 ASSERT_EQ(testAHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 253 hasher.addCharacters(testAUChars + 3, 2); 254 ASSERT_EQ(testAHash5, hasher.hash()); 255 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 256 hasher = StringHasher(); 257 hasher.addCharacters(testBUChars, 3); 258 ASSERT_EQ(testBHash3, hasher.hash()); 259 ASSERT_EQ(testBHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 260 hasher.addCharacters(testBUChars + 3, 2); 261 ASSERT_EQ(testBHash5, hasher.hash()); 262 ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 263 hasher = StringHasher(); 264 hasher.addCharacters(testBUChars, 3); 265 hasher.addCharacters(testBUChars + 3); 266 ASSERT_EQ(testBHash5, hasher.hash()); 267 ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 268 } 269 270 TEST(WTF, StringHasher_addCharactersAssumingAligned) 271 { 272 StringHasher hasher; 273 274 // Hashing zero characters. 275 hasher = StringHasher(); 276 hasher.addCharactersAssumingAligned(static_cast<LChar*>(0), 0); 277 ASSERT_EQ(emptyStringHash, hasher.hash()); 278 ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 279 hasher = StringHasher(); 280 hasher.addCharactersAssumingAligned(nullLChars, 0); 281 ASSERT_EQ(emptyStringHash, hasher.hash()); 282 ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 283 hasher = StringHasher(); 284 hasher.addCharactersAssumingAligned(static_cast<UChar*>(0), 0); 285 ASSERT_EQ(emptyStringHash, hasher.hash()); 286 ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 287 hasher = StringHasher(); 288 hasher.addCharactersAssumingAligned(nullUChars, 0); 289 ASSERT_EQ(emptyStringHash, hasher.hash()); 290 ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 291 hasher = StringHasher(); 292 hasher.addCharactersAssumingAligned(nullUChars); 293 ASSERT_EQ(emptyStringHash, hasher.hash()); 294 ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 295 296 // Hashing one character. 297 hasher = StringHasher(); 298 hasher.addCharactersAssumingAligned(nullLChars, 1); 299 ASSERT_EQ(singleNullCharacterHash, hasher.hash()); 300 ASSERT_EQ(singleNullCharacterHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 301 hasher = StringHasher(); 302 hasher.addCharactersAssumingAligned(nullUChars, 1); 303 ASSERT_EQ(singleNullCharacterHash, hasher.hash()); 304 ASSERT_EQ(singleNullCharacterHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 305 306 // Hashing five characters, all at once. 307 hasher = StringHasher(); 308 hasher.addCharactersAssumingAligned(testALChars, 5); 309 ASSERT_EQ(testAHash5, hasher.hash()); 310 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 311 hasher = StringHasher(); 312 hasher.addCharactersAssumingAligned(testALChars); 313 ASSERT_EQ(testAHash5, hasher.hash()); 314 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 315 hasher = StringHasher(); 316 hasher.addCharactersAssumingAligned(testAUChars, 5); 317 ASSERT_EQ(testAHash5, hasher.hash()); 318 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 319 hasher = StringHasher(); 320 hasher.addCharactersAssumingAligned(testAUChars); 321 ASSERT_EQ(testAHash5, hasher.hash()); 322 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 323 hasher = StringHasher(); 324 hasher.addCharactersAssumingAligned(testBUChars, 5); 325 ASSERT_EQ(testBHash5, hasher.hash()); 326 ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 327 hasher = StringHasher(); 328 hasher.addCharactersAssumingAligned(testBUChars); 329 ASSERT_EQ(testBHash5, hasher.hash()); 330 ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 331 332 // Hashing five characters, in groups of two, then the last one. 333 hasher = StringHasher(); 334 hasher.addCharactersAssumingAligned(testALChars, 2); 335 ASSERT_EQ(testAHash2, hasher.hash()); 336 ASSERT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 337 hasher.addCharactersAssumingAligned(testALChars + 2, 2); 338 ASSERT_EQ(testAHash4, hasher.hash()); 339 ASSERT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 340 hasher.addCharactersAssumingAligned(testALChars + 4, 1); 341 ASSERT_EQ(testAHash5, hasher.hash()); 342 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 343 hasher = StringHasher(); 344 hasher.addCharactersAssumingAligned(testALChars, 2); 345 hasher.addCharactersAssumingAligned(testALChars + 2, 2); 346 hasher.addCharactersAssumingAligned(testALChars + 4); 347 ASSERT_EQ(testAHash5, hasher.hash()); 348 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 349 hasher = StringHasher(); 350 hasher.addCharactersAssumingAligned(testAUChars, 2); 351 ASSERT_EQ(testAHash2, hasher.hash()); 352 ASSERT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 353 hasher.addCharactersAssumingAligned(testAUChars + 2, 2); 354 ASSERT_EQ(testAHash4, hasher.hash()); 355 ASSERT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 356 hasher.addCharactersAssumingAligned(testAUChars + 4, 1); 357 ASSERT_EQ(testAHash5, hasher.hash()); 358 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 359 hasher = StringHasher(); 360 hasher.addCharactersAssumingAligned(testAUChars, 2); 361 hasher.addCharactersAssumingAligned(testAUChars + 2, 2); 362 hasher.addCharactersAssumingAligned(testAUChars + 4); 363 ASSERT_EQ(testAHash5, hasher.hash()); 364 ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 365 hasher = StringHasher(); 366 hasher.addCharactersAssumingAligned(testBUChars, 2); 367 ASSERT_EQ(testBHash2, hasher.hash()); 368 ASSERT_EQ(testBHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 369 hasher.addCharactersAssumingAligned(testBUChars + 2, 2); 370 ASSERT_EQ(testBHash4, hasher.hash()); 371 ASSERT_EQ(testBHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 372 hasher.addCharactersAssumingAligned(testBUChars + 4, 1); 373 ASSERT_EQ(testBHash5, hasher.hash()); 374 ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 375 hasher = StringHasher(); 376 hasher.addCharactersAssumingAligned(testBUChars, 2); 377 hasher.addCharactersAssumingAligned(testBUChars + 2, 2); 378 hasher.addCharactersAssumingAligned(testBUChars + 4); 379 ASSERT_EQ(testBHash5, hasher.hash()); 380 ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 381 382 // Hashing five characters, first two characters one at a time, 383 // then two more, then the last one. 384 hasher = StringHasher(); 385 hasher.addCharacter(testBUChars[0]); 386 ASSERT_EQ(testBHash1, hasher.hash()); 387 ASSERT_EQ(testBHash1 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 388 hasher.addCharacter(testBUChars[1]); 389 ASSERT_EQ(testBHash2, hasher.hash()); 390 ASSERT_EQ(testBHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 391 hasher.addCharactersAssumingAligned(testBUChars[2], testBUChars[3]); 392 ASSERT_EQ(testBHash4, hasher.hash()); 393 ASSERT_EQ(testBHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 394 hasher.addCharactersAssumingAligned(testBUChars + 4); 395 ASSERT_EQ(testBHash5, hasher.hash()); 396 ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked()); 397 } 398 399 TEST(WTF, StringHasher_computeHash) 400 { 401 ASSERT_EQ(emptyStringHash, StringHasher::computeHash(static_cast<LChar*>(0), 0)); 402 ASSERT_EQ(emptyStringHash, StringHasher::computeHash(nullLChars, 0)); 403 ASSERT_EQ(emptyStringHash, StringHasher::computeHash(static_cast<UChar*>(0), 0)); 404 ASSERT_EQ(emptyStringHash, StringHasher::computeHash(nullUChars, 0)); 405 406 ASSERT_EQ(singleNullCharacterHash, StringHasher::computeHash(nullLChars, 1)); 407 ASSERT_EQ(singleNullCharacterHash, StringHasher::computeHash(nullUChars, 1)); 408 409 ASSERT_EQ(testAHash5, StringHasher::computeHash(testALChars, 5)); 410 ASSERT_EQ(testAHash5, StringHasher::computeHash(testAUChars, 5)); 411 ASSERT_EQ(testBHash5, StringHasher::computeHash(testBUChars, 5)); 412 } 413 414 TEST(WTF, StringHasher_computeHashAndMaskTop8Bits) 415 { 416 ASSERT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(static_cast<LChar*>(0), 0)); 417 ASSERT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(nullLChars, 0)); 418 ASSERT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(static_cast<UChar*>(0), 0)); 419 ASSERT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(nullUChars, 0)); 420 421 ASSERT_EQ(singleNullCharacterHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(nullLChars, 1)); 422 ASSERT_EQ(singleNullCharacterHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(nullUChars, 1)); 423 424 ASSERT_EQ(testAHash5 & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(testALChars, 5)); 425 ASSERT_EQ(testAHash5 & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(testAUChars, 5)); 426 ASSERT_EQ(testBHash5 & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(testBUChars, 5)); 427 } 428 429 TEST(WTF, StringHasher_hashMemory) 430 { 431 ASSERT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::hashMemory(0, 0)); 432 ASSERT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::hashMemory(nullUChars, 0)); 433 ASSERT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::hashMemory<0>(0)); 434 ASSERT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::hashMemory<0>(nullUChars)); 435 436 ASSERT_EQ(singleNullCharacterHash & 0xFFFFFF, StringHasher::hashMemory(nullUChars, 2)); 437 ASSERT_EQ(singleNullCharacterHash & 0xFFFFFF, StringHasher::hashMemory<2>(nullUChars)); 438 439 ASSERT_EQ(testAHash5 & 0xFFFFFF, StringHasher::hashMemory(testAUChars, 10)); 440 ASSERT_EQ(testAHash5 & 0xFFFFFF, StringHasher::hashMemory<10>(testAUChars)); 441 ASSERT_EQ(testBHash5 & 0xFFFFFF, StringHasher::hashMemory(testBUChars, 10)); 442 ASSERT_EQ(testBHash5 & 0xFFFFFF, StringHasher::hashMemory<10>(testBUChars)); 443 } 444 445 } // namespace 446