Alexandre Rames | b78f139 | 2016-07-01 14:22:22 +0100 | [diff] [blame] | 1 | // Copyright 2014, VIXL authors |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 2 | // 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 Langlois | 1bce007 | 2017-06-06 17:58:58 +0100 | [diff] [blame] | 27 | #include <cstdio> |
Pierre Langlois | 78973f2 | 2016-08-10 14:35:56 +0100 | [diff] [blame] | 28 | #include <cstdlib> |
| 29 | #include <cstring> |
Alexandre Rames | b68bacb | 2016-05-24 08:56:23 +0100 | [diff] [blame] | 30 | |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 31 | #include "test-runner.h" |
| 32 | |
| 33 | // Initialize the list as empty. |
| 34 | vixl::Test* vixl::Test::first_ = NULL; |
| 35 | vixl::Test* vixl::Test::last_ = NULL; |
| 36 | |
Georgia Kouveli | 1cb7144 | 2017-01-30 13:35:28 +0000 | [diff] [blame] | 37 | bool vixl::Test::verbose_ = false; |
| 38 | |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 39 | // No tracing to start with. |
| 40 | bool vixl::Test::trace_sim_ = false; |
| 41 | bool vixl::Test::trace_reg_ = false; |
| 42 | bool vixl::Test::trace_write_ = false; |
Jacob Bramley | e79723a | 2016-06-07 17:50:47 +0100 | [diff] [blame] | 43 | bool vixl::Test::trace_branch_ = false; |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 44 | |
Pierre Langlois | bc01be6 | 2016-10-12 14:33:00 +0100 | [diff] [blame] | 45 | // Do not disassemble by default. |
| 46 | bool vixl::Test::disassemble_ = false; |
Jacob Bramley | d817e1e | 2017-06-22 11:31:43 +0100 | [diff] [blame] | 47 | bool vixl::Test::disassemble_infrastructure_ = false; |
Pierre Langlois | bc01be6 | 2016-10-12 14:33:00 +0100 | [diff] [blame] | 48 | |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 49 | // No colour highlight by default. |
| 50 | bool vixl::Test::coloured_trace_ = false; |
| 51 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 52 | // Don't generate traces by default. |
| 53 | bool vixl::Test::generate_test_trace_ = false; |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 54 | |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 55 | // Look for 'search' in the arguments. |
| 56 | static 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 | |
| 66 | static bool IsOption(const char* arg) { |
| 67 | // Any argument like "--option" is an option. |
| 68 | return ((arg[0] == '-') && (arg[1] == '-')); |
| 69 | } |
| 70 | |
| 71 | |
Pierre Langlois | bde2e4b | 2017-01-24 17:41:26 +0000 | [diff] [blame] | 72 | static void NormalizeOption(char* arg) { |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 73 | // 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 Langlois | bde2e4b | 2017-01-24 17:41:26 +0000 | [diff] [blame] | 76 | for (char* c = arg; *c != '\0'; c++) { |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 77 | if (*c == '_') { |
| 78 | *c = '-'; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | static void PrintHelpMessage() { |
Pierre Langlois | bde2e4b | 2017-01-24 17:41:26 +0000 | [diff] [blame] | 85 | printf( |
| 86 | "Usage: ./test [options] [test names]\n" |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 87 | "Run all tests specified on the command line.\n" |
Jacob Bramley | d817e1e | 2017-06-22 11:31:43 +0100 | [diff] [blame] | 88 | "--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 Bramley | d817e1e | 2017-06-22 11:31:43 +0100 | [diff] [blame] | 92 | "--trace_all " |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 93 | "Enable all trace options, plus --coloured_trace.\n" |
Jacob Bramley | d817e1e | 2017-06-22 11:31:43 +0100 | [diff] [blame] | 94 | "--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 Bramley | d817e1e | 2017-06-22 11:31:43 +0100 | [diff] [blame] | 104 | "--generate_test_trace " |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 105 | "Print result traces for SIM_* and TRACE_* tests.\n"); |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | int 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 Bramley | e79723a | 2016-06-07 17:50:47 +0100 | [diff] [blame] | 127 | vixl::Test::set_trace_branch(true); |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 128 | 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 Kouveli | 1cb7144 | 2017-01-30 13:35:28 +0000 | [diff] [blame] | 136 | if (IsInArgs("--verbose", argc, argv)) { |
| 137 | vixl::Test::set_verbose(true); |
| 138 | } |
| 139 | |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 140 | if (IsInArgs("--trace-write", argc, argv)) { |
| 141 | vixl::Test::set_trace_write(true); |
| 142 | } |
| 143 | |
Jacob Bramley | e79723a | 2016-06-07 17:50:47 +0100 | [diff] [blame] | 144 | if (IsInArgs("--trace-branch", argc, argv)) { |
| 145 | vixl::Test::set_trace_branch(true); |
| 146 | } |
| 147 | |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 148 | 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 Langlois | bc01be6 | 2016-10-12 14:33:00 +0100 | [diff] [blame] | 156 | if (IsInArgs("--disassemble", argc, argv)) { |
| 157 | vixl::Test::set_disassemble(true); |
Jacob Bramley | d817e1e | 2017-06-22 11:31:43 +0100 | [diff] [blame] | 158 | 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 Langlois | bc01be6 | 2016-10-12 14:33:00 +0100 | [diff] [blame] | 162 | } |
| 163 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 164 | if (IsInArgs("--generate-test-trace", argc, argv)) { |
| 165 | vixl::Test::set_generate_test_trace(true); |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 166 | } |
| 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 Bramley | e828920 | 2019-07-31 11:25:23 +0100 | [diff] [blame] | 183 | c->run(); |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 184 | } |
| 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 Bramley | e828920 | 2019-07-31 11:25:23 +0100 | [diff] [blame] | 199 | c->run(); |
armvixl | 330dc71 | 2014-11-25 10:38:32 +0000 | [diff] [blame] | 200 | 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 Bramley | e828920 | 2019-07-31 11:25:23 +0100 | [diff] [blame] | 214 | |
| 215 | void vixl::Test::set_callback(TestFunction* callback) { |
| 216 | callback_ = callback; |
| 217 | callback_with_config_ = NULL; |
| 218 | } |
| 219 | |
| 220 | void vixl::Test::set_callback(TestFunctionWithConfig* callback) { |
| 221 | callback_ = NULL; |
| 222 | callback_with_config_ = callback; |
| 223 | } |
| 224 | |
| 225 | void 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 | } |