utils_test.js 21 KB

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