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