diff options
Diffstat (limited to 'lib/x1000-installer')
-rw-r--r-- | lib/x1000-installer/test/main.c | 99 | ||||
-rw-r--r-- | lib/x1000-installer/test/test.h | 39 |
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 | ||
30 | static struct xf_nandio nio; | 24 | static int test_num_executed = 0; |
25 | static int test_num_failed = 0; | ||
26 | int test_num_asserts_executed = 0; | ||
27 | int test_num_asserts_failed = 0; | ||
31 | 28 | ||
32 | static struct xf_map testmap[] = { | 29 | void 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 | |||
50 | void 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) { | 35 | typedef 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); | 37 | struct test_info { |
64 | } | 38 | const char* name; |
39 | test_t func; | ||
40 | }; | ||
41 | |||
42 | #define TEST(x) {#x, x} | ||
43 | static const struct test_info all_tests[] = { | ||
44 | }; | ||
45 | #undef TEST | ||
65 | 46 | ||
66 | int openstream_file(void* arg, const char* file, struct xf_stream* stream) | 47 | void 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 | ||
72 | int main(int argc, char* argv[]) | 61 | int 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 | |||
25 | extern int test_num_asserts_executed; | ||
26 | extern int test_num_asserts_failed; | ||
27 | |||
28 | extern 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 | ||