1 // Copyright 2013, ARM Limited 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 27 #include <stdlib.h> 28 #include <stdio.h> 29 #include <string.h> 30 #include "cctest.h" 31 32 // Initialize the list as empty. 33 vixl::Cctest* vixl::Cctest::first_ = NULL; 34 vixl::Cctest* vixl::Cctest::last_ = NULL; 35 36 // No debugger to start with. 37 bool vixl::Cctest::debug_ = false; 38 39 // No tracing to start with. 40 bool vixl::Cctest::trace_sim_ = false; 41 bool vixl::Cctest::trace_reg_ = false; 42 43 // No colour highlight by default. 44 bool vixl::Cctest::coloured_trace_ = false; 45 46 // No instruction statistics by default. 47 bool vixl::Cctest::instruction_stats_ = false; 48 49 // Don't generate simulator test traces by default. 50 bool vixl::Cctest::sim_test_trace_ = false; 51 52 // Instantiate a Cctest and append it to the linked list. 53 vixl::Cctest::Cctest(const char* name, CctestFunction* callback) 54 : name_(name), callback_(callback), next_(NULL) { 55 // Append this cctest to the linked list. 56 if (first_ == NULL) { 57 VIXL_ASSERT(last_ == NULL); 58 first_ = this; 59 } else { 60 last_->next_ = this; 61 } 62 last_ = this; 63 } 64 65 66 // Look for 'search' in the arguments. 67 bool IsInArgs(const char* search, int argc, char* argv[]) { 68 for (int i = 1; i < argc; i++) { 69 if (strcmp(search, argv[i]) == 0) { 70 return true; 71 } 72 } 73 return false; 74 } 75 76 77 // Special keywords used as arguments must be registered here. 78 bool IsSpecialArgument(const char* arg) { 79 return (strcmp(arg, "--help") == 0) || 80 (strcmp(arg, "--list") == 0) || 81 (strcmp(arg, "--run_all") == 0) || 82 (strcmp(arg, "--debugger") == 0) || 83 (strcmp(arg, "--trace_sim") == 0) || 84 (strcmp(arg, "--trace_reg") == 0) || 85 (strcmp(arg, "--coloured_trace") == 0) || 86 (strcmp(arg, "--instruction_stats") == 0) || 87 (strcmp(arg, "--sim_test_trace") == 0); 88 } 89 90 91 void PrintHelpMessage() { 92 printf("Usage: ./cctest [options] [test names]\n" 93 "Run all tests specified on the command line.\n" 94 "--help print this help message.\n" 95 "--list list all available tests.\n" 96 "--run_all run all available tests.\n" 97 "--debugger run in the debugger.\n" 98 "--trace_sim generate a trace of simulated instructions.\n" 99 "--trace_reg generate a trace of simulated registers. " 100 "Implies --debugger.\n" 101 "--coloured_trace generate coloured trace.\n" 102 "--instruction_stats log instruction statistics to vixl_stats.csv.\n" 103 "--sim_test_trace Print result traces for SIM_* tests.\n"); 104 } 105 106 int main(int argc, char* argv[]) { 107 // Parse the arguments. Option flags must appear first, followed by an 108 // optional list of tests to run. 109 110 if (IsInArgs("--coloured_trace", argc, argv)) { 111 vixl::Cctest::set_coloured_trace(true); 112 } 113 114 if (IsInArgs("--debugger", argc, argv)) { 115 vixl::Cctest::set_debug(true); 116 } 117 118 if (IsInArgs("--trace_reg", argc, argv)) { 119 vixl::Cctest::set_trace_reg(true); 120 } 121 122 if (IsInArgs("--trace_sim", argc, argv)) { 123 vixl::Cctest::set_trace_sim(true); 124 } 125 126 if (IsInArgs("--instruction_stats", argc, argv)) { 127 vixl::Cctest::set_instruction_stats(true); 128 } 129 130 if (IsInArgs("--sim_test_trace", argc, argv)) { 131 vixl::Cctest::set_sim_test_trace(true); 132 } 133 134 if (IsInArgs("--help", argc, argv)) { 135 PrintHelpMessage(); 136 137 } else if (IsInArgs("--list", argc, argv)) { 138 // List all registered cctests. 139 for (vixl::Cctest* c = vixl::Cctest::first(); c != NULL; c = c->next()) { 140 printf("%s\n", c->name()); 141 } 142 143 } else if (IsInArgs("--run_all", argc, argv)) { 144 // Run all registered cctests. 145 for (vixl::Cctest* c = vixl::Cctest::first(); c != NULL; c = c->next()) { 146 printf("Running %s\n", c->name()); 147 c->callback()(); 148 } 149 150 } else { 151 if (argc <= 1) 152 PrintHelpMessage(); 153 // Other arguments must be tests to run. 154 int i = 1; 155 for (i = 1; i < argc; i++) { 156 if (!IsSpecialArgument(argv[i])) { 157 vixl::Cctest* c; 158 for (c = vixl::Cctest::first(); c != NULL; c = c->next()) { 159 if (strcmp(c->name(), argv[i]) == 0) { 160 c->callback()(); 161 break; 162 } 163 } 164 // Fail if we have not found a matching test to run. 165 if (c == NULL) { 166 printf("Test '%s' does not exist. Aborting...\n", argv[i]); 167 abort(); 168 } 169 } 170 } 171 } 172 173 return EXIT_SUCCESS; 174 } 175 176