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 <type_traits>
17 
18 #include "tensorflow/lite/c/builtin_op_data.h"
19 #include "tensorflow/lite/c/common.h"
20 #include "tensorflow/lite/micro/kernels/kernel_runner.h"
21 #include "tensorflow/lite/micro/test_helpers.h"
22 #include "tensorflow/lite/micro/testing/micro_test.h"
23 
24 namespace tflite {
25 namespace testing {
26 namespace {
27 
ExecuteFloorModTest(TfLiteTensor * tensors,int tensors_count)28 void ExecuteFloorModTest(TfLiteTensor* tensors, int tensors_count) {
29   int kInputArrayData[] = {2, 0, 1};
30   TfLiteIntArray* inputs_array = IntArrayFromInts(kInputArrayData);
31   int kOutputArrayData[] = {1, 2};
32   TfLiteIntArray* outputs_array = IntArrayFromInts(kOutputArrayData);
33 
34   const TfLiteRegistration registration = tflite::Register_FLOOR_MOD();
35   micro::KernelRunner runner(registration, tensors, tensors_count, inputs_array,
36                              outputs_array, nullptr);
37 
38   TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.InitAndPrepare());
39   TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.Invoke());
40 }
41 
42 template <typename T>
TestFloorMod(int * input1_dims_data,const T * input1_data,int * input2_dims_data,const T * input2_data,int * expected_dims,const T * expected_data,T * output_data)43 void TestFloorMod(int* input1_dims_data, const T* input1_data,
44                   int* input2_dims_data, const T* input2_data,
45                   int* expected_dims, const T* expected_data, T* output_data) {
46   TfLiteIntArray* input1_dims = IntArrayFromInts(input1_dims_data);
47   TfLiteIntArray* input2_dims = IntArrayFromInts(input2_dims_data);
48   TfLiteIntArray* output_dims = IntArrayFromInts(expected_dims);
49   const int output_count = ElementCount(*output_dims);
50 
51   TfLiteTensor tensors[] = {
52       CreateTensor(input1_data, input1_dims),
53       CreateTensor(input2_data, input2_dims),
54       CreateTensor(output_data, output_dims),
55   };
56   constexpr int tensors_count = std::extent<decltype(tensors)>::value;
57 
58   ExecuteFloorModTest(tensors, tensors_count);
59 
60   for (int i = 0; i < output_count; i++) {
61     TF_LITE_MICRO_EXPECT_EQ(expected_data[i], output_data[i]);
62   }
63 }
64 
65 }  // namespace
66 }  // namespace testing
67 }  // namespace tflite
68 
69 TF_LITE_MICRO_TESTS_BEGIN
70 
TF_LITE_MICRO_TEST(FloorModFloatSimple)71 TF_LITE_MICRO_TEST(FloorModFloatSimple) {
72   int kDims[] = {4, 1, 2, 2, 1};
73   constexpr float kInput1[] = {10, 9, 11, 3};
74   constexpr float kInput2[] = {2, 2, 3, 4};
75   constexpr float kExpect[] = {0, 1, 2, 3};
76   constexpr int kOutputCount = std::extent<decltype(kExpect)>::value;
77   float output_data[kOutputCount];
78 
79   tflite::testing::TestFloorMod(kDims, kInput1, kDims, kInput2, kDims, kExpect,
80                                 output_data);
81 }
82 
TF_LITE_MICRO_TEST(FloorModFloatNegativeValue)83 TF_LITE_MICRO_TEST(FloorModFloatNegativeValue) {
84   int kDims[] = {4, 1, 2, 2, 1};
85   constexpr float kInput1[] = {10, -9, -11, 7};
86   constexpr float kInput2[] = {2, 2, -3, -4};
87   constexpr float kExpect[] = {0, 1, -2, -1};
88   constexpr int kOutputCount = std::extent<decltype(kExpect)>::value;
89   float output_data[kOutputCount];
90 
91   tflite::testing::TestFloorMod(kDims, kInput1, kDims, kInput2, kDims, kExpect,
92                                 output_data);
93 }
94 
TF_LITE_MICRO_TEST(FloorModFloatBroadcast)95 TF_LITE_MICRO_TEST(FloorModFloatBroadcast) {
96   int kDims1[] = {4, 1, 2, 2, 1};
97   int kDims2[] = {1, 1};
98   constexpr float kInput1[] = {10, -9, -11, 7};
99   constexpr float kInput2[] = {-3};
100   constexpr float kExpect[] = {-2, 0, -2, -2};
101   constexpr int kOutputCount = std::extent<decltype(kExpect)>::value;
102   float output_data[kOutputCount];
103 
104   tflite::testing::TestFloorMod(kDims1, kInput1, kDims2, kInput2, kDims1,
105                                 kExpect, output_data);
106 }
107 
108 TF_LITE_MICRO_TESTS_END
109