Restructure project, start importing sc3 code.

This commit is contained in:
2023-10-18 23:44:05 -07:00
parent 3122ed6ac7
commit 5f3dc6e9f6
46 changed files with 2300 additions and 66 deletions

60
include/sctest/Assert.h Normal file
View File

@@ -0,0 +1,60 @@
///
/// \file Test.h
/// \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.
///
#ifndef SCSL_TEST_H
#define SCSL_TEST_H
#include <string>
namespace scsl {
/// TestAssert is a variant on the assert macro. This variant is intended to be
/// a drop-in replacement for the cassert macro: even in release mode, the tests
/// should still run.
///
/// If NDEBUG is set, TestAssert will throw an exception if condition is false.
/// Otherwise, it calls assert after printing the message.
///
/// \param condition If true, TestAssert throws an exception.
void TestAssert(bool condition);
/// TestAssert is a variant on the assert macro.
///
/// If NDEBUG is set, TestAssert will throw an exception if condition is false.
/// Otherwise, it calls assert after printing the message.
///
/// In addition to NDEBUG, SCSL_NOEXCEPT will suppress assertions.
///
/// \throws AssertionFailed
///
/// \param condition The condition to assert.
/// \param message The message that should be displayed if condition is false.
void TestAssert(bool condition, std::string message);
} // namespace scsl
#endif //SCSL_TEST_H

48
include/sctest/Report.h Executable file
View File

@@ -0,0 +1,48 @@
//
// Project: scccl
// File: include/test/Report.h
// Author: Kyle Isom
// Date: 2017-06-05
// Namespace: test
//
// Report.h defines a Report structure that contains information about
// the results of unit tests.
//
// 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.
#ifndef __SCTEST_REPORT_H
#define __SCTEST_REPORT_H
#include <chrono>
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;
std::chrono::time_point<std::chrono::steady_clock> Start;
std::chrono::time_point<std::chrono::steady_clock> End;
std::chrono::duration<double> Duration;
_Report();
} Report;
} // end namespace test
#endif

87
include/sctest/SimpleSuite.h Executable file
View File

@@ -0,0 +1,87 @@
//
// 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.
#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>
#include <vector>
#include <sctest/Report.h>
namespace sctest {
typedef struct {
std::string name;
std::function<bool(void)> test;
} TestCase;
class SimpleSuite {
public:
SimpleSuite();
// Silence suppresses output.
void Silence(void) { quiet = true; }
// 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.
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.
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)>);
// AddFailingTest is used to add a test that is expected to return false.
void AddFailingTest(std::string, std::function<bool(void)>);
bool Run(void);
// Reporting methods.
// Reset clears the report statistics.
void Reset(void) { report.Failing = report.Total = 0; hasRun = false; };
// IsReportReady returns true if a report is ready.
bool IsReportReady(void) { return hasRun; }
// Report returns a Report.
Report GetReport(void);
private:
bool quiet;
std::function<bool(void)> fnSetup, fnTeardown;
std::vector<TestCase> tests;
// Report functions.
Report report;
bool hasRun; // Have the tests been run yet?
};
} // end namespace test
#endif

50
include/sctest/checks.h Executable file
View File

@@ -0,0 +1,50 @@
//
// Project: scccl
// File: include/test/checks.h
// Author: Kyle Isom
// Date: 2017-06-05
// Namespace: test.
//
// checks.h defines a number of macros (which are global in scope) for
// use in test functions that return bools.
//
// 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.
#ifndef __SCTEST_CHECKS_H
#define __SCTEST_CHECKS_H
#include <scccl/math/math.h>
namespace sctest {
// The following checks are designed as shortcuts that just return false on certain
// conditions.
#define SCTEST_CHECK(x) if (!(x)) { return false; }
#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; scmath::DefaultEpsilon(eps); if (!scmath::WithinTolerance((x), (y), eps)) { return false; }}
#define SCTEST_CHECK_DEQ(x, y) { double eps; scmath::DefaultEpsilon(eps); if (!scmath::WithinTolerance((x), (y), eps)) { return false; }}
} // namespace test
#endif

57
include/sctest/debug.h Executable file
View File

@@ -0,0 +1,57 @@
//
// Project: scccl
// File: include/test/debug.h
// Author: Kyle Isom
// Date: 2017-06-05
// Namespace: test
//
// debug.h defines assertions and other debugging functions.
//
// 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.
#if 0
// Disabled for now.
#pragma once
#include <iostream>
#ifndef NDEBUG
#include <cstdlib>
#endif
namespace test {
// GenerateCoreDumps should be set at the beginning of the program, before
// multithreading. It is *not* threadsafe.
static bool GenerateCoreDumps = false;
static void
Assert(bool cond) {
#ifdef NDEBUG
std::cout << "Not a debug build, skipping assertion." << std::endl;
return;
#endif
if (!cond) {
std::cerr << "Assertion failed in " << __func__ << "(" << __FILE__ << ":" << __LINE__ << ")" << std::endl;
if (GenerateCoreDumps) {
std::abort();
}
else {
std::exit(1);
}
}
}
}
#endif