diff options
Diffstat (limited to 'minctest.c')
-rw-r--r-- | minctest.c | 94 |
1 files changed, 49 insertions, 45 deletions
@@ -16,8 +16,8 @@ * 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 @@ -25,62 +25,66 @@ #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; - gettimeofday(&test->created, NULL); - test_start_timer(test); - return test; +struct tester *test_initialize() { + struct tester *test = malloc(sizeof(struct tester)); + if (test == NULL) + return NULL; + test->passed = 0; + test->failed = 0; + test->show_pass = true; + gettimeofday(&test->created, NULL); + test_start_timer(test); + return test; } -void test_wrapup(tester* tester) { - struct timeval end; - gettimeofday(&end, NULL); - float time = ((float) end.tv_usec - tester->created.tv_usec) / 1000; - float used = ((float) tester->used) / 1000; - - if (tester->failed == 0) { - printf(KGRN "\nAll tests passed (%d) [%.3fs / %.3fs]\n" RESET, tester->passed, used, time); - } else { - printf(KRED "\n%d test%s failed" RESET " (%d passed) [%.3fs / %.3fs]\n", - tester->failed, tester->failed > 1 ? "s" : "", tester->passed, used, time); - } +void test_wrapup(struct tester* tester) { + struct timeval end; + gettimeofday(&end, NULL); + float time = (end.tv_usec - tester->created.tv_usec) / 1000.0; + float used = (tester->used) / 1000.0; + + if (tester->failed == 0) { + printf(KGRN "\nAll tests passed (%d) [%.3fs / %.3fs]\n" RESET, + tester->passed, used, time); + } else { + printf(KRED "\n%d test%s failed" RESET + " (%d passed) [%.3fs / %.3fs]\n", + tester->failed, tester->failed > 1 ? "s" : "", + tester->passed, used, time); + } } -void test_destroy(tester* tester) { - free(tester); +void test_destroy(struct 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_exit(struct tester *tester) { + bool all_passed = tester->failed == 0; + test_destroy(tester); + exit(all_passed ? 0 : -1); } -void test_start_timer(tester* tester) { - gettimeofday(&tester->start_last_test, NULL); +void test_start_timer(struct tester *tester) { + gettimeofday(&tester->start_last_test, NULL); } -void test_true(tester* tester, bool check, const char* text) { - struct timeval end; - gettimeofday(&end, NULL); - tester->used += end.tv_usec - tester->start_last_test.tv_usec; - float time = ((float) end.tv_usec - tester->start_last_test.tv_usec) / 1000; +void test_true(struct tester *tester, bool check, const char* text) { + struct timeval end; + gettimeofday(&end, NULL); + tester->used += end.tv_usec - tester->start_last_test.tv_usec; + float time = (end.tv_usec - tester->start_last_test.tv_usec) / 1000.0; - if (!check || tester->show_pass) { - printf(check ? KGRN : KRED); - printf(check ? "Test passed" : "Test failed"); - if (text != NULL) - printf(": %s", text); - printf(" [%.3fs]", time); - printf(RESET "\n"); - } + if (!check || tester->show_pass) { + puts(check ? KGRN : KRED); + puts(check ? "Test passed" : "Test failed"); + if (text != NULL) + printf(": %s", text); + printf(" [%.3fs]", time); + puts(RESET "\n"); + } - *(check ? &tester->passed : &tester->failed) += 1; + *(check ? &tester->passed : &tester->failed) += 1; - test_start_timer(tester); + test_start_timer(tester); } |