• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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/micro_time.h"
17 
18 #include "tensorflow/lite/micro/testing/micro_test.h"
19 
20 TF_LITE_MICRO_TESTS_BEGIN
21 
TF_LITE_MICRO_TEST(TestBasicTimerFunctionality)22 TF_LITE_MICRO_TEST(TestBasicTimerFunctionality) {
23   int32_t ticks_per_second = tflite::ticks_per_second();
24 
25   // Retry enough times to guarantee a tick advance, while not taking too long
26   // to complete.  With 1e6 retries, assuming each loop takes tens of cycles,
27   // this will retry for less than 10 seconds on a 10MHz platform.
28   constexpr int kMaxRetries = 1e6;
29   int start_time = tflite::GetCurrentTimeTicks();
30 
31   if (ticks_per_second != 0) {
32     for (int i = 0; i < kMaxRetries; i++) {
33       if (tflite::GetCurrentTimeTicks() - start_time > 0) {
34         break;
35       }
36     }
37   }
38 
39   // Ensure the timer is increasing. This works for the overflow case too, since
40   // (MIN_INT + x) - (MAX_INT - y) == x + y + 1.  For example,
41   // 0x80000001(min int + 1) - 0x7FFFFFFE(max int - 1) = 0x00000003 == 3.
42   // GetTicksPerSecond() == 0 means the timer is not implemented on this
43   // platform.
44   TF_LITE_MICRO_EXPECT(ticks_per_second == 0 ||
45                        tflite::GetCurrentTimeTicks() - start_time > 0);
46 }
47 
48 TF_LITE_MICRO_TESTS_END
49