1/*
2 * Copyright 2016, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(D61_BSD)
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
17#include <refos/test.h>
18
19/*! @file
20    @brief Testing helper library. */
21
22#ifdef CONFIG_REFOS_RUN_TESTS
23
24/*! @brief The width at which the test results are aligned to. This should be larger than the
25           lngest test name to ensure alignment. Too large and it may wrap around the terminal. */
26#define REFOS_TEST_WIDTH 80
27
28struct test_failure_log {
29    int line;
30    const char *file;
31    const char *func;
32    const char *check;
33};
34
35char *test_title = "";
36
37static struct test_failure_log _testsFailLog[TEST_MAX_TESTS];
38static unsigned int _testsFailed = 0;
39
40void
41test_start(const char *name)
42{
43    static char temp_buffer[TEST_TITLE_BUFFER_LEN];
44    temp_buffer[0] = '\0';
45    sprintf(temp_buffer, "%s | Testing %s ", test_title, name);
46    tprintf("%s", temp_buffer);
47    for (unsigned int i = strlen(temp_buffer); i < REFOS_TEST_WIDTH; i++) {
48        tprintf(".");
49    }
50    tprintf(" ");
51}
52
53int
54test_fail(const char *check, const char *file, const char *func, int line)
55{
56    tprintf(TEST_RED "FAILED" TEST_RESET ".\n");
57    _testsFailLog[_testsFailed].check = check;
58    _testsFailLog[_testsFailed].file = file;
59    _testsFailLog[_testsFailed].func = func;
60    _testsFailLog[_testsFailed].line = line;
61    _testsFailed++;
62    return -1;
63}
64
65int
66test_success()
67{
68    tprintf(TEST_GREEN "PASSED" TEST_RESET ".\n");
69    return 0;
70}
71
72void
73test_print_log(void)
74{
75    for (unsigned int i = 0; i < _testsFailed; i++) {
76        tprintf("%s | Test assert failed: %s, function %s, "
77                "file %s, line %d.\n", test_title, _testsFailLog[i].check, _testsFailLog[i].func,
78                _testsFailLog[i].file, _testsFailLog[i].line);
79    }
80    if (_testsFailed == 0) {
81        tprintf("%s | All is well in the universe.\n", test_title);
82    }
83}
84
85#endif /* CONFIG_REFOS_RUN_TESTS */
86