From f87f7b4f0aaccc65d03ccee5bb11915ead6fb0e1 Mon Sep 17 00:00:00 2001 From: Raymaekers Luca Date: Sun, 27 Apr 2025 12:52:06 +0200 Subject: First pass at preparing for Github --- tests/a.out | Bin 0 -> 78728 bytes tests/build.sh | 12 ++++ tests/test.h | 217 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/tests | Bin 0 -> 107832 bytes tests/tests.c | 34 +++++++++ 5 files changed, 263 insertions(+) create mode 100755 tests/a.out create mode 100755 tests/build.sh create mode 100644 tests/test.h create mode 100755 tests/tests create mode 100644 tests/tests.c (limited to 'tests') diff --git a/tests/a.out b/tests/a.out new file mode 100755 index 0000000..4e8f3c3 Binary files /dev/null and b/tests/a.out differ diff --git a/tests/build.sh b/tests/build.sh new file mode 100755 index 0000000..2bab03e --- /dev/null +++ b/tests/build.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +ScriptDir="$(dirname "$(readlink -f "$0")")" +cd "$ScriptDir" +WarningFlags="-Wno-unused-variable" + +printf 'tests.c\n' +gcc -ggdb -Wall $WarningFlags -o tests tests.c +./tests + +# printf 'archived/input_box.c\n' +# gcc -DDEBUG -ggdb -Wall -pedantic -std=c11 -I external -I . -o "$BuildDir"/input_box archived/input_box.c diff --git a/tests/test.h b/tests/test.h new file mode 100644 index 0000000..23fb5cd --- /dev/null +++ b/tests/test.h @@ -0,0 +1,217 @@ +// Library used for writing tests, must be included once for definitions and +// once for the `main()` function. +// +// DOCUMENTATION +// Expect(expr) +// Macro that will return early if the expression is false, calling +// `tb_shutdown()` to save the terminal. It will also print the expression +// and line on which it occurred for convenience +// +// test_functions TestFunctions +// Global variable containing the function (test). This array is +// 0-terminated. +// +// TESTFUNC() +// Macro for adding functions to TestFunctions array conveniently. +// +// tb_get_cell(x, y) +// Function +// +// EXAMPLE +// #define TEST_IMPL +// #include "test.h" +// +// bool FooTest() +// { +// Expect(0 == 1); +// return true; +// } +// +// test_functions TestFunctions[] = { +// TESTFUNC(FooTest), +// {0} +// } +// +// int main(int Argc, int Argv) +// { +// Test(Argc, Argv); +// } + +#ifndef TEST_H +#define TEST_H + +#include +#include + +#include "../source/chatty.h" + +/* Macro's */ +#define Expect(expr) if (!(expr)) \ + { \ + tb_shutdown(); \ + printf("\n %d: %s\n", __LINE__, #expr); \ + return false; \ + } + +#define TESTFUNC(func) { func, #func } + +/* Types */ +typedef struct { + bool (*Func)(void); + const char * Name; +} test_functions; + +typedef struct { + s32 X, Y, W, H; +} rect; + +/* Declarations */ +struct tb_cell tb_get_cell(s32 x, s32 y); +void RectCopy(rect Dest, rect Source); +bool RectCmp(rect Dest, rect Source); +bool TextCmp(s32 X, s32 Y, wchar_t *Text, s32 TextLen); +bool TextCmpWithColor(s32 X, s32 Y, wchar_t *Text, s32 TextLen, s32 fg, s32 bg); + +/* Global variables */ +void (*Before)(void); +void (*After)(void); + +/* Functions*/ +struct tb_cell +tb_get_cell(s32 x, s32 y) +{ + return global.back.cells[global.width * y + x]; +} + +void +RectCopy(rect Dest, rect Source) +{ + for (u32 Y = 0; Y < Source.H; Y++) + { + for (u32 X = 0; X < Source.W; X++) + { + struct tb_cell Cell = tb_get_cell(Source.X + X, Source.Y + Y); + tb_set_cell(Dest.X + X, Dest.Y + Y, Cell.ch, Cell.fg, Cell.bg); + } + } +} + +bool +RectCmp(rect Dest, rect Source) +{ + for (u32 Y = 0; Y < Source.H; Y++) + { + for (u32 X = 0; X < Source.W; X++) + { + struct tb_cell SourceCell = tb_get_cell(Source.X + X, Source.Y + Y); + struct tb_cell DestCell = tb_get_cell(Dest.X + X, Dest.Y + Y); + if (!(SourceCell.fg == DestCell.fg && + SourceCell.bg == DestCell.bg && + SourceCell.ch == DestCell.ch)) + { + return false; + } + } + } + + return true; +} + +bool +TextCmp(s32 X, s32 Y, wchar_t *Text, s32 TextLen) +{ + for (u32 At = 0; + At < TextLen; + At++) + { + struct tb_cell DestCell = tb_get_cell(X++, Y); + if (DestCell.ch != Text[At]) return false; + } + + return true; +} + +bool +TextCmpWithColor(s32 X, s32 Y, wchar_t *Text, s32 TextLen, s32 fg, s32 bg) +{ + for (u32 At = 0; + At < TextLen; + At++) + { + struct tb_cell DestCell = tb_get_cell(X++, Y); + if (DestCell.ch != Text[At] || + DestCell.fg != fg || + DestCell.bg != bg) + { + return false; + } + } + + return true; +} + +int +Test(test_functions *TestFunctions, int Argc, char *Argv[]) +{ + u32 TestFunctionsLen = 0; + while (TestFunctions[TestFunctionsLen].Func) TestFunctionsLen++; + + if (Argc > 1) + { + for (u32 Arg = 0; + Arg < Argc; + Arg++) + { + char *Function = Argv[Arg]; + + for (u32 TestFunc = 0; + TestFunc < TestFunctionsLen; + TestFunc++) + { + if(!strcmp(Function, TestFunctions[TestFunc].Name)) + { + printf("%s ", TestFunctions[TestFunc].Name); + + if (Before) Before(); + bool Ret = TestFunctions[TestFunc].Func(); + if (After) After(); + + if (Ret) + { + printf("\033[32mPASSED\033[0m\n"); + } + else + { + printf("\033[31mFAILED\033[0m\n"); \ + } + } + } + } + } + else + { + for (int At = 0; + TestFunctions[At].Func; + At++) + { + printf("%s ", TestFunctions[At].Name); + + if (Before) Before(); + bool Ret = TestFunctions[At].Func(); + if (After) After(); + + if (Ret) + { + printf("\033[32mPASSED\033[0m\n"); + } + else + { + printf("\033[31mFAILED\033[0m\n"); \ + } + } + } + + return 0; +} + +#endif // TEST_H diff --git a/tests/tests b/tests/tests new file mode 100755 index 0000000..1ee83c7 Binary files /dev/null and b/tests/tests differ diff --git a/tests/tests.c b/tests/tests.c new file mode 100644 index 0000000..93ba4cc --- /dev/null +++ b/tests/tests.c @@ -0,0 +1,34 @@ +#define MAX_INPUT_LEN 255 + +#define TB_IMPL +#include "../source/termbox2.h" +#include "../source/chatty.h" + +#define TEST_IMPL +#include "test.h" + +#define Assert(expr) if (!(expr)) *(u8*)0 = 0 + +bool +DrawingTest(void) +{ + struct tb_event ev = {0}; + + return true; +} + +int +main(int Argc, char* Argv[]) +{ + test_functions TestFunctions[] = { + TESTFUNC(DrawingTest), + { 0 } + }; + + Assert(setlocale(LC_ALL, "")); + + Before = (void(*)(void))tb_init; + After = (void(*)(void))tb_shutdown; + + return(Test(TestFunctions, Argc, Argv)); +} -- cgit v1.2.3