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 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 20 | var testCase = require('nodeunit').testCase; |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 21 | var binary = require('thrift/binary'); |
| 22 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 23 | module.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 Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 41 | |
| 42 | // Min I16 |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 43 | test.equal(-32768, binary.readI16([0x80, 0x00])); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 44 | // Max I16 |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 45 | test.equal(32767, binary.readI16([0x7f, 0xff])); |
| 46 | test.done(); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 47 | }, |
| 48 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 49 | "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 Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 53 | |
| 54 | // Min I16 |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 55 | test.deepEqual([0x80, 0x00], binary.writeI16([], -32768)); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 56 | // Max I16 |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 57 | test.deepEqual([0x7f, 0xff], binary.writeI16([], 32767)); |
| 58 | test.done(); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 59 | }, |
| 60 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 61 | "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 Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 65 | |
| 66 | // Min I32 |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 67 | test.equal(-2147483648, binary.readI32([0x80, 0x00, 0x00, 0x00])); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 68 | // Max I32 |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 69 | test.equal(2147483647, binary.readI32([0x7f, 0xff, 0xff, 0xff])); |
| 70 | test.done(); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 71 | }, |
| 72 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 73 | "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 Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 77 | |
| 78 | // Min I32 |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 79 | test.deepEqual([0x80, 0x00, 0x00, 0x00], binary.writeI32([], -2147483648)); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 80 | // Max I32 |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 81 | test.deepEqual([0x7f, 0xff, 0xff, 0xff], binary.writeI32([], 2147483647)); |
| 82 | test.done(); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 83 | }, |
| 84 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 85 | "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 Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 91 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 92 | test.equal(Math.PI, binary.readDouble([0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18])) |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 93 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 94 | 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 Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 96 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 97 | test.ok(isNaN(binary.readDouble([0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))) |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 98 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 99 | test.equal(1/3, binary.readDouble([0x3f, 0xd5, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55])) |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 100 | |
| 101 | // Min subnormal positive double |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 102 | test.equal(4.9406564584124654e-324, binary.readDouble([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01])) |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 103 | // Min normal positive double |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 104 | test.equal(2.2250738585072014e-308, binary.readDouble([0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])) |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 105 | // Max positive double |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 106 | test.equal(1.7976931348623157e308, binary.readDouble([0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])) |
| 107 | test.done(); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 108 | }, |
| 109 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 110 | "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 Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 115 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 116 | test.deepEqual([0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18], binary.writeDouble([], Math.PI)); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 117 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 118 | 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 Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 120 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 121 | test.deepEqual([0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], NaN)); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 122 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 123 | test.deepEqual([0x3f, 0xd5, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55], binary.writeDouble([], 1/3)); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 124 | |
| 125 | // Min subnormal positive double |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 126 | test.deepEqual([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01], binary.writeDouble([], 4.9406564584124654e-324)); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 127 | // Min normal positive double |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 128 | test.deepEqual([0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], 2.2250738585072014e-308)); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 129 | // Max positive double |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 130 | test.deepEqual([0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], binary.writeDouble([], 1.7976931348623157e308)); |
| 131 | test.done(); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 132 | } |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 133 | }); |