From b027063c030cc0cd034b6ec3aa6bc4885b019c88 Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Sun, 28 Nov 2021 13:44:16 +0000 Subject: x1000-installer: simple test suite runner Change-Id: I0b9ee81cbd8dda593924b2f7c32a1d1d87ce84b0 --- lib/x1000-installer/test/main.c | 99 +++++++++++++++++------------------------ lib/x1000-installer/test/test.h | 39 ++++++++++++++++ 2 files changed, 79 insertions(+), 59 deletions(-) create mode 100644 lib/x1000-installer/test/test.h (limited to 'lib/x1000-installer/test') diff --git a/lib/x1000-installer/test/main.c b/lib/x1000-installer/test/main.c index e952fb9d86..4d16271c98 100644 --- a/lib/x1000-installer/test/main.c +++ b/lib/x1000-installer/test/main.c @@ -19,54 +19,43 @@ * ****************************************************************************/ -#include "xf_nandio.h" -#include "xf_error.h" -#include "xf_update.h" -#include "file.h" #include -#include -#include -static struct xf_nandio nio; +static int test_num_executed = 0; +static int test_num_failed = 0; +int test_num_asserts_executed = 0; +int test_num_asserts_failed = 0; -static struct xf_map testmap[] = { - { - .name = "src/xf_error.c", - .offset = 0x00000000, - .length = 0x00004000, - }, - { - .name = "src/xf_update.c", - .offset = 0x00010000, - .length = 0x00004000, - }, - { - .name = "src/xf_package.c", - .offset = 0x00020000, - .length = 0x00001800, - }, -}; - -void checkrc(int rc) +void test_failure(const char* file, int line, const char* msg) { - if(rc == XF_E_SUCCESS) - return; + fprintf(stderr, "%s:%d: ASSERTION FAILED: %s\n", file, line, msg); + ++test_num_asserts_failed; +} - if(rc == XF_E_NAND) { - printf("NAND error: %d\n", nio.nand_err); - } else { - printf("error: %s\n", xf_strerror(rc)); - printf(" CurBlock = %lu\n", (unsigned long)nio.cur_block); - printf(" CurOffset = %lu\n", (unsigned long)nio.offset_in_block); - } +typedef void(*test_t)(void); - exit(1); -} +struct test_info { + const char* name; + test_t func; +}; + +#define TEST(x) {#x, x} +static const struct test_info all_tests[] = { +}; +#undef TEST -int openstream_file(void* arg, const char* file, struct xf_stream* stream) +void run_test(const struct test_info* tinfo) { - (void)arg; - return xf_open_file(file, O_RDONLY, stream); + int asserts_now = test_num_asserts_failed; + ++test_num_executed; + + fprintf(stderr, "RUN %s\n", tinfo->name); + tinfo->func(); + + if(test_num_asserts_failed > asserts_now) { + fprintf(stderr, " %s: FAILED!\n", tinfo->name); + ++test_num_failed; + } } int main(int argc, char* argv[]) @@ -74,25 +63,17 @@ int main(int argc, char* argv[]) (void)argc; (void)argv; - nand_trace_reset(65535); + size_t num_tests = sizeof(all_tests) / sizeof(struct test_info); + for(size_t i = 0; i < num_tests; ++i) + run_test(&all_tests[i]); - int rc = xf_nandio_init(&nio); - checkrc(rc); - - rc = xf_updater_run(XF_UPDATE, &nio, - testmap, sizeof(testmap)/sizeof(struct xf_map), - openstream_file, NULL); - checkrc(rc); - - for(size_t i = 0; i < nand_trace_length; ++i) { - const char* types[] = {"READ", "PROGRAM", "ERASE"}; - const char* excep[] = {"NONE", "DOUBLE_PROGRAMMED", "CLEARED"}; - printf("%s %s %lu\n", - types[nand_trace[i].type], - excep[nand_trace[i].exception], - (unsigned long)nand_trace[i].addr); - } + fprintf(stderr, "------------------------------------------\n"); + fprintf(stderr, "TEST COMPLETE\n"); + fprintf(stderr, " Tests %d failed / %d executed\n", + test_num_failed, test_num_executed); + fprintf(stderr, " Assertions %d failed / %d executed\n", + test_num_asserts_failed, test_num_asserts_executed); - xf_nandio_destroy(&nio); - return 0; + if(test_num_failed > 0) + return 1; } diff --git a/lib/x1000-installer/test/test.h b/lib/x1000-installer/test/test.h new file mode 100644 index 0000000000..3d47a19d87 --- /dev/null +++ b/lib/x1000-installer/test/test.h @@ -0,0 +1,39 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2021 Aidan MacDonald + * + * 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 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#ifndef TEST_H +#define TEST_H + +extern int test_num_asserts_executed; +extern int test_num_asserts_failed; + +extern void test_failure(const char* file, int line, const char* msg); + +#define T_ASSERT(cond) \ + do { \ + ++test_num_asserts_executed; \ + if(!(cond)) { \ + test_failure(__FILE__, __LINE__, #cond); \ + goto cleanup; \ + } \ + } while(0) + +#endif -- cgit v1.2.3