91 lines
2.3 KiB
C++
91 lines
2.3 KiB
C++
/// Josh's Test Library
|
|
/// A no-frills, straightforward unit testing module in and for Modern C++.
|
|
/// Created by Joshua O'Leary @ Redacted Software, June 2024
|
|
/// Maintained by Maxine Hayes @ Redacted Software.
|
|
/// This work is dedicated to the public domain.
|
|
/// Contact: josh@redacted.cc, maxi@redacted.cc
|
|
|
|
/// @file jtest.hpp
|
|
/// @desc
|
|
/// @edit 2024-08-21
|
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
#include <functional>
|
|
|
|
namespace jtest
|
|
{
|
|
class check_fail {};
|
|
class check_eq_fail : public check_fail {};
|
|
class check_ne_fail : public check_fail {};
|
|
|
|
/// Raises an exception if the given condition is false.
|
|
void check(bool condition)
|
|
{
|
|
if (!condition)
|
|
throw check_fail();
|
|
}
|
|
|
|
/// Raises an exception if the given types do not evaluate as equal.
|
|
template <typename T>
|
|
void check_eq(T a, T b)
|
|
{
|
|
if (a != b)
|
|
throw check_eq_fail();
|
|
}
|
|
|
|
/// Raises an exception if the given types do evaluate as equal.
|
|
template <typename T>
|
|
void check_ne(T a, T b)
|
|
{
|
|
if (a == b)
|
|
throw check_ne_fail();
|
|
}
|
|
|
|
/// Raises an exception of the given float values are not equal (up to the given epsilon).
|
|
void check_float_eq(float a, float b, float epsilon = 1e-3f)
|
|
{
|
|
if ( ! (std::abs(a - b) <= epsilon))
|
|
throw check_eq_fail();
|
|
}
|
|
|
|
void check_float_ne(float a, float b, float epsilon = 1e-3f)
|
|
{
|
|
if ( (std::abs(a - b) <= epsilon))
|
|
throw check_ne_fail();
|
|
}
|
|
|
|
void check_float_eq_exact(float a, float b)
|
|
{
|
|
if (a != b)
|
|
throw check_eq_fail();
|
|
}
|
|
|
|
|
|
void check_string_eq(std::string a, std::string b)
|
|
{
|
|
if (a != b)
|
|
throw check_eq_fail();
|
|
}
|
|
|
|
void check_string_ne(std::string a, std::string b)
|
|
{
|
|
if (a == b)
|
|
throw check_ne_fail();
|
|
}
|
|
void check_throws(std::function<void()> callback);
|
|
void check_nothrows(std::function<void()> callback);
|
|
|
|
// TODO: Implement death check:
|
|
// Spawn a new process, and execute the callback in that process.
|
|
// This verifies that a piece of code would cause the process to terminate.
|
|
void check_death(std::function<void()> callback);
|
|
|
|
|
|
/// Raises an exception, which causes the test to fail.
|
|
void fail_test()
|
|
{
|
|
throw check_fail();
|
|
}
|
|
} |