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 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 20 | var test = require('tape'); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 21 | var binary = require('thrift/binary'); |
| 22 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 23 | var cases = { |
| 24 | "Should read signed byte": function(assert){ |
| 25 | assert.equal(1, binary.readByte(0x01)); |
| 26 | assert.equal(-1, binary.readByte(0xFF)); |
Randy Abernethy | 96f4f07 | 2015-02-10 02:29:15 -0800 | [diff] [blame] | 27 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 28 | assert.equal(127, binary.readByte(0x7F)); |
| 29 | assert.equal(-128, binary.readByte(0x80)); |
| 30 | assert.end(); |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 31 | }, |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 32 | "Should write byte": function(assert){ |
| 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 | }, |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 37 | "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 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 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 49 | "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 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 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 61 | "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 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 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 73 | "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 Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 77 | |
| 78 | // Min I32 |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 79 | assert.deepEqual([0x80, 0x00, 0x00, 0x00], binary.writeI32([], -2147483648)); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 80 | // Max I32 |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 81 | assert.deepEqual([0x7f, 0xff, 0xff, 0xff], binary.writeI32([], 2147483647)); |
| 82 | assert.end(); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 83 | }, |
| 84 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 85 | "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 Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 91 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 92 | assert.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 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 94 | 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 Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 96 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 97 | assert.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 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 99 | assert.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 |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 102 | assert.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 |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 104 | assert.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 |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 106 | assert.equal(1.7976931348623157e308, binary.readDouble([0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])) |
| 107 | assert.end(); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 108 | }, |
| 109 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 110 | "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 Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 115 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 116 | assert.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 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 118 | 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 Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 120 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 121 | assert.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 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 123 | assert.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 |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 126 | assert.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 |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 128 | assert.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 |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 130 | assert.deepEqual([0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], binary.writeDouble([], 1.7976931348623157e308)); |
| 131 | assert.end(); |
Henrique Mendonca | 50fb501 | 2012-10-26 07:29:47 +0000 | [diff] [blame] | 132 | } |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame^] | 133 | }; |
| 134 | |
| 135 | Object.keys(cases).forEach(function(caseName) { |
| 136 | test(caseName, cases[caseName]); |
Randy Abernethy | cf743d7 | 2015-02-02 05:56:14 -0800 | [diff] [blame] | 137 | }); |