ByteBuffer.spec.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const ByteBuffer_1 = require("./ByteBuffer");
  4. describe("ByteBuffer", () => {
  5. it("should put value moving the position", () => {
  6. // given
  7. const buffer = ByteBuffer_1.default.allocate(3);
  8. // when
  9. buffer.put(123);
  10. // then
  11. expect(buffer.data[0]).toBe(123);
  12. expect(buffer.position).toBe(1);
  13. });
  14. it("should resize when values overflow ", () => {
  15. // given
  16. const buffer = ByteBuffer_1.default.allocate(1);
  17. buffer.put(123);
  18. // when
  19. buffer.put(42);
  20. // then
  21. expect(buffer.data[0]).toBe(123);
  22. expect(buffer.data[1]).toBe(42);
  23. });
  24. it("should get value moving the position", () => {
  25. // given
  26. const buffer = ByteBuffer_1.default.allocate(1);
  27. buffer.put(123);
  28. buffer.resetPosition();
  29. // when
  30. const value = buffer.get();
  31. // then
  32. expect(value).toBe(123);
  33. expect(buffer.position).toBe(1);
  34. });
  35. it("should put int32 value moving the position", () => {
  36. // given
  37. const buffer = ByteBuffer_1.default.allocate(8);
  38. // when
  39. buffer.putInt32(123);
  40. // then
  41. expect(buffer.data[3]).toBe(123);
  42. expect(buffer.position).toBe(4);
  43. });
  44. it("should resize when int32 values overflow ", () => {
  45. // given
  46. const buffer = ByteBuffer_1.default.allocate(1);
  47. // when
  48. buffer.putInt32(42);
  49. // then
  50. expect(buffer.data[3]).toBe(42);
  51. expect(buffer.position).toBe(4);
  52. });
  53. it("should get int32 value moving the position", () => {
  54. // given
  55. const buffer = ByteBuffer_1.default.allocate(1);
  56. buffer.putInt32(123);
  57. buffer.resetPosition();
  58. // when
  59. const value = buffer.getInt32();
  60. // then
  61. expect(value).toBe(123);
  62. expect(buffer.position).toBe(4);
  63. });
  64. it("should put int64 value moving the position", () => {
  65. // given
  66. const buffer = ByteBuffer_1.default.allocate(8);
  67. // when
  68. buffer.putInt64(123);
  69. // then
  70. expect(buffer.data[7]).toBe(123);
  71. expect(buffer.position).toBe(8);
  72. });
  73. it("should resize when int64 values overflow ", () => {
  74. // given
  75. const buffer = ByteBuffer_1.default.allocate(1);
  76. // when
  77. buffer.putInt64(42);
  78. // then
  79. expect(buffer.data[7]).toBe(42);
  80. expect(buffer.position).toBe(8);
  81. });
  82. it("should get int64 value moving the position", () => {
  83. // given
  84. const buffer = ByteBuffer_1.default.allocate(1);
  85. buffer.putInt64(Number.MAX_SAFE_INTEGER);
  86. buffer.resetPosition();
  87. // when
  88. const value = buffer.getInt64();
  89. // then
  90. expect(value).toBe(Number.MAX_SAFE_INTEGER);
  91. expect(buffer.position).toBe(8);
  92. });
  93. it("should copy all data when putting array", () => {
  94. // given
  95. const buffer = ByteBuffer_1.default.allocate(1024);
  96. const array = new Uint8Array([1, 2, 3, 4]);
  97. // when
  98. buffer.putArray(array);
  99. // then
  100. buffer.resetPosition();
  101. expect(buffer.get()).toBe(1);
  102. expect(buffer.get()).toBe(2);
  103. expect(buffer.get()).toBe(3);
  104. expect(buffer.get()).toBe(4);
  105. });
  106. it("should resize when putting array bigger than capacity", () => {
  107. // given
  108. const buffer = ByteBuffer_1.default.allocate(1024);
  109. const array = new Uint8Array([1, 2, 3, 4]);
  110. // when
  111. buffer.position = 1022;
  112. buffer.putArray(array);
  113. // then
  114. buffer.position = 1022;
  115. expect(buffer.get()).toBe(1);
  116. expect(buffer.get()).toBe(2);
  117. expect(buffer.get()).toBe(3);
  118. expect(buffer.get()).toBe(4);
  119. });
  120. });
  121. //# sourceMappingURL=ByteBuffer.spec.js.map