Implement check variants v1

This commit is contained in:
2024-06-26 12:13:19 -04:00
parent 75eb8f52d9
commit 20ed600b89
2 changed files with 37 additions and 17 deletions

View File

@@ -6,10 +6,8 @@
#include <vector>
#include <jlog/jlog.hpp>
namespace jtest {
// Requirements
//
namespace jtest {
// Can't we just store a struct in a global vector per test?
// -maxine
@@ -79,12 +77,34 @@ namespace jtest {
// TODO: Implement check variants
// TODO: implement streaming a custom failure message with << operator
// i.e. : check(my_cond) << "The condition is not true!"
bool check(bool condition) {
if (!condition)
throw std::runtime_error("Test check failed!!");
return condition;
}
template <typename T>
bool check_eq(T a, T b) {
if (a != b)
throw std::runtime_error("Test check failed!!");
return true;
}
bool check_float_eq(float a, float b, float epsilon = 1e-3f) {
if (std::abs(a - b) > epsilon) {
throw std::runtime_error ("Test check failed!!");
}
return true;
}
bool check_double_eq(double a, double b, double epsilon = 1e-3f) {
if (std::abs(a - b) > epsilon) {
throw std::runtime_error ("Test check failed!!");
}
return true;
}
bool test(const std::string& testname, const std::function<void()>& callback, const std::string& file, int line)
{
bool passed = true;

View File

@@ -1,20 +1,23 @@
//
// Created by dawsh on 6/16/24.
//
#include <cassert>
#include "include/jtest/jtest.hpp"
// 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
// This work is dedicated to the public domain.
// Contact: josh@redacted.cc, git.redacted.cc/josh
// TODO: Provide introspection insofar as which assertion check failed.
// TODO: Provide alternate checks (google test has specific assertations for handling floats, for example) (Are these actually necessary??)
// TODO: Implement log-file-specification-capability in jlog so we can log to test_results.txt specifically.
// TODO: Provide benchmarking on test running-time
#include <jtest/jtest.hpp>
void TestA() { jtest::check("Bruh" == "Bruh"); }
void TestB() { jtest::check(6*6 == 36); }
void TestC() { jtest::check(6+9 == 69); }
int main(int argc, char** argv)
{
TEST("Test1", []{
jtest::check(2+2 == 4);
});
@@ -37,12 +40,9 @@ int main(int argc, char** argv)
});
*/
/*
TEST("LMAO");
TEST("KEKERINO")
TEST(":)")
*/
TEST("TestGroup::A", TestA);
TEST("TestGroup::B", TestB);
TEST("TestGroup::C", TestC);
// Doesn't actually do anything yet
jtest::run_tests();
}