[chromium] TestWebKitAPI WTF.StringHasher_addCharacters is broken on Chromium Android
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WTF / StringHasher.cpp
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
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[5] = { 0x41, 0x95, 0xFFFF, 0x1080, 0x01 };
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 #if OS(ANDROID)
111 // Failing on Android. http://webkit.org/b/111284
112 # define MAYBE_StringHasher_addCharacters DISABLED_StringHasher_addCharacters
113 #else
114 # define MAYBE_StringHasher_addCharacters StringHasher_addCharacters
115 #endif
116
117 TEST(WTF, MAYBE_StringHasher_addCharacters)
118 {
119     StringHasher hasher;
120
121     // Hashing zero characters.
122     hasher = StringHasher();
123     hasher.addCharacters(static_cast<LChar*>(0), 0);
124     ASSERT_EQ(emptyStringHash, hasher.hash());
125     ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
126     hasher = StringHasher();
127     hasher.addCharacters(nullLChars, 0);
128     ASSERT_EQ(emptyStringHash, hasher.hash());
129     ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
130     hasher = StringHasher();
131     hasher.addCharacters(nullLChars);
132     ASSERT_EQ(emptyStringHash, hasher.hash());
133     ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
134     hasher = StringHasher();
135     hasher.addCharacters(static_cast<UChar*>(0), 0);
136     ASSERT_EQ(emptyStringHash, hasher.hash());
137     ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
138     hasher = StringHasher();
139     hasher.addCharacters(nullUChars, 0);
140     ASSERT_EQ(emptyStringHash, hasher.hash());
141     ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
142     hasher = StringHasher();
143     hasher.addCharacters(nullUChars);
144     ASSERT_EQ(emptyStringHash, hasher.hash());
145     ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
146
147     // Hashing one character.
148     hasher = StringHasher();
149     hasher.addCharacters(nullLChars, 1);
150     ASSERT_EQ(singleNullCharacterHash, hasher.hash());
151     ASSERT_EQ(singleNullCharacterHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
152     hasher = StringHasher();
153     hasher.addCharacters(nullUChars, 1);
154     ASSERT_EQ(singleNullCharacterHash, hasher.hash());
155     ASSERT_EQ(singleNullCharacterHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
156
157     // Hashing five characters, all at once.
158     hasher = StringHasher();
159     hasher.addCharacters(testALChars, 5);
160     ASSERT_EQ(testAHash5, hasher.hash());
161     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
162     hasher = StringHasher();
163     hasher.addCharacters(testALChars);
164     ASSERT_EQ(testAHash5, hasher.hash());
165     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
166     hasher = StringHasher();
167     hasher.addCharacters(testAUChars, 5);
168     ASSERT_EQ(testAHash5, hasher.hash());
169     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
170     hasher = StringHasher();
171     hasher.addCharacters(testAUChars);
172     ASSERT_EQ(testAHash5, hasher.hash());
173     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
174     hasher = StringHasher();
175     hasher.addCharacters(testBUChars, 5);
176     ASSERT_EQ(testBHash5, hasher.hash());
177     ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
178     hasher = StringHasher();
179     hasher.addCharacters(testBUChars);
180     ASSERT_EQ(testBHash5, hasher.hash());
181     ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
182
183     // Hashing five characters, in groups of two, then the last one.
184     hasher = StringHasher();
185     hasher.addCharacters(testALChars, 2);
186     ASSERT_EQ(testAHash2, hasher.hash());
187     ASSERT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
188     hasher.addCharacters(testALChars + 2, 2);
189     ASSERT_EQ(testAHash4, hasher.hash());
190     ASSERT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
191     hasher.addCharacters(testALChars + 4, 1);
192     ASSERT_EQ(testAHash5, hasher.hash());
193     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
194     hasher = StringHasher();
195     hasher.addCharacters(testALChars, 2);
196     hasher.addCharacters(testALChars + 2, 2);
197     hasher.addCharacters(testALChars + 4);
198     ASSERT_EQ(testAHash5, hasher.hash());
199     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
200     hasher = StringHasher();
201     hasher.addCharacters(testAUChars, 2);
202     ASSERT_EQ(testAHash2, hasher.hash());
203     ASSERT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
204     hasher.addCharacters(testAUChars + 2, 2);
205     ASSERT_EQ(testAHash4, hasher.hash());
206     ASSERT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
207     hasher.addCharacters(testAUChars + 4, 1);
208     ASSERT_EQ(testAHash5, hasher.hash());
209     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
210     hasher = StringHasher();
211     hasher.addCharacters(testAUChars, 2);
212     hasher.addCharacters(testAUChars + 2, 2);
213     hasher.addCharacters(testAUChars + 4);
214     ASSERT_EQ(testAHash5, hasher.hash());
215     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
216     hasher = StringHasher();
217     hasher.addCharacters(testBUChars, 2);
218     ASSERT_EQ(testBHash2, hasher.hash());
219     ASSERT_EQ(testBHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
220     hasher.addCharacters(testBUChars + 2, 2);
221     ASSERT_EQ(testBHash4, hasher.hash());
222     ASSERT_EQ(testBHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
223     hasher.addCharacters(testBUChars + 4, 1);
224     ASSERT_EQ(testBHash5, hasher.hash());
225     ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
226     hasher = StringHasher();
227     hasher.addCharacters(testBUChars, 2);
228     hasher.addCharacters(testBUChars + 2, 2);
229     hasher.addCharacters(testBUChars + 4);
230     ASSERT_EQ(testBHash5, hasher.hash());
231     ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
232
233     // Hashing five characters, the first three, then the last two.
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, 2);
239     ASSERT_EQ(testAHash5, hasher.hash());
240     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
241     hasher = StringHasher();
242     hasher.addCharacters(testALChars, 3);
243     ASSERT_EQ(testAHash3, hasher.hash());
244     ASSERT_EQ(testAHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
245     hasher.addCharacters(testALChars + 3);
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(testAUChars, 3);
257     ASSERT_EQ(testAHash3, hasher.hash());
258     ASSERT_EQ(testAHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
259     hasher.addCharacters(testAUChars + 3, 2);
260     ASSERT_EQ(testAHash5, hasher.hash());
261     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
262     hasher = StringHasher();
263     hasher.addCharacters(testBUChars, 3);
264     ASSERT_EQ(testBHash3, hasher.hash());
265     ASSERT_EQ(testBHash3 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
266     hasher.addCharacters(testBUChars + 3, 2);
267     ASSERT_EQ(testBHash5, hasher.hash());
268     ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
269     hasher = StringHasher();
270     hasher.addCharacters(testBUChars, 3);
271     hasher.addCharacters(testBUChars + 3);
272     ASSERT_EQ(testBHash5, hasher.hash());
273     ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
274 }
275
276 #if OS(ANDROID)
277 // Failing on Android. http://webkit.org/b/111284
278 # define MAYBE_StringHasher_addCharactersAssumingAligned DISABLED_StringHasher_addCharactersAssumingAligned
279 #else
280 # define MAYBE_StringHasher_addCharactersAssumingAligned StringHasher_addCharactersAssumingAligned
281 #endif
282
283 TEST(WTF, MAYBE_StringHasher_addCharactersAssumingAligned)
284 {
285     StringHasher hasher;
286
287     // Hashing zero characters.
288     hasher = StringHasher();
289     hasher.addCharactersAssumingAligned(static_cast<LChar*>(0), 0);
290     ASSERT_EQ(emptyStringHash, hasher.hash());
291     ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
292     hasher = StringHasher();
293     hasher.addCharactersAssumingAligned(nullLChars, 0);
294     ASSERT_EQ(emptyStringHash, hasher.hash());
295     ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
296     hasher = StringHasher();
297     hasher.addCharactersAssumingAligned(static_cast<UChar*>(0), 0);
298     ASSERT_EQ(emptyStringHash, hasher.hash());
299     ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
300     hasher = StringHasher();
301     hasher.addCharactersAssumingAligned(nullUChars, 0);
302     ASSERT_EQ(emptyStringHash, hasher.hash());
303     ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
304     hasher = StringHasher();
305     hasher.addCharactersAssumingAligned(nullUChars);
306     ASSERT_EQ(emptyStringHash, hasher.hash());
307     ASSERT_EQ(emptyStringHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
308
309     // Hashing one character.
310     hasher = StringHasher();
311     hasher.addCharactersAssumingAligned(nullLChars, 1);
312     ASSERT_EQ(singleNullCharacterHash, hasher.hash());
313     ASSERT_EQ(singleNullCharacterHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
314     hasher = StringHasher();
315     hasher.addCharactersAssumingAligned(nullUChars, 1);
316     ASSERT_EQ(singleNullCharacterHash, hasher.hash());
317     ASSERT_EQ(singleNullCharacterHash & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
318
319     // Hashing five characters, all at once.
320     hasher = StringHasher();
321     hasher.addCharactersAssumingAligned(testALChars, 5);
322     ASSERT_EQ(testAHash5, hasher.hash());
323     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
324     hasher = StringHasher();
325     hasher.addCharactersAssumingAligned(testALChars);
326     ASSERT_EQ(testAHash5, hasher.hash());
327     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
328     hasher = StringHasher();
329     hasher.addCharactersAssumingAligned(testAUChars, 5);
330     ASSERT_EQ(testAHash5, hasher.hash());
331     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
332     hasher = StringHasher();
333     hasher.addCharactersAssumingAligned(testAUChars);
334     ASSERT_EQ(testAHash5, hasher.hash());
335     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
336     hasher = StringHasher();
337     hasher.addCharactersAssumingAligned(testBUChars, 5);
338     ASSERT_EQ(testBHash5, hasher.hash());
339     ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
340     hasher = StringHasher();
341     hasher.addCharactersAssumingAligned(testBUChars);
342     ASSERT_EQ(testBHash5, hasher.hash());
343     ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
344
345     // Hashing five characters, in groups of two, then the last one.
346     hasher = StringHasher();
347     hasher.addCharactersAssumingAligned(testALChars, 2);
348     ASSERT_EQ(testAHash2, hasher.hash());
349     ASSERT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
350     hasher.addCharactersAssumingAligned(testALChars + 2, 2);
351     ASSERT_EQ(testAHash4, hasher.hash());
352     ASSERT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
353     hasher.addCharactersAssumingAligned(testALChars + 4, 1);
354     ASSERT_EQ(testAHash5, hasher.hash());
355     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
356     hasher = StringHasher();
357     hasher.addCharactersAssumingAligned(testALChars, 2);
358     hasher.addCharactersAssumingAligned(testALChars + 2, 2);
359     hasher.addCharactersAssumingAligned(testALChars + 4);
360     ASSERT_EQ(testAHash5, hasher.hash());
361     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
362     hasher = StringHasher();
363     hasher.addCharactersAssumingAligned(testAUChars, 2);
364     ASSERT_EQ(testAHash2, hasher.hash());
365     ASSERT_EQ(testAHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
366     hasher.addCharactersAssumingAligned(testAUChars + 2, 2);
367     ASSERT_EQ(testAHash4, hasher.hash());
368     ASSERT_EQ(testAHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
369     hasher.addCharactersAssumingAligned(testAUChars + 4, 1);
370     ASSERT_EQ(testAHash5, hasher.hash());
371     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
372     hasher = StringHasher();
373     hasher.addCharactersAssumingAligned(testAUChars, 2);
374     hasher.addCharactersAssumingAligned(testAUChars + 2, 2);
375     hasher.addCharactersAssumingAligned(testAUChars + 4);
376     ASSERT_EQ(testAHash5, hasher.hash());
377     ASSERT_EQ(testAHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
378     hasher = StringHasher();
379     hasher.addCharactersAssumingAligned(testBUChars, 2);
380     ASSERT_EQ(testBHash2, hasher.hash());
381     ASSERT_EQ(testBHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
382     hasher.addCharactersAssumingAligned(testBUChars + 2, 2);
383     ASSERT_EQ(testBHash4, hasher.hash());
384     ASSERT_EQ(testBHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
385     hasher.addCharactersAssumingAligned(testBUChars + 4, 1);
386     ASSERT_EQ(testBHash5, hasher.hash());
387     ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
388     hasher = StringHasher();
389     hasher.addCharactersAssumingAligned(testBUChars, 2);
390     hasher.addCharactersAssumingAligned(testBUChars + 2, 2);
391     hasher.addCharactersAssumingAligned(testBUChars + 4);
392     ASSERT_EQ(testBHash5, hasher.hash());
393     ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
394
395     // Hashing five characters, first two characters one at a time,
396     // then two more, then the last one.
397     hasher = StringHasher();
398     hasher.addCharacter(testBUChars[0]);
399     ASSERT_EQ(testBHash1, hasher.hash());
400     ASSERT_EQ(testBHash1 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
401     hasher.addCharacter(testBUChars[1]);
402     ASSERT_EQ(testBHash2, hasher.hash());
403     ASSERT_EQ(testBHash2 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
404     hasher.addCharactersAssumingAligned(testBUChars[2], testBUChars[3]);
405     ASSERT_EQ(testBHash4, hasher.hash());
406     ASSERT_EQ(testBHash4 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
407     hasher.addCharactersAssumingAligned(testBUChars + 4);
408     ASSERT_EQ(testBHash5, hasher.hash());
409     ASSERT_EQ(testBHash5 & 0xFFFFFF, hasher.hashWithTop8BitsMasked());
410 }
411
412 TEST(WTF, StringHasher_computeHash)
413 {
414     ASSERT_EQ(emptyStringHash, StringHasher::computeHash(static_cast<LChar*>(0), 0));
415     ASSERT_EQ(emptyStringHash, StringHasher::computeHash(nullLChars, 0));
416     ASSERT_EQ(emptyStringHash, StringHasher::computeHash(static_cast<UChar*>(0), 0));
417     ASSERT_EQ(emptyStringHash, StringHasher::computeHash(nullUChars, 0));
418
419     ASSERT_EQ(singleNullCharacterHash, StringHasher::computeHash(nullLChars, 1));
420     ASSERT_EQ(singleNullCharacterHash, StringHasher::computeHash(nullUChars, 1));
421
422     ASSERT_EQ(testAHash5, StringHasher::computeHash(testALChars, 5));
423     ASSERT_EQ(testAHash5, StringHasher::computeHash(testAUChars, 5));
424     ASSERT_EQ(testBHash5, StringHasher::computeHash(testBUChars, 5));
425 }
426
427 TEST(WTF, StringHasher_computeHashAndMaskTop8Bits)
428 {
429     ASSERT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(static_cast<LChar*>(0), 0));
430     ASSERT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(nullLChars, 0));
431     ASSERT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(static_cast<UChar*>(0), 0));
432     ASSERT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(nullUChars, 0));
433
434     ASSERT_EQ(singleNullCharacterHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(nullLChars, 1));
435     ASSERT_EQ(singleNullCharacterHash & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(nullUChars, 1));
436
437     ASSERT_EQ(testAHash5 & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(testALChars, 5));
438     ASSERT_EQ(testAHash5 & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(testAUChars, 5));
439     ASSERT_EQ(testBHash5 & 0xFFFFFF, StringHasher::computeHashAndMaskTop8Bits(testBUChars, 5));
440 }
441
442 TEST(WTF, StringHasher_hashMemory)
443 {
444     ASSERT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::hashMemory(0, 0));
445     ASSERT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::hashMemory(nullUChars, 0));
446     ASSERT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::hashMemory<0>(0));
447     ASSERT_EQ(emptyStringHash & 0xFFFFFF, StringHasher::hashMemory<0>(nullUChars));
448
449     ASSERT_EQ(singleNullCharacterHash & 0xFFFFFF, StringHasher::hashMemory(nullUChars, 2));
450     ASSERT_EQ(singleNullCharacterHash & 0xFFFFFF, StringHasher::hashMemory<2>(nullUChars));
451
452     ASSERT_EQ(testAHash5 & 0xFFFFFF, StringHasher::hashMemory(testAUChars, 10));
453     ASSERT_EQ(testAHash5 & 0xFFFFFF, StringHasher::hashMemory<10>(testAUChars));
454     ASSERT_EQ(testBHash5 & 0xFFFFFF, StringHasher::hashMemory(testBUChars, 10));
455     ASSERT_EQ(testBHash5 & 0xFFFFFF, StringHasher::hashMemory<10>(testBUChars));
456 }
457
458 } // namespace TestWebKitAPI