/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * Mikael Zayenz Lagerkvist * * Copyright: * Christian Schulte, 2004 * Mikael Zayenz Lagerkvist, 2005 * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.dev * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ #include "test/test.hh" #ifdef GECODE_HAS_MTRACE #include #endif #include #include #include #include #include #include #include namespace Test { // Log stream std::ostringstream olog; /// A command-line name and its corresponding test tag struct TestTagDescription { const char* name; TestTag tag; }; /// Tags supported by the test runner static const TestTagDescription test_tag_descriptions[] = { {"check", TestTag::check}, {"standard", TestTag::standard}, {"sweep", TestTag::sweep} }; TestTags TestTags::all(void) { TestTags tags; for (const TestTagDescription& description : test_tag_descriptions) tags.add(description.tag); return tags; } /// Return the tag set for \a name, or an empty set if not known static TestTags tag_set(const char* name) { for (const TestTagDescription& description : test_tag_descriptions) if (!strcmp(name, description.name)) return TestTags(description.tag); if (!strcmp(name, "all")) return TestTags::all(); return TestTags(); } /// Print all tag names static void print_tags(std::ostream& os) { for (const TestTagDescription& description : test_tag_descriptions) os << description.name << std::endl; } /// Convert the known tag names to a command-line choice list static std::string tag_choices(void) { std::string choices; for (const TestTagDescription& description : test_tag_descriptions) { if (!choices.empty()) choices += "|"; choices += description.name; } return choices + "|all"; } /// Convert \a tags to a comma-separated string static std::string tags_to_string(TestTags tags) { std::string s; for (const TestTagDescription& description : test_tag_descriptions) { if (tags.overlaps(TestTags(description.tag))) { if (!s.empty()) s += ","; s += description.name; } } return s; } /* * Base class for tests * */ Base::Base(std::string s) : Base(s, TestTags::standard()) {} Base::Base(std::string s, TestTags t) : _name(std::move(s)), _tags(t), _next(_tests), _rand(Gecode::Support::RandomGenerator()) { _tests = this; _n_tests++; } Base* Base::_tests = nullptr; unsigned int Base::_n_tests = 0; /// Sort tests by name class SortByName { public: forceinline bool operator()(Base* x, Base* y) { return x->name() > y->name(); } }; void Base::sort() { Base** b = Gecode::heap.alloc(_n_tests); unsigned int i=0; for (Base* t = _tests; t != nullptr; t = t->next()) b[i++] = t; SortByName sbn; Gecode::Support::quicksort(b, _n_tests,sbn); i=0; _tests = nullptr; for ( ; i < _n_tests; i++) { b[i]->next(_tests); _tests = b[i]; } Gecode::heap.free(b,_n_tests); } Base::~Base() = default; Options opt; const int Options::defiter; const unsigned int Options::deffixprob; void report_error(const std::string& name, unsigned int seed, const Options& options, std::ostream& ostream) { ostream << "Options: -replay " << seed; if (options.fixprob != Test::Options::deffixprob) ostream << " -fixprob " << options.fixprob; ostream << " -test " << name << std::endl; if (options.log) ostream << olog.str(); } void Options::parse(int argc, char* argv[]) { int i = 1; while (i < argc) { if (!strcmp(argv[i],"-help") || !strcmp(argv[i],"--help")) { std::cerr << "Options for testing:" << std::endl << "\t-threads (unsigned int) default: " << threads << std::endl << "\t\tnumber of threads to use. If 0, as many threads as there are cores are used.\n" << "\t\tThreaded execution and logging can not be used at the same time." << std::endl << "\t-seed (unsigned int or \"time\") default: " << seed << std::endl << "\t\tseed for random number generator (unsigned int)," << std::endl << "\t\tor \"time\" for a random seed based on " << "current time" << std::endl << "\t-replay (unsigned int)\n" << "\t\treplay the exact -test name once, on one thread\n" << "\t-fixprob (unsigned int) default: " << fixprob << std::endl << "\t\t1/fixprob is the probability of computing a fixpoint" << std::endl << "\t-iter (unsigned int) default: " <(atoi(argv[i])); if (argument == 0) { threads = Gecode::Support::Thread::npu(); } else { threads = argument; } } else if (!strcmp(argv[i],"-seed") || !strcmp(argv[i],"-replay")) { replay = !strcmp(argv[i],"-replay"); if (++i == argc) goto missing; if (!strcmp(argv[i],"time")) { seed = static_cast(time(nullptr)); } else { seed = static_cast(atoi(argv[i])); } } else if (!strcmp(argv[i],"-iter")) { if (++i == argc) goto missing; iter = static_cast(atoi(argv[i])); } else if (!strcmp(argv[i],"-fixprob")) { if (++i == argc) goto missing; fixprob = static_cast(atoi(argv[i])); } else if (!strcmp(argv[i],"-test")) { if (++i == argc) goto missing; if (argv[i][0] == '^') testpat.emplace_back(MT_FIRST, argv[i] + 1); else if (argv[i][0] == '-') testpat.emplace_back(MT_NOT, argv[i] + 1); else testpat.emplace_back(MT_ANY, argv[i]); } else if (!strcmp(argv[i],"-tag")) { if (++i == argc) goto missing; TestTags tag = tag_set(argv[i]); if (tag.empty()) { std::cerr << "Erroneous argument (-tag)" << std::endl << " unknown tag: " << argv[i] << std::endl; exit(EXIT_FAILURE); } testtags.add(tag); } else if (!strcmp(argv[i],"-start")) { if (++i == argc) goto missing; start_from = argv[i]; } else if (!strcmp(argv[i],"-log")) { log = true; } else if (!strcmp(argv[i],"-stop")) { if (++i == argc) goto missing; if(argv[i][0] == 't') { stop = true; } else if (argv[i][0] == 'f') { stop = false; } } else if (!strcmp(argv[i],"-list")) { list = true; } else if (!strcmp(argv[i],"-list-tags")) { list_tags = true; } else if (!strcmp(argv[i],"-list-with-tags")) { list_with_tags = true; } i++; } if (replay) { if (testpat.size() != 1 || testpat[0].first != MT_ANY || start_from != nullptr) { std::cerr << "-replay requires one exact -test name." << std::endl; exit(EXIT_FAILURE); } threads = 1; iter = 1; } if (threads > 1 && log) { std::cerr << "Logging and multi threading can not be used jointly." << std::endl; exit(EXIT_FAILURE); } return; missing: std::cerr << "Erroneous argument (" << argv[i-1] << ")" << std::endl << " missing parameter" << std::endl; exit(EXIT_FAILURE); } bool Options::is_test_name_matching(const std::string& test_name) const { if (replay) return testpat.size() == 1 && test_name == testpat[0].second; if (!testpat.empty()) { bool positive_patterns = false; bool match_found = false; for (const auto& type_pattern: testpat) { const auto& type = type_pattern.first; const auto& pattern = type_pattern.second; if (type == MT_NOT) { // Negative pattern if (test_name.find(pattern) != std::string::npos) { // Test matches a negative pattern, should not run return false; } } else { // Positive pattern positive_patterns = true; if (!match_found) { // No match found yet, test with current pattern if (((type == MT_ANY) && (test_name.find(pattern) != std::string::npos)) || ((type == MT_FIRST) && (test_name.find(pattern) == 0))) match_found = true; } } } if (positive_patterns && match_found) { // Some positive pattern matched the test name return true; } else if (positive_patterns && !match_found) { // No positive pattern matched the test name return false; } else { // No positive patterns, but no negative pattern ruled the test name out return true; } } else { // With no test-patterns, all tests should run. return true; } } bool Options::is_test_tags_matching(TestTags tags) const { return testtags.empty() || tags.overlaps(testtags); } /// Run a single test, returning true iff the test succeeded bool run_test(Base* test, unsigned int test_seed, const Options& options, std::ostream& ostream) { unsigned int seed = options.replay ? options.seed : test_seed; try { ostream << test->name() << " "; ostream.flush(); test->_rand.seed(seed); for (unsigned int i = options.iter; i--;) { // Normalize evolved states so the reported seed restores this iteration. test->_rand.seed(test->_rand.seed()); seed = test->_rand.seed(); if (test->run()) { ostream << '+'; ostream.flush(); } else { ostream << "-" << std::endl; report_error(test->name(), seed, options, ostream); return false; } } ostream << std::endl; return true; } catch (Gecode::Exception& e) { ostream << "Exception in \"Gecode::" << e.what() << "." << std::endl << "Stopping..." << std::endl; report_error(test->name(), seed, options, ostream); return false; } } /// Report test cases; iterations of a test count as one case. void print_test_summary(size_t selected, size_t passed, size_t failed) { const size_t ran = passed + failed; std::cout << "\nTest summary: " << selected << " selected, " << ran << " ran, " << passed << " passed, " << failed << " failed, " << selected - ran << " not run." << std::endl; } /// Run all the tests with the supplied options. int run_tests(const std::vector& tests, const Options& options) { Gecode::Support::RandomGenerator seed_sequence(options.seed); size_t passed = 0; size_t failed = 0; for (auto test : tests) { unsigned int test_seed = seed_sequence.next(); if (run_test(test, test_seed, options, std::cout)) { ++passed; } else { ++failed; if (options.stop) break; } } print_test_summary(tests.size(), passed, failed); return failed == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } class TestExecutor; /** * Class that manages the state and control for running tests. */ class TestExecutionControl { /// All the tests to run const std::vector& tests; /// The options to use when running tests const Options& options; /// Mutex controlling output from the threads Gecode::Support::Mutex output_mutex; /// The next starting index among the tests. std::atomic next_tests; /// Completed test cases std::atomic passed; std::atomic failed; /// The number of test runners that are to be set up. std::atomic running_threads; /// Flag indicating some thread is waiting on the execution to be done. std::atomic execution_done_wait_started; /// Event for signalling that execution is done. Gecode::Support::Event execution_done_event; friend class TestExecutor; /// Choose a batch size based on the number of tests to divide static size_t choose_batch_size(size_t test_count, int thread_count) { const int batches_per_thread = 5; std::vector batch_sizes = {25, 10, 5, 2}; for (auto batch_size : batch_sizes) { if (test_count > batch_size * thread_count * batches_per_thread) { return batch_size; } } return 1; } public: TestExecutionControl(const std::vector& tests0, const Options& options0, int thread_count) : tests(tests0), options(options0), output_mutex(), next_tests(0), passed(0), failed(0), running_threads(thread_count), execution_done_wait_started(false), execution_done_event() {} /// Report the results after all threads have finished and return the exit status. int report_result() { assert(running_threads.load() == 0); print_test_summary(tests.size(), passed.load(), failed.load()); return failed.load() == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } /** Wait for test-runners to be completed. * * Important: may only be called once by a single thread! */ void await_test_runners_completed() { #ifndef NDEBUG bool some_waiting = #endif execution_done_wait_started.exchange(true); assert(some_waiting == false && "Only one thread is allowed to await the result"); execution_done_event.wait(); } private: /// Record a completed test case. void record_result(bool success) { if (success) ++passed; else ++failed; } /// Indicate that a thread is done executing. void thread_done() { if (running_threads.fetch_sub(1) == 1) { execution_done_event.signal(); } } /// Get the start of the next batch and the batch size. /// Important, may be a number larger than available number of tests std::pair next_batch() { const size_t current_start = next_tests.load(); const size_t batch_size = choose_batch_size(tests.size() - current_start, running_threads); const size_t next_start = next_tests.fetch_add(batch_size); return std::make_pair(next_start, batch_size); } /// True iff the runners should continue running tests bool continue_testing() { // Note: implementation should be cheap to call form multiple threads often return !options.stop || failed.load() == 0; } /// Write a string to standard output, synchronized across all test runners void write_output(std::string output) { Gecode::Support::Lock lock(output_mutex); std::cout << output; std::cout.flush(); } }; /** * Class that is responsible for running tests. */ class TestExecutor : public Gecode::Support::Runnable { /// The common controller for running tests TestExecutionControl& tec; /// The initial seed to start with const int initial_seed; public: TestExecutor(TestExecutionControl& tec, const int initialSeed) : tec(tec), initial_seed(initialSeed) {} void run(void) override { // Set up a local source of randomness seeds based on the initial seed supplied. Gecode::Support::RandomGenerator seed_sequence(initial_seed); // Loop runs tests continuously in batches from the test execution control while (true) { // Get next batch const std::pair& start_and_size = tec.next_batch(); const size_t batch_start = start_and_size.first; const size_t batch_size = start_and_size.second; if (batch_start >= tec.tests.size()) { // No more tests to run break; } // Run each test in the batch, handling output and failures for (size_t i = batch_start; i < batch_start + batch_size && i < tec.tests.size(); ++i) { if (!tec.continue_testing()) { break; } auto test = tec.tests[i]; unsigned int test_seed = seed_sequence.next(); std::ostringstream test_output; tec.record_result(run_test(test, test_seed, tec.options, test_output)); tec.write_output(test_output.str()); } if (!tec.continue_testing()) { break; } } // Signal that this thread is done. Last one signals the waiting main thread. tec.thread_done(); } }; /// Run all the tests with the supplied options in parallel. int run_tests_parallel(const std::vector& tests, const Options& options) { using namespace Gecode::Support; RandomGenerator seed_sequence(options.seed); TestExecutionControl tec(tests, options, options.threads); for (unsigned int i = 0; i < options.threads; ++i) { Thread::run(new TestExecutor(tec, seed_sequence.next())); } tec.await_test_runners_completed(); return tec.report_result(); } std::vector select_tests(const Options& options) { std::vector tests; bool started = options.start_from == nullptr; for (Base* t = Base::tests(); t != nullptr; t = t->next()) { if (!started) { if (t->name().find(options.start_from) != std::string::npos) { started = true; } else { continue; } } if (options.is_test_name_matching(t->name()) && options.is_test_tags_matching(t->tags())) { tests.emplace_back(t); } } return tests; } int run_registered_tests(int argc, char* argv[]) { opt = Options(); opt.parse(argc, argv); if (opt.list_tags) { print_tags(std::cout); return EXIT_SUCCESS; } Base::sort(); if (opt.list || opt.list_with_tags) { for (Base* t = Base::tests(); t != nullptr; t = t->next()) { std::cout << t->name(); if (opt.list_with_tags) std::cout << " [" << tags_to_string(t->tags()) << "]"; std::cout << std::endl; } return EXIT_SUCCESS; } const std::vector tests = select_tests(opt); if (opt.replay && tests.size() != 1) { std::cerr << "-replay requires exactly one registered test." << std::endl; return EXIT_FAILURE; } Gecode::Support::Timer timer; timer.start(); const int result = opt.threads > 1 ? run_tests_parallel(tests, opt) : run_tests(tests, opt); std::ostringstream elapsed; elapsed << std::fixed << std::setprecision(3) << timer.stop() / 1000.0; std::cout << "Elapsed time: " << elapsed.str() << " s." << std::endl; return result; } } std::ostream& operator<<(std::ostream& os, const Test::ind& i) { for (int j=i.l; j--; ) os << " "; return os; } // STATISTICS: test-core