deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / test / cctest / test-javascript-arm64.cc
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include <limits.h>
29
30 #include "src/v8.h"
31
32 #include "src/api.h"
33 #include "src/base/platform/platform.h"
34 #include "src/compilation-cache.h"
35 #include "src/execution.h"
36 #include "src/isolate.h"
37 #include "src/parser.h"
38 #include "src/unicode-inl.h"
39 #include "src/utils.h"
40 #include "test/cctest/cctest.h"
41
42 using ::v8::Context;
43 using ::v8::Extension;
44 using ::v8::Function;
45 using ::v8::FunctionTemplate;
46 using ::v8::Handle;
47 using ::v8::HandleScope;
48 using ::v8::Local;
49 using ::v8::Message;
50 using ::v8::MessageCallback;
51 using ::v8::Object;
52 using ::v8::ObjectTemplate;
53 using ::v8::Persistent;
54 using ::v8::Script;
55 using ::v8::StackTrace;
56 using ::v8::String;
57 using ::v8::TryCatch;
58 using ::v8::Undefined;
59 using ::v8::V8;
60 using ::v8::Value;
61
62 static void ExpectBoolean(bool expected, Local<Value> result) {
63   CHECK(result->IsBoolean());
64   CHECK_EQ(expected, result->BooleanValue());
65 }
66
67
68 static void ExpectInt32(int32_t expected, Local<Value> result) {
69   CHECK(result->IsInt32());
70   CHECK_EQ(expected, result->Int32Value());
71 }
72
73
74 static void ExpectNumber(double expected, Local<Value> result) {
75   CHECK(result->IsNumber());
76   CHECK_EQ(expected, result->NumberValue());
77 }
78
79
80 static void ExpectUndefined(Local<Value> result) {
81   CHECK(result->IsUndefined());
82 }
83
84
85 // Tests are sorted by order of implementation.
86
87 TEST(simple_value) {
88   LocalContext env;
89   v8::HandleScope scope(env->GetIsolate());
90   Local<Value> result = CompileRun("0x271828;");
91   ExpectInt32(0x271828, result);
92 }
93
94
95 TEST(global_variable) {
96   LocalContext env;
97   v8::HandleScope scope(env->GetIsolate());
98   Local<Value> result = CompileRun("var my_global_var = 0x123; my_global_var;");
99   ExpectInt32(0x123, result);
100 }
101
102
103 TEST(simple_function_call) {
104   LocalContext env;
105   v8::HandleScope scope(env->GetIsolate());
106   Local<Value> result = CompileRun(
107       "function foo() { return 0x314; }"
108       "foo();");
109   ExpectInt32(0x314, result);
110 }
111
112
113 TEST(binary_op) {
114   LocalContext env;
115   v8::HandleScope scope(env->GetIsolate());
116   Local<Value> result = CompileRun(
117       "function foo() {"
118       "  var a = 0x1200;"
119       "  var b = 0x0035;"
120       "  return 2 * (a + b - 1);"
121       "}"
122       "foo();");
123   ExpectInt32(0x2468, result);
124 }
125
126 static void if_comparison_testcontext_helper(
127     char const * op,
128     char const * lhs,
129     char const * rhs,
130     int          expect) {
131   char buffer[256];
132   snprintf(buffer, sizeof(buffer),
133            "var lhs = %s;"
134            "var rhs = %s;"
135            "if ( lhs %s rhs ) { 1; }"
136            "else { 0; }",
137            lhs, rhs, op);
138   Local<Value> result = CompileRun(buffer);
139   ExpectInt32(expect, result);
140 }
141
142 static void if_comparison_effectcontext_helper(
143     char const * op,
144     char const * lhs,
145     char const * rhs,
146     int          expect) {
147   char buffer[256];
148   snprintf(buffer, sizeof(buffer),
149            "var lhs = %s;"
150            "var rhs = %s;"
151            "var test = lhs %s rhs;"
152            "if ( test ) { 1; }"
153            "else { 0; }",
154            lhs, rhs, op);
155   Local<Value> result = CompileRun(buffer);
156   ExpectInt32(expect, result);
157 }
158
159 static void if_comparison_helper(
160     char const * op,
161     int          expect_when_lt,
162     int          expect_when_eq,
163     int          expect_when_gt) {
164   // TODO(all): Non-SMI tests.
165
166   if_comparison_testcontext_helper(op, "1", "3", expect_when_lt);
167   if_comparison_testcontext_helper(op, "5", "5", expect_when_eq);
168   if_comparison_testcontext_helper(op, "9", "7", expect_when_gt);
169
170   if_comparison_effectcontext_helper(op, "1", "3", expect_when_lt);
171   if_comparison_effectcontext_helper(op, "5", "5", expect_when_eq);
172   if_comparison_effectcontext_helper(op, "9", "7", expect_when_gt);
173 }
174
175
176 TEST(if_comparison) {
177   LocalContext env;
178   v8::HandleScope scope(env->GetIsolate());
179
180   if_comparison_helper("<",   1, 0, 0);
181   if_comparison_helper("<=",  1, 1, 0);
182   if_comparison_helper("==",  0, 1, 0);
183   if_comparison_helper("===", 0, 1, 0);
184   if_comparison_helper(">=",  0, 1, 1);
185   if_comparison_helper(">",   0, 0, 1);
186   if_comparison_helper("!=",  1, 0, 1);
187   if_comparison_helper("!==", 1, 0, 1);
188 }
189
190
191 TEST(unary_plus) {
192   LocalContext env;
193   v8::HandleScope scope(env->GetIsolate());
194   Local<Value> result;
195   // SMI
196   result = CompileRun("var a = 1234; +a");
197   ExpectInt32(1234, result);
198   // Number
199   result = CompileRun("var a = 1234.5; +a");
200   ExpectNumber(1234.5, result);
201   // String (SMI)
202   result = CompileRun("var a = '1234'; +a");
203   ExpectInt32(1234, result);
204   // String (Number)
205   result = CompileRun("var a = '1234.5'; +a");
206   ExpectNumber(1234.5, result);
207   // Check side effects.
208   result = CompileRun("var a = 1234; +(a = 4321); a");
209   ExpectInt32(4321, result);
210 }
211
212
213 TEST(unary_minus) {
214   LocalContext env;
215   v8::HandleScope scope(env->GetIsolate());
216   Local<Value> result;
217   result = CompileRun("var a = 1234; -a");
218   ExpectInt32(-1234, result);
219   result = CompileRun("var a = 1234.5; -a");
220   ExpectNumber(-1234.5, result);
221   result = CompileRun("var a = 1234; -(a = 4321); a");
222   ExpectInt32(4321, result);
223   result = CompileRun("var a = '1234'; -a");
224   ExpectInt32(-1234, result);
225   result = CompileRun("var a = '1234.5'; -a");
226   ExpectNumber(-1234.5, result);
227 }
228
229
230 TEST(unary_void) {
231   LocalContext env;
232   v8::HandleScope scope(env->GetIsolate());
233   Local<Value> result;
234   result = CompileRun("var a = 1234; void (a);");
235   ExpectUndefined(result);
236   result = CompileRun("var a = 0; void (a = 42); a");
237   ExpectInt32(42, result);
238   result = CompileRun("var a = 0; void (a = 42);");
239   ExpectUndefined(result);
240 }
241
242
243 TEST(unary_not) {
244   LocalContext env;
245   v8::HandleScope scope(env->GetIsolate());
246   Local<Value> result;
247   result = CompileRun("var a = 1234; !a");
248   ExpectBoolean(false, result);
249   result = CompileRun("var a = 0; !a");
250   ExpectBoolean(true, result);
251   result = CompileRun("var a = 0; !(a = 1234); a");
252   ExpectInt32(1234, result);
253   result = CompileRun("var a = '1234'; !a");
254   ExpectBoolean(false, result);
255   result = CompileRun("var a = ''; !a");
256   ExpectBoolean(true, result);
257   result = CompileRun("var a = 1234; !!a");
258   ExpectBoolean(true, result);
259   result = CompileRun("var a = 0; !!a");
260   ExpectBoolean(false, result);
261   result = CompileRun("var a = 0; if ( !a ) { 1; } else { 0; }");
262   ExpectInt32(1, result);
263   result = CompileRun("var a = 1; if ( !a ) { 1; } else { 0; }");
264   ExpectInt32(0, result);
265 }