diff options
author | Camil Staps | 2015-12-02 20:02:11 +0100 |
---|---|---|
committer | Camil Staps | 2015-12-02 20:04:46 +0100 |
commit | 2f564f6fdc21a4f851db2f85cf5416fee5c7d339 (patch) | |
tree | 42a6aef3e95d91ce1c2eeb2a4b5ffff0a5d6288a /cstest.c | |
parent | Initial commit (diff) |
First version
Diffstat (limited to 'cstest.c')
-rw-r--r-- | cstest.c | 67 |
1 files changed, 67 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>. + */ +#include <stdio.h> +#include <malloc.h> +#include <stdlib.h> +#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; +} + |