diff --git a/Makefile b/Makefile index 0fe7f5c..17dc562 100644 --- a/Makefile +++ b/Makefile @@ -52,3 +52,5 @@ $(PREPROCESSED)/%.hpp: $(INCLUDE)/%.hpp $(PREPROCESSED)/%.cpp: $(SRC)/%.cpp $(CXX) -DPP_DEBUG $(CXXFLAGS) -E $< > $@ clang-format -i $@ + +include test/Makefile diff --git a/test/.ccls b/test/.ccls new file mode 100644 index 0000000..bce04dd --- /dev/null +++ b/test/.ccls @@ -0,0 +1,8 @@ +g++ +%cpp -std=c++20 +%hpp +-I../include +-I. +-Wall +-Wextra +-Wpedantic diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..4e44143 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,33 @@ +CXX = g++ +CXXFLAGS = -std=c++20 -O3 -Wall -Wextra -Wpedantic +LDFLAGS = + +INCLUDE = include test +CXXFLAGS += $(addprefix -I, $(INCLUDE)) + +TEST = test +BIN_TEST = $(BIN)/$(TEST) + +_TESTS = example.cpp +TESTS = $(addprefix $(TEST)/, $(_TESTS)) + +OBJ_TEST = $(addprefix $(BIN_TEST)/, $(_TESTS:.cpp=)) + +.PHONY: test + +dirs_test: + mkdir -p $(BIN_TEST) + +test_start: + @echo + @echo "---------- STARTING TESTS ----------" + @echo + +test: dirs_test test_start $(OBJ_TEST) + $(foreach _test,$(OBJ_TEST),@./$(_test)) + @echo + @echo "---------- FINISHED TESTS ----------" + @echo + +$(BIN_TEST)/%: $(TEST)/%.cpp + @$(CXX) $< $(CXXFLAGS) -o $@ diff --git a/test/example.cpp b/test/example.cpp new file mode 100644 index 0000000..bc5c678 --- /dev/null +++ b/test/example.cpp @@ -0,0 +1,8 @@ +#include + +TEST_START() + TEST(tests work, + assert(1 == 1, "c++ broke") + assert(1 != 0, "c++ broke #2") + ) +TEST_FINISH() diff --git a/test/test.hpp b/test/test.hpp new file mode 100644 index 0000000..e37f18d --- /dev/null +++ b/test/test.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include +#include + +#define FG(r, g, b) "\033[38;2;" #r ";" #g ";" #b "m" +#define BG(r, g, b) "\033[48;2;" #r ";" #g ";" #b "m" +#define RESET "\033[0m" + +#define TEST_START() \ + int main() {\ + uint tests_failed = 0;\ + uint tests_total = 0;\ + std::cout << "STARTING TEST FILE " << __FILE__ << "\n"; + +#define TEST_FINISH() \ + std::cout << "FINISHED TEST FILE " << __FILE__ << "\n";\ + std::cout << "PASSED TESTS: "\ + << (tests_failed != 0 ? BG(255, 0, 0) : BG(0, 255, 0))\ + << FG(0, 0, 0)\ + << (tests_total - tests_failed) << "/" << tests_total\ + << RESET\ + << "\n";\ + return 0;\ + } + +#define assert(condition, message) \ + asserts_total += 1;\ + if (!(condition)) {\ + asserts_failed += 1;\ + std::cout << " " << BG(255, 0, 0) << FG(0, 0, 0) << #condition << ": " << message << " (" << __FILE__ << ":" << __LINE__ << ")" << RESET << "\n";\ + } + +#define TEST(name, ...) \ + uint asserts_failed = 0;\ + uint asserts_total = 0;\ + tests_total += 1;\ + std::cout << " RUNNING TEST `" << #name << "`\n";\ + __VA_ARGS__\ + std::cout << " PASSED ASSERTS: "\ + << (asserts_failed != 0 ? BG(255, 0, 0) : BG(0, 255, 0))\ + << FG(0, 0, 0)\ + << (asserts_total - asserts_failed) << "/" << asserts_total\ + << RESET\ + << "\n";\ + if (asserts_failed > 0) {\ + tests_failed += 1;\ + }