summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAidan MacDonald <amachronic@protonmail.com>2021-11-28 13:44:16 +0000
committerAidan MacDonald <amachronic@protonmail.com>2021-11-28 13:45:20 +0000
commitb027063c030cc0cd034b6ec3aa6bc4885b019c88 (patch)
tree4dd698a19e03cf2225a4cf28a5046a1b9ff7f136 /lib
parent7ca8623927280cdceebfce8e4413960efac90446 (diff)
downloadrockbox-b027063c030cc0cd034b6ec3aa6bc4885b019c88.tar.gz
rockbox-b027063c030cc0cd034b6ec3aa6bc4885b019c88.zip
x1000-installer: simple test suite runner
Change-Id: I0b9ee81cbd8dda593924b2f7c32a1d1d87ce84b0
Diffstat (limited to 'lib')
-rw-r--r--lib/x1000-installer/test/main.c99
-rw-r--r--lib/x1000-installer/test/test.h39
2 files changed, 79 insertions, 59 deletions
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 @@
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21 21
22#include "xf_nandio.h"
23#include "xf_error.h"
24#include "xf_update.h"
25#include "file.h"
26#include <stdio.h> 22#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29 23
30static struct xf_nandio nio; 24static int test_num_executed = 0;
25static int test_num_failed = 0;
26int test_num_asserts_executed = 0;
27int test_num_asserts_failed = 0;
31 28
32static struct xf_map testmap[] = { 29void test_failure(const char* file, int line, const char* msg)
33 {
34 .name = "src/xf_error.c",
35 .offset = 0x00000000,
36 .length = 0x00004000,
37 },
38 {
39 .name = "src/xf_update.c",
40 .offset = 0x00010000,
41 .length = 0x00004000,
42 },
43 {
44 .name = "src/xf_package.c",
45 .offset = 0x00020000,
46 .length = 0x00001800,
47 },
48};
49
50void checkrc(int rc)
51{ 30{
52 if(rc == XF_E_SUCCESS) 31 fprintf(stderr, "%s:%d: ASSERTION FAILED: %s\n", file, line, msg);
53 return; 32 ++test_num_asserts_failed;
33}
54 34
55 if(rc == XF_E_NAND) { 35typedef void(*test_t)(void);
56 printf("NAND error: %d\n", nio.nand_err);
57 } else {
58 printf("error: %s\n", xf_strerror(rc));
59 printf(" CurBlock = %lu\n", (unsigned long)nio.cur_block);
60 printf(" CurOffset = %lu\n", (unsigned long)nio.offset_in_block);
61 }
62 36
63 exit(1); 37struct test_info {
64} 38 const char* name;
39 test_t func;
40};
41
42#define TEST(x) {#x, x}
43static const struct test_info all_tests[] = {
44};
45#undef TEST
65 46
66int openstream_file(void* arg, const char* file, struct xf_stream* stream) 47void run_test(const struct test_info* tinfo)
67{ 48{
68 (void)arg; 49 int asserts_now = test_num_asserts_failed;
69 return xf_open_file(file, O_RDONLY, stream); 50 ++test_num_executed;
51
52 fprintf(stderr, "RUN %s\n", tinfo->name);
53 tinfo->func();
54
55 if(test_num_asserts_failed > asserts_now) {
56 fprintf(stderr, " %s: FAILED!\n", tinfo->name);
57 ++test_num_failed;
58 }
70} 59}
71 60
72int main(int argc, char* argv[]) 61int main(int argc, char* argv[])
@@ -74,25 +63,17 @@ int main(int argc, char* argv[])
74 (void)argc; 63 (void)argc;
75 (void)argv; 64 (void)argv;
76 65
77 nand_trace_reset(65535); 66 size_t num_tests = sizeof(all_tests) / sizeof(struct test_info);
67 for(size_t i = 0; i < num_tests; ++i)
68 run_test(&all_tests[i]);
78 69
79 int rc = xf_nandio_init(&nio); 70 fprintf(stderr, "------------------------------------------\n");
80 checkrc(rc); 71 fprintf(stderr, "TEST COMPLETE\n");
81 72 fprintf(stderr, " Tests %d failed / %d executed\n",
82 rc = xf_updater_run(XF_UPDATE, &nio, 73 test_num_failed, test_num_executed);
83 testmap, sizeof(testmap)/sizeof(struct xf_map), 74 fprintf(stderr, " Assertions %d failed / %d executed\n",
84 openstream_file, NULL); 75 test_num_asserts_failed, test_num_asserts_executed);
85 checkrc(rc);
86
87 for(size_t i = 0; i < nand_trace_length; ++i) {
88 const char* types[] = {"READ", "PROGRAM", "ERASE"};
89 const char* excep[] = {"NONE", "DOUBLE_PROGRAMMED", "CLEARED"};
90 printf("%s %s %lu\n",
91 types[nand_trace[i].type],
92 excep[nand_trace[i].exception],
93 (unsigned long)nand_trace[i].addr);
94 }
95 76
96 xf_nandio_destroy(&nio); 77 if(test_num_failed > 0)
97 return 0; 78 return 1;
98} 79}
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 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2021 Aidan MacDonald
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#ifndef TEST_H
23#define TEST_H
24
25extern int test_num_asserts_executed;
26extern int test_num_asserts_failed;
27
28extern void test_failure(const char* file, int line, const char* msg);
29
30#define T_ASSERT(cond) \
31 do { \
32 ++test_num_asserts_executed; \
33 if(!(cond)) { \
34 test_failure(__FILE__, __LINE__, #cond); \
35 goto cleanup; \
36 } \
37 } while(0)
38
39#endif