| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.crypt.hash32Test'); |
| 16 |
goog.setTestOnly('goog.crypt.hash32Test'); |
| 17 |
|
| 18 |
goog.require('goog.crypt.hash32'); |
| 19 |
goog.require('goog.testing.TestCase'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
|
| 22 |
// NOTE: This test uses a custom test case, see end of script block |
| 23 |
|
| 24 |
// Test data based on known input/output pairs generated using |
| 25 |
// http://go/hash.java |
| 26 |
|
| 27 |
function testEncodeInteger() { |
| 28 |
assertEquals(898813988, goog.crypt.hash32.encodeInteger(305419896)); |
| 29 |
} |
| 30 |
|
| 31 |
function testEncodeByteArray() { |
| 32 |
assertEquals( |
| 33 |
-1497024495, goog.crypt.hash32.encodeByteArray([10, 20, 30, 40])); |
| 34 |
assertEquals( |
| 35 |
-961586214, goog.crypt.hash32.encodeByteArray([3, 1, 4, 1, 5, 9])); |
| 36 |
assertEquals( |
| 37 |
-1482202299, goog.crypt.hash32.encodeByteArray([127, 0, 0, 0, 123, 45])); |
| 38 |
assertEquals(170907881, goog.crypt.hash32.encodeByteArray([9, 1, 1])); |
| 39 |
} |
| 40 |
|
| 41 |
function testKnownByteArrays() { |
| 42 |
for (var i = 0; i < byteArrays.length; i++) { |
| 43 |
assertEquals( |
| 44 |
byteArrays[i], goog.crypt.hash32.encodeByteArray(createByteArray(i))); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
function testEncodeString() { |
| 49 |
assertEquals(-937588052, goog.crypt.hash32.encodeString('Hello, world')); |
| 50 |
assertEquals(62382810, goog.crypt.hash32.encodeString('Sch\xF6n')); |
| 51 |
} |
| 52 |
|
| 53 |
function testEncodeStringUtf8() { |
| 54 |
assertEquals(-937588052, goog.crypt.hash32.encodeStringUtf8('Hello, world')); |
| 55 |
assertEquals(-833263351, goog.crypt.hash32.encodeStringUtf8('Sch\xF6n')); |
| 56 |
|
| 57 |
assertEquals(-1771620293, goog.crypt.hash32.encodeStringUtf8('\u043A\u0440')); |
| 58 |
} |
| 59 |
|
| 60 |
function testEncodeString_ascii() { |
| 61 |
assertEquals( |
| 62 |
'For ascii characters UTF8 should be the same', |
| 63 |
goog.crypt.hash32.encodeStringUtf8('abc123'), |
| 64 |
goog.crypt.hash32.encodeString('abc123')); |
| 65 |
|
| 66 |
assertEquals( |
| 67 |
'For ascii characters UTF8 should be the same', |
| 68 |
goog.crypt.hash32.encodeStringUtf8('The,quick.brown-fox'), |
| 69 |
goog.crypt.hash32.encodeString('The,quick.brown-fox')); |
| 70 |
|
| 71 |
assertNotEquals( |
| 72 |
'For non-ascii characters UTF-8 encoding is different', |
| 73 |
goog.crypt.hash32.encodeStringUtf8('Sch\xF6n'), |
| 74 |
goog.crypt.hash32.encodeString('Sch\xF6n')); |
| 75 |
} |
| 76 |
|
| 77 |
function testEncodeString_poe() { |
| 78 |
var poe = "Once upon a midnight dreary, while I pondered weak and weary," + |
| 79 |
"Over many a quaint and curious volume of forgotten lore," + |
| 80 |
"While I nodded, nearly napping, suddenly there came a tapping," + |
| 81 |
"As of some one gently rapping, rapping at my chamber door." + |
| 82 |
"`'Tis some visitor,' I muttered, `tapping at my chamber door -" + |
| 83 |
"Only this, and nothing more.'" + |
| 84 |
"Ah, distinctly I remember it was in the bleak December," + |
| 85 |
"And each separate dying ember wrought its ghost upon the floor." + |
| 86 |
"Eagerly I wished the morrow; - vainly I had sought to borrow" + |
| 87 |
"From my books surcease of sorrow - sorrow for the lost Lenore -" + |
| 88 |
"For the rare and radiant maiden whom the angels named Lenore -" + |
| 89 |
"Nameless here for evermore." + |
| 90 |
"And the silken sad uncertain rustling of each purple curtain" + |
| 91 |
"Thrilled me - filled me with fantastic terrors never felt before;" + |
| 92 |
"So that now, to still the beating of my heart, I stood repeating" + |
| 93 |
"`'Tis some visitor entreating entrance at my chamber door -" + |
| 94 |
"Some late visitor entreating entrance at my chamber door; -" + |
| 95 |
"This it is, and nothing more,'" + |
| 96 |
"Presently my soul grew stronger; hesitating then no longer," + |
| 97 |
"`Sir,' said I, `or Madam, truly your forgiveness I implore;" + |
| 98 |
"But the fact is I was napping, and so gently you came rapping," + |
| 99 |
"And so faintly you came tapping, tapping at my chamber door," + |
| 100 |
"That I scarce was sure I heard you' - here I opened wide the door; -" + |
| 101 |
"Darkness there, and nothing more." + |
| 102 |
"Deep into that darkness peering, long I stood there wondering, " + |
| 103 |
"fearing," + |
| 104 |
"Doubting, dreaming dreams no mortal ever dared to dream before" + |
| 105 |
"But the silence was unbroken, and the darkness gave no token," + |
| 106 |
"And the only word there spoken was the whispered word, `Lenore!'" + |
| 107 |
"This I whispered, and an echo murmured back the word, `Lenore!'" + |
| 108 |
"Merely this and nothing more." + |
| 109 |
"Back into the chamber turning, all my soul within me burning," + |
| 110 |
"Soon again I heard a tapping somewhat louder than before." + |
| 111 |
"`Surely,\' said I, `surely that is something at my window lattice;" + |
| 112 |
"Let me see then, what thereat is, and this mystery explore -" + |
| 113 |
"Let my heart be still a moment and this mystery explore; -" + |
| 114 |
"'Tis the wind and nothing more!'"; |
| 115 |
|
| 116 |
assertEquals(147608747, goog.crypt.hash32.encodeString(poe)); |
| 117 |
assertEquals(147608747, goog.crypt.hash32.encodeStringUtf8(poe)); |
| 118 |
} |
| 119 |
|
| 120 |
function testBenchmarking() { |
| 121 |
if (!testCase) return; |
| 122 |
// Not a real test, just outputs some timing |
| 123 |
function makeString(n) { |
| 124 |
var str = []; |
| 125 |
for (var i = 0; i < n; i++) { |
| 126 |
str.push(String.fromCharCode(Math.round(Math.random() * 500))); |
| 127 |
} |
| 128 |
return str.join(''); |
| 129 |
} |
| 130 |
for (var i = 0; i < 50000; i += 10000) { |
| 131 |
var str = makeString(i); |
| 132 |
var start = goog.now(); |
| 133 |
var hash = goog.crypt.hash32.encodeString(str); |
| 134 |
var diff = goog.now() - start; |
| 135 |
testCase.saveMessage( |
| 136 |
'testBenchmarking : hashing ' + i + ' chars in ' + diff + 'ms'); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
function createByteArray(n) { |
| 141 |
var arr = []; |
| 142 |
for (var i = 0; i < n; i++) { |
| 143 |
arr.push(i); |
| 144 |
} |
| 145 |
return arr; |
| 146 |
} |
| 147 |
|
| 148 |
var byteArrays = { |
| 149 |
0: 1539411136, |
| 150 |
1: 1773524747, |
| 151 |
2: -254958930, |
| 152 |
3: 1532114172, |
| 153 |
4: 1923165449, |
| 154 |
5: 1611874589, |
| 155 |
6: 1502126780, |
| 156 |
7: -751745251, |
| 157 |
8: -292491321, |
| 158 |
9: 1106193218, |
| 159 |
10: -722791438, |
| 160 |
11: -2130666060, |
| 161 |
12: -259304553, |
| 162 |
13: 871461192, |
| 163 |
14: 865773084, |
| 164 |
15: 1615738330, |
| 165 |
16: -1836636447, |
| 166 |
17: -485722519, |
| 167 |
18: -120832227, |
| 168 |
19: 1954449704, |
| 169 |
20: 491312921, |
| 170 |
21: -1955462668, |
| 171 |
22: 168565425, |
| 172 |
23: -105893922, |
| 173 |
24: 620486614, |
| 174 |
25: -1789602428, |
| 175 |
26: 1765793554, |
| 176 |
27: 1723370948, |
| 177 |
28: -1275405721, |
| 178 |
29: 140421019, |
| 179 |
30: -1438726307, |
| 180 |
31: 538438903, |
| 181 |
32: -729123980, |
| 182 |
33: 1213490939, |
| 183 |
34: -1814248478, |
| 184 |
35: 1943703398, |
| 185 |
36: 1603073219, |
| 186 |
37: -2139639543, |
| 187 |
38: -694153941, |
| 188 |
39: 137511516, |
| 189 |
40: -249943726, |
| 190 |
41: -1166126060, |
| 191 |
42: 53464833, |
| 192 |
43: -915350862, |
| 193 |
44: 1306585409, |
| 194 |
45: 1064798289, |
| 195 |
46: 335555913, |
| 196 |
47: 224485496, |
| 197 |
48: 275599760, |
| 198 |
49: 409559869, |
| 199 |
50: 673770580, |
| 200 |
51: -2113819879, |
| 201 |
52: -791338727, |
| 202 |
53: -1716479479, |
| 203 |
54: 1795018816, |
| 204 |
55: 2020139343, |
| 205 |
56: -1652827750, |
| 206 |
57: -1509632558, |
| 207 |
58: 751641995, |
| 208 |
59: -217881377, |
| 209 |
60: -476546900, |
| 210 |
61: -1893349644, |
| 211 |
62: -729290332, |
| 212 |
63: 1359899321, |
| 213 |
64: 1811814306, |
| 214 |
65: 2100363086, |
| 215 |
66: -794920327, |
| 216 |
67: -1667555017, |
| 217 |
68: -549980099, |
| 218 |
69: -21170740, |
| 219 |
70: -1324143722, |
| 220 |
71: 1406730195, |
| 221 |
72: 2111381574, |
| 222 |
73: -1667480052, |
| 223 |
74: 1071811178, |
| 224 |
75: -1080194099, |
| 225 |
76: -181186882, |
| 226 |
77: 268677507, |
| 227 |
78: -546766334, |
| 228 |
79: 555953522, |
| 229 |
80: -981311675, |
| 230 |
81: 1988867392, |
| 231 |
82: 773172547, |
| 232 |
83: 1160806722, |
| 233 |
84: -1455460187, |
| 234 |
85: 83493600, |
| 235 |
86: 155365142, |
| 236 |
87: 1714618071, |
| 237 |
88: 1487712615, |
| 238 |
89: -810670278, |
| 239 |
90: 2031655097, |
| 240 |
91: 1286349470, |
| 241 |
92: -1873594211, |
| 242 |
93: 1875867480, |
| 243 |
94: -1096259787, |
| 244 |
95: -1054968610, |
| 245 |
96: -1723043458, |
| 246 |
97: 1278708307, |
| 247 |
98: -601104085, |
| 248 |
99: 1497928579, |
| 249 |
100: 1329732615, |
| 250 |
101: -1281696190, |
| 251 |
102: 1471511953, |
| 252 |
103: -62666299, |
| 253 |
104: 807569747, |
| 254 |
105: -1927974759, |
| 255 |
106: 1462243717, |
| 256 |
107: -862975602, |
| 257 |
108: 824369927, |
| 258 |
109: -1448816781, |
| 259 |
110: 1434162022, |
| 260 |
111: -881501413, |
| 261 |
112: -1554381107, |
| 262 |
113: -1730883204, |
| 263 |
114: 431236217, |
| 264 |
115: 1877278608, |
| 265 |
116: -673864625, |
| 266 |
117: 143000665, |
| 267 |
118: -596902829, |
| 268 |
119: 1038860559, |
| 269 |
120: 805884326, |
| 270 |
121: -1536181710, |
| 271 |
122: -1357373256, |
| 272 |
123: 1405134250, |
| 273 |
124: -860816481, |
| 274 |
125: 1393578269, |
| 275 |
126: -810682545, |
| 276 |
127: -635515639 |
| 277 |
}; |
| 278 |
|
| 279 |
var testCase; |
| 280 |
if (G_testRunner) { |
| 281 |
testCase = new goog.testing.TestCase(document.title); |
| 282 |
testCase.autoDiscoverTests(); |
| 283 |
G_testRunner.initialize(testCase); |
| 284 |
} |