source: trip-planner-front/node_modules/hdr-histogram-js/assembly/__tests__/ByteBuffer.spec.ts

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2 * This is a AssemblyScript port of the original Java version, which was written by
3 * Gil Tene as described in
4 * https://github.com/HdrHistogram/HdrHistogram
5 * and released to the public domain, as explained at
6 * http://creativecommons.org/publicdomain/zero/1.0/
7 */
8
9import ByteBuffer from "../ByteBuffer";
10
11describe("ByteBuffer", () => {
12 it("should put value moving the position", () => {
13 // given
14 const buffer = ByteBuffer.allocate(3);
15 // when
16 buffer.put(<u8>123);
17 // then
18 expect<u8>(buffer.data[0]).toBe(123);
19 expect<i32>(buffer.position).toBe(1);
20 });
21
22 it("should resize when values overflow ", () => {
23 // given
24 const buffer = ByteBuffer.allocate(1);
25 buffer.put(<u8>123);
26 // when
27 buffer.put(<u8>42);
28 // then
29 expect<u8>(buffer.data[0]).toBe(123);
30 expect(buffer.data[1]).toBe(42);
31 });
32
33 it("should get value moving the position", () => {
34 // given
35 const buffer = ByteBuffer.allocate(1);
36 buffer.put(123);
37 buffer.resetPosition();
38 // when
39 const value = buffer.get();
40 // then
41 expect(value).toBe(123);
42 expect(buffer.position).toBe(1);
43 });
44
45 it("should put int32 value moving the position", () => {
46 // given
47 const buffer = ByteBuffer.allocate(8);
48 // when
49 buffer.putInt32(123);
50 // then
51 expect(buffer.data[3]).toBe(123);
52 expect(buffer.position).toBe(4);
53 });
54
55 it("should resize when int32 values overflow ", () => {
56 // given
57 const buffer = ByteBuffer.allocate(1);
58 // when
59 buffer.putInt32(42);
60 // then
61 expect(buffer.data[3]).toBe(42);
62 expect(buffer.position).toBe(4);
63 });
64
65 it("should get int32 value moving the position", () => {
66 // given
67 const buffer = ByteBuffer.allocate(1);
68 buffer.putInt32(123);
69 buffer.resetPosition();
70 // when
71 const value = buffer.getInt32();
72 // then
73 expect(value).toBe(123);
74 expect(buffer.position).toBe(4);
75 });
76
77 it("should put int64 value moving the position", () => {
78 // given
79 const buffer = ByteBuffer.allocate(8);
80 // when
81 buffer.putInt64(123);
82 // then
83 expect(buffer.data[7]).toBe(123);
84 expect(buffer.position).toBe(8);
85 });
86
87 it("should resize when int64 values overflow ", () => {
88 // given
89 const buffer = ByteBuffer.allocate(1);
90 // when
91 buffer.putInt64(42);
92 // then
93 expect(buffer.data[7]).toBe(42);
94 expect(buffer.position).toBe(8);
95 });
96
97 it("should get int64 value moving the position", () => {
98 // given
99 const buffer = ByteBuffer.allocate(1);
100 buffer.putInt64(u64.MAX_VALUE);
101 buffer.resetPosition();
102 // when
103 const value = buffer.getInt64();
104 // then
105 expect(value).toBe(u64.MAX_VALUE);
106 expect(buffer.position).toBe(8);
107 });
108
109 it("should copy all data when putting array", () => {
110 // given
111 const buffer = ByteBuffer.allocate(1024);
112 const array = new Uint8Array(4);
113 for (let index = 0; index < array.length; index++) {
114 array[index] = <u8>(index + 1);
115 }
116 // when
117 buffer.putArray(array);
118 // then
119 buffer.resetPosition();
120 expect(buffer.get()).toBe(1);
121 expect(buffer.get()).toBe(2);
122 expect(buffer.get()).toBe(3);
123 expect(buffer.get()).toBe(4);
124 });
125
126 it("should resize when putting array bigger than capacity", () => {
127 // given
128 const buffer = ByteBuffer.allocate(1024);
129 const array = new Uint8Array(4);
130 for (let index = 0; index < array.length; index++) {
131 array[index] = <u8>(index + 1);
132 }
133 // when
134 buffer.position = 1022;
135 buffer.putArray(array);
136 // then
137 buffer.position = 1022;
138 expect(buffer.get()).toBe(1);
139 expect(buffer.get()).toBe(2);
140 expect(buffer.get()).toBe(3);
141 expect(buffer.get()).toBe(4);
142 });
143});
Note: See TracBrowser for help on using the repository browser.