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/micro_speech/audio_provider.h"
17 
18 #include <limits>
19 
20 #include "tensorflow/lite/c/common.h"
21 #include "tensorflow/lite/micro/examples/micro_speech/micro_features/micro_model_settings.h"
22 #include "tensorflow/lite/micro/micro_error_reporter.h"
23 #include "tensorflow/lite/micro/testing/micro_test.h"
24 
25 TF_LITE_MICRO_TESTS_BEGIN
26 
TF_LITE_MICRO_TEST(TestAudioProvider)27 TF_LITE_MICRO_TEST(TestAudioProvider) {
28   tflite::MicroErrorReporter micro_error_reporter;
29 
30   int audio_samples_size = 0;
31   int16_t* audio_samples = nullptr;
32   TfLiteStatus get_status =
33       GetAudioSamples(&micro_error_reporter, 0, kFeatureSliceDurationMs,
34                       &audio_samples_size, &audio_samples);
35   TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, get_status);
36   TF_LITE_MICRO_EXPECT_LE(audio_samples_size, kMaxAudioSampleSize);
37   TF_LITE_MICRO_EXPECT_NE(audio_samples, nullptr);
38 
39   // Make sure we can read all of the returned memory locations.
40   int total = 0;
41   for (int i = 0; i < audio_samples_size; ++i) {
42     total += audio_samples[i];
43   }
44 }
45 
TF_LITE_MICRO_TEST(TestTimer)46 TF_LITE_MICRO_TEST(TestTimer) {
47   // Make sure that the technically-undefined overflow behavior we rely on below
48   // works on this platform. It's still not guaranteed, but at least this is a
49   // smoke check.  Turn off when running with ASan, as it will complain about
50   // the following undefined behavior.
51 #ifndef ADDRESS_SANITIZER
52   int32_t overflow_value = std::numeric_limits<int32_t>::max();
53   overflow_value += 1;
54   TF_LITE_MICRO_EXPECT_EQ(std::numeric_limits<int32_t>::min(), overflow_value);
55 #endif
56 
57   const int32_t first_time = LatestAudioTimestamp();
58   const int32_t second_time = LatestAudioTimestamp();
59 
60   // It's possible that the timer may have wrapped around from +BIG_NUM to
61   // -BIG_NUM between the first and second calls, since we're storing
62   // milliseconds in a 32-bit integer. It's not reasonable that the call itself
63   // would have taken more than 2^31 milliseconds though, so look at the
64   // difference and rely on integer overflow to ensure it's accurate.
65   const int32_t time_delta = (second_time - first_time);
66   TF_LITE_MICRO_EXPECT_LE(0, time_delta);
67 }
68 
69 TF_LITE_MICRO_TESTS_END
70