diff options
author | Camil Staps | 2015-12-02 22:23:32 +0100 |
---|---|---|
committer | Camil Staps | 2015-12-02 22:23:32 +0100 |
commit | 2eec4c98cc47835ff410676a00c7e1796ff2da54 (patch) | |
tree | 2eab519f2fcdd27b2ede172534cf6161624f15ab /minctest.c | |
parent | First version (diff) |
Rename CSTest to MinCTest; is more to-the-point
Diffstat (limited to 'minctest.c')
-rw-r--r-- | minctest.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/minctest.c b/minctest.c new file mode 100644 index 0000000..61a3688 --- /dev/null +++ b/minctest.c @@ -0,0 +1,67 @@ +/** + * MinCTest - 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 "minctest.h" + +// Colours for terminal output +#define KRED "\x1B[31m" +#define KGRN "\x1B[32m" +#define RESET "\x1B[0m" + +tester* test_initialize(void) { + tester* test = malloc(sizeof(tester)); + if (test == NULL) return NULL; + test->passed = 0; + test->failed = 0; + test->show_pass = true; + return test; +} + +void test_wrapup(tester* 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(tester* tester) { + free(tester); +} + +void test_exit(tester* tester) { + bool all_passed = tester->failed == 0; + test_destroy(tester); + exit(all_passed ? 0 : -1); +} + +void test_true(tester* 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; +} + |