blob: 092a4ed8190d42ff746da98594254d722b6151fe [file] [log] [blame]
Alexandre Ramesb78f1392016-07-01 14:22:22 +01001// Copyright 2014, VIXL authors
armvixl330dc712014-11-25 10:38:32 +00002// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// * Redistributions of source code must retain the above copyright notice,
8// this list of conditions and the following disclaimer.
9// * Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12// * Neither the name of ARM Limited nor the names of its contributors may be
13// used to endorse or promote products derived from this software without
14// specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
Pierre Langlois1bce0072017-06-06 17:58:58 +010027#include <cstdio>
Pierre Langlois78973f22016-08-10 14:35:56 +010028#include <cstdlib>
29#include <cstring>
Alexandre Ramesb68bacb2016-05-24 08:56:23 +010030
armvixl330dc712014-11-25 10:38:32 +000031#include "test-runner.h"
32
33// Initialize the list as empty.
34vixl::Test* vixl::Test::first_ = NULL;
35vixl::Test* vixl::Test::last_ = NULL;
36
Georgia Kouveli1cb71442017-01-30 13:35:28 +000037bool vixl::Test::verbose_ = false;
38
armvixl330dc712014-11-25 10:38:32 +000039// No tracing to start with.
40bool vixl::Test::trace_sim_ = false;
41bool vixl::Test::trace_reg_ = false;
42bool vixl::Test::trace_write_ = false;
Jacob Bramleye79723a2016-06-07 17:50:47 +010043bool vixl::Test::trace_branch_ = false;
armvixl330dc712014-11-25 10:38:32 +000044
Pierre Langloisbc01be62016-10-12 14:33:00 +010045// Do not disassemble by default.
46bool vixl::Test::disassemble_ = false;
Jacob Bramleyd817e1e2017-06-22 11:31:43 +010047bool vixl::Test::disassemble_infrastructure_ = false;
Pierre Langloisbc01be62016-10-12 14:33:00 +010048
armvixl330dc712014-11-25 10:38:32 +000049// No colour highlight by default.
50bool vixl::Test::coloured_trace_ = false;
51
armvixl0f35e362016-05-10 13:57:58 +010052// Don't generate traces by default.
53bool vixl::Test::generate_test_trace_ = false;
armvixl330dc712014-11-25 10:38:32 +000054
armvixl330dc712014-11-25 10:38:32 +000055// Look for 'search' in the arguments.
56static bool IsInArgs(const char* search, int argc, char* argv[]) {
57 for (int i = 1; i < argc; i++) {
58 if (strcmp(search, argv[i]) == 0) {
59 return true;
60 }
61 }
62 return false;
63}
64
65
66static bool IsOption(const char* arg) {
67 // Any argument like "--option" is an option.
68 return ((arg[0] == '-') && (arg[1] == '-'));
69}
70
71
Pierre Langloisbde2e4b2017-01-24 17:41:26 +000072static void NormalizeOption(char* arg) {
armvixl330dc712014-11-25 10:38:32 +000073 // Squash all '_' characters in options. This allows --trace_sim and
74 // --trace-sim to be handled in the same way, for example.
75 VIXL_ASSERT(IsOption(arg));
Pierre Langloisbde2e4b2017-01-24 17:41:26 +000076 for (char* c = arg; *c != '\0'; c++) {
armvixl330dc712014-11-25 10:38:32 +000077 if (*c == '_') {
78 *c = '-';
79 }
80 }
81}
82
83
84static void PrintHelpMessage() {
Pierre Langloisbde2e4b2017-01-24 17:41:26 +000085 printf(
86 "Usage: ./test [options] [test names]\n"
armvixl330dc712014-11-25 10:38:32 +000087 "Run all tests specified on the command line.\n"
Jacob Bramleyd817e1e2017-06-22 11:31:43 +010088 "--help Print this help message.\n"
89 "--list List all available tests.\n"
90 "--run_all Run all available tests.\n"
91 "--verbose Print verbose output when available.\n"
Jacob Bramleyd817e1e2017-06-22 11:31:43 +010092 "--trace_all "
armvixl0f35e362016-05-10 13:57:58 +010093 "Enable all trace options, plus --coloured_trace.\n"
Jacob Bramleyd817e1e2017-06-22 11:31:43 +010094 "--trace_sim "
95 "Generate a trace of simulated instructions, as\n"
96 " well as disassembly from the DISASM tests.\n"
97 "--trace_reg Generate a trace of simulated registers.\n"
98 "--trace_write Generate a trace of memory writes.\n"
99 "--trace_branch Generate a trace of branches taken.\n"
100 "--disassemble Disassemble and print generated instructions.\n"
101 "--disassemble-test-code "
102 "As above, but don't disassemble infrastructure code.\n"
103 "--coloured_trace Generate coloured trace.\n"
Jacob Bramleyd817e1e2017-06-22 11:31:43 +0100104 "--generate_test_trace "
armvixl0f35e362016-05-10 13:57:58 +0100105 "Print result traces for SIM_* and TRACE_* tests.\n");
armvixl330dc712014-11-25 10:38:32 +0000106}
107
108int main(int argc, char* argv[]) {
109 // Parse the arguments. Option flags must appear first, followed by an
110 // optional list of tests to run.
111
112 int test_specifiers = 0;
113 for (int i = 1; i < argc; i++) {
114 if (IsOption(argv[i])) {
115 NormalizeOption(argv[i]);
116 } else {
117 // Anything that isn't an option is a test specifier.
118 test_specifiers++;
119 }
120 }
121
122 // Options controlling test conditions and debug output.
123
124 if (IsInArgs("--trace-all", argc, argv)) {
125 vixl::Test::set_trace_reg(true);
126 vixl::Test::set_trace_write(true);
Jacob Bramleye79723a2016-06-07 17:50:47 +0100127 vixl::Test::set_trace_branch(true);
armvixl330dc712014-11-25 10:38:32 +0000128 vixl::Test::set_trace_sim(true);
129 vixl::Test::set_coloured_trace(true);
130 }
131
132 if (IsInArgs("--coloured-trace", argc, argv)) {
133 vixl::Test::set_coloured_trace(true);
134 }
135
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000136 if (IsInArgs("--verbose", argc, argv)) {
137 vixl::Test::set_verbose(true);
138 }
139
armvixl330dc712014-11-25 10:38:32 +0000140 if (IsInArgs("--trace-write", argc, argv)) {
141 vixl::Test::set_trace_write(true);
142 }
143
Jacob Bramleye79723a2016-06-07 17:50:47 +0100144 if (IsInArgs("--trace-branch", argc, argv)) {
145 vixl::Test::set_trace_branch(true);
146 }
147
armvixl330dc712014-11-25 10:38:32 +0000148 if (IsInArgs("--trace-reg", argc, argv)) {
149 vixl::Test::set_trace_reg(true);
150 }
151
152 if (IsInArgs("--trace-sim", argc, argv)) {
153 vixl::Test::set_trace_sim(true);
154 }
155
Pierre Langloisbc01be62016-10-12 14:33:00 +0100156 if (IsInArgs("--disassemble", argc, argv)) {
157 vixl::Test::set_disassemble(true);
Jacob Bramleyd817e1e2017-06-22 11:31:43 +0100158 vixl::Test::set_disassemble_infrastructure(true);
159 } else if (IsInArgs("--disassemble-test-code", argc, argv)) {
160 vixl::Test::set_disassemble(true);
161 vixl::Test::set_disassemble_infrastructure(false);
Pierre Langloisbc01be62016-10-12 14:33:00 +0100162 }
163
armvixl0f35e362016-05-10 13:57:58 +0100164 if (IsInArgs("--generate-test-trace", argc, argv)) {
165 vixl::Test::set_generate_test_trace(true);
armvixl330dc712014-11-25 10:38:32 +0000166 }
167
168 // Basic (mutually-exclusive) operations.
169
170 if (IsInArgs("--help", argc, argv)) {
171 PrintHelpMessage();
172
173 } else if (IsInArgs("--list", argc, argv)) {
174 // List all registered tests, then exit.
175 for (vixl::Test* c = vixl::Test::first(); c != NULL; c = c->next()) {
176 printf("%s\n", c->name());
177 }
178
179 } else if (IsInArgs("--run-all", argc, argv)) {
180 // Run all registered tests.
181 for (vixl::Test* c = vixl::Test::first(); c != NULL; c = c->next()) {
182 printf("Running %s\n", c->name());
Jacob Bramleye8289202019-07-31 11:25:23 +0100183 c->run();
armvixl330dc712014-11-25 10:38:32 +0000184 }
185
186 } else {
187 // Run the specified tests.
188 if (test_specifiers == 0) {
189 printf("No tests specified.\n");
190 PrintHelpMessage();
191 return EXIT_FAILURE;
192 }
193
194 for (int i = 1; i < argc; i++) {
195 if (!IsOption(argv[i])) {
196 vixl::Test* c;
197 for (c = vixl::Test::first(); c != NULL; c = c->next()) {
198 if (strcmp(c->name(), argv[i]) == 0) {
Jacob Bramleye8289202019-07-31 11:25:23 +0100199 c->run();
armvixl330dc712014-11-25 10:38:32 +0000200 break;
201 }
202 }
203 // Fail if we have not found a matching test to run.
204 if (c == NULL) {
205 printf("Test '%s' does not exist. Aborting...\n", argv[i]);
206 abort();
207 }
208 }
209 }
210 }
211
212 return EXIT_SUCCESS;
213}
Jacob Bramleye8289202019-07-31 11:25:23 +0100214
215void vixl::Test::set_callback(TestFunction* callback) {
216 callback_ = callback;
217 callback_with_config_ = NULL;
218}
219
220void vixl::Test::set_callback(TestFunctionWithConfig* callback) {
221 callback_ = NULL;
222 callback_with_config_ = callback;
223}
224
225void vixl::Test::run() {
226 if (callback_ == NULL) {
227 VIXL_ASSERT(callback_with_config_ != NULL);
228 callback_with_config_(this);
229 } else {
230 VIXL_ASSERT(callback_with_config_ == NULL);
231 callback_();
232 }
233}