aboutsummaryrefslogtreecommitdiff
path: root/test_util.c
diff options
context:
space:
mode:
authorDavid Phillips <david@yeah.nah.nz>2021-02-13 18:21:36 +1300
committerDavid Phillips <david@yeah.nah.nz>2021-02-13 18:25:28 +1300
commit4710921284adbf5005421515fbab5dd1aa38e9aa (patch)
tree18d1850a58c58f0095c8752a76ef8584a2628dbc /test_util.c
parent8ab11293cdd2a47e7950fa9c488cbc504325cad3 (diff)
downloadaltimeter-4710921284adbf5005421515fbab5dd1aa38e9aa.tar.xz
Add test runner
This patch adds a "magic" test runner which discovers all test cases in the executable into which it is linked. This is achieved through the use of a special macro used to declare tests, so that they are added onto a linker- generated list. The linker list logic is largely taken from U-Boot source code, with minor some tweaks made. This linker-generated list is then used at runtime in order to pick out every test case which can be run, resulting in a cross-file "suite" of unity unit tests, rather than separate test executables per module. A sample test module, test_test.c is provided to illustrate the test runner's ability to correctly discover tests across multiple translation units.
Diffstat (limited to 'test_util.c')
-rw-r--r--test_util.c21
1 files changed, 5 insertions, 16 deletions
diff --git a/test_util.c b/test_util.c
index 81c3db7..c6b9b83 100644
--- a/test_util.c
+++ b/test_util.c
@@ -1,14 +1,13 @@
#include <string.h>
+#include "test_runner.h"
#include "unity.h"
#include "util.h"
-void setUp(){};
-void tearDown(){};
/* blank_to_eol should pad a buffer with spaces as far as it can, after an
* existing string therein, without overrunning, while still terminating */
-void test_blank_to_eol_happy(void)
+RUNNER_DECLARE_TEST(test_blank_to_eol_happy)
{
char buf[11] = "hello";
blank_to_eol(buf, sizeof(buf));
@@ -17,7 +16,7 @@ void test_blank_to_eol_happy(void)
/* blanking an empty string to end of buffer should fill entire buffer with
* spaces until the last byte, which should be null terminator */
-void test_blank_to_eol_empty(void)
+RUNNER_DECLARE_TEST(test_blank_to_eol_empty)
{
char buf[5] = "";
blank_to_eol(buf, sizeof(buf));
@@ -25,7 +24,7 @@ void test_blank_to_eol_empty(void)
}
/* blanking should not overrun the specified buffer size at all */
-void test_blank_to_eol_no_overrun(void)
+RUNNER_DECLARE_TEST(test_blank_to_eol_no_overrun)
{
char buf[20] = {
'h', 'e', 'l', 'l', 'o', '\0',
@@ -41,7 +40,7 @@ void test_blank_to_eol_no_overrun(void)
/* blanking a buffer which already has no '\0' within it should result in the
* buffer being terminated, truncating contents minimally as necessary to fit
* a '\0' */
-void test_blank_to_eol_overrun(void)
+RUNNER_DECLARE_TEST(test_blank_to_eol_overrun)
{
char buf[20] = "hello, world!";
blank_to_eol(buf, 6);
@@ -50,13 +49,3 @@ void test_blank_to_eol_overrun(void)
* doesn't test OOB reads, only writes */
TEST_ASSERT_EQUAL_STRING(" world!", &buf[6]);
}
-
-int main(void)
-{
- UNITY_BEGIN();
- RUN_TEST(test_blank_to_eol_happy);
- RUN_TEST(test_blank_to_eol_empty);
- RUN_TEST(test_blank_to_eol_no_overrun);
- RUN_TEST(test_blank_to_eol_overrun);
- return UNITY_END();
-}