Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: test/cctest/compiler/test-typer.cc

Issue 863283002: Convert compiler cctest to unittests: TyperTest (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Tweaks Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/cctest/types-fuzz.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <functional>
6
7 #include "src/codegen.h"
8 #include "src/compiler/js-operator.h"
9 #include "src/compiler/node-properties-inl.h"
10 #include "src/compiler/typer.h"
11 #include "test/cctest/cctest.h"
12 #include "test/cctest/compiler/graph-builder-tester.h"
13 #include "test/cctest/types-fuzz.h"
14
15 using namespace v8::internal;
16 using namespace v8::internal::compiler;
17
18
19 // TODO(titzer): generate a large set of deterministic inputs for these tests.
20 class TyperTester : public HandleAndZoneScope, public GraphAndBuilders {
21 public:
22 TyperTester()
23 : GraphAndBuilders(main_zone()),
24 types_(main_zone(), isolate()),
25 typer_(graph(), MaybeHandle<Context>()),
26 javascript_(main_zone()) {
27 Node* s = graph()->NewNode(common()->Start(3));
28 graph()->SetStart(s);
29 context_node_ = graph()->NewNode(common()->Parameter(2), graph()->start());
30 rng_ = isolate()->random_number_generator();
31
32 integers.push_back(0);
33 integers.push_back(0);
34 integers.push_back(-1);
35 integers.push_back(+1);
36 integers.push_back(-V8_INFINITY);
37 integers.push_back(+V8_INFINITY);
38 for (int i = 0; i < 5; ++i) {
39 double x = rng_->NextInt();
40 integers.push_back(x);
41 x *= rng_->NextInt();
42 if (!IsMinusZero(x)) integers.push_back(x);
43 }
44
45 int32s.push_back(0);
46 int32s.push_back(0);
47 int32s.push_back(-1);
48 int32s.push_back(+1);
49 int32s.push_back(kMinInt);
50 int32s.push_back(kMaxInt);
51 for (int i = 0; i < 10; ++i) {
52 int32s.push_back(rng_->NextInt());
53 }
54 }
55
56 Types<Type, Type*, Zone> types_;
57 Typer typer_;
58 JSOperatorBuilder javascript_;
59 Node* context_node_;
60 v8::base::RandomNumberGenerator* rng_;
61 std::vector<double> integers;
62 std::vector<double> int32s;
63
64 Isolate* isolate() { return main_isolate(); }
65 Graph* graph() { return main_graph_; }
66 CommonOperatorBuilder* common() { return &main_common_; }
67
68 Node* Parameter(int index = 0) {
69 return graph()->NewNode(common()->Parameter(index), graph()->start());
70 }
71
72 Type* TypeBinaryOp(const Operator* op, Type* lhs, Type* rhs) {
73 Node* p0 = Parameter(0);
74 Node* p1 = Parameter(1);
75 NodeProperties::SetBounds(p0, Bounds(lhs));
76 NodeProperties::SetBounds(p1, Bounds(rhs));
77 Node* n = graph()->NewNode(
78 op, p0, p1, context_node_, graph()->start(), graph()->start());
79 return NodeProperties::GetBounds(n).upper;
80 }
81
82 Type* RandomRange(bool int32 = false) {
83 std::vector<double>& numbers = int32 ? int32s : integers;
84 double i = numbers[rng_->NextInt(static_cast<int>(numbers.size()))];
85 double j = numbers[rng_->NextInt(static_cast<int>(numbers.size()))];
86 return NewRange(i, j);
87 }
88
89 Type* NewRange(double i, double j) {
90 Factory* f = isolate()->factory();
91 i::Handle<i::Object> min = f->NewNumber(i);
92 i::Handle<i::Object> max = f->NewNumber(j);
93 if (min->Number() > max->Number()) std::swap(min, max);
94 return Type::Range(min, max, main_zone());
95 }
96
97 double RandomInt(double min, double max) {
98 switch (rng_->NextInt(4)) {
99 case 0: return min;
100 case 1: return max;
101 default: break;
102 }
103 if (min == +V8_INFINITY) return +V8_INFINITY;
104 if (max == -V8_INFINITY) return -V8_INFINITY;
105 if (min == -V8_INFINITY && max == +V8_INFINITY) {
106 return rng_->NextInt() * static_cast<double>(rng_->NextInt());
107 }
108 double result = nearbyint(min + (max - min) * rng_->NextDouble());
109 if (IsMinusZero(result)) return 0;
110 if (std::isnan(result)) return rng_->NextInt(2) ? min : max;
111 DCHECK(min <= result && result <= max);
112 return result;
113 }
114
115 double RandomInt(Type::RangeType* range) {
116 return RandomInt(range->Min()->Number(), range->Max()->Number());
117 }
118
119 // Careful, this function runs O(max_width^5) trials.
120 template <class BinaryFunction>
121 void TestBinaryArithOpCloseToZero(const Operator* op, BinaryFunction opfun,
122 int max_width) {
123 const int min_min = -2 - max_width / 2;
124 const int max_min = 2 + max_width / 2;
125 for (int width = 0; width < max_width; width++) {
126 for (int lmin = min_min; lmin <= max_min; lmin++) {
127 for (int rmin = min_min; rmin <= max_min; rmin++) {
128 Type* r1 = NewRange(lmin, lmin + width);
129 Type* r2 = NewRange(rmin, rmin + width);
130 Type* expected_type = TypeBinaryOp(op, r1, r2);
131
132 for (int x1 = lmin; x1 < lmin + width; x1++) {
133 for (int x2 = rmin; x2 < rmin + width; x2++) {
134 double result_value = opfun(x1, x2);
135 Type* result_type = Type::Constant(
136 isolate()->factory()->NewNumber(result_value), main_zone());
137 CHECK(result_type->Is(expected_type));
138 }
139 }
140 }
141 }
142 }
143 }
144
145 template <class BinaryFunction>
146 void TestBinaryArithOp(const Operator* op, BinaryFunction opfun) {
147 TestBinaryArithOpCloseToZero(op, opfun, 8);
148 for (int i = 0; i < 100; ++i) {
149 Type::RangeType* r1 = RandomRange()->AsRange();
150 Type::RangeType* r2 = RandomRange()->AsRange();
151 Type* expected_type = TypeBinaryOp(op, r1, r2);
152 for (int i = 0; i < 10; i++) {
153 double x1 = RandomInt(r1);
154 double x2 = RandomInt(r2);
155 double result_value = opfun(x1, x2);
156 Type* result_type = Type::Constant(
157 isolate()->factory()->NewNumber(result_value), main_zone());
158 CHECK(result_type->Is(expected_type));
159 }
160 }
161 }
162
163 template <class BinaryFunction>
164 void TestBinaryCompareOp(const Operator* op, BinaryFunction opfun) {
165 for (int i = 0; i < 100; ++i) {
166 Type::RangeType* r1 = RandomRange()->AsRange();
167 Type::RangeType* r2 = RandomRange()->AsRange();
168 Type* expected_type = TypeBinaryOp(op, r1, r2);
169 for (int i = 0; i < 10; i++) {
170 double x1 = RandomInt(r1);
171 double x2 = RandomInt(r2);
172 bool result_value = opfun(x1, x2);
173 Type* result_type =
174 Type::Constant(result_value ? isolate()->factory()->true_value()
175 : isolate()->factory()->false_value(),
176 main_zone());
177 CHECK(result_type->Is(expected_type));
178 }
179 }
180 }
181
182 template <class BinaryFunction>
183 void TestBinaryBitOp(const Operator* op, BinaryFunction opfun) {
184 for (int i = 0; i < 100; ++i) {
185 Type::RangeType* r1 = RandomRange(true)->AsRange();
186 Type::RangeType* r2 = RandomRange(true)->AsRange();
187 Type* expected_type = TypeBinaryOp(op, r1, r2);
188 for (int i = 0; i < 10; i++) {
189 int32_t x1 = static_cast<int32_t>(RandomInt(r1));
190 int32_t x2 = static_cast<int32_t>(RandomInt(r2));
191 double result_value = opfun(x1, x2);
192 Type* result_type = Type::Constant(
193 isolate()->factory()->NewNumber(result_value), main_zone());
194 CHECK(result_type->Is(expected_type));
195 }
196 }
197 }
198
199 Type* RandomSubtype(Type* type) {
200 Type* subtype;
201 do {
202 subtype = types_.Fuzz();
203 } while (!subtype->Is(type));
204 return subtype;
205 }
206
207 void TestBinaryMonotonicity(const Operator* op) {
208 for (int i = 0; i < 50; ++i) {
209 Type* type1 = types_.Fuzz();
210 Type* type2 = types_.Fuzz();
211 Type* type = TypeBinaryOp(op, type1, type2);
212 Type* subtype1 = RandomSubtype(type1);;
213 Type* subtype2 = RandomSubtype(type2);;
214 Type* subtype = TypeBinaryOp(op, subtype1, subtype2);
215 CHECK(subtype->Is(type));
216 }
217 }
218 };
219
220
221 static int32_t shift_left(int32_t x, int32_t y) { return x << y; }
222 static int32_t shift_right(int32_t x, int32_t y) { return x >> y; }
223 static int32_t bit_or(int32_t x, int32_t y) { return x | y; }
224 static int32_t bit_and(int32_t x, int32_t y) { return x & y; }
225 static int32_t bit_xor(int32_t x, int32_t y) { return x ^ y; }
226
227
228 //------------------------------------------------------------------------------
229 // Soundness
230 // For simplicity, we currently only test soundness on expression operators
231 // that have a direct equivalent in C++. Also, testing is currently limited
232 // to ranges as input types.
233
234
235 TEST(TypeJSAdd) {
236 TyperTester t;
237 t.TestBinaryArithOp(t.javascript_.Add(), std::plus<double>());
238 }
239
240
241 TEST(TypeJSSubtract) {
242 TyperTester t;
243 t.TestBinaryArithOp(t.javascript_.Subtract(), std::minus<double>());
244 }
245
246
247 TEST(TypeJSMultiply) {
248 TyperTester t;
249 t.TestBinaryArithOp(t.javascript_.Multiply(), std::multiplies<double>());
250 }
251
252
253 TEST(TypeJSDivide) {
254 TyperTester t;
255 t.TestBinaryArithOp(t.javascript_.Divide(), std::divides<double>());
256 }
257
258
259 TEST(TypeJSModulus) {
260 TyperTester t;
261 t.TestBinaryArithOp(t.javascript_.Modulus(), modulo);
262 }
263
264
265 TEST(TypeJSBitwiseOr) {
266 TyperTester t;
267 t.TestBinaryBitOp(t.javascript_.BitwiseOr(), bit_or);
268 }
269
270
271 TEST(TypeJSBitwiseAnd) {
272 TyperTester t;
273 t.TestBinaryBitOp(t.javascript_.BitwiseAnd(), bit_and);
274 }
275
276
277 TEST(TypeJSBitwiseXor) {
278 TyperTester t;
279 t.TestBinaryBitOp(t.javascript_.BitwiseXor(), bit_xor);
280 }
281
282
283 TEST(TypeJSShiftLeft) {
284 TyperTester t;
285 t.TestBinaryBitOp(t.javascript_.ShiftLeft(), shift_left);
286 }
287
288
289 TEST(TypeJSShiftRight) {
290 TyperTester t;
291 t.TestBinaryBitOp(t.javascript_.ShiftRight(), shift_right);
292 }
293
294
295 TEST(TypeJSLessThan) {
296 TyperTester t;
297 t.TestBinaryCompareOp(t.javascript_.LessThan(), std::less<double>());
298 }
299
300
301 TEST(TypeJSLessThanOrEqual) {
302 TyperTester t;
303 t.TestBinaryCompareOp(
304 t.javascript_.LessThanOrEqual(), std::less_equal<double>());
305 }
306
307
308 TEST(TypeJSGreaterThan) {
309 TyperTester t;
310 t.TestBinaryCompareOp(t.javascript_.GreaterThan(), std::greater<double>());
311 }
312
313
314 TEST(TypeJSGreaterThanOrEqual) {
315 TyperTester t;
316 t.TestBinaryCompareOp(
317 t.javascript_.GreaterThanOrEqual(), std::greater_equal<double>());
318 }
319
320
321 TEST(TypeJSEqual) {
322 TyperTester t;
323 t.TestBinaryCompareOp(t.javascript_.Equal(), std::equal_to<double>());
324 }
325
326
327 TEST(TypeJSNotEqual) {
328 TyperTester t;
329 t.TestBinaryCompareOp(t.javascript_.NotEqual(), std::not_equal_to<double>());
330 }
331
332
333 // For numbers there's no difference between strict and non-strict equality.
334 TEST(TypeJSStrictEqual) {
335 TyperTester t;
336 t.TestBinaryCompareOp(t.javascript_.StrictEqual(), std::equal_to<double>());
337 }
338
339
340 TEST(TypeJSStrictNotEqual) {
341 TyperTester t;
342 t.TestBinaryCompareOp(
343 t.javascript_.StrictNotEqual(), std::not_equal_to<double>());
344 }
345
346
347 //------------------------------------------------------------------------------
348 // Monotonicity
349
350
351 // List should be in sync with JS_SIMPLE_BINOP_LIST.
352 #define JSBINOP_LIST(V) \
353 V(Equal) \
354 V(NotEqual) \
355 V(StrictEqual) \
356 V(StrictNotEqual) \
357 V(LessThan) \
358 V(GreaterThan) \
359 V(LessThanOrEqual) \
360 V(GreaterThanOrEqual) \
361 V(BitwiseOr) \
362 V(BitwiseXor) \
363 V(BitwiseAnd) \
364 V(ShiftLeft) \
365 V(ShiftRight) \
366 V(ShiftRightLogical) \
367 V(Add) \
368 V(Subtract) \
369 V(Multiply) \
370 V(Divide) \
371 V(Modulus)
372
373
374 #define TEST_FUNC(name) \
375 TEST(Monotonicity_##name) { \
376 TyperTester t; \
377 t.TestBinaryMonotonicity(t.javascript_.name()); \
378 }
379 JSBINOP_LIST(TEST_FUNC)
380 #undef TEST_FUNC
OLDNEW
« no previous file with comments | « no previous file | test/cctest/types-fuzz.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698