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/*! @file
14    @brief Testing helper library.
15
16    This simple lightweight testing library provides a consistent testing output interface. Root-
17    task unit tests, OS level tests and user level tests all use this library, so they output
18    results in the same format.
19*/
20
21#ifndef _REFOS_TEST_H_
22#define _REFOS_TEST_H_
23
24#include <refos/refos.h>
25#include <autoconf.h>
26
27#define REFOS_TEST_HALT_ON_ASSERT 0
28#define REFOS_TEST_VERBOSE_PRINT 0
29
30#ifdef CONFIG_REFOS_RUN_TESTS
31
32#define TEST_MAX_TESTS 128
33#define TEST_TITLE_BUFFER_LEN 256
34
35#if defined(CONFIG_REFOS_ANSI_COLOUR_OUTPUT)
36    #define TEST_RED "\033[;1;31m"
37    #define TEST_GREEN "\033[;1;32m"
38    #define TEST_GREY "\033[;0;37m"
39    #define TEST_RESET "\033[0m"
40#else
41    #define TEST_RED
42    #define TEST_GREEN
43    #define TEST_GREY
44    #define TEST_RESET
45#endif
46
47#define tprintf(x...) fprintf(stdout, x)
48
49/*! @brief The current test title. */
50extern char *test_title;
51
52/*! @brief Start a new unit test. Must be called first for every test.
53    @param name String containing the test name, to be displayed on screen.
54*/
55void test_start(const char *name);
56
57/*! @brief Internal function to fail the current test.
58
59    This function is used to implement the  test_assert macro. Must be called after test_start() and
60    before test_success(). Please use the test_assert() macro instead of directly calling this
61    function.
62
63    @param check String containing the condition being checked that failed.
64    @param file The file that the failure occured in.
65    @param func The function name that the failure occured in.
66    @param line The line number into the file.
67    @return -1, meaning failure.
68*/
69int test_fail(const char *check, const char *file, const char *func, int line);
70
71/*! @brief Set the current test to success. This should be called the last thing after a unit test
72           has been completed; it effectively ends the test.
73    @return 0, meaning success.
74*/
75int test_success(void);
76
77/*! @brief Print all the test results for every test so far, along with any failures. */
78void test_print_log(void);
79
80#if REFOS_TEST_HALT_ON_ASSERT
81    #define test_assert assert
82#else
83    #define test_assert(e) if (!(e)) return test_fail(#e, __FILE__, __func__, __LINE__)
84#endif
85
86#if REFOS_TEST_VERBOSE_PRINT
87    #define tvprintf printf
88#else
89    #define tvprintf(x...)
90#endif
91
92#endif /* CONFIG_REFOS_RUN_TESTS */
93
94#endif /* _REFOS_TEST_H_ */
95