1 /* Copyright 2019 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/c/builtin_op_data.h"
17 #include "tensorflow/lite/c/common.h"
18 #include "tensorflow/lite/micro/all_ops_resolver.h"
19 #include "tensorflow/lite/micro/kernels/kernel_runner.h"
20 #include "tensorflow/lite/micro/test_helpers.h"
21 #include "tensorflow/lite/micro/testing/micro_test.h"
22
23 namespace tflite {
24 namespace testing {
25 namespace {
26
TestNegFloat(int * input_dims_data,const float * input_data,const float * expected_output_data,int * output_dims_data,float * output_data)27 void TestNegFloat(int* input_dims_data, const float* input_data,
28 const float* expected_output_data, int* output_dims_data,
29 float* output_data) {
30 TfLiteIntArray* input_dims = IntArrayFromInts(input_dims_data);
31 TfLiteIntArray* output_dims = IntArrayFromInts(output_dims_data);
32 const int output_dims_count = ElementCount(*output_dims);
33 constexpr int inputs_size = 1;
34 constexpr int outputs_size = 1;
35 constexpr int tensors_size = inputs_size + outputs_size;
36 TfLiteTensor tensors[tensors_size] = {
37 CreateTensor(input_data, input_dims),
38 CreateTensor(output_data, output_dims),
39 };
40
41 int inputs_array_data[] = {1, 0};
42 TfLiteIntArray* inputs_array = IntArrayFromInts(inputs_array_data);
43 int outputs_array_data[] = {1, 1};
44 TfLiteIntArray* outputs_array = IntArrayFromInts(outputs_array_data);
45
46 const TfLiteRegistration registration = tflite::ops::micro::Register_NEG();
47 micro::KernelRunner runner(registration, tensors, tensors_size, inputs_array,
48 outputs_array,
49 /*builtin_data=*/nullptr);
50
51 TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.InitAndPrepare());
52 TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.Invoke());
53
54 TF_LITE_MICRO_EXPECT_EQ(expected_output_data[0], output_data[0]);
55 for (int i = 0; i < output_dims_count; ++i) {
56 TF_LITE_MICRO_EXPECT_EQ(expected_output_data[i], output_data[i]);
57 }
58 }
59
60 } // namespace
61 } // namespace testing
62 } // namespace tflite
63
64 TF_LITE_MICRO_TESTS_BEGIN
65
TF_LITE_MICRO_TEST(NegOpSingleFloat)66 TF_LITE_MICRO_TEST(NegOpSingleFloat) {
67 int dims[] = {1, 2};
68 const float input_data[] = {8.5, 0.0};
69 const float golden[] = {-8.5, 0.0};
70 float output_data[2];
71
72 tflite::testing::TestNegFloat(dims, input_data, golden, dims, output_data);
73 }
74
TF_LITE_MICRO_TEST(NegOpFloat)75 TF_LITE_MICRO_TEST(NegOpFloat) {
76 int dims[] = {2, 2, 3};
77 const float input_data[] = {-2.0f, -1.0f, 0.f, 1.0f, 2.0f, 3.0f};
78 const float golden[] = {2.0f, 1.0f, -0.f, -1.0f, -2.0f, -3.0f};
79 float output_data[6];
80
81 tflite::testing::TestNegFloat(dims, input_data, golden, dims, output_data);
82 }
83
84 TF_LITE_MICRO_TESTS_END
85