summaryrefslogtreecommitdiffstats
path: root/js/src/jsapi-tests/testLooselyEqual.cpp
blob: 8fa369f7e7d4cde2fc4926196df5bf0c25ae4ed1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <limits>
#include <math.h>

#include "js/Equality.h"  // JS::LooselyEqual
#include "js/GlobalObject.h"
#include "jsapi-tests/tests.h"

using namespace std;

struct LooseEqualityFixture : public JSAPIRuntimeTest {
  virtual ~LooseEqualityFixture() {}

  bool leq(JS::HandleValue x, JS::HandleValue y) {
    bool equal;
    CHECK(JS::LooselyEqual(cx, x, y, &equal) && equal);
    CHECK(JS::LooselyEqual(cx, y, x, &equal) && equal);
    return true;
  }

  bool nleq(JS::HandleValue x, JS::HandleValue y) {
    bool equal;
    CHECK(JS::LooselyEqual(cx, x, y, &equal) && !equal);
    CHECK(JS::LooselyEqual(cx, y, x, &equal) && !equal);
    return true;
  }
};

struct LooseEqualityData {
  JS::RootedValue qNaN;
  JS::RootedValue sNaN;
  JS::RootedValue d42;
  JS::RootedValue i42;
  JS::RootedValue undef;
  JS::RootedValue null;
  JS::RootedValue obj;
  JS::RootedValue poszero;
  JS::RootedValue negzero;

  explicit LooseEqualityData(JSContext* cx)
      : qNaN(cx),
        sNaN(cx),
        d42(cx),
        i42(cx),
        undef(cx),
        null(cx),
        obj(cx),
        poszero(cx),
        negzero(cx) {
    qNaN = JS::CanonicalizedDoubleValue(numeric_limits<double>::quiet_NaN());
    sNaN =
        JS::CanonicalizedDoubleValue(numeric_limits<double>::signaling_NaN());
    d42 = JS::DoubleValue(42.0);
    i42 = JS::Int32Value(42);
    undef = JS::UndefinedValue();
    null = JS::NullValue();
    obj = JS::ObjectOrNullValue(JS::CurrentGlobalOrNull(cx));
    poszero = JS::DoubleValue(0.0);
    negzero = JS::DoubleValue(-0.0);
#ifdef XP_WIN
#  define copysign _copysign
#endif
    MOZ_RELEASE_ASSERT(copysign(1.0, poszero.toDouble()) == 1.0);
    MOZ_RELEASE_ASSERT(copysign(1.0, negzero.toDouble()) == -1.0);
#ifdef XP_WIN
#  undef copysign
#endif
  }
};

// 11.9.3 1a
BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_undef_leq_undef) {
  LooseEqualityData d(cx);
  CHECK(leq(d.undef, d.undef));
  return true;
}
END_FIXTURE_TEST(LooseEqualityFixture, test_undef_leq_undef)

// 11.9.3 1b
BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_null_leq_null) {
  LooseEqualityData d(cx);
  CHECK(leq(d.null, d.null));
  return true;
}
END_FIXTURE_TEST(LooseEqualityFixture, test_null_leq_null)

// 11.9.3 1ci
BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_nan_nleq_all) {
  LooseEqualityData d(cx);

  CHECK(nleq(d.qNaN, d.qNaN));
  CHECK(nleq(d.qNaN, d.sNaN));

  CHECK(nleq(d.sNaN, d.sNaN));
  CHECK(nleq(d.sNaN, d.qNaN));

  CHECK(nleq(d.qNaN, d.d42));
  CHECK(nleq(d.qNaN, d.i42));
  CHECK(nleq(d.qNaN, d.undef));
  CHECK(nleq(d.qNaN, d.null));
  CHECK(nleq(d.qNaN, d.obj));

  CHECK(nleq(d.sNaN, d.d42));
  CHECK(nleq(d.sNaN, d.i42));
  CHECK(nleq(d.sNaN, d.undef));
  CHECK(nleq(d.sNaN, d.null));
  CHECK(nleq(d.sNaN, d.obj));
  return true;
}
END_FIXTURE_TEST(LooseEqualityFixture, test_nan_nleq_all)

// 11.9.3 1cii
BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_all_nleq_nan) {
  LooseEqualityData d(cx);

  CHECK(nleq(d.qNaN, d.qNaN));
  CHECK(nleq(d.qNaN, d.sNaN));

  CHECK(nleq(d.sNaN, d.sNaN));
  CHECK(nleq(d.sNaN, d.qNaN));

  CHECK(nleq(d.d42, d.qNaN));
  CHECK(nleq(d.i42, d.qNaN));
  CHECK(nleq(d.undef, d.qNaN));
  CHECK(nleq(d.null, d.qNaN));
  CHECK(nleq(d.obj, d.qNaN));

  CHECK(nleq(d.d42, d.sNaN));
  CHECK(nleq(d.i42, d.sNaN));
  CHECK(nleq(d.undef, d.sNaN));
  CHECK(nleq(d.null, d.sNaN));
  CHECK(nleq(d.obj, d.sNaN));
  return true;
}
END_FIXTURE_TEST(LooseEqualityFixture, test_all_nleq_nan)

// 11.9.3 1ciii
BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_leq_same_nums) {
  LooseEqualityData d(cx);

  CHECK(leq(d.d42, d.d42));
  CHECK(leq(d.i42, d.i42));
  CHECK(leq(d.d42, d.i42));
  CHECK(leq(d.i42, d.d42));
  return true;
}
END_FIXTURE_TEST(LooseEqualityFixture, test_leq_same_nums)

// 11.9.3 1civ
BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_pz_leq_nz) {
  LooseEqualityData d(cx);
  CHECK(leq(d.poszero, d.negzero));
  return true;
}
END_FIXTURE_TEST(LooseEqualityFixture, test_pz_leq_nz)

// 11.9.3 1cv
BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_nz_leq_pz) {
  LooseEqualityData d(cx);
  CHECK(leq(d.negzero, d.poszero));
  return true;
}
END_FIXTURE_TEST(LooseEqualityFixture, test_nz_leq_pz)

// 1cvi onwards NOT TESTED

// 11.9.3 2
BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_null_leq_undef) {
  LooseEqualityData d(cx);
  CHECK(leq(d.null, d.undef));
  return true;
}
END_FIXTURE_TEST(LooseEqualityFixture, test_null_leq_undef)

// 11.9.3 3
BEGIN_FIXTURE_TEST(LooseEqualityFixture, test_undef_leq_null) {
  LooseEqualityData d(cx);
  CHECK(leq(d.undef, d.null));
  return true;
}
END_FIXTURE_TEST(LooseEqualityFixture, test_undef_leq_null)

// 4 onwards NOT TESTED