OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef VM_UNIT_TEST_H_ | 5 #ifndef VM_UNIT_TEST_H_ |
6 #define VM_UNIT_TEST_H_ | 6 #define VM_UNIT_TEST_H_ |
7 | 7 |
8 #include "include/dart_native_api.h" | 8 #include "include/dart_native_api.h" |
9 | 9 |
10 #include "platform/globals.h" | 10 #include "platform/globals.h" |
11 | 11 |
12 #include "vm/ast.h" | 12 #include "vm/ast.h" |
13 #include "vm/dart.h" | 13 #include "vm/dart.h" |
14 #include "vm/globals.h" | 14 #include "vm/globals.h" |
15 #include "vm/heap.h" | 15 #include "vm/heap.h" |
16 #include "vm/isolate.h" | 16 #include "vm/isolate.h" |
17 #include "vm/longjump.h" | 17 #include "vm/longjump.h" |
18 #include "vm/object.h" | 18 #include "vm/object.h" |
19 #include "vm/object_store.h" | 19 #include "vm/object_store.h" |
20 #include "vm/simulator.h" | 20 #include "vm/simulator.h" |
21 #include "vm/zone.h" | 21 #include "vm/zone.h" |
22 | 22 |
23 // The UNIT_TEST_CASE macro is used for tests that do not need any | 23 // The UNIT_TEST_CASE macro is used for tests that do not need any |
24 // default isolate or zone functionality. | 24 // default isolate or zone functionality. |
25 #define UNIT_TEST_CASE(name) \ | 25 #define UNIT_TEST_CASE(name) \ |
26 void Dart_Test##name(); \ | 26 void Dart_Test##name(); \ |
27 static const dart::TestCase kRegister##name(Dart_Test##name, #name); \ | 27 static const dart::TestCase kRegister##name(Dart_Test##name, #name); \ |
28 void Dart_Test##name() | 28 void Dart_Test##name() |
29 | 29 |
| 30 // The VM_TEST_CASE macro is used for tests that need an isolate and zone |
| 31 // in order to test its functionality. This macro is used for tests that |
| 32 // are implemented using the VM code directly and do not use the Dart API |
| 33 // for calling into the VM. The safepoint execution state of threads using |
| 34 // this macro is transitioned from kThreadInNative to kThreadInVM. |
| 35 #define VM_TEST_CASE(name) \ |
| 36 static void Dart_TestHelper##name(Thread* thread); \ |
| 37 UNIT_TEST_CASE(name) \ |
| 38 { \ |
| 39 TestIsolateScope __test_isolate__; \ |
| 40 Thread* __thread__ = Thread::Current(); \ |
| 41 ASSERT(__thread__->isolate() == __test_isolate__.isolate()); \ |
| 42 TransitionNativeToVM transition(__thread__); \ |
| 43 StackZone __zone__(__thread__); \ |
| 44 HandleScope __hs__(__thread__); \ |
| 45 Dart_TestHelper##name(__thread__); \ |
| 46 } \ |
| 47 static void Dart_TestHelper##name(Thread* thread) |
| 48 |
30 // The TEST_CASE macro is used for tests that need an isolate and zone | 49 // The TEST_CASE macro is used for tests that need an isolate and zone |
31 // in order to test its functionality. | 50 // in order to test its functionality. This macro is used for tests that |
| 51 // are implemented using the Dart API for calling into the VM. The safepoint |
| 52 // execution state of threads using this macro remains kThreadNative. |
32 #define TEST_CASE(name) \ | 53 #define TEST_CASE(name) \ |
33 static void Dart_TestHelper##name(Thread* thread); \ | 54 static void Dart_TestHelper##name(Thread* thread); \ |
34 UNIT_TEST_CASE(name) \ | 55 UNIT_TEST_CASE(name) \ |
35 { \ | 56 { \ |
36 TestIsolateScope __test_isolate__; \ | 57 TestIsolateScope __test_isolate__; \ |
37 Thread* __thread__ = Thread::Current(); \ | 58 Thread* __thread__ = Thread::Current(); \ |
38 ASSERT(__thread__->isolate() == __test_isolate__.isolate()); \ | 59 ASSERT(__thread__->isolate() == __test_isolate__.isolate()); \ |
39 StackZone __zone__(__thread__); \ | 60 StackZone __zone__(__thread__); \ |
40 HandleScope __hs__(__thread__); \ | 61 HandleScope __hs__(__thread__); \ |
41 Dart_TestHelper##name(__thread__); \ | 62 Dart_TestHelper##name(__thread__); \ |
42 } \ | 63 } \ |
43 static void Dart_TestHelper##name(Thread* thread) | 64 static void Dart_TestHelper##name(Thread* thread) |
44 | 65 |
45 // The ASSEMBLER_TEST_GENERATE macro is used to generate a unit test | 66 // The ASSEMBLER_TEST_GENERATE macro is used to generate a unit test |
46 // for the assembler. | 67 // for the assembler. |
47 #define ASSEMBLER_TEST_GENERATE(name, assembler) \ | 68 #define ASSEMBLER_TEST_GENERATE(name, assembler) \ |
48 void AssemblerTestGenerate##name(Assembler* assembler) | 69 void AssemblerTestGenerate##name(Assembler* assembler) |
49 | 70 |
50 // The ASSEMBLER_TEST_EXTERN macro is used to declare a unit test | 71 // The ASSEMBLER_TEST_EXTERN macro is used to declare a unit test |
51 // for the assembler. | 72 // for the assembler. |
52 #define ASSEMBLER_TEST_EXTERN(name) \ | 73 #define ASSEMBLER_TEST_EXTERN(name) \ |
53 extern void AssemblerTestGenerate##name(Assembler* assembler); | 74 extern void AssemblerTestGenerate##name(Assembler* assembler); |
54 | 75 |
55 // The ASSEMBLER_TEST_RUN macro is used to execute the assembler unit | 76 // The ASSEMBLER_TEST_RUN macro is used to execute the assembler unit |
56 // test generated using the ASSEMBLER_TEST_GENERATE macro. | 77 // test generated using the ASSEMBLER_TEST_GENERATE macro. |
57 // C++ callee-saved registers are not preserved. Arguments may be passed in. | 78 // C++ callee-saved registers are not preserved. Arguments may be passed in. |
58 #define ASSEMBLER_TEST_RUN(name, test) \ | 79 #define ASSEMBLER_TEST_RUN(name, test) \ |
59 static void AssemblerTestRun##name(AssemblerTest* test); \ | 80 static void AssemblerTestRun##name(AssemblerTest* test); \ |
60 TEST_CASE(name) { \ | 81 VM_TEST_CASE(name) { \ |
61 Assembler __assembler__; \ | 82 Assembler __assembler__; \ |
62 AssemblerTest test(""#name, &__assembler__); \ | 83 AssemblerTest test(""#name, &__assembler__); \ |
63 AssemblerTestGenerate##name(test.assembler()); \ | 84 AssemblerTestGenerate##name(test.assembler()); \ |
64 test.Assemble(); \ | 85 test.Assemble(); \ |
65 AssemblerTestRun##name(&test); \ | 86 AssemblerTestRun##name(&test); \ |
66 } \ | 87 } \ |
67 static void AssemblerTestRun##name(AssemblerTest* test) | 88 static void AssemblerTestRun##name(AssemblerTest* test) |
68 | 89 |
69 // Populate node list with AST nodes. | 90 // Populate node list with AST nodes. |
70 #define CODEGEN_TEST_GENERATE(name, test) \ | 91 #define CODEGEN_TEST_GENERATE(name, test) \ |
71 static void CodeGenTestGenerate##name(CodeGenTest* test) | 92 static void CodeGenTestGenerate##name(CodeGenTest* test) |
72 | 93 |
73 // Populate node list with AST nodes, possibly using the provided function | 94 // Populate node list with AST nodes, possibly using the provided function |
74 // object built by a previous CODEGEN_TEST_GENERATE. | 95 // object built by a previous CODEGEN_TEST_GENERATE. |
75 #define CODEGEN_TEST2_GENERATE(name, function, test) \ | 96 #define CODEGEN_TEST2_GENERATE(name, function, test) \ |
76 static void CodeGenTestGenerate##name(const Function& function, \ | 97 static void CodeGenTestGenerate##name(const Function& function, \ |
77 CodeGenTest* test) | 98 CodeGenTest* test) |
78 | 99 |
79 | 100 |
80 // Pass the name of test and the expected results as RawObject. | 101 // Pass the name of test and the expected results as RawObject. |
81 #define CODEGEN_TEST_RUN(name, expected) \ | 102 #define CODEGEN_TEST_RUN(name, expected) \ |
82 static void CodeGenTestRun##name(const Function& function); \ | 103 static void CodeGenTestRun##name(const Function& function); \ |
83 TEST_CASE(name) { \ | 104 VM_TEST_CASE(name) { \ |
84 CodeGenTest __test__(""#name); \ | 105 CodeGenTest __test__(""#name); \ |
85 CodeGenTestGenerate##name(&__test__); \ | 106 CodeGenTestGenerate##name(&__test__); \ |
86 __test__.Compile(); \ | 107 __test__.Compile(); \ |
87 CodeGenTestRun##name(__test__.function()); \ | 108 CodeGenTestRun##name(__test__.function()); \ |
88 } \ | 109 } \ |
89 static void CodeGenTestRun##name(const Function& function) { \ | 110 static void CodeGenTestRun##name(const Function& function) { \ |
90 Object& result = Object::Handle(); \ | 111 Object& result = Object::Handle(); \ |
91 result = DartEntry::InvokeFunction(function, Object::empty_array()); \ | 112 result = DartEntry::InvokeFunction(function, Object::empty_array()); \ |
92 EXPECT(!result.IsError()); \ | 113 EXPECT(!result.IsError()); \ |
93 Instance& actual = Instance::Handle(); \ | 114 Instance& actual = Instance::Handle(); \ |
94 actual ^= result.raw(); \ | 115 actual ^= result.raw(); \ |
95 EXPECT(actual.CanonicalizeEquals(Instance::Handle(expected))); \ | 116 EXPECT(actual.CanonicalizeEquals(Instance::Handle(expected))); \ |
96 } | 117 } |
97 | 118 |
98 | 119 |
99 // Pass the name of test, and use the generated function to call it | 120 // Pass the name of test, and use the generated function to call it |
100 // and evaluate its result. | 121 // and evaluate its result. |
101 #define CODEGEN_TEST_RAW_RUN(name, function) \ | 122 #define CODEGEN_TEST_RAW_RUN(name, function) \ |
102 static void CodeGenTestRun##name(const Function& function); \ | 123 static void CodeGenTestRun##name(const Function& function); \ |
103 TEST_CASE(name) { \ | 124 VM_TEST_CASE(name) { \ |
104 CodeGenTest __test__(""#name); \ | 125 CodeGenTest __test__(""#name); \ |
105 CodeGenTestGenerate##name(&__test__); \ | 126 CodeGenTestGenerate##name(&__test__); \ |
106 __test__.Compile(); \ | 127 __test__.Compile(); \ |
107 CodeGenTestRun##name(__test__.function()); \ | 128 CodeGenTestRun##name(__test__.function()); \ |
108 } \ | 129 } \ |
109 static void CodeGenTestRun##name(const Function& function) | 130 static void CodeGenTestRun##name(const Function& function) |
110 | 131 |
111 | 132 |
112 // Generate code for two sequences of AST nodes and execute the first one. | 133 // Generate code for two sequences of AST nodes and execute the first one. |
113 // The first one may reference the Function object generated by the second one. | 134 // The first one may reference the Function object generated by the second one. |
114 #define CODEGEN_TEST2_RUN(name1, name2, expected) \ | 135 #define CODEGEN_TEST2_RUN(name1, name2, expected) \ |
115 static void CodeGenTestRun##name1(const Function& function); \ | 136 static void CodeGenTestRun##name1(const Function& function); \ |
116 TEST_CASE(name1) { \ | 137 VM_TEST_CASE(name1) { \ |
117 /* Generate code for name2 */ \ | 138 /* Generate code for name2 */ \ |
118 CodeGenTest __test2__(""#name2); \ | 139 CodeGenTest __test2__(""#name2); \ |
119 CodeGenTestGenerate##name2(&__test2__); \ | 140 CodeGenTestGenerate##name2(&__test2__); \ |
120 __test2__.Compile(); \ | 141 __test2__.Compile(); \ |
121 /* Generate code for name1, providing function2 */ \ | 142 /* Generate code for name1, providing function2 */ \ |
122 CodeGenTest __test1__(""#name1); \ | 143 CodeGenTest __test1__(""#name1); \ |
123 CodeGenTestGenerate##name1(__test2__.function(), &__test1__); \ | 144 CodeGenTestGenerate##name1(__test2__.function(), &__test1__); \ |
124 __test1__.Compile(); \ | 145 __test1__.Compile(); \ |
125 CodeGenTestRun##name1(__test1__.function()); \ | 146 CodeGenTestRun##name1(__test1__.function()); \ |
126 } \ | 147 } \ |
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
549 // Yields: | 570 // Yields: |
550 // | 571 // |
551 // out = "\"id\":\"\"" | 572 // out = "\"id\":\"\"" |
552 // | 573 // |
553 void ElideJSONSubstring(const char* prefix, const char* in, char* out); | 574 void ElideJSONSubstring(const char* prefix, const char* in, char* out); |
554 | 575 |
555 | 576 |
556 } // namespace dart | 577 } // namespace dart |
557 | 578 |
558 #endif // VM_UNIT_TEST_H_ | 579 #endif // VM_UNIT_TEST_H_ |
OLD | NEW |