1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | const ByteBuffer_1 = require("./ByteBuffer");
|
---|
4 | const ZigZagEncoding_1 = require("./ZigZagEncoding");
|
---|
5 | describe("Zig Zag Encoding", () => {
|
---|
6 | it("should encode int using one byte when value is less than 64", () => {
|
---|
7 | // given
|
---|
8 | const buffer = ByteBuffer_1.default.allocate(4);
|
---|
9 | // when
|
---|
10 | ZigZagEncoding_1.default.encode(buffer, 56);
|
---|
11 | // then
|
---|
12 | expect(buffer.data).toHaveLength(4);
|
---|
13 | expect(buffer.data[0]).toBe(112);
|
---|
14 | });
|
---|
15 | it("should encode int using several bytes when value is more than 64", () => {
|
---|
16 | // given
|
---|
17 | const buffer = ByteBuffer_1.default.allocate(4);
|
---|
18 | // when
|
---|
19 | ZigZagEncoding_1.default.encode(buffer, 456);
|
---|
20 | // then
|
---|
21 | expect(buffer.data).toHaveLength(4);
|
---|
22 | expect(Array.from(buffer.data)).toEqual([144, 7, 0, 0]);
|
---|
23 | });
|
---|
24 | it("should encode negative int using several bytes when value is more than 64", () => {
|
---|
25 | // given
|
---|
26 | const buffer = ByteBuffer_1.default.allocate(4);
|
---|
27 | // when
|
---|
28 | ZigZagEncoding_1.default.encode(buffer, -456);
|
---|
29 | // then
|
---|
30 | expect(buffer.data).toHaveLength(4);
|
---|
31 | expect(Array.from(buffer.data)).toEqual([143, 7, 0, 0]);
|
---|
32 | });
|
---|
33 | it("should encode large safe int greater than 2^32", () => {
|
---|
34 | // given
|
---|
35 | const buffer = ByteBuffer_1.default.allocate(4);
|
---|
36 | // when
|
---|
37 | ZigZagEncoding_1.default.encode(buffer, Math.pow(2, 50));
|
---|
38 | // then
|
---|
39 | expect(buffer.data).toHaveLength(8);
|
---|
40 | expect(Array.from(buffer.data)).toEqual([
|
---|
41 | 128,
|
---|
42 | 128,
|
---|
43 | 128,
|
---|
44 | 128,
|
---|
45 | 128,
|
---|
46 | 128,
|
---|
47 | 128,
|
---|
48 | 4,
|
---|
49 | ]);
|
---|
50 | });
|
---|
51 | it("should decode int using one byte", () => {
|
---|
52 | // given
|
---|
53 | const buffer = ByteBuffer_1.default.allocate(8);
|
---|
54 | ZigZagEncoding_1.default.encode(buffer, 56);
|
---|
55 | buffer.resetPosition();
|
---|
56 | // when
|
---|
57 | const value = ZigZagEncoding_1.default.decode(buffer);
|
---|
58 | // then
|
---|
59 | expect(value).toBe(56);
|
---|
60 | });
|
---|
61 | it("should decode int using multiple bytes", () => {
|
---|
62 | // given
|
---|
63 | const buffer = ByteBuffer_1.default.allocate(8);
|
---|
64 | ZigZagEncoding_1.default.encode(buffer, 70000);
|
---|
65 | ZigZagEncoding_1.default.encode(buffer, 56);
|
---|
66 | buffer.resetPosition();
|
---|
67 | // when
|
---|
68 | const value = ZigZagEncoding_1.default.decode(buffer);
|
---|
69 | // then
|
---|
70 | expect(value).toBe(70000);
|
---|
71 | });
|
---|
72 | it("should decode negative int using multiple bytes", () => {
|
---|
73 | // given
|
---|
74 | const buffer = ByteBuffer_1.default.allocate(8);
|
---|
75 | ZigZagEncoding_1.default.encode(buffer, -1515);
|
---|
76 | ZigZagEncoding_1.default.encode(buffer, 56);
|
---|
77 | buffer.resetPosition();
|
---|
78 | // when
|
---|
79 | const value = ZigZagEncoding_1.default.decode(buffer);
|
---|
80 | // then
|
---|
81 | expect(value).toBe(-1515);
|
---|
82 | });
|
---|
83 | it("should decode large safe int greater than 2^32", () => {
|
---|
84 | // given
|
---|
85 | const buffer = ByteBuffer_1.default.allocate(4);
|
---|
86 | ZigZagEncoding_1.default.encode(buffer, Math.pow(2, 50) + 1234);
|
---|
87 | ZigZagEncoding_1.default.encode(buffer, 56);
|
---|
88 | buffer.resetPosition();
|
---|
89 | // when
|
---|
90 | const value = ZigZagEncoding_1.default.decode(buffer);
|
---|
91 | // then
|
---|
92 | expect(value).toBe(Math.pow(2, 50) + 1234);
|
---|
93 | });
|
---|
94 | });
|
---|
95 | //# sourceMappingURL=ZigZagEncoding.spec.js.map |
---|