|
| 1 | +/*============================================================================ |
| 2 | +* ET: embedded test (super-simple embedded testing framework) |
| 3 | +* GitHub: https://github.com/QuantumLeaps/Embedded-Test |
| 4 | +* |
| 5 | +* Q u a n t u m L e a P s |
| 6 | +* ------------------------ |
| 7 | +* Modern Embedded Software |
| 8 | +* |
| 9 | +* Copyright (C) 2005 Quantum Leaps, <state-machine.com>. |
| 10 | +* |
| 11 | +* SPDX-License-Identifier: MIT |
| 12 | +* |
| 13 | +* Permission is hereby granted, free of charge, to any person obtaining a |
| 14 | +* copy of this software and associated documentation files (the "Software"), |
| 15 | +* to deal in the Software without restriction, including without limitation |
| 16 | +* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 17 | +* and/or sell copies of the Software, and to permit persons to whom the |
| 18 | +* Software is furnished to do so, subject to the following conditions: |
| 19 | +* |
| 20 | +* The above copyright notice and this permission notice shall be included in |
| 21 | +* all copies or substantial portions of the Software. |
| 22 | +* |
| 23 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 26 | +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 28 | +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 29 | +* DEALINGS IN THE SOFTWARE. |
| 30 | +============================================================================*/ |
| 31 | +#include "et.h" /* ET: embedded test */ |
| 32 | + |
| 33 | +/*..........................................................................*/ |
| 34 | +static void print_str(char const *str); |
| 35 | +static void print_dec(unsigned const num); |
| 36 | +static void print_summary(unsigned ok); |
| 37 | +static void test_end(void); |
| 38 | +static int str_cmp(char const *str1, char const *str2); |
| 39 | + |
| 40 | +static unsigned l_test_count; |
| 41 | +static unsigned l_skip_count; |
| 42 | +static unsigned l_skip_last; |
| 43 | + |
| 44 | +static char const *l_expect_assert_module; |
| 45 | +static int l_expect_assert_label; |
| 46 | + |
| 47 | +/*..........................................................................*/ |
| 48 | +int main(int argc, char *argv[]) { |
| 49 | + ET_onInit(argc, argv); |
| 50 | + |
| 51 | + print_str("\nET embedded test " ET_VERSION |
| 52 | + ", https://github.com/QuantumLeaps/ET\n"); |
| 53 | + print_str("---------------- group: "); |
| 54 | + print_str(ET_group_); |
| 55 | + print_str(" -----------------\n"); |
| 56 | + |
| 57 | + ET_run_(); |
| 58 | + |
| 59 | + test_end(); |
| 60 | + print_summary(1U); |
| 61 | + |
| 62 | + ET_onExit(0); /* success */ |
| 63 | +} |
| 64 | + |
| 65 | +/*..........................................................................*/ |
| 66 | +int ET_test_(char const *title, int skip) { |
| 67 | + test_end(); |
| 68 | + ++l_test_count; |
| 69 | + ET_onPrintChar('['); |
| 70 | + print_dec(l_test_count); |
| 71 | + print_str("] \""); |
| 72 | + print_str(title); |
| 73 | + print_str("\" "); |
| 74 | + if (skip) { |
| 75 | + ++l_skip_count; |
| 76 | + } |
| 77 | + else { |
| 78 | + setup(); |
| 79 | + ET_onPrintChar('.'); |
| 80 | + } |
| 81 | + l_skip_last = skip; |
| 82 | + return skip == 0; |
| 83 | +} |
| 84 | +/*..........................................................................*/ |
| 85 | +static void test_end(void) { |
| 86 | + if (l_expect_assert_module != (char const *)0) { |
| 87 | + ET_fail("Expected Assertion didn't fire", |
| 88 | + l_expect_assert_module, l_expect_assert_label); |
| 89 | + } |
| 90 | + else if (l_test_count > 0) { |
| 91 | + if (l_skip_last) { |
| 92 | + print_str(" SKIPPED\n"); |
| 93 | + l_skip_last = 0; |
| 94 | + } |
| 95 | + else { |
| 96 | + teardown(); |
| 97 | + print_str(". PASSED\n"); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | +/*..........................................................................*/ |
| 102 | +void ET_fail(char const *cond, char const *group, int line) { |
| 103 | + print_str(" FAILED\n--> "); |
| 104 | + print_str(group); |
| 105 | + ET_onPrintChar(':'); |
| 106 | + print_dec(line); |
| 107 | + ET_onPrintChar(' '); |
| 108 | + print_str(cond); |
| 109 | + ET_onPrintChar('\n'); |
| 110 | + print_summary(0U); |
| 111 | + |
| 112 | + ET_onExit(-1); /* failure */ |
| 113 | +} |
| 114 | +/*..........................................................................*/ |
| 115 | +void ET_expect_assert(char const *module, int label) { |
| 116 | + l_expect_assert_module = module; |
| 117 | + l_expect_assert_label = label; |
| 118 | +} |
| 119 | +/*..........................................................................*/ |
| 120 | +void ET_verify_assert_(char const *module, int label) { |
| 121 | + if ((l_expect_assert_label == label) |
| 122 | + && (str_cmp(module, l_expect_assert_module) == 0)) |
| 123 | + { |
| 124 | + l_expect_assert_module = (char const *)0; |
| 125 | + test_end(); |
| 126 | + print_str("Assertion (expected) --> Exiting\n"); |
| 127 | + print_summary(1U); |
| 128 | + |
| 129 | + ET_onExit(0); /* success */ |
| 130 | + } |
| 131 | + else { |
| 132 | + ET_fail("Unexpected assertion", module, label); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/*..........................................................................*/ |
| 137 | +static void print_summary(unsigned ok) { |
| 138 | + print_str("------------ "); |
| 139 | + print_dec(l_test_count); |
| 140 | + print_str(" test(s), "); |
| 141 | + print_dec(l_skip_count); |
| 142 | + print_str(" skipped -------------\n"); |
| 143 | + print_str(ok ? "OK\n" : "FAILED\n"); |
| 144 | +} |
| 145 | +/*..........................................................................*/ |
| 146 | +static void print_str(char const *str) { |
| 147 | + for (; *str != '\0'; ++str) { |
| 148 | + ET_onPrintChar(*str); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +/*..........................................................................*/ |
| 153 | +static void print_dec(unsigned const num) { |
| 154 | + /* find power of 10 of the first decimal digit of the number */ |
| 155 | + unsigned pwr10 = 1U; |
| 156 | + for (; num > (pwr10 * 9U); pwr10 *= 10U) { |
| 157 | + } |
| 158 | + /* print the decimal digits of the number... */ |
| 159 | + do { |
| 160 | + ET_onPrintChar((char)('0' + ((num / pwr10) % 10U))); |
| 161 | + pwr10 /= 10U; |
| 162 | + } while (pwr10 != 0U); |
| 163 | +} |
| 164 | + |
| 165 | +/*..........................................................................*/ |
| 166 | +static int str_cmp(char const *str1, char const *str2) { |
| 167 | + while (*str1 == *str2++) { |
| 168 | + if (*str1++ == '\0') { |
| 169 | + return 0; |
| 170 | + } |
| 171 | + } |
| 172 | + --str2; |
| 173 | + return *(unsigned char *)str1 - *(unsigned char *)str2; |
| 174 | +} |
0 commit comments