utils_test.js 20 KB

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