blob: 38ba63411e49de8d201c89877eff558dbdd66f0c [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
Randy Abernethyd8187c52015-02-16 01:25:53 -080020var test = require('tape');
Henrique Mendonca50fb5012012-10-26 07:29:47 +000021var binary = require('thrift/binary');
22
Randy Abernethyd8187c52015-02-16 01:25:53 -080023var cases = {
24 "Should read signed byte": function(assert){
25 assert.equal(1, binary.readByte(0x01));
26 assert.equal(-1, binary.readByte(0xFF));
Randy Abernethy96f4f072015-02-10 02:29:15 -080027
Randy Abernethyd8187c52015-02-16 01:25:53 -080028 assert.equal(127, binary.readByte(0x7F));
29 assert.equal(-128, binary.readByte(0x80));
30 assert.end();
Henrique99be0272013-05-10 23:43:12 +020031 },
Randy Abernethyd8187c52015-02-16 01:25:53 -080032 "Should write byte": function(assert){
33 //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
Randy Abernethyd8187c52015-02-16 01:25:53 -080079 assert.deepEqual([0x80, 0x00, 0x00, 0x00], binary.writeI32([], -2147483648));
Henrique Mendonca50fb5012012-10-26 07:29:47 +000080 // Max I32
Randy Abernethyd8187c52015-02-16 01:25:53 -080081 assert.deepEqual([0x7f, 0xff, 0xff, 0xff], binary.writeI32([], 2147483647));
82 assert.end();
Henrique Mendonca50fb5012012-10-26 07:29:47 +000083 },
84
Randy Abernethyd8187c52015-02-16 01:25:53 -080085 "Should read doubles": function(assert) {
86 assert.equal(0, binary.readDouble([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
87 assert.equal(0, binary.readDouble([0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
88 assert.equal(1, binary.readDouble([0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
89 assert.equal(2, binary.readDouble([0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
90 assert.equal(-2, binary.readDouble([0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
Henrique Mendonca50fb5012012-10-26 07:29:47 +000091
Randy Abernethyd8187c52015-02-16 01:25:53 -080092 assert.equal(Math.PI, binary.readDouble([0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18]))
Henrique Mendonca50fb5012012-10-26 07:29:47 +000093
Randy Abernethyd8187c52015-02-16 01:25:53 -080094 assert.equal(Infinity, binary.readDouble([0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
95 assert.equal(-Infinity, binary.readDouble([0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
Henrique Mendonca50fb5012012-10-26 07:29:47 +000096
Randy Abernethyd8187c52015-02-16 01:25:53 -080097 assert.ok(isNaN(binary.readDouble([0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])))
Henrique Mendonca50fb5012012-10-26 07:29:47 +000098
Randy Abernethyd8187c52015-02-16 01:25:53 -080099 assert.equal(1/3, binary.readDouble([0x3f, 0xd5, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55]))
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000100
101 // Min subnormal positive double
Randy Abernethyd8187c52015-02-16 01:25:53 -0800102 assert.equal(4.9406564584124654e-324, binary.readDouble([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]))
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000103 // Min normal positive double
Randy Abernethyd8187c52015-02-16 01:25:53 -0800104 assert.equal(2.2250738585072014e-308, binary.readDouble([0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000105 // Max positive double
Randy Abernethyd8187c52015-02-16 01:25:53 -0800106 assert.equal(1.7976931348623157e308, binary.readDouble([0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]))
107 assert.end();
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000108 },
109
Randy Abernethyd8187c52015-02-16 01:25:53 -0800110 "Should write doubles": function(assert) {
111 assert.deepEqual([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], 0));
112 assert.deepEqual([0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], 1));
113 assert.deepEqual([0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], 2));
114 assert.deepEqual([0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], -2));
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000115
Randy Abernethyd8187c52015-02-16 01:25:53 -0800116 assert.deepEqual([0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18], binary.writeDouble([], Math.PI));
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000117
Randy Abernethyd8187c52015-02-16 01:25:53 -0800118 assert.deepEqual([0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], Infinity));
119 assert.deepEqual([0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], -Infinity));
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000120
Randy Abernethyd8187c52015-02-16 01:25:53 -0800121 assert.deepEqual([0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], NaN));
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000122
Randy Abernethyd8187c52015-02-16 01:25:53 -0800123 assert.deepEqual([0x3f, 0xd5, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55], binary.writeDouble([], 1/3));
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000124
125 // Min subnormal positive double
Randy Abernethyd8187c52015-02-16 01:25:53 -0800126 assert.deepEqual([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01], binary.writeDouble([], 4.9406564584124654e-324));
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000127 // Min normal positive double
Randy Abernethyd8187c52015-02-16 01:25:53 -0800128 assert.deepEqual([0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], 2.2250738585072014e-308));
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000129 // Max positive double
Randy Abernethyd8187c52015-02-16 01:25:53 -0800130 assert.deepEqual([0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], binary.writeDouble([], 1.7976931348623157e308));
131 assert.end();
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000132 }
Randy Abernethyd8187c52015-02-16 01:25:53 -0800133};
134
135Object.keys(cases).forEach(function(caseName) {
136 test(caseName, cases[caseName]);
Randy Abernethycf743d72015-02-02 05:56:14 -0800137});