add initial test work

This commit is contained in:
jacekpoz 2024-04-28 17:43:53 +02:00
parent edeb39adc2
commit 119a024045
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
5 changed files with 99 additions and 0 deletions

View file

@ -52,3 +52,5 @@ $(PREPROCESSED)/%.hpp: $(INCLUDE)/%.hpp
$(PREPROCESSED)/%.cpp: $(SRC)/%.cpp
$(CXX) -DPP_DEBUG $(CXXFLAGS) -E $< > $@
clang-format -i $@
include test/Makefile

8
test/.ccls Normal file
View file

@ -0,0 +1,8 @@
g++
%cpp -std=c++20
%hpp
-I../include
-I.
-Wall
-Wextra
-Wpedantic

33
test/Makefile Normal file
View file

@ -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 $@

8
test/example.cpp Normal file
View file

@ -0,0 +1,8 @@
#include <test.hpp>
TEST_START()
TEST(tests work,
assert(1 == 1, "c++ broke")
assert(1 != 0, "c++ broke #2")
)
TEST_FINISH()

48
test/test.hpp Normal file
View file

@ -0,0 +1,48 @@
#pragma once
#include <iostream>
#include <string>
#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;\
}