blob: 187cd1874344519d2d48c63b029f41d83f5fc63f [file] [log] [blame]
Henrique Mendonca50fb5012012-10-26 07:29:47 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
bforbisda1169d2018-10-28 11:27:38 -040020const test = require("tape");
21const binary = require("thrift/binary");
Henrique Mendonca50fb5012012-10-26 07:29:47 +000022
bforbisda1169d2018-10-28 11:27:38 -040023const cases = {
24 "Should read signed byte": function(assert) {
Randy Abernethyd8187c52015-02-16 01:25:53 -080025 assert.equal(1, binary.readByte(0x01));
bforbisda1169d2018-10-28 11:27:38 -040026 assert.equal(-1, binary.readByte(0xff));
Randy Abernethy96f4f072015-02-10 02:29:15 -080027
bforbisda1169d2018-10-28 11:27:38 -040028 assert.equal(127, binary.readByte(0x7f));
Randy Abernethyd8187c52015-02-16 01:25:53 -080029 assert.equal(-128, binary.readByte(0x80));
30 assert.end();
Henrique99be0272013-05-10 23:43:12 +020031 },
bforbisda1169d2018-10-28 11:27:38 -040032 "Should write byte": function(assert) {
Randy Abernethyd8187c52015-02-16 01:25:53 -080033 //Protocol simply writes to the buffer. Nothing to test.. yet.
34 assert.ok(true);
35 assert.end();
Henrique99be0272013-05-10 23:43:12 +020036 },
Randy Abernethyd8187c52015-02-16 01:25:53 -080037 "Should read I16": function(assert) {
38 assert.equal(0, binary.readI16([0x00, 0x00]));
39 assert.equal(1, binary.readI16([0x00, 0x01]));
40 assert.equal(-1, binary.readI16([0xff, 0xff]));
Henrique Mendonca50fb5012012-10-26 07:29:47 +000041
42 // Min I16
Randy Abernethyd8187c52015-02-16 01:25:53 -080043 assert.equal(-32768, binary.readI16([0x80, 0x00]));
Henrique Mendonca50fb5012012-10-26 07:29:47 +000044 // Max I16
Randy Abernethyd8187c52015-02-16 01:25:53 -080045 assert.equal(32767, binary.readI16([0x7f, 0xff]));
46 assert.end();
Henrique Mendonca50fb5012012-10-26 07:29:47 +000047 },
48
Randy Abernethyd8187c52015-02-16 01:25:53 -080049 "Should write I16": function(assert) {
50 assert.deepEqual([0x00, 0x00], binary.writeI16([], 0));
51 assert.deepEqual([0x00, 0x01], binary.writeI16([], 1));
52 assert.deepEqual([0xff, 0xff], binary.writeI16([], -1));
Henrique Mendonca50fb5012012-10-26 07:29:47 +000053
54 // Min I16
Randy Abernethyd8187c52015-02-16 01:25:53 -080055 assert.deepEqual([0x80, 0x00], binary.writeI16([], -32768));
Henrique Mendonca50fb5012012-10-26 07:29:47 +000056 // Max I16
Randy Abernethyd8187c52015-02-16 01:25:53 -080057 assert.deepEqual([0x7f, 0xff], binary.writeI16([], 32767));
58 assert.end();
Henrique Mendonca50fb5012012-10-26 07:29:47 +000059 },
60
Randy Abernethyd8187c52015-02-16 01:25:53 -080061 "Should read I32": function(assert) {
62 assert.equal(0, binary.readI32([0x00, 0x00, 0x00, 0x00]));
63 assert.equal(1, binary.readI32([0x00, 0x00, 0x00, 0x01]));
64 assert.equal(-1, binary.readI32([0xff, 0xff, 0xff, 0xff]));
Henrique Mendonca50fb5012012-10-26 07:29:47 +000065
66 // Min I32
Randy Abernethyd8187c52015-02-16 01:25:53 -080067 assert.equal(-2147483648, binary.readI32([0x80, 0x00, 0x00, 0x00]));
Henrique Mendonca50fb5012012-10-26 07:29:47 +000068 // Max I32
Randy Abernethyd8187c52015-02-16 01:25:53 -080069 assert.equal(2147483647, binary.readI32([0x7f, 0xff, 0xff, 0xff]));
70 assert.end();
Henrique Mendonca50fb5012012-10-26 07:29:47 +000071 },
72
Randy Abernethyd8187c52015-02-16 01:25:53 -080073 "Should write I32": function(assert) {
74 assert.deepEqual([0x00, 0x00, 0x00, 0x00], binary.writeI32([], 0));
75 assert.deepEqual([0x00, 0x00, 0x00, 0x01], binary.writeI32([], 1));
76 assert.deepEqual([0xff, 0xff, 0xff, 0xff], binary.writeI32([], -1));
Henrique Mendonca50fb5012012-10-26 07:29:47 +000077
78 // Min I32
bforbisda1169d2018-10-28 11:27:38 -040079 assert.deepEqual(
80 [0x80, 0x00, 0x00, 0x00],
81 binary.writeI32([], -2147483648)
82 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +000083 // Max I32
Randy Abernethyd8187c52015-02-16 01:25:53 -080084 assert.deepEqual([0x7f, 0xff, 0xff, 0xff], binary.writeI32([], 2147483647));
85 assert.end();
Henrique Mendonca50fb5012012-10-26 07:29:47 +000086 },
87
Randy Abernethyd8187c52015-02-16 01:25:53 -080088 "Should read doubles": function(assert) {
bforbisda1169d2018-10-28 11:27:38 -040089 assert.equal(
90 0,
91 binary.readDouble([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
92 );
93 assert.equal(
94 0,
95 binary.readDouble([0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
96 );
97 assert.equal(
98 1,
99 binary.readDouble([0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
100 );
101 assert.equal(
102 2,
103 binary.readDouble([0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
104 );
105 assert.equal(
106 -2,
107 binary.readDouble([0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
108 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000109
bforbisda1169d2018-10-28 11:27:38 -0400110 assert.equal(
111 Math.PI,
112 binary.readDouble([0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18])
113 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000114
bforbisda1169d2018-10-28 11:27:38 -0400115 assert.equal(
116 Infinity,
117 binary.readDouble([0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
118 );
119 assert.equal(
120 -Infinity,
121 binary.readDouble([0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
122 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000123
bforbisda1169d2018-10-28 11:27:38 -0400124 assert.ok(
125 isNaN(binary.readDouble([0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
126 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000127
bforbisda1169d2018-10-28 11:27:38 -0400128 assert.equal(
129 1 / 3,
130 binary.readDouble([0x3f, 0xd5, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55])
131 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000132
133 // Min subnormal positive double
bforbisda1169d2018-10-28 11:27:38 -0400134 assert.equal(
135 4.9406564584124654e-324,
136 binary.readDouble([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01])
137 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000138 // Min normal positive double
bforbisda1169d2018-10-28 11:27:38 -0400139 assert.equal(
140 2.2250738585072014e-308,
141 binary.readDouble([0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
142 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000143 // Max positive double
bforbisda1169d2018-10-28 11:27:38 -0400144 assert.equal(
145 1.7976931348623157e308,
146 binary.readDouble([0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
147 );
Randy Abernethyd8187c52015-02-16 01:25:53 -0800148 assert.end();
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000149 },
150
Randy Abernethyd8187c52015-02-16 01:25:53 -0800151 "Should write doubles": function(assert) {
bforbisda1169d2018-10-28 11:27:38 -0400152 assert.deepEqual(
153 [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
154 binary.writeDouble([], 0)
155 );
156 assert.deepEqual(
157 [0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
158 binary.writeDouble([], 1)
159 );
160 assert.deepEqual(
161 [0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
162 binary.writeDouble([], 2)
163 );
164 assert.deepEqual(
165 [0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
166 binary.writeDouble([], -2)
167 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000168
bforbisda1169d2018-10-28 11:27:38 -0400169 assert.deepEqual(
170 [0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18],
171 binary.writeDouble([], Math.PI)
172 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000173
bforbisda1169d2018-10-28 11:27:38 -0400174 assert.deepEqual(
175 [0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
176 binary.writeDouble([], Infinity)
177 );
178 assert.deepEqual(
179 [0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
180 binary.writeDouble([], -Infinity)
181 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000182
bforbisda1169d2018-10-28 11:27:38 -0400183 assert.deepEqual(
184 [0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
185 binary.writeDouble([], NaN)
186 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000187
bforbisda1169d2018-10-28 11:27:38 -0400188 assert.deepEqual(
189 [0x3f, 0xd5, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55],
190 binary.writeDouble([], 1 / 3)
191 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000192
193 // Min subnormal positive double
bforbisda1169d2018-10-28 11:27:38 -0400194 assert.deepEqual(
195 [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01],
196 binary.writeDouble([], 4.9406564584124654e-324)
197 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000198 // Min normal positive double
bforbisda1169d2018-10-28 11:27:38 -0400199 assert.deepEqual(
200 [0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
201 binary.writeDouble([], 2.2250738585072014e-308)
202 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000203 // Max positive double
bforbisda1169d2018-10-28 11:27:38 -0400204 assert.deepEqual(
205 [0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
206 binary.writeDouble([], 1.7976931348623157e308)
207 );
Randy Abernethyd8187c52015-02-16 01:25:53 -0800208 assert.end();
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000209 }
Randy Abernethyd8187c52015-02-16 01:25:53 -0800210};
211
212Object.keys(cases).forEach(function(caseName) {
213 test(caseName, cases[caseName]);
Randy Abernethycf743d72015-02-02 05:56:14 -0800214});