uint8arrays_test.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * @fileoverview Tests for uint8arrays.js.
  3. */
  4. goog.module('protobuf.binary.Uint8ArraysTest');
  5. goog.setTestOnly();
  6. const {concatenateByteArrays} = goog.require('protobuf.binary.uint8arrays');
  7. describe('concatenateByteArrays does', () => {
  8. it('concatenate empty array', () => {
  9. const byteArrays = [];
  10. expect(concatenateByteArrays(byteArrays)).toEqual(new Uint8Array(0));
  11. });
  12. it('concatenate Uint8Arrays', () => {
  13. const byteArrays = [new Uint8Array([0x01]), new Uint8Array([0x02])];
  14. expect(concatenateByteArrays(byteArrays)).toEqual(new Uint8Array([
  15. 0x01, 0x02
  16. ]));
  17. });
  18. it('concatenate array of bytes', () => {
  19. const byteArrays = [[0x01], [0x02]];
  20. expect(concatenateByteArrays(byteArrays)).toEqual(new Uint8Array([
  21. 0x01, 0x02
  22. ]));
  23. });
  24. it('concatenate array of non-bytes', () => {
  25. // Note in unchecked mode we produce invalid output for invalid inputs.
  26. // This test just documents our behavior in those cases.
  27. // These values might change at any point and are not considered
  28. // what the implementation should be doing here.
  29. const byteArrays = [[40.0], [256]];
  30. expect(concatenateByteArrays(byteArrays)).toEqual(new Uint8Array([
  31. 0x28, 0x00
  32. ]));
  33. });
  34. it('throw for null array', () => {
  35. expect(
  36. () => concatenateByteArrays(
  37. /** @type {!Array<!Uint8Array>} */ (/** @type {*} */ (null))))
  38. .toThrow();
  39. });
  40. });