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