et.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. static void print_str(char const *str);
  34. static void print_dec(unsigned const num);
  35. static void print_summary(unsigned ok);
  36. static void test_end(void);
  37. static int str_cmp(char const *str1, char const *str2);
  38. static unsigned l_test_count;
  39. static unsigned l_skip_count;
  40. static unsigned l_skip_last;
  41. static char const *l_expect_assert_module;
  42. static int l_expect_assert_label;
  43. /*..........................................................................*/
  44. int main(int argc, char *argv[]) {
  45. ET_onInit(argc, argv);
  46. print_str("\nET embedded test " ET_VERSION
  47. ", https://github.com/QuantumLeaps/ET\n");
  48. print_str("---------------- group: ");
  49. print_str(ET_group_);
  50. print_str(" -----------------\n");
  51. ET_run_();
  52. test_end();
  53. print_summary(1U);
  54. ET_onExit(0); /* success */
  55. }
  56. /*..........................................................................*/
  57. int ET_test_(char const *title, int skip) {
  58. test_end();
  59. ++l_test_count;
  60. ET_onPrintChar('[');
  61. print_dec(l_test_count);
  62. print_str("] \"");
  63. print_str(title);
  64. print_str("\" ");
  65. if (skip) {
  66. ++l_skip_count;
  67. }
  68. else {
  69. setup();
  70. ET_onPrintChar('.');
  71. }
  72. l_skip_last = skip;
  73. return skip == 0;
  74. }
  75. /*..........................................................................*/
  76. static void test_end(void) {
  77. if (l_expect_assert_module != (char const *)0) {
  78. ET_fail("Expected Assertion didn't fire",
  79. l_expect_assert_module, l_expect_assert_label);
  80. }
  81. else if (l_test_count > 0) {
  82. if (l_skip_last) {
  83. print_str(" SKIPPED\n");
  84. l_skip_last = 0;
  85. }
  86. else {
  87. teardown();
  88. print_str(". PASSED\n");
  89. }
  90. }
  91. }
  92. /*..........................................................................*/
  93. void ET_fail(char const *cond, char const *group, int line) {
  94. print_str(" FAILED\n--> ");
  95. print_str(group);
  96. ET_onPrintChar(':');
  97. print_dec(line);
  98. ET_onPrintChar(' ');
  99. print_str(cond);
  100. ET_onPrintChar('\n');
  101. print_summary(0U);
  102. ET_onExit(-1); /* failure */
  103. }
  104. /*..........................................................................*/
  105. void ET_expect_assert(char const *module, int label) {
  106. l_expect_assert_module = module;
  107. l_expect_assert_label = label;
  108. }
  109. /*..........................................................................*/
  110. void ET_verify_assert_(char const *module, int label) {
  111. if ((l_expect_assert_label == label)
  112. && (str_cmp(module, l_expect_assert_module) == 0))
  113. {
  114. l_expect_assert_module = (char const *)0;
  115. test_end();
  116. print_str("Assertion (expected) --> Exiting\n");
  117. print_summary(1U);
  118. ET_onExit(0); /* success */
  119. }
  120. else {
  121. ET_fail("Unexpected assertion", module, label);
  122. }
  123. }
  124. /*..........................................................................*/
  125. static void print_summary(unsigned ok) {
  126. print_str("------------ ");
  127. print_dec(l_test_count);
  128. print_str(" test(s), ");
  129. print_dec(l_skip_count);
  130. print_str(" skipped -------------\n");
  131. print_str(ok ? "OK\n" : "FAILED\n");
  132. }
  133. /*..........................................................................*/
  134. static void print_str(char const *str) {
  135. for (; *str != '\0'; ++str) {
  136. ET_onPrintChar(*str);
  137. }
  138. }
  139. /*..........................................................................*/
  140. static void print_dec(unsigned const num) {
  141. /* find power of 10 of the first decimal digit of the number */
  142. unsigned pwr10 = 1U;
  143. for (; num > (pwr10 * 9U); pwr10 *= 10U) {
  144. }
  145. /* print the decimal digits of the number... */
  146. do {
  147. ET_onPrintChar((char)('0' + ((num / pwr10) % 10U)));
  148. pwr10 /= 10U;
  149. } while (pwr10 != 0U);
  150. }
  151. /*..........................................................................*/
  152. static int str_cmp(char const *str1, char const *str2) {
  153. while (*str1 == *str2++) {
  154. if (*str1++ == '\0') {
  155. return 0;
  156. }
  157. }
  158. --str2;
  159. return *(unsigned char *)str1 - *(unsigned char *)str2;
  160. }