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 #include "tensorflow/lite/micro/examples/magic_wand/gesture_predictor.h"
17 
18 #include "tensorflow/lite/micro/examples/magic_wand/constants.h"
19 #include "tensorflow/lite/micro/testing/micro_test.h"
20 
21 TF_LITE_MICRO_TESTS_BEGIN
22 
TF_LITE_MICRO_TEST(SuccessfulPrediction)23 TF_LITE_MICRO_TEST(SuccessfulPrediction) {
24   // Use the threshold from the 0th gesture.
25   float probabilities[kGestureCount] = {kDetectionThreshold, 0.0, 0.0, 0.0};
26   int prediction;
27   // Loop just too few times to trigger a prediction.
28   for (int i = 0; i < kPredictionHistoryLength - 1; i++) {
29     prediction = PredictGesture(probabilities);
30     TF_LITE_MICRO_EXPECT_EQ(prediction, kNoGesture);
31   }
32   // Call once more, triggering a prediction
33   // for category 0.
34   prediction = PredictGesture(probabilities);
35   TF_LITE_MICRO_EXPECT_EQ(prediction, 0);
36 }
37 
TF_LITE_MICRO_TEST(FailPartWayThere)38 TF_LITE_MICRO_TEST(FailPartWayThere) {
39   // Use the threshold from the 0th gesture.
40   float probabilities[kGestureCount] = {kDetectionThreshold, 0.0, 0.0, 0.0};
41   int prediction;
42   // Loop just too few times to trigger a prediction.
43   for (int i = 0; i <= kPredictionHistoryLength - 1; i++) {
44     prediction = PredictGesture(probabilities);
45     TF_LITE_MICRO_EXPECT_EQ(prediction, kNoGesture);
46   }
47   // Call with a different prediction, triggering a failure.
48   probabilities[0] = 0.0;
49   probabilities[2] = 1.0;
50   prediction = PredictGesture(probabilities);
51   TF_LITE_MICRO_EXPECT_EQ(prediction, kNoGesture);
52 }
53 
TF_LITE_MICRO_TEST(InsufficientProbability)54 TF_LITE_MICRO_TEST(InsufficientProbability) {
55   // Just below the detection threshold.
56   float probabilities[kGestureCount] = {kDetectionThreshold - 0.1f, 0.0, 0.0,
57                                         0.0};
58   int prediction;
59   // Loop the exact right number of times
60   for (int i = 0; i <= kPredictionHistoryLength; i++) {
61     prediction = PredictGesture(probabilities);
62     TF_LITE_MICRO_EXPECT_EQ(prediction, kNoGesture);
63   }
64 }
65 
66 TF_LITE_MICRO_TESTS_END
67