micro_test.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. // An ultra-lightweight testing framework designed for use with microcontroller
  13. // applications. Its only dependency is on TensorFlow Lite's ErrorReporter
  14. // interface, where log messages are output. This is designed to be usable even
  15. // when no standard C or C++ libraries are available, and without any dynamic
  16. // memory allocation or reliance on global constructors.
  17. //
  18. // To build a test, you use syntax similar to gunit, but with some extra
  19. // decoration to create a hidden 'main' function containing each of the tests to
  20. // be run. Your code should look something like:
  21. // ----------------------------------------------------------------------------
  22. // #include "path/to/this/header"
  23. //
  24. // TF_LITE_MICRO_TESTS_BEGIN
  25. //
  26. // TF_LITE_MICRO_TEST(SomeTest) {
  27. // TF_LITE_LOG_EXPECT_EQ(true, true);
  28. // }
  29. //
  30. // TF_LITE_MICRO_TESTS_END
  31. // ----------------------------------------------------------------------------
  32. // If you compile this for your platform, you'll get a normal binary that you
  33. // should be able to run. Executing it will output logging information like this
  34. // to stderr (or whatever equivalent is available and written to by
  35. // ErrorReporter):
  36. // ----------------------------------------------------------------------------
  37. // Testing SomeTest
  38. // 1/1 tests passed
  39. // ~~~ALL TESTS PASSED~~~
  40. // ----------------------------------------------------------------------------
  41. // This is designed to be human-readable, so you can just run tests manually,
  42. // but the string "~~~ALL TESTS PASSED~~~" should only appear if all of the
  43. // tests do pass. This makes it possible to integrate with automated test
  44. // systems by scanning the output logs and looking for that magic value.
  45. //
  46. // This framework is intended to be a rudimentary alternative to no testing at
  47. // all on systems that struggle to run more conventional approaches, so use with
  48. // caution!
  49. #ifndef TENSORFLOW_LITE_MICRO_TESTING_MICRO_TEST_H_
  50. #define TENSORFLOW_LITE_MICRO_TESTING_MICRO_TEST_H_
  51. #include "tensorflow/lite/c/common.h"
  52. #include "tensorflow/lite/micro/micro_error_reporter.h"
  53. namespace micro_test {
  54. extern int tests_passed;
  55. extern int tests_failed;
  56. extern bool is_test_complete;
  57. extern bool did_test_fail;
  58. extern tflite::ErrorReporter* reporter;
  59. } // namespace micro_test
  60. #define TF_LITE_MICRO_TESTS_BEGIN \
  61. namespace micro_test { \
  62. int tests_passed; \
  63. int tests_failed; \
  64. bool is_test_complete; \
  65. bool did_test_fail; \
  66. tflite::ErrorReporter* reporter; \
  67. } \
  68. \
  69. int main(int argc, char** argv) { \
  70. micro_test::tests_passed = 0; \
  71. micro_test::tests_failed = 0; \
  72. tflite::MicroErrorReporter error_reporter; \
  73. micro_test::reporter = &error_reporter;
  74. #define TF_LITE_MICRO_TESTS_END \
  75. micro_test::reporter->Report( \
  76. "%d/%d tests passed", micro_test::tests_passed, \
  77. (micro_test::tests_failed + micro_test::tests_passed)); \
  78. if (micro_test::tests_failed == 0) { \
  79. micro_test::reporter->Report("~~~ALL TESTS PASSED~~~\n"); \
  80. return kTfLiteOk; \
  81. } else { \
  82. micro_test::reporter->Report("~~~SOME TESTS FAILED~~~\n"); \
  83. return kTfLiteError; \
  84. } \
  85. }
  86. // TODO(petewarden): I'm going to hell for what I'm doing to this poor for loop.
  87. #define TF_LITE_MICRO_TEST(name) \
  88. micro_test::reporter->Report("Testing " #name); \
  89. for (micro_test::is_test_complete = false, \
  90. micro_test::did_test_fail = false; \
  91. !micro_test::is_test_complete; micro_test::is_test_complete = true, \
  92. micro_test::tests_passed += (micro_test::did_test_fail) ? 0 : 1, \
  93. micro_test::tests_failed += (micro_test::did_test_fail) ? 1 : 0)
  94. #define TF_LITE_MICRO_EXPECT(x) \
  95. do { \
  96. if (!(x)) { \
  97. micro_test::reporter->Report(#x " failed at %s:%d", __FILE__, __LINE__); \
  98. micro_test::did_test_fail = true; \
  99. } \
  100. } while (false)
  101. // TODO(b/139142772): this macro is used with types other than ints even though
  102. // the printf specifier is %d.
  103. #define TF_LITE_MICRO_EXPECT_EQ(x, y) \
  104. do { \
  105. auto vx = x; \
  106. auto vy = y; \
  107. if ((vx) != (vy)) { \
  108. micro_test::reporter->Report(#x " == " #y " failed at %s:%d (%d vs %d)", \
  109. __FILE__, __LINE__, static_cast<int>(vx), \
  110. static_cast<int>(vy)); \
  111. micro_test::did_test_fail = true; \
  112. } \
  113. } while (false)
  114. #define TF_LITE_MICRO_EXPECT_NE(x, y) \
  115. do { \
  116. if ((x) == (y)) { \
  117. micro_test::reporter->Report(#x " != " #y " failed at %s:%d", __FILE__, \
  118. __LINE__); \
  119. micro_test::did_test_fail = true; \
  120. } \
  121. } while (false)
  122. // TODO(wangtz): Making it more generic once needed.
  123. #define TF_LITE_MICRO_ARRAY_ELEMENT_EXPECT_NEAR(arr1, idx1, arr2, idx2, \
  124. epsilon) \
  125. do { \
  126. auto delta = ((arr1)[(idx1)] > (arr2)[(idx2)]) \
  127. ? ((arr1)[(idx1)] - (arr2)[(idx2)]) \
  128. : ((arr2)[(idx2)] - (arr1)[(idx1)]); \
  129. if (delta > epsilon) { \
  130. micro_test::reporter->Report( \
  131. #arr1 "[%d] (%f) near " #arr2 "[%d] (%f) failed at %s:%d", \
  132. static_cast<int>(idx1), static_cast<float>((arr1)[(idx1)]), \
  133. static_cast<int>(idx2), static_cast<float>((arr2)[(idx2)]), \
  134. __FILE__, __LINE__); \
  135. micro_test::did_test_fail = true; \
  136. } \
  137. } while (false)
  138. #define TF_LITE_MICRO_EXPECT_NEAR(x, y, epsilon) \
  139. do { \
  140. auto vx = (x); \
  141. auto vy = (y); \
  142. auto delta = ((vx) > (vy)) ? ((vx) - (vy)) : ((vy) - (vx)); \
  143. if (delta > epsilon) { \
  144. micro_test::reporter->Report( \
  145. #x " (%f) near " #y " (%f) failed at %s:%d", \
  146. static_cast<double>(vx), static_cast<double>(vy), __FILE__, \
  147. __LINE__); \
  148. micro_test::did_test_fail = true; \
  149. } \
  150. } while (false)
  151. #define TF_LITE_MICRO_EXPECT_GT(x, y) \
  152. do { \
  153. if ((x) <= (y)) { \
  154. micro_test::reporter->Report(#x " > " #y " failed at %s:%d", __FILE__, \
  155. __LINE__); \
  156. micro_test::did_test_fail = true; \
  157. } \
  158. } while (false)
  159. #define TF_LITE_MICRO_EXPECT_LT(x, y) \
  160. do { \
  161. if ((x) >= (y)) { \
  162. micro_test::reporter->Report(#x " < " #y " failed at %s:%d", __FILE__, \
  163. __LINE__); \
  164. micro_test::did_test_fail = true; \
  165. } \
  166. } while (false)
  167. #define TF_LITE_MICRO_EXPECT_GE(x, y) \
  168. do { \
  169. if ((x) < (y)) { \
  170. micro_test::reporter->Report(#x " >= " #y " failed at %s:%d", __FILE__, \
  171. __LINE__); \
  172. micro_test::did_test_fail = true; \
  173. } \
  174. } while (false)
  175. #define TF_LITE_MICRO_EXPECT_LE(x, y) \
  176. do { \
  177. if ((x) > (y)) { \
  178. micro_test::reporter->Report(#x " <= " #y " failed at %s:%d", __FILE__, \
  179. __LINE__); \
  180. micro_test::did_test_fail = true; \
  181. } \
  182. } while (false)
  183. #define TF_LITE_MICRO_EXPECT_TRUE(x) \
  184. do { \
  185. if (!(x)) { \
  186. micro_test::reporter->Report(#x " was not true failed at %s:%d", \
  187. __FILE__, __LINE__); \
  188. micro_test::did_test_fail = true; \
  189. } \
  190. } while (false)
  191. #define TF_LITE_MICRO_EXPECT_FALSE(x) \
  192. do { \
  193. if (x) { \
  194. micro_test::reporter->Report(#x " was not false failed at %s:%d", \
  195. __FILE__, __LINE__); \
  196. micro_test::did_test_fail = true; \
  197. } \
  198. } while (false)
  199. #define TF_LITE_MICRO_FAIL(msg) \
  200. do { \
  201. micro_test::reporter->Report("FAIL: %s", msg, __FILE__, __LINE__); \
  202. micro_test::did_test_fail = true; \
  203. } while (false)
  204. #define TF_LITE_MICRO_EXPECT_STRING_EQ(string1, string2) \
  205. do { \
  206. for (int i = 0; string1[i] != '\0' && string2[i] != '\0'; i++) { \
  207. if (string1[i] != string2[i]) { \
  208. micro_test::reporter->Report("FAIL: %s did not match %s", string1, \
  209. string2, __FILE__, __LINE__); \
  210. micro_test::did_test_fail = true; \
  211. } \
  212. } \
  213. } while (false)
  214. #endif // TENSORFLOW_LITE_MICRO_TESTING_MICRO_TEST_H_