aboutsummaryrefslogtreecommitdiff
path: root/test/test-runner.cc
blob: 3382eb10461e46837167f5aa2012b9c7f71f353a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2014, ARM Limited
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//   * Neither the name of ARM Limited nor the names of its contributors may be
//     used to endorse or promote products derived from this software without
//     specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "test-runner.h"

// Initialize the list as empty.
vixl::Test* vixl::Test::first_ = NULL;
vixl::Test* vixl::Test::last_ = NULL;

// No debugger to start with.
bool vixl::Test::debug_ = false;

// No tracing to start with.
bool vixl::Test::trace_sim_ = false;
bool vixl::Test::trace_reg_ = false;
bool vixl::Test::trace_write_ = false;

// No colour highlight by default.
bool vixl::Test::coloured_trace_ = false;

// No instruction statistics by default.
bool vixl::Test::instruction_stats_ = false;

// Don't generate traces by default.
bool vixl::Test::generate_test_trace_ = false;

// Instantiate a Test and append it to the linked list.
vixl::Test::Test(const char* name, TestFunction* callback)
  : name_(name), callback_(callback), next_(NULL) {
  // Append this test to the linked list.
  if (first_ == NULL) {
    VIXL_ASSERT(last_ == NULL);
    first_ = this;
  } else {
    last_->next_ = this;
  }
  last_ = this;
}


// Look for 'search' in the arguments.
static bool IsInArgs(const char* search, int argc, char* argv[]) {
  for (int i = 1; i < argc; i++) {
    if (strcmp(search, argv[i]) == 0) {
      return true;
    }
  }
  return false;
}


static bool IsOption(const char* arg) {
  // Any argument like "--option" is an option.
  return ((arg[0] == '-') && (arg[1] == '-'));
}


static void NormalizeOption(char * arg) {
  // Squash all '_' characters in options. This allows --trace_sim and
  // --trace-sim to be handled in the same way, for example.
  VIXL_ASSERT(IsOption(arg));
  for (char * c = arg; *c != '\0'; c++) {
    if (*c == '_') {
      *c = '-';
    }
  }
}


static void PrintHelpMessage() {
  printf("Usage:  ./test [options] [test names]\n"
      "Run all tests specified on the command line.\n"
      "--help                 Print this help message.\n"
      "--list                 List all available tests.\n"
      "--run_all              Run all available tests.\n"
      "--debugger             Run in the debugger.\n"
      "--trace_all            "
      "Enable all trace options, plus --coloured_trace.\n"
      "--trace_sim            Generate a trace of simulated instructions, as\n"
      "                       well as disassembly from the DISASM tests.\n"
      "--trace_reg            Generate a trace of simulated registers.\n"
      "--trace_write          Generate a trace of memory writes.\n"
      "--coloured_trace       Generate coloured trace.\n"
      "--instruction_stats    Log instruction statistics to vixl_stats.csv.\n"
      "--generate_test_trace  "
      "Print result traces for SIM_* and TRACE_* tests.\n");
}

int main(int argc, char* argv[]) {
  // Parse the arguments. Option flags must appear first, followed by an
  // optional list of tests to run.

  int test_specifiers = 0;
  for (int i = 1; i < argc; i++) {
    if (IsOption(argv[i])) {
      NormalizeOption(argv[i]);
    } else {
      // Anything that isn't an option is a test specifier.
      test_specifiers++;
    }
  }

  // Options controlling test conditions and debug output.

  if (IsInArgs("--trace-all", argc, argv)) {
    vixl::Test::set_trace_reg(true);
    vixl::Test::set_trace_write(true);
    vixl::Test::set_trace_sim(true);
    vixl::Test::set_coloured_trace(true);
  }

  if (IsInArgs("--coloured-trace", argc, argv)) {
    vixl::Test::set_coloured_trace(true);
  }

  if (IsInArgs("--debugger", argc, argv)) {
    vixl::Test::set_debug(true);
  }

  if (IsInArgs("--trace-write", argc, argv)) {
    vixl::Test::set_trace_write(true);
  }

  if (IsInArgs("--trace-reg", argc, argv)) {
    vixl::Test::set_trace_reg(true);
  }

  if (IsInArgs("--trace-sim", argc, argv)) {
    vixl::Test::set_trace_sim(true);
  }

  if (IsInArgs("--instruction-stats", argc, argv)) {
    vixl::Test::set_instruction_stats(true);
  }

  if (IsInArgs("--generate-test-trace", argc, argv)) {
    vixl::Test::set_generate_test_trace(true);
  }

  // Basic (mutually-exclusive) operations.

  if (IsInArgs("--help", argc, argv)) {
    PrintHelpMessage();

  } else if (IsInArgs("--list", argc, argv)) {
    // List all registered tests, then exit.
    for (vixl::Test* c = vixl::Test::first(); c != NULL; c = c->next()) {
      printf("%s\n", c->name());
    }

  } else if (IsInArgs("--run-all", argc, argv)) {
    // Run all registered tests.
    for (vixl::Test* c = vixl::Test::first(); c != NULL; c = c->next()) {
      printf("Running %s\n", c->name());
      c->callback()();
    }

  } else {
    // Run the specified tests.
    if (test_specifiers == 0) {
      printf("No tests specified.\n");
      PrintHelpMessage();
      return EXIT_FAILURE;
    }

    for (int i = 1; i < argc; i++) {
      if (!IsOption(argv[i])) {
        vixl::Test* c;
        for (c = vixl::Test::first(); c != NULL; c = c->next()) {
          if (strcmp(c->name(), argv[i]) == 0) {
            c->callback()();
            break;
          }
        }
        // Fail if we have not found a matching test to run.
        if (c == NULL) {
          printf("Test '%s' does not exist. Aborting...\n", argv[i]);
          abort();
        }
      }
    }
  }

  return EXIT_SUCCESS;
}