blob: 1e749d8e3d5af8901a3c2f0e54ba72888b700fa8 [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");
Cameron Martin21ed4a22024-04-22 11:08:19 +010021const binary = require("thrift/lib/nodejs/lib/thrift/binary");
Henrique Mendonca50fb5012012-10-26 07:29:47 +000022
bforbisda1169d2018-10-28 11:27:38 -040023const cases = {
Cameron Martincaef0ed2025-01-15 11:58:39 +010024 "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 },
Cameron Martincaef0ed2025-01-15 11:58:39 +010032 "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 },
Cameron Martincaef0ed2025-01-15 11:58:39 +010037 "Should read I16": function (assert) {
Randy Abernethyd8187c52015-02-16 01:25:53 -080038 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
Cameron Martincaef0ed2025-01-15 11:58:39 +010049 "Should write I16": function (assert) {
Randy Abernethyd8187c52015-02-16 01:25:53 -080050 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
Cameron Martincaef0ed2025-01-15 11:58:39 +010061 "Should read I32": function (assert) {
Randy Abernethyd8187c52015-02-16 01:25:53 -080062 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
Cameron Martincaef0ed2025-01-15 11:58:39 +010073 "Should write I32": function (assert) {
Randy Abernethyd8187c52015-02-16 01:25:53 -080074 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],
Cameron Martincaef0ed2025-01-15 11:58:39 +010081 binary.writeI32([], -2147483648),
bforbisda1169d2018-10-28 11:27:38 -040082 );
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
Cameron Martincaef0ed2025-01-15 11:58:39 +010088 "Should read doubles": function (assert) {
bforbisda1169d2018-10-28 11:27:38 -040089 assert.equal(
90 0,
Cameron Martincaef0ed2025-01-15 11:58:39 +010091 binary.readDouble([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
bforbisda1169d2018-10-28 11:27:38 -040092 );
93 assert.equal(
94 0,
Cameron Martincaef0ed2025-01-15 11:58:39 +010095 binary.readDouble([0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
bforbisda1169d2018-10-28 11:27:38 -040096 );
97 assert.equal(
98 1,
Cameron Martincaef0ed2025-01-15 11:58:39 +010099 binary.readDouble([0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
bforbisda1169d2018-10-28 11:27:38 -0400100 );
101 assert.equal(
102 2,
Cameron Martincaef0ed2025-01-15 11:58:39 +0100103 binary.readDouble([0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
bforbisda1169d2018-10-28 11:27:38 -0400104 );
105 assert.equal(
106 -2,
Cameron Martincaef0ed2025-01-15 11:58:39 +0100107 binary.readDouble([0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
bforbisda1169d2018-10-28 11:27:38 -0400108 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000109
bforbisda1169d2018-10-28 11:27:38 -0400110 assert.equal(
111 Math.PI,
Cameron Martincaef0ed2025-01-15 11:58:39 +0100112 binary.readDouble([0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18]),
bforbisda1169d2018-10-28 11:27:38 -0400113 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000114
bforbisda1169d2018-10-28 11:27:38 -0400115 assert.equal(
116 Infinity,
Cameron Martincaef0ed2025-01-15 11:58:39 +0100117 binary.readDouble([0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
bforbisda1169d2018-10-28 11:27:38 -0400118 );
119 assert.equal(
120 -Infinity,
Cameron Martincaef0ed2025-01-15 11:58:39 +0100121 binary.readDouble([0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
bforbisda1169d2018-10-28 11:27:38 -0400122 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000123
bforbisda1169d2018-10-28 11:27:38 -0400124 assert.ok(
Cameron Martincaef0ed2025-01-15 11:58:39 +0100125 isNaN(
126 binary.readDouble([0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
127 ),
bforbisda1169d2018-10-28 11:27:38 -0400128 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000129
bforbisda1169d2018-10-28 11:27:38 -0400130 assert.equal(
131 1 / 3,
Cameron Martincaef0ed2025-01-15 11:58:39 +0100132 binary.readDouble([0x3f, 0xd5, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55]),
bforbisda1169d2018-10-28 11:27:38 -0400133 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000134
135 // Min subnormal positive double
bforbisda1169d2018-10-28 11:27:38 -0400136 assert.equal(
137 4.9406564584124654e-324,
Cameron Martincaef0ed2025-01-15 11:58:39 +0100138 binary.readDouble([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]),
bforbisda1169d2018-10-28 11:27:38 -0400139 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000140 // Min normal positive double
bforbisda1169d2018-10-28 11:27:38 -0400141 assert.equal(
142 2.2250738585072014e-308,
Cameron Martincaef0ed2025-01-15 11:58:39 +0100143 binary.readDouble([0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
bforbisda1169d2018-10-28 11:27:38 -0400144 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000145 // Max positive double
bforbisda1169d2018-10-28 11:27:38 -0400146 assert.equal(
147 1.7976931348623157e308,
Cameron Martincaef0ed2025-01-15 11:58:39 +0100148 binary.readDouble([0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
bforbisda1169d2018-10-28 11:27:38 -0400149 );
Randy Abernethyd8187c52015-02-16 01:25:53 -0800150 assert.end();
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000151 },
152
Cameron Martincaef0ed2025-01-15 11:58:39 +0100153 "Should write doubles": function (assert) {
bforbisda1169d2018-10-28 11:27:38 -0400154 assert.deepEqual(
155 [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
Cameron Martincaef0ed2025-01-15 11:58:39 +0100156 binary.writeDouble([], 0),
bforbisda1169d2018-10-28 11:27:38 -0400157 );
158 assert.deepEqual(
159 [0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
Cameron Martincaef0ed2025-01-15 11:58:39 +0100160 binary.writeDouble([], 1),
bforbisda1169d2018-10-28 11:27:38 -0400161 );
162 assert.deepEqual(
163 [0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
Cameron Martincaef0ed2025-01-15 11:58:39 +0100164 binary.writeDouble([], 2),
bforbisda1169d2018-10-28 11:27:38 -0400165 );
166 assert.deepEqual(
167 [0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
Cameron Martincaef0ed2025-01-15 11:58:39 +0100168 binary.writeDouble([], -2),
bforbisda1169d2018-10-28 11:27:38 -0400169 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000170
bforbisda1169d2018-10-28 11:27:38 -0400171 assert.deepEqual(
172 [0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18],
Cameron Martincaef0ed2025-01-15 11:58:39 +0100173 binary.writeDouble([], Math.PI),
bforbisda1169d2018-10-28 11:27:38 -0400174 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000175
bforbisda1169d2018-10-28 11:27:38 -0400176 assert.deepEqual(
177 [0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
Cameron Martincaef0ed2025-01-15 11:58:39 +0100178 binary.writeDouble([], Infinity),
bforbisda1169d2018-10-28 11:27:38 -0400179 );
180 assert.deepEqual(
181 [0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
Cameron Martincaef0ed2025-01-15 11:58:39 +0100182 binary.writeDouble([], -Infinity),
bforbisda1169d2018-10-28 11:27:38 -0400183 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000184
bforbisda1169d2018-10-28 11:27:38 -0400185 assert.deepEqual(
186 [0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
Cameron Martincaef0ed2025-01-15 11:58:39 +0100187 binary.writeDouble([], NaN),
bforbisda1169d2018-10-28 11:27:38 -0400188 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000189
bforbisda1169d2018-10-28 11:27:38 -0400190 assert.deepEqual(
191 [0x3f, 0xd5, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55],
Cameron Martincaef0ed2025-01-15 11:58:39 +0100192 binary.writeDouble([], 1 / 3),
bforbisda1169d2018-10-28 11:27:38 -0400193 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000194
195 // Min subnormal positive double
bforbisda1169d2018-10-28 11:27:38 -0400196 assert.deepEqual(
197 [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01],
Cameron Martincaef0ed2025-01-15 11:58:39 +0100198 binary.writeDouble([], 4.9406564584124654e-324),
bforbisda1169d2018-10-28 11:27:38 -0400199 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000200 // Min normal positive double
bforbisda1169d2018-10-28 11:27:38 -0400201 assert.deepEqual(
202 [0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
Cameron Martincaef0ed2025-01-15 11:58:39 +0100203 binary.writeDouble([], 2.2250738585072014e-308),
bforbisda1169d2018-10-28 11:27:38 -0400204 );
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000205 // Max positive double
bforbisda1169d2018-10-28 11:27:38 -0400206 assert.deepEqual(
207 [0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
Cameron Martincaef0ed2025-01-15 11:58:39 +0100208 binary.writeDouble([], 1.7976931348623157e308),
bforbisda1169d2018-10-28 11:27:38 -0400209 );
Randy Abernethyd8187c52015-02-16 01:25:53 -0800210 assert.end();
Cameron Martincaef0ed2025-01-15 11:58:39 +0100211 },
Randy Abernethyd8187c52015-02-16 01:25:53 -0800212};
213
Cameron Martincaef0ed2025-01-15 11:58:39 +0100214Object.keys(cases).forEach(function (caseName) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800215 test(caseName, cases[caseName]);
Randy Abernethycf743d72015-02-02 05:56:14 -0800216});