| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/v8.h" | 5 #include "src/v8.h" |
| 6 | 6 |
| 7 #include "src/factory.h" | 7 #include "src/factory.h" |
| 8 #include "src/heap/heap.h" | 8 #include "src/heap/heap.h" |
| 9 #include "src/heap/heap-inl.h" | 9 #include "src/heap/heap-inl.h" |
| 10 #include "src/runtime/runtime.h" | 10 #include "src/runtime/runtime.h" |
| 11 #include "test/unittests/test-utils.h" | 11 #include "test/unittests/test-utils.h" |
| 12 | 12 |
| 13 namespace v8 { | 13 namespace v8 { |
| 14 namespace internal { | 14 namespace internal { |
| 15 namespace interpreter { | 15 namespace interpreter { |
| 16 | 16 |
| 17 class RuntimeInterpreterTest : public TestWithIsolateAndZone { | 17 class RuntimeInterpreterTest : public TestWithIsolateAndZone { |
| 18 public: | 18 public: |
| 19 typedef Object* (*RuntimeMethod)(int, Object**, Isolate*); | 19 typedef Object* (*RuntimeMethod)(int, Object**, Isolate*); |
| 20 | 20 |
| 21 RuntimeInterpreterTest() {} | 21 RuntimeInterpreterTest() {} |
| 22 ~RuntimeInterpreterTest() override {} | 22 ~RuntimeInterpreterTest() override {} |
| 23 | 23 |
| 24 bool TestOperatorWithObjects(RuntimeMethod method, Handle<Object> lhs, | 24 bool TestOperatorWithObjects(RuntimeMethod method, Handle<Object> lhs, |
| 25 Handle<Object> rhs, bool expected); | 25 Handle<Object> rhs, bool expected); |
| 26 bool TestOperator(RuntimeMethod method, int32_t lhs, int32_t rhs, | |
| 27 bool expected); | |
| 28 bool TestOperator(RuntimeMethod method, double lhs, double rhs, | |
| 29 bool expected); | |
| 30 bool TestOperator(RuntimeMethod method, const char* lhs, const char* rhs, | |
| 31 bool expected); | |
| 32 }; | 26 }; |
| 33 | 27 |
| 34 | 28 |
| 35 bool RuntimeInterpreterTest::TestOperatorWithObjects(RuntimeMethod method, | 29 bool RuntimeInterpreterTest::TestOperatorWithObjects(RuntimeMethod method, |
| 36 Handle<Object> lhs, | 30 Handle<Object> lhs, |
| 37 Handle<Object> rhs, | 31 Handle<Object> rhs, |
| 38 bool expected) { | 32 bool expected) { |
| 39 Object* args_object[] = {*rhs, *lhs}; | 33 Object* args_object[] = {*rhs, *lhs}; |
| 40 Handle<Object> result = | 34 Handle<Object> result = |
| 41 handle(method(2, &args_object[1], isolate()), isolate()); | 35 handle(method(2, &args_object[1], isolate()), isolate()); |
| 42 CHECK(result->IsTrue() || result->IsFalse()); | 36 CHECK(result->IsTrue() || result->IsFalse()); |
| 43 return result->IsTrue() == expected; | 37 return result->IsTrue() == expected; |
| 44 } | 38 } |
| 45 | 39 |
| 46 | 40 |
| 47 bool RuntimeInterpreterTest::TestOperator(RuntimeMethod method, int32_t lhs, | |
| 48 int32_t rhs, bool expected) { | |
| 49 Handle<Object> x = isolate()->factory()->NewNumberFromInt(lhs); | |
| 50 Handle<Object> y = isolate()->factory()->NewNumberFromInt(rhs); | |
| 51 return TestOperatorWithObjects(method, x, y, expected); | |
| 52 } | |
| 53 | |
| 54 | |
| 55 bool RuntimeInterpreterTest::TestOperator(RuntimeMethod method, double lhs, | |
| 56 double rhs, bool expected) { | |
| 57 Handle<Object> x = isolate()->factory()->NewNumber(lhs); | |
| 58 Handle<Object> y = isolate()->factory()->NewNumber(rhs); | |
| 59 CHECK_EQ(HeapNumber::cast(*x)->value(), lhs); | |
| 60 CHECK_EQ(HeapNumber::cast(*y)->value(), rhs); | |
| 61 return TestOperatorWithObjects(method, x, y, expected); | |
| 62 } | |
| 63 | |
| 64 | |
| 65 bool RuntimeInterpreterTest::TestOperator(RuntimeMethod method, const char* lhs, | |
| 66 const char* rhs, bool expected) { | |
| 67 Handle<Object> x = isolate()->factory()->NewStringFromAsciiChecked(lhs); | |
| 68 Handle<Object> y = isolate()->factory()->NewStringFromAsciiChecked(rhs); | |
| 69 return TestOperatorWithObjects(method, x, y, expected); | |
| 70 } | |
| 71 | |
| 72 | |
| 73 TEST_F(RuntimeInterpreterTest, TestOperatorsWithIntegers) { | |
| 74 int32_t inputs[] = {kMinInt, Smi::kMinValue, -17, -1, 0, 1, | |
| 75 991, Smi::kMaxValue, kMaxInt}; | |
| 76 TRACED_FOREACH(int, lhs, inputs) { | |
| 77 TRACED_FOREACH(int, rhs, inputs) { | |
| 78 #define INTEGER_OPERATOR_CHECK(r, op, x, y) \ | |
| 79 CHECK(TestOperator(Runtime_Interpreter##r, x, y, x op y)) | |
| 80 INTEGER_OPERATOR_CHECK(LessThan, <, lhs, rhs); | |
| 81 INTEGER_OPERATOR_CHECK(GreaterThan, >, lhs, rhs); | |
| 82 INTEGER_OPERATOR_CHECK(LessThanOrEqual, <=, lhs, rhs); | |
| 83 INTEGER_OPERATOR_CHECK(GreaterThanOrEqual, >=, lhs, rhs); | |
| 84 #undef INTEGER_OPERATOR_CHECK | |
| 85 } | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 | |
| 90 TEST_F(RuntimeInterpreterTest, TestOperatorsWithDoubles) { | |
| 91 double inputs[] = {std::numeric_limits<double>::min(), | |
| 92 std::numeric_limits<double>::max(), | |
| 93 -0.001, | |
| 94 0.01, | |
| 95 3.14, | |
| 96 -6.02214086e23}; | |
| 97 TRACED_FOREACH(double, lhs, inputs) { | |
| 98 TRACED_FOREACH(double, rhs, inputs) { | |
| 99 #define DOUBLE_OPERATOR_CHECK(r, op, x, y) \ | |
| 100 CHECK(TestOperator(Runtime_Interpreter##r, x, y, x op y)) | |
| 101 DOUBLE_OPERATOR_CHECK(LessThan, <, lhs, rhs); | |
| 102 DOUBLE_OPERATOR_CHECK(GreaterThan, >, lhs, rhs); | |
| 103 DOUBLE_OPERATOR_CHECK(LessThanOrEqual, <=, lhs, rhs); | |
| 104 DOUBLE_OPERATOR_CHECK(GreaterThanOrEqual, >=, lhs, rhs); | |
| 105 #undef DOUBLE_OPERATOR_CHECK | |
| 106 } | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 | |
| 111 TEST_F(RuntimeInterpreterTest, TestOperatorsWithString) { | |
| 112 const char* inputs[] = {"abc", "a", "def", "0"}; | |
| 113 TRACED_FOREACH(const char*, lhs, inputs) { | |
| 114 TRACED_FOREACH(const char*, rhs, inputs) { | |
| 115 #define STRING_OPERATOR_CHECK(r, op, x, y) \ | |
| 116 CHECK(TestOperator(Runtime_Interpreter##r, x, y, \ | |
| 117 std::string(x) op std::string(y))) | |
| 118 STRING_OPERATOR_CHECK(LessThan, <, lhs, rhs); | |
| 119 STRING_OPERATOR_CHECK(GreaterThan, >, lhs, rhs); | |
| 120 STRING_OPERATOR_CHECK(LessThanOrEqual, <=, lhs, rhs); | |
| 121 STRING_OPERATOR_CHECK(GreaterThanOrEqual, >=, lhs, rhs); | |
| 122 #undef STRING_OPERATOR_CHECK | |
| 123 } | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 | |
| 128 TEST_F(RuntimeInterpreterTest, ToBoolean) { | 41 TEST_F(RuntimeInterpreterTest, ToBoolean) { |
| 129 double quiet_nan = std::numeric_limits<double>::quiet_NaN(); | 42 double quiet_nan = std::numeric_limits<double>::quiet_NaN(); |
| 130 std::pair<Handle<Object>, bool> cases[] = { | 43 std::pair<Handle<Object>, bool> cases[] = { |
| 131 std::make_pair(isolate()->factory()->NewNumberFromInt(0), false), | 44 std::make_pair(isolate()->factory()->NewNumberFromInt(0), false), |
| 132 std::make_pair(isolate()->factory()->NewNumberFromInt(1), true), | 45 std::make_pair(isolate()->factory()->NewNumberFromInt(1), true), |
| 133 std::make_pair(isolate()->factory()->NewNumberFromInt(100), true), | 46 std::make_pair(isolate()->factory()->NewNumberFromInt(100), true), |
| 134 std::make_pair(isolate()->factory()->NewNumberFromInt(-1), true), | 47 std::make_pair(isolate()->factory()->NewNumberFromInt(-1), true), |
| 135 std::make_pair(isolate()->factory()->NewNumber(7.7), true), | 48 std::make_pair(isolate()->factory()->NewNumber(7.7), true), |
| 136 std::make_pair(isolate()->factory()->NewNumber(0.00001), true), | 49 std::make_pair(isolate()->factory()->NewNumber(0.00001), true), |
| 137 std::make_pair(isolate()->factory()->NewNumber(quiet_nan), false), | 50 std::make_pair(isolate()->factory()->NewNumber(quiet_nan), false), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 151 Runtime_InterpreterToBoolean(1, &args_object[0], isolate()), isolate()); | 64 Runtime_InterpreterToBoolean(1, &args_object[0], isolate()), isolate()); |
| 152 CHECK(result->IsBoolean()); | 65 CHECK(result->IsBoolean()); |
| 153 CHECK_EQ(result->IsTrue(), value_expected_tuple.second); | 66 CHECK_EQ(result->IsTrue(), value_expected_tuple.second); |
| 154 } | 67 } |
| 155 } | 68 } |
| 156 | 69 |
| 157 | 70 |
| 158 } // namespace interpreter | 71 } // namespace interpreter |
| 159 } // namespace internal | 72 } // namespace internal |
| 160 } // namespace v8 | 73 } // namespace v8 |
| OLD | NEW |