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