blob: 844c0b06eb79f02fbc146baa98452a0aeca285e5 [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 Langlois78973f22016-08-10 14:35:56 +010027#include <cstdlib>
28#include <cstring>
29#include <cstdio>
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 debugger to start with.
40bool vixl::Test::debug_ = false;
41
42// No tracing to start with.
43bool vixl::Test::trace_sim_ = false;
44bool vixl::Test::trace_reg_ = false;
45bool vixl::Test::trace_write_ = false;
Jacob Bramleye79723a2016-06-07 17:50:47 +010046bool vixl::Test::trace_branch_ = false;
armvixl330dc712014-11-25 10:38:32 +000047
Pierre Langloisbc01be62016-10-12 14:33:00 +010048// Do not disassemble by default.
49bool vixl::Test::disassemble_ = false;
50
armvixl330dc712014-11-25 10:38:32 +000051// No colour highlight by default.
52bool vixl::Test::coloured_trace_ = false;
53
54// No instruction statistics by default.
55bool vixl::Test::instruction_stats_ = false;
56
armvixl0f35e362016-05-10 13:57:58 +010057// Don't generate traces by default.
58bool vixl::Test::generate_test_trace_ = false;
armvixl330dc712014-11-25 10:38:32 +000059
60// Instantiate a Test and append it to the linked list.
61vixl::Test::Test(const char* name, TestFunction* callback)
Pierre Langloisbde2e4b2017-01-24 17:41:26 +000062 : name_(name), callback_(callback), next_(NULL) {
armvixl330dc712014-11-25 10:38:32 +000063 // Append this test to the linked list.
64 if (first_ == NULL) {
65 VIXL_ASSERT(last_ == NULL);
66 first_ = this;
67 } else {
68 last_->next_ = this;
69 }
70 last_ = this;
71}
72
73
74// Look for 'search' in the arguments.
75static bool IsInArgs(const char* search, int argc, char* argv[]) {
76 for (int i = 1; i < argc; i++) {
77 if (strcmp(search, argv[i]) == 0) {
78 return true;
79 }
80 }
81 return false;
82}
83
84
85static bool IsOption(const char* arg) {
86 // Any argument like "--option" is an option.
87 return ((arg[0] == '-') && (arg[1] == '-'));
88}
89
90
Pierre Langloisbde2e4b2017-01-24 17:41:26 +000091static void NormalizeOption(char* arg) {
armvixl330dc712014-11-25 10:38:32 +000092 // Squash all '_' characters in options. This allows --trace_sim and
93 // --trace-sim to be handled in the same way, for example.
94 VIXL_ASSERT(IsOption(arg));
Pierre Langloisbde2e4b2017-01-24 17:41:26 +000095 for (char* c = arg; *c != '\0'; c++) {
armvixl330dc712014-11-25 10:38:32 +000096 if (*c == '_') {
97 *c = '-';
98 }
99 }
100}
101
102
103static void PrintHelpMessage() {
Pierre Langloisbde2e4b2017-01-24 17:41:26 +0000104 printf(
105 "Usage: ./test [options] [test names]\n"
armvixl330dc712014-11-25 10:38:32 +0000106 "Run all tests specified on the command line.\n"
armvixl0f35e362016-05-10 13:57:58 +0100107 "--help Print this help message.\n"
108 "--list List all available tests.\n"
109 "--run_all Run all available tests.\n"
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000110 "--verbose Print verbose output when available.\n"
armvixl0f35e362016-05-10 13:57:58 +0100111 "--debugger Run in the debugger.\n"
112 "--trace_all "
113 "Enable all trace options, plus --coloured_trace.\n"
114 "--trace_sim Generate a trace of simulated instructions, as\n"
115 " well as disassembly from the DISASM tests.\n"
116 "--trace_reg Generate a trace of simulated registers.\n"
117 "--trace_write Generate a trace of memory writes.\n"
Jacob Bramleye79723a2016-06-07 17:50:47 +0100118 "--trace_branch Generate a trace of branches taken.\n"
Pierre Langloisbc01be62016-10-12 14:33:00 +0100119 "--disassemble Disassemble and print generated instructions.\n"
armvixl0f35e362016-05-10 13:57:58 +0100120 "--coloured_trace Generate coloured trace.\n"
121 "--instruction_stats Log instruction statistics to vixl_stats.csv.\n"
122 "--generate_test_trace "
123 "Print result traces for SIM_* and TRACE_* tests.\n");
armvixl330dc712014-11-25 10:38:32 +0000124}
125
126int main(int argc, char* argv[]) {
127 // Parse the arguments. Option flags must appear first, followed by an
128 // optional list of tests to run.
129
130 int test_specifiers = 0;
131 for (int i = 1; i < argc; i++) {
132 if (IsOption(argv[i])) {
133 NormalizeOption(argv[i]);
134 } else {
135 // Anything that isn't an option is a test specifier.
136 test_specifiers++;
137 }
138 }
139
140 // Options controlling test conditions and debug output.
141
142 if (IsInArgs("--trace-all", argc, argv)) {
143 vixl::Test::set_trace_reg(true);
144 vixl::Test::set_trace_write(true);
Jacob Bramleye79723a2016-06-07 17:50:47 +0100145 vixl::Test::set_trace_branch(true);
armvixl330dc712014-11-25 10:38:32 +0000146 vixl::Test::set_trace_sim(true);
147 vixl::Test::set_coloured_trace(true);
148 }
149
150 if (IsInArgs("--coloured-trace", argc, argv)) {
151 vixl::Test::set_coloured_trace(true);
152 }
153
Georgia Kouveli1cb71442017-01-30 13:35:28 +0000154 if (IsInArgs("--verbose", argc, argv)) {
155 vixl::Test::set_verbose(true);
156 }
157
armvixl330dc712014-11-25 10:38:32 +0000158 if (IsInArgs("--debugger", argc, argv)) {
159 vixl::Test::set_debug(true);
160 }
161
162 if (IsInArgs("--trace-write", argc, argv)) {
163 vixl::Test::set_trace_write(true);
164 }
165
Jacob Bramleye79723a2016-06-07 17:50:47 +0100166 if (IsInArgs("--trace-branch", argc, argv)) {
167 vixl::Test::set_trace_branch(true);
168 }
169
armvixl330dc712014-11-25 10:38:32 +0000170 if (IsInArgs("--trace-reg", argc, argv)) {
171 vixl::Test::set_trace_reg(true);
172 }
173
174 if (IsInArgs("--trace-sim", argc, argv)) {
175 vixl::Test::set_trace_sim(true);
176 }
177
Pierre Langloisbc01be62016-10-12 14:33:00 +0100178 if (IsInArgs("--disassemble", argc, argv)) {
179 vixl::Test::set_disassemble(true);
180 }
181
armvixl330dc712014-11-25 10:38:32 +0000182 if (IsInArgs("--instruction-stats", argc, argv)) {
183 vixl::Test::set_instruction_stats(true);
184 }
185
armvixl0f35e362016-05-10 13:57:58 +0100186 if (IsInArgs("--generate-test-trace", argc, argv)) {
187 vixl::Test::set_generate_test_trace(true);
armvixl330dc712014-11-25 10:38:32 +0000188 }
189
190 // Basic (mutually-exclusive) operations.
191
192 if (IsInArgs("--help", argc, argv)) {
193 PrintHelpMessage();
194
195 } else if (IsInArgs("--list", argc, argv)) {
196 // List all registered tests, then exit.
197 for (vixl::Test* c = vixl::Test::first(); c != NULL; c = c->next()) {
198 printf("%s\n", c->name());
199 }
200
201 } else if (IsInArgs("--run-all", argc, argv)) {
202 // Run all registered tests.
203 for (vixl::Test* c = vixl::Test::first(); c != NULL; c = c->next()) {
204 printf("Running %s\n", c->name());
205 c->callback()();
206 }
207
208 } else {
209 // Run the specified tests.
210 if (test_specifiers == 0) {
211 printf("No tests specified.\n");
212 PrintHelpMessage();
213 return EXIT_FAILURE;
214 }
215
216 for (int i = 1; i < argc; i++) {
217 if (!IsOption(argv[i])) {
218 vixl::Test* c;
219 for (c = vixl::Test::first(); c != NULL; c = c->next()) {
220 if (strcmp(c->name(), argv[i]) == 0) {
221 c->callback()();
222 break;
223 }
224 }
225 // Fail if we have not found a matching test to run.
226 if (c == NULL) {
227 printf("Test '%s' does not exist. Aborting...\n", argv[i]);
228 abort();
229 }
230 }
231 }
232 }
233
234 return EXIT_SUCCESS;
235}