Slow working on bringing the old code up to standard.
- Documentation updates - most of the old files use non-Doxygen or no/minimal header comments. - Rework SimpleSuite to be more useful. - Coverity-surfaced fixes.
This commit is contained in:
@@ -35,16 +35,11 @@ namespace sctest {
|
||||
#define SCTEST_CHECK_FALSE(x) if ((x)) { return false; }
|
||||
#define SCTEST_CHECK_EQ(x, y) if ((x) != (y)) { return false; }
|
||||
#define SCTEST_CHECK_NE(x, y) if ((x) == (y)) { return false; }
|
||||
#define SCTEST_CHECK_ZERO(x) if ((x) != 0) { return false; }
|
||||
#define SCTEST_CHECK_GTZ(x) if ((x) > 0) { return false; }
|
||||
#define SCTEST_CHECK_GEZ(x) if ((x) >= 0) { return false; }
|
||||
#define SCTEST_CHECK_LEZ(x) if ((x) <= 0) { return false; }
|
||||
#define SCTEST_CHECK_LTZ(x) if ((x) < 0) { return false; }
|
||||
#define SCTEST_CHECK_FEQ(x, y) { float eps; scmp::DefaultEpsilon(eps); if (!scmp::WithinTolerance((x), (y), eps)) { return false; }}
|
||||
#define SCTEST_CHECK_DEQ(x, y) { double eps; scmp::DefaultEpsilon(eps); if (!scmp::WithinTolerance((x), (y), eps)) { return false; }}
|
||||
|
||||
#define SCTEST_CHECK_FEQ_EPS(x, y, eps) { if (!scmp::WithinTolerance((x), (y), eps)) { return false; }}
|
||||
#define SCTEST_CHECK_DEQ_EPS(x, y, eps) { if (!scmp::WithinTolerance((x), (y), eps)) { return false; }}
|
||||
#define SCTEST_CHECK_FEQ_EPS(x, y, eps) { if (!scmp::WithinTolerance<float>((x), (y), eps)) { return false; }}
|
||||
#define SCTEST_CHECK_DEQ_EPS(x, y, eps) { if (!scmp::WithinTolerance<double>((x), (y), eps)) { return false; }}
|
||||
|
||||
|
||||
} // namespace sctest
|
||||
|
||||
@@ -28,21 +28,42 @@
|
||||
|
||||
namespace sctest {
|
||||
|
||||
typedef struct _Report {
|
||||
// Failing stores the number of failing tests; for tests added
|
||||
// with AddTest, this is a test that returned false. For tests
|
||||
// added with AddFailingTest, this is a test that returned true.
|
||||
size_t Failing;
|
||||
|
||||
// Total is the number of tests registered during the last run.
|
||||
size_t Total;
|
||||
class Report {
|
||||
public:
|
||||
/// \brief Failing returns the count of failed tests.
|
||||
///
|
||||
/// \details If a test is run and expected to pass, but fails,
|
||||
/// it is marked as failed. If a test is expected to
|
||||
/// fail, but passes, it is marked as failed.
|
||||
///
|
||||
/// \return The number of tests that failed.
|
||||
size_t Failing() const;
|
||||
|
||||
std::chrono::time_point<std::chrono::steady_clock> Start;
|
||||
std::chrono::time_point<std::chrono::steady_clock> End;
|
||||
std::chrono::duration<double> Duration;
|
||||
/// \brief Total is the number of tests registered.
|
||||
size_t Total() const;
|
||||
|
||||
void Failed();
|
||||
void AddTest(size_t testCount = 0);
|
||||
void Reset(size_t testCount = 0);
|
||||
|
||||
void Start();
|
||||
void End();
|
||||
std::chrono::duration<double, std::milli>
|
||||
Elapsed() const;
|
||||
|
||||
Report();
|
||||
private:
|
||||
size_t failing{};
|
||||
size_t total{};
|
||||
|
||||
std::chrono::time_point<std::chrono::steady_clock> start;
|
||||
std::chrono::time_point<std::chrono::steady_clock> end;
|
||||
};
|
||||
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const Report &report);
|
||||
} // end namespace sctest
|
||||
|
||||
_Report();
|
||||
} Report;
|
||||
|
||||
} // end namespace test
|
||||
#endif
|
||||
|
||||
@@ -1,30 +1,28 @@
|
||||
//
|
||||
// Project: scccl
|
||||
// File: include/test/SimpleSuite.h
|
||||
// Author: Kyle Isom
|
||||
// Date: 2017-06-05
|
||||
// Namespace: test
|
||||
//
|
||||
// SimpleSuite.h defines the SimpleSuite class for unit testing.
|
||||
//
|
||||
// Copyright 2017 Kyle Isom <kyle@imap.cc>
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
///
|
||||
/// \file SimpleSuite.h
|
||||
/// \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.
|
||||
///
|
||||
|
||||
#ifndef SCTEST_SIMPLESUITE_H
|
||||
#define SCTEST_SIMPLESUITE_H
|
||||
|
||||
// SimpleSuite.h
|
||||
// This header file defines the interface for a simple suite of tests.
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
@@ -34,54 +32,91 @@
|
||||
|
||||
namespace sctest {
|
||||
|
||||
typedef struct {
|
||||
std::string name;
|
||||
std::function<bool(void)> test;
|
||||
} TestCase;
|
||||
|
||||
/// \brief UnitTest describes a single unit test. It is a predicate:
|
||||
/// did the test pass?
|
||||
struct UnitTest {
|
||||
/// What name should be shown when running tests?
|
||||
std::string name;
|
||||
|
||||
/// This is the test function to be run.
|
||||
std::function<bool()> test;
|
||||
};
|
||||
|
||||
/// \brief SimpleSuite is a test-running harness for simple tests.
|
||||
///
|
||||
/// A simple test is defined as a test that takes no arguments and
|
||||
/// returns a boolean status where true indicates the test has passed.
|
||||
class SimpleSuite {
|
||||
public:
|
||||
public:
|
||||
SimpleSuite();
|
||||
|
||||
// Silence suppresses output.
|
||||
void Silence(void) { quiet = true; }
|
||||
/// \brief Silence suppresses output.
|
||||
void Silence();
|
||||
|
||||
// Setup defines a setup function; this should be a predicate. This function
|
||||
// is called at the start of the Run method, before tests are run.
|
||||
/// \brief Define a suite setup function.
|
||||
///
|
||||
/// If present, this setup function is called at the start of
|
||||
/// the Run method, before tests are run. It should be a
|
||||
/// predicate: if it returns false, tests automatically fail.
|
||||
void Setup(std::function<bool(void)> setupFn) { fnSetup = setupFn; }
|
||||
|
||||
// Teardown defines a teardown function; this should be a predicate. This
|
||||
// function is called at the end of the Run method, after all tests have run.
|
||||
/// \brief Define a teardown function.
|
||||
///
|
||||
/// If present, this teardown function is called at the end of
|
||||
/// the Run method, after all tests have run.
|
||||
void Teardown(std::function<bool(void)> teardownFn) { fnTeardown = teardownFn; }
|
||||
|
||||
// AddTest is used to add a test that is expected to return true.
|
||||
void AddTest(std::string, std::function<bool(void)>);
|
||||
/// \brief Register a new simple test.
|
||||
///
|
||||
/// \param label The text that will identify test when
|
||||
/// running.
|
||||
/// \param test This test should return true if the test has
|
||||
/// passed.
|
||||
void AddTest(std::string label, std::function<bool(void)> test);
|
||||
|
||||
// AddFailingTest is used to add a test that is expected to return false.
|
||||
void AddFailingTest(std::string, std::function<bool(void)>);
|
||||
/// \brief Register a test that is expected to return false.
|
||||
///
|
||||
/// \param label The text that will identify test when
|
||||
/// running.
|
||||
/// \param test This test should return false if the test has
|
||||
/// passed.
|
||||
void AddFailingTest(std::string label, std::function<bool(void)> test);
|
||||
|
||||
bool Run(void);
|
||||
/// \brief Run all the registered tests.
|
||||
///
|
||||
/// \return True if all tests have passed.
|
||||
bool Run();
|
||||
|
||||
// Reporting methods.
|
||||
/// Reporting methods.
|
||||
|
||||
// Reset clears the report statistics.
|
||||
void Reset(void) { report.Failing = report.Total = 0; hasRun = false; };
|
||||
/// \brief Reset clears the report statistics.
|
||||
///
|
||||
/// Reset will preserve the setup and teardown functions, just
|
||||
/// resetting the suite's internal state.
|
||||
void Reset();
|
||||
|
||||
// IsReportReady returns true if a report is ready.
|
||||
bool IsReportReady(void) { return hasRun; }
|
||||
/// \brief
|
||||
// HasRun returns true if a report is ready.
|
||||
bool HasRun() const;
|
||||
|
||||
// Report returns a Report.
|
||||
Report GetReport(void);
|
||||
|
||||
private:
|
||||
|
||||
private:
|
||||
bool quiet;
|
||||
std::function<bool(void)> fnSetup, fnTeardown;
|
||||
std::vector<TestCase> tests;
|
||||
std::function<bool(void)> fnSetup, fnTeardown;
|
||||
std::vector<UnitTest> tests;
|
||||
|
||||
// Report functions.
|
||||
Report report;
|
||||
bool hasRun; // Have the tests been run yet?
|
||||
Report report;
|
||||
bool hasRun; // Have the tests been run yet?
|
||||
bool hasPassed;
|
||||
};
|
||||
|
||||
} // end namespace test
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, SimpleSuite &suite);
|
||||
|
||||
|
||||
} // namespace sctest
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user