Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 95 additions & 3 deletions testsuite/CoinTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <string>
#include <vector>
Expand Down Expand Up @@ -100,14 +101,91 @@ inline void check_equal(const A & a, const B & b,
}
}

inline int run_all(void)
inline void print_usage(const char * program)
{
std::fprintf(stderr,
"Usage: %s [--list] [--test NAME | --filter TEXT] [--max-tests N]\n",
program);
}

inline bool parse_max_tests(const char * text, size_t & value)
{
if (!text || !text[0]) return false;
char * end = NULL;
const unsigned long parsed = std::strtoul(text, &end, 10);
if (*end != '\0' || parsed == 0) return false;
value = static_cast<size_t>(parsed);
return true;
}

inline int run_all(int argc, char ** argv)
{
const std::vector<TestCase> & tests = registry();
const char * exact_name = NULL;
const char * name_filter = NULL;
size_t max_tests = 0;
bool list_tests = false;

for (int i = 1; i < argc; ++i) {
const char * arg = argv[i];
if (std::strcmp(arg, "--list") == 0) {
list_tests = true;
}
else if (std::strcmp(arg, "--test") == 0 ||
std::strcmp(arg, "--filter") == 0) {
if (i + 1 >= argc) {
print_usage(argv[0]);
return 2;
}
if (std::strcmp(arg, "--test") == 0) {
if (exact_name || name_filter) {
print_usage(argv[0]);
return 2;
}
exact_name = argv[++i];
}
else {
if (exact_name || name_filter) {
print_usage(argv[0]);
return 2;
}
name_filter = argv[++i];
}
}
else if (std::strcmp(arg, "--max-tests") == 0) {
if (i + 1 >= argc || !parse_max_tests(argv[++i], max_tests)) {
print_usage(argv[0]);
return 2;
}
}
else {
print_usage(argv[0]);
return 2;
}
}

if (list_tests) {
for (size_t i = 0; i < tests.size(); ++i) {
const TestCase & tc = tests[i];
std::fprintf(stdout, "%zu\t%s\t%s:%d\n", i + 1, tc.name, tc.file, tc.line);
}
return 0;
}

int failedtests = 0;
int totalchecks = 0;
size_t selectedtests = 0;

for (size_t i = 0; i < tests.size(); ++i) {
const TestCase & tc = tests[i];
if (exact_name && std::strcmp(tc.name, exact_name) != 0) continue;
if (name_filter && !std::strstr(tc.name, name_filter)) continue;
if (max_tests && selectedtests >= max_tests) break;
selectedtests += 1;

std::fprintf(stderr, "[RUN %zu/%zu] %s (%s:%d)\n",
i + 1, tests.size(), tc.name, tc.file, tc.line);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running bin/CoinTests --max-tests 10 shows [RUN 1/325]. I would expect here for it to show [RUN 1/10], what do you think? Same for --filter.

std::fflush(stderr);

Context ctx;
current_context() = &ctx;
Expand All @@ -123,6 +201,9 @@ inline int run_all(void)
}

current_context() = NULL;
std::fprintf(stderr, "[DONE %zu/%zu] %s\n",
i + 1, tests.size(), tc.name);
std::fflush(stderr);
totalchecks += ctx.checks;

if (ctx.failed) {
Expand All @@ -135,15 +216,26 @@ inline int run_all(void)
}
}

if (selectedtests == 0) {
std::fprintf(stderr, "[ERROR] no tests selected\n");
return 2;
}

if (failedtests == 0) {
std::fprintf(stderr, "[OK] %zu tests, %d checks\n", tests.size(), totalchecks);
std::fprintf(stderr, "[OK] %zu tests, %d checks\n", selectedtests, totalchecks);
return 0;
}

std::fprintf(stderr, "[FAIL] %d/%zu tests failed, %d checks\n", failedtests, tests.size(), totalchecks);
std::fprintf(stderr, "[FAIL] %d/%zu tests failed, %d checks\n",
failedtests, selectedtests, totalchecks);
return 1;
}

inline int run_all(void)
{
return run_all(0, NULL);
}

} // namespace CoinTest

// ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion testsuite/TestSuiteMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main(int argc, char* argv[])
SoInteraction::init();
TestSuite::Init();

int rc = CoinTest::run_all();
int rc = CoinTest::run_all(argc, argv);

SoDB::finish();

Expand Down