utils_test.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. // CommonJS-LoadFromFile: google_protobuf
  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 hash strings to hex strings should be lossless.
  163. */
  164. it('testHashToHexConversion', function() {
  165. var result;
  166. var convert = jspb.utils.hash64ToHexString;
  167. result = convert(toHashString(0x00000000, 0x00000000));
  168. assertEquals('0x0000000000000000', result);
  169. result = convert(toHashString(0xFFFFFFFF, 0xFFFFFFFF));
  170. assertEquals('0xffffffffffffffff', result);
  171. result = convert(toHashString(0x12345678, 0x9ABCDEF0));
  172. assertEquals('0x9abcdef012345678', result);
  173. });
  174. /**
  175. * Going from hex strings to hash strings should be lossless.
  176. */
  177. it('testHexToHashConversion', function() {
  178. var result;
  179. var convert = jspb.utils.hexStringToHash64;
  180. result = convert('0x0000000000000000');
  181. assertEquals(String.fromCharCode.apply(null,
  182. [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), result);
  183. result = convert('0xffffffffffffffff');
  184. assertEquals(String.fromCharCode.apply(null,
  185. [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result);
  186. // Hex string is big-endian, hash string is little-endian.
  187. result = convert('0x123456789ABCDEF0');
  188. assertEquals(String.fromCharCode.apply(null,
  189. [0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12]), result);
  190. // Capitalization should not matter.
  191. result = convert('0x0000abcdefABCDEF');
  192. assertEquals(String.fromCharCode.apply(null,
  193. [0xEF, 0xCD, 0xAB, 0xEF, 0xCD, 0xAB, 0x00, 0x00]), result);
  194. });
  195. /**
  196. * Going from numbers to hash strings should be lossless for up to 53 bits of
  197. * precision.
  198. */
  199. it('testNumberToHashConversion', function() {
  200. var result;
  201. var convert = jspb.utils.numberToHash64;
  202. result = convert(0x0000000000000);
  203. assertEquals('0x0000000000000000', jspb.utils.hash64ToHexString(result));
  204. result = convert(0xFFFFFFFFFFFFF);
  205. assertEquals('0x000fffffffffffff', jspb.utils.hash64ToHexString(result));
  206. result = convert(0x123456789ABCD);
  207. assertEquals('0x000123456789abcd', jspb.utils.hash64ToHexString(result));
  208. result = convert(0xDCBA987654321);
  209. assertEquals('0x000dcba987654321', jspb.utils.hash64ToHexString(result));
  210. // 53 bits of precision should not be truncated.
  211. result = convert(0x10000000000001);
  212. assertEquals('0x0010000000000001', jspb.utils.hash64ToHexString(result));
  213. // 54 bits of precision should be truncated.
  214. result = convert(0x20000000000001);
  215. assertNotEquals(
  216. '0x0020000000000001', jspb.utils.hash64ToHexString(result));
  217. });
  218. /**
  219. * Sanity check the behavior of Javascript's strings when doing funny things
  220. * with unicode characters.
  221. */
  222. it('sanityCheckUnicodeStrings', function() {
  223. var strings = new Array(65536);
  224. // All possible unsigned 16-bit values should be storable in a string, they
  225. // shouldn't do weird things with the length of the string, and they should
  226. // come back out of the string unchanged.
  227. for (var i = 0; i < 65536; i++) {
  228. strings[i] = 'a' + String.fromCharCode(i) + 'a';
  229. if (3 != strings[i].length) throw 'fail!';
  230. if (i != strings[i].charCodeAt(1)) throw 'fail!';
  231. }
  232. // Each unicode character should compare equal to itself and not equal to a
  233. // different unicode character.
  234. for (var i = 0; i < 65536; i++) {
  235. if (strings[i] != strings[i]) throw 'fail!';
  236. if (strings[i] == strings[(i + 1) % 65536]) throw 'fail!';
  237. }
  238. });
  239. /**
  240. * Tests conversion from 32-bit floating point numbers to split64 numbers.
  241. */
  242. it('testFloat32ToSplit64', function() {
  243. var f32_eps = jspb.BinaryConstants.FLOAT32_EPS;
  244. var f32_min = jspb.BinaryConstants.FLOAT32_MIN;
  245. var f32_max = jspb.BinaryConstants.FLOAT32_MAX;
  246. // NaN.
  247. jspb.utils.splitFloat32(NaN);
  248. if (!isNaN(jspb.utils.joinFloat32(jspb.utils.split64Low,
  249. jspb.utils.split64High))) {
  250. throw 'fail!';
  251. }
  252. /**
  253. * @param {number} x
  254. * @param {number=} opt_bits
  255. */
  256. function test(x, opt_bits) {
  257. jspb.utils.splitFloat32(x);
  258. if (goog.isDef(opt_bits)) {
  259. if (opt_bits != jspb.utils.split64Low) throw 'fail!';
  260. }
  261. if (truncate(x) != jspb.utils.joinFloat32(jspb.utils.split64Low,
  262. jspb.utils.split64High)) {
  263. throw 'fail!';
  264. }
  265. }
  266. // Positive and negative infinity.
  267. test(Infinity, 0x7f800000);
  268. test(-Infinity, 0xff800000);
  269. // Positive and negative zero.
  270. test(0, 0x00000000);
  271. test(-0, 0x80000000);
  272. // Positive and negative epsilon.
  273. test(f32_eps, 0x00000001);
  274. test(-f32_eps, 0x80000001);
  275. // Positive and negative min.
  276. test(f32_min, 0x00800000);
  277. test(-f32_min, 0x80800000);
  278. // Positive and negative max.
  279. test(f32_max, 0x7F7FFFFF);
  280. test(-f32_max, 0xFF7FFFFF);
  281. // Various positive values.
  282. var cursor = f32_eps * 10;
  283. while (cursor != Infinity) {
  284. test(cursor);
  285. cursor *= 1.1;
  286. }
  287. // Various negative values.
  288. cursor = -f32_eps * 10;
  289. while (cursor != -Infinity) {
  290. test(cursor);
  291. cursor *= 1.1;
  292. }
  293. });
  294. /**
  295. * Tests conversion from 64-bit floating point numbers to split64 numbers.
  296. */
  297. it('testFloat64ToSplit64', function() {
  298. var f64_eps = jspb.BinaryConstants.FLOAT64_EPS;
  299. var f64_min = jspb.BinaryConstants.FLOAT64_MIN;
  300. var f64_max = jspb.BinaryConstants.FLOAT64_MAX;
  301. // NaN.
  302. jspb.utils.splitFloat64(NaN);
  303. if (!isNaN(jspb.utils.joinFloat64(jspb.utils.split64Low,
  304. jspb.utils.split64High))) {
  305. throw 'fail!';
  306. }
  307. /**
  308. * @param {number} x
  309. * @param {number=} opt_highBits
  310. * @param {number=} opt_lowBits
  311. */
  312. function test(x, opt_highBits, opt_lowBits) {
  313. jspb.utils.splitFloat64(x);
  314. if (goog.isDef(opt_highBits)) {
  315. if (opt_highBits != jspb.utils.split64High) throw 'fail!';
  316. }
  317. if (goog.isDef(opt_lowBits)) {
  318. if (opt_lowBits != jspb.utils.split64Low) throw 'fail!';
  319. }
  320. if (x != jspb.utils.joinFloat64(jspb.utils.split64Low,
  321. jspb.utils.split64High)) {
  322. throw 'fail!';
  323. }
  324. }
  325. // Positive and negative infinity.
  326. test(Infinity, 0x7ff00000, 0x00000000);
  327. test(-Infinity, 0xfff00000, 0x00000000);
  328. // Positive and negative zero.
  329. test(0, 0x00000000, 0x00000000);
  330. test(-0, 0x80000000, 0x00000000);
  331. // Positive and negative epsilon.
  332. test(f64_eps, 0x00000000, 0x00000001);
  333. test(-f64_eps, 0x80000000, 0x00000001);
  334. // Positive and negative min.
  335. test(f64_min, 0x00100000, 0x00000000);
  336. test(-f64_min, 0x80100000, 0x00000000);
  337. // Positive and negative max.
  338. test(f64_max, 0x7FEFFFFF, 0xFFFFFFFF);
  339. test(-f64_max, 0xFFEFFFFF, 0xFFFFFFFF);
  340. // Various positive values.
  341. var cursor = f64_eps * 10;
  342. while (cursor != Infinity) {
  343. test(cursor);
  344. cursor *= 1.1;
  345. }
  346. // Various negative values.
  347. cursor = -f64_eps * 10;
  348. while (cursor != -Infinity) {
  349. test(cursor);
  350. cursor *= 1.1;
  351. }
  352. });
  353. /**
  354. * Tests counting packed varints.
  355. */
  356. it('testCountVarints', function() {
  357. var writer = new jspb.BinaryWriter();
  358. var count = 0;
  359. for (var i = 1; i < 1000000000; i *= 1.1) {
  360. writer.rawWriteVarint(Math.floor(i));
  361. count++;
  362. }
  363. var buffer = new Uint8Array(writer.getResultBuffer());
  364. assertEquals(count, jspb.utils.countVarints(buffer, 0, buffer.length));
  365. });
  366. /**
  367. * Tests counting matching varint fields.
  368. */
  369. it('testCountVarintFields', function() {
  370. var writer = new jspb.BinaryWriter();
  371. var count = 0;
  372. for (var i = 1; i < 1000000000; i *= 1.1) {
  373. writer.writeUint64(1, Math.floor(i));
  374. count++;
  375. }
  376. writer.writeString(2, 'terminator');
  377. var buffer = new Uint8Array(writer.getResultBuffer());
  378. assertEquals(count,
  379. jspb.utils.countVarintFields(buffer, 0, buffer.length, 1));
  380. writer = new jspb.BinaryWriter();
  381. count = 0;
  382. for (var i = 1; i < 1000000000; i *= 1.1) {
  383. writer.writeUint64(123456789, Math.floor(i));
  384. count++;
  385. }
  386. writer.writeString(2, 'terminator');
  387. buffer = new Uint8Array(writer.getResultBuffer());
  388. assertEquals(count,
  389. jspb.utils.countVarintFields(buffer, 0, buffer.length, 123456789));
  390. });
  391. /**
  392. * Tests counting matching fixed32 fields.
  393. */
  394. it('testCountFixed32Fields', function() {
  395. var writer = new jspb.BinaryWriter();
  396. var count = 0;
  397. for (var i = 1; i < 1000000000; i *= 1.1) {
  398. writer.writeFixed32(1, Math.floor(i));
  399. count++;
  400. }
  401. writer.writeString(2, 'terminator');
  402. var buffer = new Uint8Array(writer.getResultBuffer());
  403. assertEquals(count,
  404. jspb.utils.countFixed32Fields(buffer, 0, buffer.length, 1));
  405. writer = new jspb.BinaryWriter();
  406. count = 0;
  407. for (var i = 1; i < 1000000000; i *= 1.1) {
  408. writer.writeFixed32(123456789, Math.floor(i));
  409. count++;
  410. }
  411. writer.writeString(2, 'terminator');
  412. buffer = new Uint8Array(writer.getResultBuffer());
  413. assertEquals(count,
  414. jspb.utils.countFixed32Fields(buffer, 0, buffer.length, 123456789));
  415. });
  416. /**
  417. * Tests counting matching fixed64 fields.
  418. */
  419. it('testCountFixed64Fields', function() {
  420. var writer = new jspb.BinaryWriter();
  421. var count = 0;
  422. for (var i = 1; i < 1000000000; i *= 1.1) {
  423. writer.writeDouble(1, i);
  424. count++;
  425. }
  426. writer.writeString(2, 'terminator');
  427. var buffer = new Uint8Array(writer.getResultBuffer());
  428. assertEquals(count,
  429. jspb.utils.countFixed64Fields(buffer, 0, buffer.length, 1));
  430. writer = new jspb.BinaryWriter();
  431. count = 0;
  432. for (var i = 1; i < 1000000000; i *= 1.1) {
  433. writer.writeDouble(123456789, i);
  434. count++;
  435. }
  436. writer.writeString(2, 'terminator');
  437. buffer = new Uint8Array(writer.getResultBuffer());
  438. assertEquals(count,
  439. jspb.utils.countFixed64Fields(buffer, 0, buffer.length, 123456789));
  440. });
  441. /**
  442. * Tests counting matching delimited fields.
  443. */
  444. it('testCountDelimitedFields', function() {
  445. var writer = new jspb.BinaryWriter();
  446. var count = 0;
  447. for (var i = 1; i < 1000; i *= 1.1) {
  448. writer.writeBytes(1, [Math.floor(i)]);
  449. count++;
  450. }
  451. writer.writeString(2, 'terminator');
  452. var buffer = new Uint8Array(writer.getResultBuffer());
  453. assertEquals(count,
  454. jspb.utils.countDelimitedFields(buffer, 0, buffer.length, 1));
  455. writer = new jspb.BinaryWriter();
  456. count = 0;
  457. for (var i = 1; i < 1000; i *= 1.1) {
  458. writer.writeBytes(123456789, [Math.floor(i)]);
  459. count++;
  460. }
  461. writer.writeString(2, 'terminator');
  462. buffer = new Uint8Array(writer.getResultBuffer());
  463. assertEquals(count,
  464. jspb.utils.countDelimitedFields(buffer, 0, buffer.length, 123456789));
  465. });
  466. /**
  467. * Tests byte format for debug strings.
  468. */
  469. it('testDebugBytesToTextFormat', function() {
  470. assertEquals('""', jspb.utils.debugBytesToTextFormat(null));
  471. assertEquals('"\\x00\\x10\\xff"',
  472. jspb.utils.debugBytesToTextFormat([0, 16, 255]));
  473. });
  474. /**
  475. * Tests converting byte blob sources into byte blobs.
  476. */
  477. it('testByteSourceToUint8Array', function() {
  478. var convert = jspb.utils.byteSourceToUint8Array;
  479. var sourceData = [];
  480. for (var i = 0; i < 256; i++) {
  481. sourceData.push(i);
  482. }
  483. var sourceBytes = new Uint8Array(sourceData);
  484. var sourceBuffer = sourceBytes.buffer;
  485. var sourceBase64 = goog.crypt.base64.encodeByteArray(sourceData);
  486. var sourceString = String.fromCharCode.apply(null, sourceData);
  487. function check(result) {
  488. assertEquals(Uint8Array, result.constructor);
  489. assertEquals(sourceData.length, result.length);
  490. for (var i = 0; i < result.length; i++) {
  491. assertEquals(sourceData[i], result[i]);
  492. }
  493. }
  494. // Converting Uint8Arrays into Uint8Arrays should be a no-op.
  495. assertEquals(sourceBytes, convert(sourceBytes));
  496. // Converting Array.<numbers> into Uint8Arrays should work.
  497. check(convert(sourceData));
  498. // Converting ArrayBuffers into Uint8Arrays should work.
  499. check(convert(sourceBuffer));
  500. // Converting base64-encoded strings into Uint8Arrays should work.
  501. check(convert(sourceBase64));
  502. // Converting binary-data strings into Uint8Arrays should work.
  503. check(convert(sourceString, /* opt_stringIsRawBytes = */ true));
  504. });
  505. });