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