Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 20 | const test = require("tape"); |
Cameron Martin | 21ed4a2 | 2024-04-22 11:08:19 +0100 | [diff] [blame] | 21 | const binary = require("thrift/lib/nodejs/lib/thrift/binary"); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 22 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 23 | const cases = { |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 24 | "Should read signed byte": function (assert) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 25 | assert.equal(1, binary.readByte(0x01)); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 26 | assert.equal(-1, binary.readByte(0xff)); |
Randy Abernethy | 96f4f07 | 2015-02-10 02:29:15 -0800 | [diff] [blame] | 27 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 28 | assert.equal(127, binary.readByte(0x7f)); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 29 | assert.equal(-128, binary.readByte(0x80)); |
| 30 | assert.end(); |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 31 | }, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 32 | "Should write byte": function (assert) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 33 | //Protocol simply writes to the buffer. Nothing to test.. yet. |
| 34 | assert.ok(true); |
| 35 | assert.end(); |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 36 | }, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 37 | "Should read I16": function (assert) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 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 Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 41 | |
| 42 | // Min I16 |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 43 | assert.equal(-32768, binary.readI16([0x80, 0x00])); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 44 | // Max I16 |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 45 | assert.equal(32767, binary.readI16([0x7f, 0xff])); |
| 46 | assert.end(); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 47 | }, |
| 48 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 49 | "Should write I16": function (assert) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 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 Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 53 | |
| 54 | // Min I16 |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 55 | assert.deepEqual([0x80, 0x00], binary.writeI16([], -32768)); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 56 | // Max I16 |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 57 | assert.deepEqual([0x7f, 0xff], binary.writeI16([], 32767)); |
| 58 | assert.end(); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 59 | }, |
| 60 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 61 | "Should read I32": function (assert) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 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 Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 65 | |
| 66 | // Min I32 |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 67 | assert.equal(-2147483648, binary.readI32([0x80, 0x00, 0x00, 0x00])); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 68 | // Max I32 |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 69 | assert.equal(2147483647, binary.readI32([0x7f, 0xff, 0xff, 0xff])); |
| 70 | assert.end(); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 71 | }, |
| 72 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 73 | "Should write I32": function (assert) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 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 Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 77 | |
| 78 | // Min I32 |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 79 | assert.deepEqual( |
| 80 | [0x80, 0x00, 0x00, 0x00], |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 81 | binary.writeI32([], -2147483648), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 82 | ); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 83 | // Max I32 |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 84 | assert.deepEqual([0x7f, 0xff, 0xff, 0xff], binary.writeI32([], 2147483647)); |
| 85 | assert.end(); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 86 | }, |
| 87 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 88 | "Should read doubles": function (assert) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 89 | assert.equal( |
| 90 | 0, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 91 | binary.readDouble([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 92 | ); |
| 93 | assert.equal( |
| 94 | 0, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 95 | binary.readDouble([0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 96 | ); |
| 97 | assert.equal( |
| 98 | 1, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 99 | binary.readDouble([0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 100 | ); |
| 101 | assert.equal( |
| 102 | 2, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 103 | binary.readDouble([0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 104 | ); |
| 105 | assert.equal( |
| 106 | -2, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 107 | binary.readDouble([0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 108 | ); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 109 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 110 | assert.equal( |
| 111 | Math.PI, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 112 | binary.readDouble([0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18]), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 113 | ); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 114 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 115 | assert.equal( |
| 116 | Infinity, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 117 | binary.readDouble([0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 118 | ); |
| 119 | assert.equal( |
| 120 | -Infinity, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 121 | binary.readDouble([0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 122 | ); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 123 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 124 | assert.ok( |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 125 | isNaN( |
| 126 | binary.readDouble([0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
| 127 | ), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 128 | ); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 129 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 130 | assert.equal( |
| 131 | 1 / 3, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 132 | binary.readDouble([0x3f, 0xd5, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55]), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 133 | ); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 134 | |
| 135 | // Min subnormal positive double |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 136 | assert.equal( |
| 137 | 4.9406564584124654e-324, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 138 | binary.readDouble([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 139 | ); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 140 | // Min normal positive double |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 141 | assert.equal( |
| 142 | 2.2250738585072014e-308, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 143 | binary.readDouble([0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 144 | ); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 145 | // Max positive double |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 146 | assert.equal( |
| 147 | 1.7976931348623157e308, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 148 | binary.readDouble([0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 149 | ); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 150 | assert.end(); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 151 | }, |
| 152 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 153 | "Should write doubles": function (assert) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 154 | assert.deepEqual( |
| 155 | [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 156 | binary.writeDouble([], 0), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 157 | ); |
| 158 | assert.deepEqual( |
| 159 | [0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 160 | binary.writeDouble([], 1), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 161 | ); |
| 162 | assert.deepEqual( |
| 163 | [0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 164 | binary.writeDouble([], 2), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 165 | ); |
| 166 | assert.deepEqual( |
| 167 | [0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 168 | binary.writeDouble([], -2), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 169 | ); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 170 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 171 | assert.deepEqual( |
| 172 | [0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18], |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 173 | binary.writeDouble([], Math.PI), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 174 | ); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 175 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 176 | assert.deepEqual( |
| 177 | [0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 178 | binary.writeDouble([], Infinity), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 179 | ); |
| 180 | assert.deepEqual( |
| 181 | [0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 182 | binary.writeDouble([], -Infinity), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 183 | ); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 184 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 185 | assert.deepEqual( |
| 186 | [0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 187 | binary.writeDouble([], NaN), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 188 | ); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 189 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 190 | assert.deepEqual( |
| 191 | [0x3f, 0xd5, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55], |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 192 | binary.writeDouble([], 1 / 3), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 193 | ); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 194 | |
| 195 | // Min subnormal positive double |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 196 | assert.deepEqual( |
| 197 | [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01], |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 198 | binary.writeDouble([], 4.9406564584124654e-324), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 199 | ); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 200 | // Min normal positive double |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 201 | assert.deepEqual( |
| 202 | [0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 203 | binary.writeDouble([], 2.2250738585072014e-308), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 204 | ); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 205 | // Max positive double |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 206 | assert.deepEqual( |
| 207 | [0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 208 | binary.writeDouble([], 1.7976931348623157e308), |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 209 | ); |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 210 | assert.end(); |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 211 | }, |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 212 | }; |
| 213 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 214 | Object.keys(cases).forEach(function (caseName) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 215 | test(caseName, cases[caseName]); |
Randy Abernethy | cf743d7 | 2015-02-02 05:56:14 -0800 | [diff] [blame] | 216 | }); |