From 2f564f6fdc21a4f851db2f85cf5416fee5c7d339 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Wed, 2 Dec 2015 20:02:11 +0100 Subject: First version --- cstest.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 cstest.c (limited to 'cstest.c') diff --git a/cstest.c b/cstest.c new file mode 100644 index 0000000..1b2a3ef --- /dev/null +++ b/cstest.c @@ -0,0 +1,67 @@ +/** + * CSTest - Minimalistic C unit test framework + * Copyright (C) 2015 Camil Staps + + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include +#include "cstest.h" + +// Colours for terminal output +#define KRED "\x1B[31m" +#define KGRN "\x1B[32m" +#define RESET "\x1B[0m" + +cstester* test_initialize(void) { + cstester* test = malloc(sizeof(cstester)); + if (test == NULL) return NULL; + test->passed = 0; + test->failed = 0; + test->show_pass = true; + return test; +} + +void test_wrapup(cstester* tester) { + if (tester->failed == 0) { + printf(KGRN "\nAll tests passed (%d)\n" RESET, tester->passed); + } else { + printf(KRED "\n%d test%s failed" RESET " (%d passed)\n", + tester->failed, tester->failed > 1 ? "s" : "", tester->passed); + } +} + +void test_destroy(cstester* tester) { + free(tester); +} + +void test_exit(cstester* tester) { + bool all_passed = tester->failed == 0; + test_destroy(tester); + exit(all_passed ? 0 : -1); +} + +void test_true(cstester* tester, bool check, const char* text) { + if (!check || tester->show_pass) { + printf(check ? KGRN : KRED); + printf(check ? "Test passed" : "Test failed"); + if (text != NULL) + printf(": %s", text); + printf("\n" RESET); + } + + *(check ? &tester->passed : &tester->failed) += 1; +} + -- cgit v1.2.3