utils_test.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. /**
  31. * @fileoverview Test cases for jspb's helper functions.
  32. *
  33. * Test suite is written using Jasmine -- see http://jasmine.github.io/
  34. *
  35. * @author aappleby@google.com (Austin Appleby)
  36. */
  37. goog.require('goog.crypt.base64');
  38. goog.require('goog.testing.asserts');
  39. goog.require('jspb.BinaryConstants');
  40. goog.require('jspb.BinaryWriter');
  41. goog.require('jspb.utils');
  42. /**
  43. * @param {number} x
  44. * @return {number}
  45. */
  46. function truncate(x) {
  47. var temp = new Float32Array(1);
  48. temp[0] = x;
  49. return temp[0];
  50. }
  51. /**
  52. * Converts an 64-bit integer in split representation to a 64-bit hash string
  53. * (8 bits encoded per character).
  54. * @param {number} bitsLow The low 32 bits of the split 64-bit integer.
  55. * @param {number} bitsHigh The high 32 bits of the split 64-bit integer.
  56. * @return {string} The encoded hash string, 8 bits per character.
  57. */
  58. function toHashString(bitsLow, bitsHigh) {
  59. return String.fromCharCode((bitsLow >>> 0) & 0xFF,
  60. (bitsLow >>> 8) & 0xFF,
  61. (bitsLow >>> 16) & 0xFF,
  62. (bitsLow >>> 24) & 0xFF,
  63. (bitsHigh >>> 0) & 0xFF,
  64. (bitsHigh >>> 8) & 0xFF,
  65. (bitsHigh >>> 16) & 0xFF,
  66. (bitsHigh >>> 24) & 0xFF);
  67. }
  68. describe('binaryUtilsTest', function() {
  69. /**
  70. * Tests lossless binary-to-decimal conversion.
  71. */
  72. it('testDecimalConversion', function() {
  73. // Check some magic numbers.
  74. var result =
  75. jspb.utils.joinUnsignedDecimalString(0x89e80001, 0x8ac72304);
  76. assertEquals('10000000000000000001', result);
  77. result = jspb.utils.joinUnsignedDecimalString(0xacd05f15, 0x1b69b4b);
  78. assertEquals('123456789123456789', result);
  79. result = jspb.utils.joinUnsignedDecimalString(0xeb1f0ad2, 0xab54a98c);
  80. assertEquals('12345678901234567890', result);
  81. result = jspb.utils.joinUnsignedDecimalString(0xe3b70cb1, 0x891087b8);
  82. assertEquals('9876543210987654321', result);
  83. // Check limits.
  84. result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x00000000);
  85. assertEquals('0', result);
  86. result = jspb.utils.joinUnsignedDecimalString(0xFFFFFFFF, 0xFFFFFFFF);
  87. assertEquals('18446744073709551615', result);
  88. // Check each bit of the low dword.
  89. for (var i = 0; i < 32; i++) {
  90. var low = (1 << i) >>> 0;
  91. result = jspb.utils.joinUnsignedDecimalString(low, 0);
  92. assertEquals('' + Math.pow(2, i), result);
  93. }
  94. // Check the first 20 bits of the high dword.
  95. for (var i = 0; i < 20; i++) {
  96. var high = (1 << i) >>> 0;
  97. result = jspb.utils.joinUnsignedDecimalString(0, high);
  98. assertEquals('' + Math.pow(2, 32 + i), result);
  99. }
  100. // V8's internal double-to-string conversion is inaccurate for values above
  101. // 2^52, even if they're representable integers - check the rest of the bits
  102. // manually against the correct string representations of 2^N.
  103. result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x00100000);
  104. assertEquals('4503599627370496', result);
  105. result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x00200000);
  106. assertEquals('9007199254740992', result);
  107. result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x00400000);
  108. assertEquals('18014398509481984', result);
  109. result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x00800000);
  110. assertEquals('36028797018963968', result);
  111. result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x01000000);
  112. assertEquals('72057594037927936', result);
  113. result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x02000000);
  114. assertEquals('144115188075855872', result);
  115. result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x04000000);
  116. assertEquals('288230376151711744', result);
  117. result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x08000000);
  118. assertEquals('576460752303423488', result);
  119. result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x10000000);
  120. assertEquals('1152921504606846976', result);
  121. result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x20000000);
  122. assertEquals('2305843009213693952', result);
  123. result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x40000000);
  124. assertEquals('4611686018427387904', result);
  125. result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x80000000);
  126. assertEquals('9223372036854775808', result);
  127. });
  128. /**
  129. * Going from hash strings to decimal strings should also be lossless.
  130. */
  131. it('testHashToDecimalConversion', function() {
  132. var result;
  133. var convert = jspb.utils.hash64ToDecimalString;
  134. result = convert(toHashString(0x00000000, 0x00000000), false);
  135. assertEquals('0', result);
  136. result = convert(toHashString(0x00000000, 0x00000000), true);
  137. assertEquals('0', result);
  138. result = convert(toHashString(0xFFFFFFFF, 0xFFFFFFFF), false);
  139. assertEquals('18446744073709551615', result);
  140. result = convert(toHashString(0xFFFFFFFF, 0xFFFFFFFF), true);
  141. assertEquals('-1', result);
  142. result = convert(toHashString(0x00000000, 0x80000000), false);
  143. assertEquals('9223372036854775808', result);
  144. result = convert(toHashString(0x00000000, 0x80000000), true);
  145. assertEquals('-9223372036854775808', result);
  146. result = convert(toHashString(0xacd05f15, 0x01b69b4b), false);
  147. assertEquals('123456789123456789', result);
  148. result = convert(toHashString(~0xacd05f15 + 1, ~0x01b69b4b), true);
  149. assertEquals('-123456789123456789', result);
  150. // And converting arrays of hashes should work the same way.
  151. result = jspb.utils.hash64ArrayToDecimalStrings([
  152. toHashString(0xFFFFFFFF, 0xFFFFFFFF),
  153. toHashString(0x00000000, 0x80000000),
  154. toHashString(0xacd05f15, 0x01b69b4b)], false);
  155. assertEquals(3, result.length);
  156. assertEquals('18446744073709551615', result[0]);
  157. assertEquals('9223372036854775808', result[1]);
  158. assertEquals('123456789123456789', result[2]);
  159. });
  160. /*
  161. * Going from decimal strings to hash strings should be lossless.
  162. */
  163. it('testDecimalToHashConversion', function() {
  164. var result;
  165. var convert = jspb.utils.decimalStringToHash64;
  166. result = convert('0');
  167. assertEquals(String.fromCharCode.apply(null,
  168. [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), result);
  169. result = convert('-1');
  170. assertEquals(String.fromCharCode.apply(null,
  171. [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result);
  172. result = convert('18446744073709551615');
  173. assertEquals(String.fromCharCode.apply(null,
  174. [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result);
  175. result = convert('9223372036854775808');
  176. assertEquals(String.fromCharCode.apply(null,
  177. [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), result);
  178. result = convert('-9223372036854775808');
  179. assertEquals(String.fromCharCode.apply(null,
  180. [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), result);
  181. result = convert('123456789123456789');
  182. assertEquals(String.fromCharCode.apply(null,
  183. [0x15, 0x5F, 0xD0, 0xAC, 0x4B, 0x9B, 0xB6, 0x01]), result);
  184. result = convert('-123456789123456789');
  185. assertEquals(String.fromCharCode.apply(null,
  186. [0xEB, 0xA0, 0x2F, 0x53, 0xB4, 0x64, 0x49, 0xFE]), result);
  187. });
  188. /**
  189. * Going from hash strings to hex strings should be lossless.
  190. */
  191. it('testHashToHexConversion', function() {
  192. var result;
  193. var convert = jspb.utils.hash64ToHexString;
  194. result = convert(toHashString(0x00000000, 0x00000000));
  195. assertEquals('0x0000000000000000', result);
  196. result = convert(toHashString(0xFFFFFFFF, 0xFFFFFFFF));
  197. assertEquals('0xffffffffffffffff', result);
  198. result = convert(toHashString(0x12345678, 0x9ABCDEF0));
  199. assertEquals('0x9abcdef012345678', result);
  200. });
  201. /**
  202. * Going from hex strings to hash strings should be lossless.
  203. */
  204. it('testHexToHashConversion', function() {
  205. var result;
  206. var convert = jspb.utils.hexStringToHash64;
  207. result = convert('0x0000000000000000');
  208. assertEquals(String.fromCharCode.apply(null,
  209. [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), result);
  210. result = convert('0xffffffffffffffff');
  211. assertEquals(String.fromCharCode.apply(null,
  212. [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result);
  213. // Hex string is big-endian, hash string is little-endian.
  214. result = convert('0x123456789ABCDEF0');
  215. assertEquals(String.fromCharCode.apply(null,
  216. [0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12]), result);
  217. // Capitalization should not matter.
  218. result = convert('0x0000abcdefABCDEF');
  219. assertEquals(String.fromCharCode.apply(null,
  220. [0xEF, 0xCD, 0xAB, 0xEF, 0xCD, 0xAB, 0x00, 0x00]), result);
  221. });
  222. /**
  223. * Going from numbers to hash strings should be lossless for up to 53 bits of
  224. * precision.
  225. */
  226. it('testNumberToHashConversion', function() {
  227. var result;
  228. var convert = jspb.utils.numberToHash64;
  229. result = convert(0x0000000000000);
  230. assertEquals('0x0000000000000000', jspb.utils.hash64ToHexString(result));
  231. result = convert(0xFFFFFFFFFFFFF);
  232. assertEquals('0x000fffffffffffff', jspb.utils.hash64ToHexString(result));
  233. result = convert(0x123456789ABCD);
  234. assertEquals('0x000123456789abcd', jspb.utils.hash64ToHexString(result));
  235. result = convert(0xDCBA987654321);
  236. assertEquals('0x000dcba987654321', jspb.utils.hash64ToHexString(result));
  237. // 53 bits of precision should not be truncated.
  238. result = convert(0x10000000000001);
  239. assertEquals('0x0010000000000001', jspb.utils.hash64ToHexString(result));
  240. // 54 bits of precision should be truncated.
  241. result = convert(0x20000000000001);
  242. assertNotEquals(
  243. '0x0020000000000001', jspb.utils.hash64ToHexString(result));
  244. });
  245. /**
  246. * Sanity check the behavior of Javascript's strings when doing funny things
  247. * with unicode characters.
  248. */
  249. it('sanityCheckUnicodeStrings', function() {
  250. var strings = new Array(65536);
  251. // All possible unsigned 16-bit values should be storable in a string, they
  252. // shouldn't do weird things with the length of the string, and they should
  253. // come back out of the string unchanged.
  254. for (var i = 0; i < 65536; i++) {
  255. strings[i] = 'a' + String.fromCharCode(i) + 'a';
  256. if (3 != strings[i].length) throw 'fail!';
  257. if (i != strings[i].charCodeAt(1)) throw 'fail!';
  258. }
  259. // Each unicode character should compare equal to itself and not equal to a
  260. // different unicode character.
  261. for (var i = 0; i < 65536; i++) {
  262. if (strings[i] != strings[i]) throw 'fail!';
  263. if (strings[i] == strings[(i + 1) % 65536]) throw 'fail!';
  264. }
  265. });
  266. /**
  267. * Tests conversion from 32-bit floating point numbers to split64 numbers.
  268. */
  269. it('testFloat32ToSplit64', function() {
  270. var f32_eps = jspb.BinaryConstants.FLOAT32_EPS;
  271. var f32_min = jspb.BinaryConstants.FLOAT32_MIN;
  272. var f32_max = jspb.BinaryConstants.FLOAT32_MAX;
  273. // NaN.
  274. jspb.utils.splitFloat32(NaN);
  275. if (!isNaN(jspb.utils.joinFloat32(jspb.utils.split64Low,
  276. jspb.utils.split64High))) {
  277. throw 'fail!';
  278. }
  279. /**
  280. * @param {number} x
  281. * @param {number=} opt_bits
  282. */
  283. function test(x, opt_bits) {
  284. jspb.utils.splitFloat32(x);
  285. if (opt_bits !== undefined) {
  286. if (opt_bits != jspb.utils.split64Low) throw 'fail!';
  287. }
  288. if (truncate(x) != jspb.utils.joinFloat32(jspb.utils.split64Low,
  289. jspb.utils.split64High)) {
  290. throw 'fail!';
  291. }
  292. }
  293. // Positive and negative infinity.
  294. test(Infinity, 0x7f800000);
  295. test(-Infinity, 0xff800000);
  296. // Positive and negative zero.
  297. test(0, 0x00000000);
  298. test(-0, 0x80000000);
  299. // Positive and negative epsilon.
  300. test(f32_eps, 0x00000001);
  301. test(-f32_eps, 0x80000001);
  302. // Positive and negative min.
  303. test(f32_min, 0x00800000);
  304. test(-f32_min, 0x80800000);
  305. // Positive and negative max.
  306. test(f32_max, 0x7F7FFFFF);
  307. test(-f32_max, 0xFF7FFFFF);
  308. // Various positive values.
  309. var cursor = f32_eps * 10;
  310. while (cursor != Infinity) {
  311. test(cursor);
  312. cursor *= 1.1;
  313. }
  314. // Various negative values.
  315. cursor = -f32_eps * 10;
  316. while (cursor != -Infinity) {
  317. test(cursor);
  318. cursor *= 1.1;
  319. }
  320. });
  321. /**
  322. * Tests conversion from 64-bit floating point numbers to split64 numbers.
  323. */
  324. it('testFloat64ToSplit64', function() {
  325. var f64_eps = jspb.BinaryConstants.FLOAT64_EPS;
  326. var f64_min = jspb.BinaryConstants.FLOAT64_MIN;
  327. var f64_max = jspb.BinaryConstants.FLOAT64_MAX;
  328. // NaN.
  329. jspb.utils.splitFloat64(NaN);
  330. if (!isNaN(jspb.utils.joinFloat64(jspb.utils.split64Low,
  331. jspb.utils.split64High))) {
  332. throw 'fail!';
  333. }
  334. /**
  335. * @param {number} x
  336. * @param {number=} opt_highBits
  337. * @param {number=} opt_lowBits
  338. */
  339. function test(x, opt_highBits, opt_lowBits) {
  340. jspb.utils.splitFloat64(x);
  341. if (opt_highBits !== undefined) {
  342. if (opt_highBits != jspb.utils.split64High) throw 'fail!';
  343. }
  344. if (opt_lowBits !== undefined) {
  345. if (opt_lowBits != jspb.utils.split64Low) throw 'fail!';
  346. }
  347. if (x != jspb.utils.joinFloat64(jspb.utils.split64Low,
  348. jspb.utils.split64High)) {
  349. throw 'fail!';
  350. }
  351. }
  352. // Positive and negative infinity.
  353. test(Infinity, 0x7ff00000, 0x00000000);
  354. test(-Infinity, 0xfff00000, 0x00000000);
  355. // Positive and negative zero.
  356. test(0, 0x00000000, 0x00000000);
  357. test(-0, 0x80000000, 0x00000000);
  358. // Positive and negative epsilon.
  359. test(f64_eps, 0x00000000, 0x00000001);
  360. test(-f64_eps, 0x80000000, 0x00000001);
  361. // Positive and negative min.
  362. test(f64_min, 0x00100000, 0x00000000);
  363. test(-f64_min, 0x80100000, 0x00000000);
  364. // Positive and negative max.
  365. test(f64_max, 0x7FEFFFFF, 0xFFFFFFFF);
  366. test(-f64_max, 0xFFEFFFFF, 0xFFFFFFFF);
  367. // Various positive values.
  368. var cursor = f64_eps * 10;
  369. while (cursor != Infinity) {
  370. test(cursor);
  371. cursor *= 1.1;
  372. }
  373. // Various negative values.
  374. cursor = -f64_eps * 10;
  375. while (cursor != -Infinity) {
  376. test(cursor);
  377. cursor *= 1.1;
  378. }
  379. });
  380. /**
  381. * Tests counting packed varints.
  382. */
  383. it('testCountVarints', function() {
  384. var values = [];
  385. for (var i = 1; i < 1000000000; i *= 1.1) {
  386. values.push(Math.floor(i));
  387. }
  388. var writer = new jspb.BinaryWriter();
  389. writer.writePackedUint64(1, values);
  390. var buffer = new Uint8Array(writer.getResultBuffer());
  391. // We should have two more varints than we started with - one for the field
  392. // tag, one for the packed length.
  393. assertEquals(values.length + 2,
  394. jspb.utils.countVarints(buffer, 0, buffer.length));
  395. });
  396. /**
  397. * Tests counting matching varint fields.
  398. */
  399. it('testCountVarintFields', function() {
  400. var writer = new jspb.BinaryWriter();
  401. var count = 0;
  402. for (var i = 1; i < 1000000000; i *= 1.1) {
  403. writer.writeUint64(1, Math.floor(i));
  404. count++;
  405. }
  406. writer.writeString(2, 'terminator');
  407. var buffer = new Uint8Array(writer.getResultBuffer());
  408. assertEquals(count,
  409. jspb.utils.countVarintFields(buffer, 0, buffer.length, 1));
  410. writer = new jspb.BinaryWriter();
  411. count = 0;
  412. for (var i = 1; i < 1000000000; i *= 1.1) {
  413. writer.writeUint64(123456789, Math.floor(i));
  414. count++;
  415. }
  416. writer.writeString(2, 'terminator');
  417. buffer = new Uint8Array(writer.getResultBuffer());
  418. assertEquals(count,
  419. jspb.utils.countVarintFields(buffer, 0, buffer.length, 123456789));
  420. });
  421. /**
  422. * Tests counting matching fixed32 fields.
  423. */
  424. it('testCountFixed32Fields', function() {
  425. var writer = new jspb.BinaryWriter();
  426. var count = 0;
  427. for (var i = 1; i < 1000000000; i *= 1.1) {
  428. writer.writeFixed32(1, Math.floor(i));
  429. count++;
  430. }
  431. writer.writeString(2, 'terminator');
  432. var buffer = new Uint8Array(writer.getResultBuffer());
  433. assertEquals(count,
  434. jspb.utils.countFixed32Fields(buffer, 0, buffer.length, 1));
  435. writer = new jspb.BinaryWriter();
  436. count = 0;
  437. for (var i = 1; i < 1000000000; i *= 1.1) {
  438. writer.writeFixed32(123456789, Math.floor(i));
  439. count++;
  440. }
  441. writer.writeString(2, 'terminator');
  442. buffer = new Uint8Array(writer.getResultBuffer());
  443. assertEquals(count,
  444. jspb.utils.countFixed32Fields(buffer, 0, buffer.length, 123456789));
  445. });
  446. /**
  447. * Tests counting matching fixed64 fields.
  448. */
  449. it('testCountFixed64Fields', function() {
  450. var writer = new jspb.BinaryWriter();
  451. var count = 0;
  452. for (var i = 1; i < 1000000000; i *= 1.1) {
  453. writer.writeDouble(1, i);
  454. count++;
  455. }
  456. writer.writeString(2, 'terminator');
  457. var buffer = new Uint8Array(writer.getResultBuffer());
  458. assertEquals(count,
  459. jspb.utils.countFixed64Fields(buffer, 0, buffer.length, 1));
  460. writer = new jspb.BinaryWriter();
  461. count = 0;
  462. for (var i = 1; i < 1000000000; i *= 1.1) {
  463. writer.writeDouble(123456789, i);
  464. count++;
  465. }
  466. writer.writeString(2, 'terminator');
  467. buffer = new Uint8Array(writer.getResultBuffer());
  468. assertEquals(count,
  469. jspb.utils.countFixed64Fields(buffer, 0, buffer.length, 123456789));
  470. });
  471. /**
  472. * Tests counting matching delimited fields.
  473. */
  474. it('testCountDelimitedFields', function() {
  475. var writer = new jspb.BinaryWriter();
  476. var count = 0;
  477. for (var i = 1; i < 1000; i *= 1.1) {
  478. writer.writeBytes(1, [Math.floor(i)]);
  479. count++;
  480. }
  481. writer.writeString(2, 'terminator');
  482. var buffer = new Uint8Array(writer.getResultBuffer());
  483. assertEquals(count,
  484. jspb.utils.countDelimitedFields(buffer, 0, buffer.length, 1));
  485. writer = new jspb.BinaryWriter();
  486. count = 0;
  487. for (var i = 1; i < 1000; i *= 1.1) {
  488. writer.writeBytes(123456789, [Math.floor(i)]);
  489. count++;
  490. }
  491. writer.writeString(2, 'terminator');
  492. buffer = new Uint8Array(writer.getResultBuffer());
  493. assertEquals(count,
  494. jspb.utils.countDelimitedFields(buffer, 0, buffer.length, 123456789));
  495. });
  496. /**
  497. * Tests byte format for debug strings.
  498. */
  499. it('testDebugBytesToTextFormat', function() {
  500. assertEquals('""', jspb.utils.debugBytesToTextFormat(null));
  501. assertEquals('"\\x00\\x10\\xff"',
  502. jspb.utils.debugBytesToTextFormat([0, 16, 255]));
  503. });
  504. /**
  505. * Tests converting byte blob sources into byte blobs.
  506. */
  507. it('testByteSourceToUint8Array', function() {
  508. var convert = jspb.utils.byteSourceToUint8Array;
  509. var sourceData = [];
  510. for (var i = 0; i < 256; i++) {
  511. sourceData.push(i);
  512. }
  513. var sourceBytes = new Uint8Array(sourceData);
  514. var sourceBuffer = sourceBytes.buffer;
  515. var sourceBase64 = goog.crypt.base64.encodeByteArray(sourceData);
  516. var sourceString = String.fromCharCode.apply(null, sourceData);
  517. function check(result) {
  518. assertEquals(Uint8Array, result.constructor);
  519. assertEquals(sourceData.length, result.length);
  520. for (var i = 0; i < result.length; i++) {
  521. assertEquals(sourceData[i], result[i]);
  522. }
  523. }
  524. // Converting Uint8Arrays into Uint8Arrays should be a no-op.
  525. assertEquals(sourceBytes, convert(sourceBytes));
  526. // Converting Array.<numbers> into Uint8Arrays should work.
  527. check(convert(sourceData));
  528. // Converting ArrayBuffers into Uint8Arrays should work.
  529. check(convert(sourceBuffer));
  530. // Converting base64-encoded strings into Uint8Arrays should work.
  531. check(convert(sourceBase64));
  532. });
  533. });