hash32_test.js 8.3 KB

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