blob: 1790c1b80d0165986fe4f4719fbe9de82e3fe946 [file] [log] [blame]
Ozan Can Altioke46419b2018-03-20 15:02:28 +03001/*
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 /* jshint -W100 */
20
21/*
22 * JavaScript test suite for double constants inside
Ozan Can Altiokcaf7da92019-01-07 14:14:11 +030023 * DoubleConstantsTest.thrift. These tests will run against Normal (-gen js)
Ozan Can Altioke46419b2018-03-20 15:02:28 +030024 * Apache Thrift interfaces.
25 *
Ozan Can Altioke46419b2018-03-20 15:02:28 +030026 * To compile client code for this test use:
Ozan Can Altiokcaf7da92019-01-07 14:14:11 +030027 * $ thrift -gen js DoubleConstantsTest.thrift
Ozan Can Altioke46419b2018-03-20 15:02:28 +030028 */
29
30// double assertion threshold
Brian Forbisb5d6ea32018-08-25 23:39:29 -040031const EPSILON = 0.0000001;
Ozan Can Altioke46419b2018-03-20 15:02:28 +030032
33// Work around for old API used by QUnitAdapter of jsTestDriver
34if (typeof QUnit.log == 'function') {
35 // When using real QUnit (fron PhantomJS) log failures to console
36 QUnit.log(function(details) {
37 if (!details.result) {
38 console.log('======== FAIL ========');
39 console.log('TestName: ' + details.name);
40 if (details.message) console.log(details.message);
41 console.log('Expected: ' + details.expected);
42 console.log('Actual : ' + details.actual);
43 console.log('======================');
44 }
45 });
46}
47
48QUnit.module('Double rendering');
49
50 QUnit.test('Double (rendering)', function(assert) {
51 console.log('Double rendering test -- starts');
Brian Forbisb5d6ea32018-08-25 23:39:29 -040052 const EXPECTED_DOUBLE_ASSIGNED_TO_INT_CONSTANT = 1;
53 const EXPECTED_DOUBLE_ASSIGNED_TO_NEGATIVE_INT_CONSTANT = -100;
54 const EXPECTED_DOUBLE_ASSIGNED_TO_LARGEST_INT_CONSTANT = 9223372036854775807;
55 const EXPECTED_DOUBLE_ASSIGNED_TO_SMALLEST_INT_CONSTANT = -9223372036854775807;
56 const EXPECTED_DOUBLE_ASSIGNED_TO_DOUBLE_WITH_MANY_DECIMALS = 3.14159265359;
57 const EXPECTED_DOUBLE_ASSIGNED_TO_FRACTIONAL_DOUBLE = 1000000.1;
58 const EXPECTED_DOUBLE_ASSIGNED_TO_NEGATIVE_FRACTIONAL_DOUBLE = -1000000.1;
59 const EXPECTED_DOUBLE_ASSIGNED_TO_LARGE_DOUBLE = 1.7e+308;
60 const EXPECTED_DOUBLE_ASSIGNED_TO_LARGE_FRACTIONAL_DOUBLE = 9223372036854775816.43;
61 const EXPECTED_DOUBLE_ASSIGNED_TO_SMALL_DOUBLE = -1.7e+308;
62 const EXPECTED_DOUBLE_ASSIGNED_TO_NEGATIVE_BUT_LARGE_FRACTIONAL_DOUBLE = -9223372036854775816.43;
Ozan Can Altioke46419b2018-03-20 15:02:28 +030063 assert.ok(
64 Math.abs(EXPECTED_DOUBLE_ASSIGNED_TO_INT_CONSTANT - DOUBLE_ASSIGNED_TO_INT_CONSTANT_TEST) <= EPSILON);
65 assert.ok(
66 Math.abs(
67 EXPECTED_DOUBLE_ASSIGNED_TO_NEGATIVE_INT_CONSTANT -
68 DOUBLE_ASSIGNED_TO_NEGATIVE_INT_CONSTANT_TEST) <= EPSILON);
69 assert.ok(
70 Math.abs(
71 EXPECTED_DOUBLE_ASSIGNED_TO_LARGEST_INT_CONSTANT -
72 DOUBLE_ASSIGNED_TO_LARGEST_INT_CONSTANT_TEST) <= EPSILON);
73 assert.ok(
74 Math.abs(
75 EXPECTED_DOUBLE_ASSIGNED_TO_SMALLEST_INT_CONSTANT -
76 DOUBLE_ASSIGNED_TO_SMALLEST_INT_CONSTANT_TEST) <= EPSILON);
77 assert.ok(
78 Math.abs(
79 EXPECTED_DOUBLE_ASSIGNED_TO_DOUBLE_WITH_MANY_DECIMALS -
80 DOUBLE_ASSIGNED_TO_DOUBLE_WITH_MANY_DECIMALS_TEST) <= EPSILON);
81 assert.ok(
82 Math.abs(
83 EXPECTED_DOUBLE_ASSIGNED_TO_FRACTIONAL_DOUBLE -
84 DOUBLE_ASSIGNED_TO_FRACTIONAL_DOUBLE_TEST) <= EPSILON);
85 assert.ok(
86 Math.abs(
87 EXPECTED_DOUBLE_ASSIGNED_TO_NEGATIVE_FRACTIONAL_DOUBLE -
88 DOUBLE_ASSIGNED_TO_NEGATIVE_FRACTIONAL_DOUBLE_TEST) <= EPSILON);
89 assert.ok(
90 Math.abs(
91 EXPECTED_DOUBLE_ASSIGNED_TO_LARGE_DOUBLE -
92 DOUBLE_ASSIGNED_TO_LARGE_DOUBLE_TEST) <= EPSILON);
93 assert.ok(
94 Math.abs(
95 EXPECTED_DOUBLE_ASSIGNED_TO_LARGE_FRACTIONAL_DOUBLE -
96 DOUBLE_ASSIGNED_TO_LARGE_FRACTIONAL_DOUBLE_TEST) <= EPSILON);
97 assert.ok(
98 Math.abs(
99 EXPECTED_DOUBLE_ASSIGNED_TO_SMALL_DOUBLE -
100 DOUBLE_ASSIGNED_TO_SMALL_DOUBLE_TEST) <= EPSILON);
101 assert.ok(
102 Math.abs(
103 EXPECTED_DOUBLE_ASSIGNED_TO_NEGATIVE_BUT_LARGE_FRACTIONAL_DOUBLE -
104 DOUBLE_ASSIGNED_TO_NEGATIVE_BUT_LARGE_FRACTIONAL_DOUBLE_TEST) <= EPSILON);
105 assert.equal(typeof DOUBLE_ASSIGNED_TO_INT_CONSTANT_TEST, 'number');
106 assert.equal(typeof DOUBLE_ASSIGNED_TO_NEGATIVE_INT_CONSTANT_TEST, 'number');
107 assert.equal(typeof DOUBLE_ASSIGNED_TO_LARGEST_INT_CONSTANT_TEST, 'number');
108 assert.equal(typeof DOUBLE_ASSIGNED_TO_SMALLEST_INT_CONSTANT_TEST, 'number');
109 assert.equal(typeof DOUBLE_ASSIGNED_TO_DOUBLE_WITH_MANY_DECIMALS_TEST, 'number');
110 assert.equal(typeof DOUBLE_ASSIGNED_TO_FRACTIONAL_DOUBLE_TEST, 'number');
111 assert.equal(typeof DOUBLE_ASSIGNED_TO_NEGATIVE_FRACTIONAL_DOUBLE_TEST, 'number');
112 assert.equal(typeof DOUBLE_ASSIGNED_TO_LARGE_DOUBLE_TEST, 'number');
113 assert.equal(typeof DOUBLE_ASSIGNED_TO_LARGE_FRACTIONAL_DOUBLE_TEST, 'number');
114 assert.equal(typeof DOUBLE_ASSIGNED_TO_SMALL_DOUBLE_TEST, 'number');
115 assert.equal(typeof DOUBLE_ASSIGNED_TO_NEGATIVE_BUT_LARGE_FRACTIONAL_DOUBLE_TEST, 'number');
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400116 const EXPECTED_DOUBLE_LIST =
Ozan Can Altioke46419b2018-03-20 15:02:28 +0300117 [1,-100,100,9223372036854775807,-9223372036854775807,3.14159265359,1000000.1,-1000000.1,1.7e+308,-1.7e+308,
118 9223372036854775816.43,-9223372036854775816.43];
119 assert.equal(DOUBLE_LIST_TEST.length, EXPECTED_DOUBLE_LIST.length);
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400120 for (let i = 0; i < EXPECTED_DOUBLE_LIST.length; ++i) {
Ozan Can Altioke46419b2018-03-20 15:02:28 +0300121 assert.ok(Math.abs(EXPECTED_DOUBLE_LIST[i] - DOUBLE_LIST_TEST[i]) <= EPSILON);
122 }
123 console.log('Double rendering test -- ends');
124 });
125