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 | |
| 20 | var assert = require('assert'); |
| 21 | var binary = require('thrift/binary'); |
| 22 | |
| 23 | module.exports = { |
| 24 | "Should read I16": function() { |
| 25 | assert.equal(0, binary.readI16([0x00, 0x00])); |
| 26 | assert.equal(1, binary.readI16([0x00, 0x01])); |
| 27 | assert.equal(-1, binary.readI16([0xff, 0xff])); |
| 28 | |
| 29 | // Min I16 |
| 30 | assert.equal(-32768, binary.readI16([0x80, 0x00])); |
| 31 | // Max I16 |
| 32 | assert.equal(32767, binary.readI16([0x7f, 0xff])); |
| 33 | }, |
| 34 | |
| 35 | "Should write I16": function() { |
| 36 | assert.deepEqual([0x00, 0x00], binary.writeI16([], 0)); |
| 37 | assert.deepEqual([0x00, 0x01], binary.writeI16([], 1)); |
| 38 | assert.deepEqual([0xff, 0xff], binary.writeI16([], -1)); |
| 39 | |
| 40 | // Min I16 |
| 41 | assert.deepEqual([0x80, 0x00], binary.writeI16([], -32768)); |
| 42 | // Max I16 |
| 43 | assert.deepEqual([0x7f, 0xff], binary.writeI16([], 32767)); |
| 44 | }, |
| 45 | |
| 46 | "Should read I32": function() { |
| 47 | assert.equal(0, binary.readI32([0x00, 0x00, 0x00, 0x00])); |
| 48 | assert.equal(1, binary.readI32([0x00, 0x00, 0x00, 0x01])); |
| 49 | assert.equal(-1, binary.readI32([0xff, 0xff, 0xff, 0xff])); |
| 50 | |
| 51 | // Min I32 |
| 52 | assert.equal(-2147483648, binary.readI32([0x80, 0x00, 0x00, 0x00])); |
| 53 | // Max I32 |
| 54 | assert.equal(2147483647, binary.readI32([0x7f, 0xff, 0xff, 0xff])); |
| 55 | }, |
| 56 | |
| 57 | "Should write I32": function() { |
| 58 | assert.deepEqual([0x00, 0x00, 0x00, 0x00], binary.writeI32([], 0)); |
| 59 | assert.deepEqual([0x00, 0x00, 0x00, 0x01], binary.writeI32([], 1)); |
| 60 | assert.deepEqual([0xff, 0xff, 0xff, 0xff], binary.writeI32([], -1)); |
| 61 | |
| 62 | // Min I32 |
| 63 | assert.deepEqual([0x80, 0x00, 0x00, 0x00], binary.writeI32([], -2147483648)); |
| 64 | // Max I32 |
| 65 | assert.deepEqual([0x7f, 0xff, 0xff, 0xff], binary.writeI32([], 2147483647)); |
| 66 | }, |
| 67 | |
| 68 | "Should read doubles": function() { |
| 69 | assert.equal(0, binary.readDouble([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])) |
| 70 | assert.equal(0, binary.readDouble([0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])) |
| 71 | assert.equal(1, binary.readDouble([0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])) |
| 72 | assert.equal(2, binary.readDouble([0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])) |
| 73 | assert.equal(-2, binary.readDouble([0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])) |
| 74 | |
| 75 | assert.equal(Math.PI, binary.readDouble([0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18])) |
| 76 | |
| 77 | assert.equal(Infinity, binary.readDouble([0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])) |
| 78 | assert.equal(-Infinity, binary.readDouble([0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])) |
| 79 | |
| 80 | assert.ok(isNaN(binary.readDouble([0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))) |
| 81 | |
| 82 | assert.equal(1/3, binary.readDouble([0x3f, 0xd5, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55])) |
| 83 | |
| 84 | // Min subnormal positive double |
| 85 | assert.equal(4.9406564584124654e-324, binary.readDouble([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01])) |
| 86 | // Min normal positive double |
| 87 | assert.equal(2.2250738585072014e-308, binary.readDouble([0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])) |
| 88 | // Max positive double |
| 89 | assert.equal(1.7976931348623157e308, binary.readDouble([0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])) |
| 90 | }, |
| 91 | |
| 92 | "Should write doubles": function() { |
| 93 | assert.deepEqual([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], 0)); |
| 94 | assert.deepEqual([0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], 1)); |
| 95 | assert.deepEqual([0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], 2)); |
| 96 | assert.deepEqual([0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], -2)); |
| 97 | |
| 98 | assert.deepEqual([0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18], binary.writeDouble([], Math.PI)); |
| 99 | |
| 100 | assert.deepEqual([0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], Infinity)); |
| 101 | assert.deepEqual([0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], -Infinity)); |
| 102 | |
| 103 | assert.deepEqual([0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], NaN)); |
| 104 | |
| 105 | assert.deepEqual([0x3f, 0xd5, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55], binary.writeDouble([], 1/3)); |
| 106 | |
| 107 | // Min subnormal positive double |
| 108 | assert.deepEqual([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01], binary.writeDouble([], 4.9406564584124654e-324)); |
| 109 | // Min normal positive double |
| 110 | assert.deepEqual([0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], binary.writeDouble([], 2.2250738585072014e-308)); |
| 111 | // Max positive double |
| 112 | assert.deepEqual([0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], binary.writeDouble([], 1.7976931348623157e308)); |
| 113 | } |
| 114 | } |