blob: c7f6f72fff709a804f386fd548d5cc393f4b78d1 [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
Henrique99be0272013-05-10 23:43:12 +020020var testCase = require('nodeunit').testCase;
Henrique Mendonca50fb5012012-10-26 07:29:47 +000021var binary = require('thrift/binary');
22
Henrique99be0272013-05-10 23:43:12 +020023module.exports = testCase({
24 "Should read signed byte": function(test){
25 test.equal(1, binary.readByte([0x01]));
26 test.equal(-1, binary.readByte([0xFF]));
27
28 test.equal(127, binary.readByte([0x7F]));
29 test.equal(-128, binary.readByte([0x80]));
30 test.done();
31 },
32 "Should write byte": function(test){
33 //Protocol simply writes to the buffer. Nothing to test.. yet.
34 test.ok(true);
35 test.done();
36 },
37 "Should read I16": function(test) {
38 test.equal(0, binary.readI16([0x00, 0x00]));
39 test.equal(1, binary.readI16([0x00, 0x01]));
40 test.equal(-1, binary.readI16([0xff, 0xff]));
Henrique Mendonca50fb5012012-10-26 07:29:47 +000041
42 // Min I16
Henrique99be0272013-05-10 23:43:12 +020043 test.equal(-32768, binary.readI16([0x80, 0x00]));
Henrique Mendonca50fb5012012-10-26 07:29:47 +000044 // Max I16
Henrique99be0272013-05-10 23:43:12 +020045 test.equal(32767, binary.readI16([0x7f, 0xff]));
46 test.done();
Henrique Mendonca50fb5012012-10-26 07:29:47 +000047 },
48
Henrique99be0272013-05-10 23:43:12 +020049 "Should write I16": function(test) {
50 test.deepEqual([0x00, 0x00], binary.writeI16([], 0));
51 test.deepEqual([0x00, 0x01], binary.writeI16([], 1));
52 test.deepEqual([0xff, 0xff], binary.writeI16([], -1));
Henrique Mendonca50fb5012012-10-26 07:29:47 +000053
54 // Min I16
Henrique99be0272013-05-10 23:43:12 +020055 test.deepEqual([0x80, 0x00], binary.writeI16([], -32768));
Henrique Mendonca50fb5012012-10-26 07:29:47 +000056 // Max I16
Henrique99be0272013-05-10 23:43:12 +020057 test.deepEqual([0x7f, 0xff], binary.writeI16([], 32767));
58 test.done();
Henrique Mendonca50fb5012012-10-26 07:29:47 +000059 },
60
Henrique99be0272013-05-10 23:43:12 +020061 "Should read I32": function(test) {
62 test.equal(0, binary.readI32([0x00, 0x00, 0x00, 0x00]));
63 test.equal(1, binary.readI32([0x00, 0x00, 0x00, 0x01]));
64 test.equal(-1, binary.readI32([0xff, 0xff, 0xff, 0xff]));
Henrique Mendonca50fb5012012-10-26 07:29:47 +000065
66 // Min I32
Henrique99be0272013-05-10 23:43:12 +020067 test.equal(-2147483648, binary.readI32([0x80, 0x00, 0x00, 0x00]));
Henrique Mendonca50fb5012012-10-26 07:29:47 +000068 // Max I32
Henrique99be0272013-05-10 23:43:12 +020069 test.equal(2147483647, binary.readI32([0x7f, 0xff, 0xff, 0xff]));
70 test.done();
Henrique Mendonca50fb5012012-10-26 07:29:47 +000071 },
72
Henrique99be0272013-05-10 23:43:12 +020073 "Should write I32": function(test) {
74 test.deepEqual([0x00, 0x00, 0x00, 0x00], binary.writeI32([], 0));
75 test.deepEqual([0x00, 0x00, 0x00, 0x01], binary.writeI32([], 1));
76 test.deepEqual([0xff, 0xff, 0xff, 0xff], binary.writeI32([], -1));
Henrique Mendonca50fb5012012-10-26 07:29:47 +000077
78 // Min I32
Henrique99be0272013-05-10 23:43:12 +020079 test.deepEqual([0x80, 0x00, 0x00, 0x00], binary.writeI32([], -2147483648));
Henrique Mendonca50fb5012012-10-26 07:29:47 +000080 // Max I32
Henrique99be0272013-05-10 23:43:12 +020081 test.deepEqual([0x7f, 0xff, 0xff, 0xff], binary.writeI32([], 2147483647));
82 test.done();
Henrique Mendonca50fb5012012-10-26 07:29:47 +000083 },
84
Henrique99be0272013-05-10 23:43:12 +020085 "Should read doubles": function(test) {
86 test.equal(0, binary.readDouble([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
87 test.equal(0, binary.readDouble([0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
88 test.equal(1, binary.readDouble([0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
89 test.equal(2, binary.readDouble([0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
90 test.equal(-2, binary.readDouble([0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
Henrique Mendonca50fb5012012-10-26 07:29:47 +000091
Henrique99be0272013-05-10 23:43:12 +020092 test.equal(Math.PI, binary.readDouble([0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18]))
Henrique Mendonca50fb5012012-10-26 07:29:47 +000093
Henrique99be0272013-05-10 23:43:12 +020094 test.equal(Infinity, binary.readDouble([0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
95 test.equal(-Infinity, binary.readDouble([0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
Henrique Mendonca50fb5012012-10-26 07:29:47 +000096
Henrique99be0272013-05-10 23:43:12 +020097 test.ok(isNaN(binary.readDouble([0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])))
Henrique Mendonca50fb5012012-10-26 07:29:47 +000098
Henrique99be0272013-05-10 23:43:12 +020099 test.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
Henrique99be0272013-05-10 23:43:12 +0200102 test.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
Henrique99be0272013-05-10 23:43:12 +0200104 test.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
Henrique99be0272013-05-10 23:43:12 +0200106 test.equal(1.7976931348623157e308, binary.readDouble([0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]))
107 test.done();
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000108 },
109
Henrique99be0272013-05-10 23:43:12 +0200110 "Should write doubles": function(test) {
111 test.deepEqual([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], 0));
112 test.deepEqual([0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], 1));
113 test.deepEqual([0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], 2));
114 test.deepEqual([0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], -2));
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000115
Henrique99be0272013-05-10 23:43:12 +0200116 test.deepEqual([0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18], binary.writeDouble([], Math.PI));
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000117
Henrique99be0272013-05-10 23:43:12 +0200118 test.deepEqual([0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], Infinity));
119 test.deepEqual([0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], -Infinity));
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000120
Henrique99be0272013-05-10 23:43:12 +0200121 test.deepEqual([0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], NaN));
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000122
Henrique99be0272013-05-10 23:43:12 +0200123 test.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
Henrique99be0272013-05-10 23:43:12 +0200126 test.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
Henrique99be0272013-05-10 23:43:12 +0200128 test.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
Henrique99be0272013-05-10 23:43:12 +0200130 test.deepEqual([0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], binary.writeDouble([], 1.7976931348623157e308));
131 test.done();
Henrique Mendonca50fb5012012-10-26 07:29:47 +0000132 }
Henrique99be0272013-05-10 23:43:12 +0200133});