blob: 25b87b857a06c53a3e28d2b5c3e5d61c38ffe672 [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
37// No debugger to start with.
38bool vixl::Test::debug_ = false;
39
40// No tracing to start with.
41bool vixl::Test::trace_sim_ = false;
42bool vixl::Test::trace_reg_ = false;
43bool vixl::Test::trace_write_ = false;
44
45// No colour highlight by default.
46bool vixl::Test::coloured_trace_ = false;
47
48// No instruction statistics by default.
49bool vixl::Test::instruction_stats_ = false;
50
armvixl0f35e362016-05-10 13:57:58 +010051// Don't generate traces by default.
52bool vixl::Test::generate_test_trace_ = false;
armvixl330dc712014-11-25 10:38:32 +000053
54// Instantiate a Test and append it to the linked list.
55vixl::Test::Test(const char* name, TestFunction* callback)
56 : name_(name), callback_(callback), next_(NULL) {
57 // Append this test to the linked list.
58 if (first_ == NULL) {
59 VIXL_ASSERT(last_ == NULL);
60 first_ = this;
61 } else {
62 last_->next_ = this;
63 }
64 last_ = this;
65}
66
67
68// Look for 'search' in the arguments.
69static bool IsInArgs(const char* search, int argc, char* argv[]) {
70 for (int i = 1; i < argc; i++) {
71 if (strcmp(search, argv[i]) == 0) {
72 return true;
73 }
74 }
75 return false;
76}
77
78
79static bool IsOption(const char* arg) {
80 // Any argument like "--option" is an option.
81 return ((arg[0] == '-') && (arg[1] == '-'));
82}
83
84
85static void NormalizeOption(char * arg) {
86 // Squash all '_' characters in options. This allows --trace_sim and
87 // --trace-sim to be handled in the same way, for example.
88 VIXL_ASSERT(IsOption(arg));
89 for (char * c = arg; *c != '\0'; c++) {
90 if (*c == '_') {
91 *c = '-';
92 }
93 }
94}
95
96
97static void PrintHelpMessage() {
98 printf("Usage: ./test [options] [test names]\n"
99 "Run all tests specified on the command line.\n"
armvixl0f35e362016-05-10 13:57:58 +0100100 "--help Print this help message.\n"
101 "--list List all available tests.\n"
102 "--run_all Run all available tests.\n"
103 "--debugger Run in the debugger.\n"
104 "--trace_all "
105 "Enable all trace options, plus --coloured_trace.\n"
106 "--trace_sim Generate a trace of simulated instructions, as\n"
107 " well as disassembly from the DISASM tests.\n"
108 "--trace_reg Generate a trace of simulated registers.\n"
109 "--trace_write Generate a trace of memory writes.\n"
110 "--coloured_trace Generate coloured trace.\n"
111 "--instruction_stats Log instruction statistics to vixl_stats.csv.\n"
112 "--generate_test_trace "
113 "Print result traces for SIM_* and TRACE_* tests.\n");
armvixl330dc712014-11-25 10:38:32 +0000114}
115
116int main(int argc, char* argv[]) {
117 // Parse the arguments. Option flags must appear first, followed by an
118 // optional list of tests to run.
119
120 int test_specifiers = 0;
121 for (int i = 1; i < argc; i++) {
122 if (IsOption(argv[i])) {
123 NormalizeOption(argv[i]);
124 } else {
125 // Anything that isn't an option is a test specifier.
126 test_specifiers++;
127 }
128 }
129
130 // Options controlling test conditions and debug output.
131
132 if (IsInArgs("--trace-all", argc, argv)) {
133 vixl::Test::set_trace_reg(true);
134 vixl::Test::set_trace_write(true);
135 vixl::Test::set_trace_sim(true);
136 vixl::Test::set_coloured_trace(true);
137 }
138
139 if (IsInArgs("--coloured-trace", argc, argv)) {
140 vixl::Test::set_coloured_trace(true);
141 }
142
143 if (IsInArgs("--debugger", argc, argv)) {
144 vixl::Test::set_debug(true);
145 }
146
147 if (IsInArgs("--trace-write", argc, argv)) {
148 vixl::Test::set_trace_write(true);
149 }
150
151 if (IsInArgs("--trace-reg", argc, argv)) {
152 vixl::Test::set_trace_reg(true);
153 }
154
155 if (IsInArgs("--trace-sim", argc, argv)) {
156 vixl::Test::set_trace_sim(true);
157 }
158
159 if (IsInArgs("--instruction-stats", argc, argv)) {
160 vixl::Test::set_instruction_stats(true);
161 }
162
armvixl0f35e362016-05-10 13:57:58 +0100163 if (IsInArgs("--generate-test-trace", argc, argv)) {
164 vixl::Test::set_generate_test_trace(true);
armvixl330dc712014-11-25 10:38:32 +0000165 }
166
167 // Basic (mutually-exclusive) operations.
168
169 if (IsInArgs("--help", argc, argv)) {
170 PrintHelpMessage();
171
172 } else if (IsInArgs("--list", argc, argv)) {
173 // List all registered tests, then exit.
174 for (vixl::Test* c = vixl::Test::first(); c != NULL; c = c->next()) {
175 printf("%s\n", c->name());
176 }
177
178 } else if (IsInArgs("--run-all", argc, argv)) {
179 // Run all registered tests.
180 for (vixl::Test* c = vixl::Test::first(); c != NULL; c = c->next()) {
181 printf("Running %s\n", c->name());
182 c->callback()();
183 }
184
185 } else {
186 // Run the specified tests.
187 if (test_specifiers == 0) {
188 printf("No tests specified.\n");
189 PrintHelpMessage();
190 return EXIT_FAILURE;
191 }
192
193 for (int i = 1; i < argc; i++) {
194 if (!IsOption(argv[i])) {
195 vixl::Test* c;
196 for (c = vixl::Test::first(); c != NULL; c = c->next()) {
197 if (strcmp(c->name(), argv[i]) == 0) {
198 c->callback()();
199 break;
200 }
201 }
202 // Fail if we have not found a matching test to run.
203 if (c == NULL) {
204 printf("Test '%s' does not exist. Aborting...\n", argv[i]);
205 abort();
206 }
207 }
208 }
209 }
210
211 return EXIT_SUCCESS;
212}
213