Index: test/cctest/asmjs/test-asm-lexer.cc |
diff --git a/test/cctest/asmjs/test-asm-lexer.cc b/test/cctest/asmjs/test-asm-lexer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9bc630d518ea4ad64ce7db21d77f611388738a24 |
--- /dev/null |
+++ b/test/cctest/asmjs/test-asm-lexer.cc |
@@ -0,0 +1,219 @@ |
+// Copyright 2016 the V8 project authors. All rights reserved. |
vogelheim
2017/03/14 13:36:38
nitpick: 2017
vogelheim
2017/03/14 13:36:38
Please consider using unittests/.., rather than cc
bradn
2017/03/15 07:53:04
Done.
bradn
2017/03/15 07:53:04
Done.
Thanks for the suggestion!
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "src/asmjs/asm-lexer.h" |
+#include "src/v8.h" |
+#include "test/cctest/cctest.h" |
+ |
+namespace v8 { |
+namespace internal { |
+ |
+#define SKIP(t) \ |
+ do { \ |
+ CHECK_EQ((t), lexer.Token()); \ |
+ lexer.Next(); \ |
+ } while (0); |
+ |
+#define TOK(t) AsmJsLexer::kToken_##t |
+ |
+#define SKIP_GLOBAL() \ |
+ do { \ |
+ CHECK(lexer.IsGlobal()); \ |
+ lexer.Next(); \ |
+ } while (0); |
+ |
+#define SKIP_LOCAL() \ |
+ do { \ |
+ CHECK(lexer.IsLocal()); \ |
+ lexer.Next(); \ |
+ } while (0); |
+ |
+#define CHECK_FOR_END() CHECK(lexer.Token() == AsmJsLexer::kEndOfInput); |
+ |
+#define SETUP_SOURCE(ssource) \ |
+ v8::V8::Initialize(); \ |
+ v8::Isolate* isolate = CcTest::isolate(); \ |
+ v8::HandleScope handles(isolate); \ |
+ i::Isolate* i_isolate = CcTest::i_isolate(); \ |
+ i::Factory* factory = i_isolate->factory(); \ |
+ i::Handle<i::String> source = factory->InternalizeUtf8String(ssource); \ |
+ i::Handle<i::Script> script = factory->NewScript(source); \ |
+ AsmJsLexer lexer(i_isolate, script, 0, source->length()); |
+ |
+TEST(SimpleFunction) { |
+ SETUP_SOURCE("function foo() { return; }"); |
+ SKIP(TOK(function)); |
+ DCHECK_EQ("foo", lexer.name()); |
+ SKIP_GLOBAL(); |
+ SKIP('('); |
+ SKIP(')'); |
+ SKIP('{'); |
+ SKIP(TOK(return )); |
+ SKIP(';'); |
+ SKIP('}'); |
+ CHECK_FOR_END(); |
+} |
+ |
+TEST(JSKeywords) { |
+ SETUP_SOURCE( |
+ "arguments break case const continue\n" |
+ "default do else eval for function\n" |
+ "if new return switch var while\n"); |
+ SKIP(TOK(arguments)); |
+ SKIP(TOK(break)); |
+ SKIP(TOK(case)); |
+ SKIP(TOK(const)); |
+ SKIP(TOK(continue)); |
+ SKIP(TOK(default)); |
+ SKIP(TOK(do)); |
+ SKIP(TOK(else)); |
+ SKIP(TOK(eval)); |
+ SKIP(TOK(for)); |
+ SKIP(TOK(function)); |
+ SKIP(TOK(if)); |
+ SKIP(TOK(new)); |
+ SKIP(TOK(return )); |
+ SKIP(TOK(switch)); |
+ SKIP(TOK(var)); |
+ SKIP(TOK(while)); |
+ CHECK_FOR_END(); |
+} |
+ |
+TEST(JSOperatorsSpread) { |
+ SETUP_SOURCE( |
+ "+ - * / % & | ^ ~ << >> >>>\n" |
+ "< > <= >= == !=\n"); |
+ SKIP('+'); |
+ SKIP('-'); |
+ SKIP('*'); |
+ SKIP('/'); |
+ SKIP('%'); |
+ SKIP('&'); |
+ SKIP('|'); |
+ SKIP('^'); |
+ SKIP('~'); |
+ SKIP(TOK(SHL)); |
+ SKIP(TOK(SAR)); |
+ SKIP(TOK(SHR)); |
+ SKIP('<'); |
+ SKIP('>'); |
+ SKIP(TOK(LE)); |
+ SKIP(TOK(GE)); |
+ SKIP(TOK(EQ)); |
+ SKIP(TOK(NE)); |
+ CHECK_FOR_END(); |
+} |
+ |
+TEST(JSOperatorsTight) { |
+ SETUP_SOURCE( |
+ "+-*/%&|^~<<>> >>>\n" |
+ "<><=>= ==!=\n"); |
+ SKIP('+'); |
+ SKIP('-'); |
+ SKIP('*'); |
+ SKIP('/'); |
+ SKIP('%'); |
+ SKIP('&'); |
+ SKIP('|'); |
+ SKIP('^'); |
+ SKIP('~'); |
+ SKIP(TOK(SHL)); |
+ SKIP(TOK(SAR)); |
+ SKIP(TOK(SHR)); |
+ SKIP('<'); |
+ SKIP('>'); |
+ SKIP(TOK(LE)); |
+ SKIP(TOK(GE)); |
+ SKIP(TOK(EQ)); |
+ SKIP(TOK(NE)); |
+ CHECK_FOR_END(); |
+} |
+ |
+TEST(UsesOfAsm) { |
+ SETUP_SOURCE("'use asm' \"use asm\"\n"); |
+ SKIP(TOK(UseAsm)); |
+ SKIP(TOK(UseAsm)); |
+ CHECK_FOR_END(); |
+} |
+ |
+TEST(GlobalAssignment) { |
+ SETUP_SOURCE("var x = x + x;"); |
+ SKIP(TOK(var)); |
+ CHECK_EQ("x", lexer.name()); |
+ AsmJsLexer::token_t x = lexer.Token(); |
+ SKIP_GLOBAL(); |
+ SKIP('='); |
+ SKIP(x); |
+ SKIP('+'); |
+ SKIP(x); |
+ SKIP(';'); |
+ CHECK_FOR_END(); |
+} |
+ |
+TEST(LocalAssignment) { |
+ SETUP_SOURCE("var x = x + x;"); |
+ lexer.SetLocalScope(true); |
+ SKIP(TOK(var)); |
+ CHECK_EQ("x", lexer.name()); |
+ AsmJsLexer::token_t x = lexer.Token(); |
+ SKIP_LOCAL(); |
+ SKIP('='); |
+ SKIP(x); |
+ SKIP('+'); |
+ SKIP(x); |
+ SKIP(';'); |
+ CHECK_FOR_END(); |
+} |
+ |
+TEST(Numbers) { |
+ SETUP_SOURCE("1 1.2 0x1f 1.e3"); |
+ |
+ CHECK(lexer.IsUnsigned()); |
+ CHECK_EQ(1, lexer.AsUnsigned()); |
+ lexer.Next(); |
+ |
+ CHECK(lexer.IsDouble()); |
+ CHECK_EQ(1.2, lexer.AsDouble()); |
+ lexer.Next(); |
+ |
+ CHECK(lexer.IsUnsigned()); |
+ CHECK_EQ(31, lexer.AsUnsigned()); |
+ lexer.Next(); |
+ |
+ CHECK(lexer.IsDouble()); |
+ CHECK_EQ(1.0e3, lexer.AsDouble()); |
+ lexer.Next(); |
+ |
+ CHECK_FOR_END(); |
+} |
+ |
+TEST(Rewind1) { |
+ SETUP_SOURCE("+ - * /"); |
+ SKIP('+'); |
+ lexer.Rewind(); |
+ SKIP('+'); |
+ SKIP('-'); |
+ lexer.Rewind(); |
+ SKIP('-'); |
+ SKIP('*'); |
+ lexer.Rewind(); |
+ SKIP('*'); |
+ SKIP('/'); |
+ lexer.Rewind(); |
+ SKIP('/'); |
+ CHECK_FOR_END(); |
+} |
+ |
+TEST(Comments) { |
+ SETUP_SOURCE( |
+ "var // This is a test /* */\n" |
+ "function /* this */ ^"); |
+ SKIP(TOK(var)); |
+ SKIP(TOK(function)); |
+ SKIP('^'); |
+ CHECK_FOR_END(); |
+} |
+ |
+} // namespace internal |
+} // namespace v8 |