Restructure project, start importing sc3 code.
This commit is contained in:
70
src/test/Assert.cc
Normal file
70
src/test/Assert.cc
Normal file
@@ -0,0 +1,70 @@
|
||||
///
|
||||
/// \file Test.cc
|
||||
/// \author K. Isom <kyle@imap.cc>
|
||||
/// \date 2023-10-09
|
||||
/// \brief Tooling to assist in building test programs..
|
||||
///
|
||||
/// Copyright 2023 K. Isom <kyle@imap.cc>
|
||||
///
|
||||
/// Permission to use, copy, modify, and/or distribute this software for
|
||||
/// any purpose with or without fee is hereby granted, provided that
|
||||
/// the above copyright notice and this permission notice appear in all /// copies.
|
||||
///
|
||||
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
/// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
/// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
|
||||
/// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
/// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
/// PERFORMANCE OF THIS SOFTWARE.
|
||||
///
|
||||
|
||||
#include <scsl/Exceptions.h>
|
||||
#include <sctest/Assert.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
namespace scsl {
|
||||
|
||||
void
|
||||
TestAssert(bool condition, std::string message)
|
||||
{
|
||||
#if defined(NDEBUG) || defined(SCSL_NOEXCEPT)
|
||||
if (!condition) {
|
||||
throw AssertionFailed(message);
|
||||
}
|
||||
#else
|
||||
if (!condition) {
|
||||
std::cerr << message << std::endl;
|
||||
}
|
||||
assert(condition);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
TestAssert(bool condition)
|
||||
{
|
||||
#if defined(NDEBUG)
|
||||
if (condition) {
|
||||
return;
|
||||
}
|
||||
#if defined(SCSL_NOEXCEPT)
|
||||
std::cerr << "Assertion failed!\n";
|
||||
#else
|
||||
std::stringstream msg;
|
||||
|
||||
msg << "assertion failed at " << __FILE__ << ":" << __LINE__;
|
||||
throw AssertionFailed(msg.str());
|
||||
#endif
|
||||
#else
|
||||
assert(condition);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
} // namespace scsl
|
||||
37
src/test/Report.cc
Normal file
37
src/test/Report.cc
Normal file
@@ -0,0 +1,37 @@
|
||||
///
|
||||
/// \file src/test/Report.cpp
|
||||
/// \author Kyle Isom
|
||||
/// \date 2017-06-07
|
||||
///
|
||||
/// \brief Defines a Report structure that contains information about
|
||||
/// the results of unit tests.
|
||||
///
|
||||
/// Copyright 2017 K. Isom <kyle@imap.cc>
|
||||
///
|
||||
/// Permission to use, copy, modify, and/or distribute this software for
|
||||
/// any purpose with or without fee is hereby granted, provided that
|
||||
/// the above copyright notice and this permission notice appear in all /// copies.
|
||||
///
|
||||
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
/// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
/// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
|
||||
/// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
/// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
/// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include <sctest/Report.h>
|
||||
|
||||
|
||||
namespace sctest {
|
||||
|
||||
|
||||
_Report::_Report()
|
||||
: Failing (0), Total(0), Start(std::chrono::steady_clock::now()),
|
||||
End(std::chrono::steady_clock::now()), Duration(0) {}
|
||||
|
||||
|
||||
} // end namespace test
|
||||
108
src/test/SimpleSuite.cc
Executable file
108
src/test/SimpleSuite.cc
Executable file
@@ -0,0 +1,108 @@
|
||||
///
|
||||
/// \file SimpleSuite.cc
|
||||
/// \author K. Isom <kyle@imap.cc>
|
||||
/// \date 2017-06-05
|
||||
/// \brief Defines a simple unit testing framework.
|
||||
///
|
||||
/// Copyright 2017 K. Isom <kyle@imap.cc>
|
||||
///
|
||||
/// Permission to use, copy, modify, and/or distribute this software for
|
||||
/// any purpose with or without fee is hereby granted, provided that
|
||||
/// the above copyright notice and this permission notice appear in all /// copies.
|
||||
///
|
||||
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
/// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
/// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
|
||||
/// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
/// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
/// PERFORMANCE OF THIS SOFTWARE.
|
||||
///
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
|
||||
#include <sctest/SimpleSuite.h>
|
||||
|
||||
|
||||
namespace sctest {
|
||||
|
||||
#define unless(cond) if (!(cond))
|
||||
|
||||
|
||||
static bool
|
||||
stub()
|
||||
{ return true; }
|
||||
|
||||
SimpleSuite::SimpleSuite()
|
||||
: quiet(false), fnSetup(stub), fnTeardown(stub), tests(),
|
||||
report(), hasRun(false)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SimpleSuite::AddTest(std::string name, std::function<bool()> test)
|
||||
{
|
||||
TestCase test_case = {name, test};
|
||||
tests.push_back(test_case);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SimpleSuite::AddFailingTest(std::string name, std::function<bool()> test)
|
||||
{
|
||||
// auto ntest = [&test]() { return !test(); };
|
||||
TestCase test_case = {name, [&test]() { return !test(); }};
|
||||
tests.push_back(test_case);
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
SimpleSuite::Run()
|
||||
{
|
||||
report.Start = std::chrono::steady_clock::now();
|
||||
unless(quiet) { std::cout << "Setting up the tests.\n"; }
|
||||
unless(fnSetup()) { return false; }
|
||||
|
||||
// Reset the failed test counts.
|
||||
report.Failing = 0;
|
||||
|
||||
bool result = true;
|
||||
hasRun = true;
|
||||
report.Total = tests.size();
|
||||
for (size_t i = 0; i < report.Total && result; i++) {
|
||||
TestCase tc = tests.at(i);
|
||||
unless(quiet) {
|
||||
std::cout << "[" << i + 1 << "/" << report.Total << "] Running test " << tc.name << ": ";
|
||||
}
|
||||
|
||||
result = tc.test();
|
||||
if (quiet) { continue; }
|
||||
|
||||
if (result) {
|
||||
std::cout << "[PASS]";
|
||||
} else {
|
||||
std::cout << "[FAIL]";
|
||||
report.Failing++;
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
unless(quiet) { std::cout << "Tearing down the tests.\n"; }
|
||||
unless(fnTeardown()) { return false; }
|
||||
report.End = std::chrono::steady_clock::now();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Report
|
||||
SimpleSuite::GetReport()
|
||||
{
|
||||
return report;
|
||||
}
|
||||
|
||||
|
||||
} // end namespace sctest
|
||||
Reference in New Issue
Block a user