source: trip-planner-front/node_modules/hdr-histogram-js/src/ZigZagEncoding.spec.ts@ e29cc2e

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

initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1import ByteBuffer from "./ByteBuffer";
2import ZigZagEncoding from "./ZigZagEncoding";
3
4describe("Zig Zag Encoding", () => {
5 it("should encode int using one byte when value is less than 64", () => {
6 // given
7 const buffer = ByteBuffer.allocate(4);
8 // when
9 ZigZagEncoding.encode(buffer, 56);
10 // then
11 expect(buffer.data).toHaveLength(4);
12 expect(buffer.data[0]).toBe(112);
13 });
14
15 it("should encode int using several bytes when value is more than 64", () => {
16 // given
17 const buffer = ByteBuffer.allocate(4);
18 // when
19 ZigZagEncoding.encode(buffer, 456);
20 // then
21 expect(buffer.data).toHaveLength(4);
22 expect(Array.from(buffer.data)).toEqual([144, 7, 0, 0]);
23 });
24
25 it("should encode negative int using several bytes when value is more than 64", () => {
26 // given
27 const buffer = ByteBuffer.allocate(4);
28 // when
29 ZigZagEncoding.encode(buffer, -456);
30 // then
31 expect(buffer.data).toHaveLength(4);
32 expect(Array.from(buffer.data)).toEqual([143, 7, 0, 0]);
33 });
34
35 it("should encode large safe int greater than 2^32", () => {
36 // given
37 const buffer = ByteBuffer.allocate(4);
38 // when
39 ZigZagEncoding.encode(buffer, Math.pow(2, 50));
40 // then
41 expect(buffer.data).toHaveLength(8);
42 expect(Array.from(buffer.data)).toEqual([
43 128,
44 128,
45 128,
46 128,
47 128,
48 128,
49 128,
50 4,
51 ]);
52 });
53
54 it("should decode int using one byte", () => {
55 // given
56 const buffer = ByteBuffer.allocate(8);
57 ZigZagEncoding.encode(buffer, 56);
58 buffer.resetPosition();
59 // when
60 const value = ZigZagEncoding.decode(buffer);
61 // then
62 expect(value).toBe(56);
63 });
64
65 it("should decode int using multiple bytes", () => {
66 // given
67 const buffer = ByteBuffer.allocate(8);
68 ZigZagEncoding.encode(buffer, 70000);
69 ZigZagEncoding.encode(buffer, 56);
70 buffer.resetPosition();
71 // when
72 const value = ZigZagEncoding.decode(buffer);
73 // then
74 expect(value).toBe(70000);
75 });
76
77 it("should decode negative int using multiple bytes", () => {
78 // given
79 const buffer = ByteBuffer.allocate(8);
80 ZigZagEncoding.encode(buffer, -1515);
81 ZigZagEncoding.encode(buffer, 56);
82 buffer.resetPosition();
83 // when
84 const value = ZigZagEncoding.decode(buffer);
85 // then
86 expect(value).toBe(-1515);
87 });
88
89 it("should decode large safe int greater than 2^32", () => {
90 // given
91 const buffer = ByteBuffer.allocate(4);
92 ZigZagEncoding.encode(buffer, Math.pow(2, 50) + 1234);
93 ZigZagEncoding.encode(buffer, 56);
94 buffer.resetPosition();
95 // when
96 const value = ZigZagEncoding.decode(buffer);
97 // then
98 expect(value).toBe(Math.pow(2, 50) + 1234);
99 });
100});
Note: See TracBrowser for help on using the repository browser.