Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include <limits> |
michael@0 | 6 | #include <math.h> |
michael@0 | 7 | |
michael@0 | 8 | #include "jsapi-tests/tests.h" |
michael@0 | 9 | |
michael@0 | 10 | using namespace std; |
michael@0 | 11 | |
michael@0 | 12 | struct LooseEqualityFixture : public JSAPITest |
michael@0 | 13 | { |
michael@0 | 14 | virtual ~LooseEqualityFixture() {} |
michael@0 | 15 | |
michael@0 | 16 | bool leq(JS::HandleValue x, JS::HandleValue y) { |
michael@0 | 17 | bool equal; |
michael@0 | 18 | CHECK(JS_LooselyEqual(cx, x, y, &equal) && equal); |
michael@0 | 19 | CHECK(JS_LooselyEqual(cx, y, x, &equal) && equal); |
michael@0 | 20 | return true; |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | bool nleq(JS::HandleValue x, JS::HandleValue y) { |
michael@0 | 24 | bool equal; |
michael@0 | 25 | CHECK(JS_LooselyEqual(cx, x, y, &equal) && !equal); |
michael@0 | 26 | CHECK(JS_LooselyEqual(cx, y, x, &equal) && !equal); |
michael@0 | 27 | return true; |
michael@0 | 28 | } |
michael@0 | 29 | }; |
michael@0 | 30 | |
michael@0 | 31 | struct LooseEqualityData |
michael@0 | 32 | { |
michael@0 | 33 | JS::RootedValue qNaN; |
michael@0 | 34 | JS::RootedValue sNaN; |
michael@0 | 35 | JS::RootedValue d42; |
michael@0 | 36 | JS::RootedValue i42; |
michael@0 | 37 | JS::RootedValue undef; |
michael@0 | 38 | JS::RootedValue null; |
michael@0 | 39 | JS::RootedValue obj; |
michael@0 | 40 | JS::RootedValue poszero; |
michael@0 | 41 | JS::RootedValue negzero; |
michael@0 | 42 | |
michael@0 | 43 | LooseEqualityData(JSContext *cx) |
michael@0 | 44 | : qNaN(cx), |
michael@0 | 45 | sNaN(cx), |
michael@0 | 46 | d42(cx), |
michael@0 | 47 | i42(cx), |
michael@0 | 48 | undef(cx), |
michael@0 | 49 | null(cx), |
michael@0 | 50 | obj(cx), |
michael@0 | 51 | poszero(cx), |
michael@0 | 52 | negzero(cx) |
michael@0 | 53 | { |
michael@0 | 54 | qNaN = DOUBLE_TO_JSVAL(numeric_limits<double>::quiet_NaN()); |
michael@0 | 55 | sNaN = DOUBLE_TO_JSVAL(numeric_limits<double>::signaling_NaN()); |
michael@0 | 56 | d42 = DOUBLE_TO_JSVAL(42.0); |
michael@0 | 57 | i42 = INT_TO_JSVAL(42); |
michael@0 | 58 | undef = JSVAL_VOID; |
michael@0 | 59 | null = JSVAL_NULL; |
michael@0 | 60 | obj = OBJECT_TO_JSVAL(JS::CurrentGlobalOrNull(cx)); |
michael@0 | 61 | poszero = DOUBLE_TO_JSVAL(0.0); |
michael@0 | 62 | negzero = DOUBLE_TO_JSVAL(-0.0); |
michael@0 | 63 | #ifdef XP_WIN |
michael@0 | 64 | # define copysign _copysign |
michael@0 | 65 | #endif |
michael@0 | 66 | JS_ASSERT(copysign(1.0, JSVAL_TO_DOUBLE(poszero)) == 1.0); |
michael@0 | 67 | JS_ASSERT(copysign(1.0, JSVAL_TO_DOUBLE(negzero)) == -1.0); |
michael@0 | 68 | #ifdef XP_WIN |
michael@0 | 69 | # undef copysign |
michael@0 | 70 | #endif |
michael@0 | 71 | } |
michael@0 | 72 | }; |
michael@0 | 73 | |
michael@0 | 74 | // 11.9.3 1a |
michael@0 | 75 | BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_undef_leq_undef) |
michael@0 | 76 | { |
michael@0 | 77 | LooseEqualityData d(cx); |
michael@0 | 78 | CHECK(leq(d.undef, d.undef)); |
michael@0 | 79 | return true; |
michael@0 | 80 | } |
michael@0 | 81 | END_FIXTURE_TEST(LooseEqualityFixture, test_undef_leq_undef) |
michael@0 | 82 | |
michael@0 | 83 | // 11.9.3 1b |
michael@0 | 84 | BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_null_leq_null) |
michael@0 | 85 | { |
michael@0 | 86 | LooseEqualityData d(cx); |
michael@0 | 87 | CHECK(leq(d.null, d.null)); |
michael@0 | 88 | return true; |
michael@0 | 89 | } |
michael@0 | 90 | END_FIXTURE_TEST(LooseEqualityFixture, test_null_leq_null) |
michael@0 | 91 | |
michael@0 | 92 | // 11.9.3 1ci |
michael@0 | 93 | BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_nan_nleq_all) |
michael@0 | 94 | { |
michael@0 | 95 | LooseEqualityData d(cx); |
michael@0 | 96 | |
michael@0 | 97 | CHECK(nleq(d.qNaN, d.qNaN)); |
michael@0 | 98 | CHECK(nleq(d.qNaN, d.sNaN)); |
michael@0 | 99 | |
michael@0 | 100 | CHECK(nleq(d.sNaN, d.sNaN)); |
michael@0 | 101 | CHECK(nleq(d.sNaN, d.qNaN)); |
michael@0 | 102 | |
michael@0 | 103 | CHECK(nleq(d.qNaN, d.d42)); |
michael@0 | 104 | CHECK(nleq(d.qNaN, d.i42)); |
michael@0 | 105 | CHECK(nleq(d.qNaN, d.undef)); |
michael@0 | 106 | CHECK(nleq(d.qNaN, d.null)); |
michael@0 | 107 | CHECK(nleq(d.qNaN, d.obj)); |
michael@0 | 108 | |
michael@0 | 109 | CHECK(nleq(d.sNaN, d.d42)); |
michael@0 | 110 | CHECK(nleq(d.sNaN, d.i42)); |
michael@0 | 111 | CHECK(nleq(d.sNaN, d.undef)); |
michael@0 | 112 | CHECK(nleq(d.sNaN, d.null)); |
michael@0 | 113 | CHECK(nleq(d.sNaN, d.obj)); |
michael@0 | 114 | return true; |
michael@0 | 115 | } |
michael@0 | 116 | END_FIXTURE_TEST(LooseEqualityFixture, test_nan_nleq_all) |
michael@0 | 117 | |
michael@0 | 118 | // 11.9.3 1cii |
michael@0 | 119 | BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_all_nleq_nan) |
michael@0 | 120 | { |
michael@0 | 121 | LooseEqualityData d(cx); |
michael@0 | 122 | |
michael@0 | 123 | CHECK(nleq(d.qNaN, d.qNaN)); |
michael@0 | 124 | CHECK(nleq(d.qNaN, d.sNaN)); |
michael@0 | 125 | |
michael@0 | 126 | CHECK(nleq(d.sNaN, d.sNaN)); |
michael@0 | 127 | CHECK(nleq(d.sNaN, d.qNaN)); |
michael@0 | 128 | |
michael@0 | 129 | CHECK(nleq(d.d42, d.qNaN)); |
michael@0 | 130 | CHECK(nleq(d.i42, d.qNaN)); |
michael@0 | 131 | CHECK(nleq(d.undef, d.qNaN)); |
michael@0 | 132 | CHECK(nleq(d.null, d.qNaN)); |
michael@0 | 133 | CHECK(nleq(d.obj, d.qNaN)); |
michael@0 | 134 | |
michael@0 | 135 | CHECK(nleq(d.d42, d.sNaN)); |
michael@0 | 136 | CHECK(nleq(d.i42, d.sNaN)); |
michael@0 | 137 | CHECK(nleq(d.undef, d.sNaN)); |
michael@0 | 138 | CHECK(nleq(d.null, d.sNaN)); |
michael@0 | 139 | CHECK(nleq(d.obj, d.sNaN)); |
michael@0 | 140 | return true; |
michael@0 | 141 | } |
michael@0 | 142 | END_FIXTURE_TEST(LooseEqualityFixture, test_all_nleq_nan) |
michael@0 | 143 | |
michael@0 | 144 | // 11.9.3 1ciii |
michael@0 | 145 | BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_leq_same_nums) |
michael@0 | 146 | { |
michael@0 | 147 | LooseEqualityData d(cx); |
michael@0 | 148 | |
michael@0 | 149 | CHECK(leq(d.d42, d.d42)); |
michael@0 | 150 | CHECK(leq(d.i42, d.i42)); |
michael@0 | 151 | CHECK(leq(d.d42, d.i42)); |
michael@0 | 152 | CHECK(leq(d.i42, d.d42)); |
michael@0 | 153 | return true; |
michael@0 | 154 | } |
michael@0 | 155 | END_FIXTURE_TEST(LooseEqualityFixture, test_leq_same_nums) |
michael@0 | 156 | |
michael@0 | 157 | // 11.9.3 1civ |
michael@0 | 158 | BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_pz_leq_nz) |
michael@0 | 159 | { |
michael@0 | 160 | LooseEqualityData d(cx); |
michael@0 | 161 | CHECK(leq(d.poszero, d.negzero)); |
michael@0 | 162 | return true; |
michael@0 | 163 | } |
michael@0 | 164 | END_FIXTURE_TEST(LooseEqualityFixture, test_pz_leq_nz) |
michael@0 | 165 | |
michael@0 | 166 | // 11.9.3 1cv |
michael@0 | 167 | BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_nz_leq_pz) |
michael@0 | 168 | { |
michael@0 | 169 | LooseEqualityData d(cx); |
michael@0 | 170 | CHECK(leq(d.negzero, d.poszero)); |
michael@0 | 171 | return true; |
michael@0 | 172 | } |
michael@0 | 173 | END_FIXTURE_TEST(LooseEqualityFixture, test_nz_leq_pz) |
michael@0 | 174 | |
michael@0 | 175 | // 1cvi onwards NOT TESTED |
michael@0 | 176 | |
michael@0 | 177 | // 11.9.3 2 |
michael@0 | 178 | BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_null_leq_undef) |
michael@0 | 179 | { |
michael@0 | 180 | LooseEqualityData d(cx); |
michael@0 | 181 | CHECK(leq(d.null, d.undef)); |
michael@0 | 182 | return true; |
michael@0 | 183 | } |
michael@0 | 184 | END_FIXTURE_TEST(LooseEqualityFixture, test_null_leq_undef) |
michael@0 | 185 | |
michael@0 | 186 | // 11.9.3 3 |
michael@0 | 187 | BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_undef_leq_null) |
michael@0 | 188 | { |
michael@0 | 189 | LooseEqualityData d(cx); |
michael@0 | 190 | CHECK(leq(d.undef, d.null)); |
michael@0 | 191 | return true; |
michael@0 | 192 | } |
michael@0 | 193 | END_FIXTURE_TEST(LooseEqualityFixture, test_undef_leq_null) |
michael@0 | 194 | |
michael@0 | 195 | // 4 onwards NOT TESTED |