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/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
TestLogicalOp(const TfLiteRegistration & registration,const int * input1_dims_data,const bool * input1_data,const int * input2_dims_data,const bool * input2_data,const int * output_dims_data,const bool * expected_output_data,bool * output_data)27 void TestLogicalOp(const TfLiteRegistration& registration,
28 const int* input1_dims_data, const bool* input1_data,
29 const int* input2_dims_data, const bool* input2_data,
30 const int* output_dims_data,
31 const bool* expected_output_data, bool* output_data) {
32 TfLiteIntArray* input1_dims = IntArrayFromInts(input1_dims_data);
33 TfLiteIntArray* input2_dims = IntArrayFromInts(input2_dims_data);
34 TfLiteIntArray* output_dims = IntArrayFromInts(output_dims_data);
35 const int output_dims_count = ElementCount(*output_dims);
36
37 constexpr int inputs_size = 2;
38 constexpr int outputs_size = 1;
39 constexpr int tensors_size = inputs_size + outputs_size;
40 TfLiteTensor tensors[tensors_size] = {
41 CreateTensor(input1_data, input1_dims),
42 CreateTensor(input2_data, input2_dims),
43 CreateTensor(output_data, output_dims),
44 };
45
46 int inputs_array_data[] = {2, 0, 1};
47 TfLiteIntArray* inputs_array = IntArrayFromInts(inputs_array_data);
48 int outputs_array_data[] = {1, 2};
49 TfLiteIntArray* outputs_array = IntArrayFromInts(outputs_array_data);
50
51 micro::KernelRunner runner(registration, tensors, tensors_size, inputs_array,
52 outputs_array,
53 /*builtin_data=*/nullptr);
54
55 TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.InitAndPrepare());
56 TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.Invoke());
57
58 TF_LITE_MICRO_EXPECT_EQ(output_dims_count, 4);
59 for (int i = 0; i < output_dims_count; ++i) {
60 TF_LITE_MICRO_EXPECT_EQ(expected_output_data[i], output_data[i]);
61 }
62 }
63
64 } // namespace
65 } // namespace testing
66 } // namespace tflite
67
68 TF_LITE_MICRO_TESTS_BEGIN
69
TF_LITE_MICRO_TEST(LogicalOr)70 TF_LITE_MICRO_TEST(LogicalOr) {
71 const int shape[] = {4, 1, 1, 1, 4};
72 const bool input1[] = {true, false, false, true};
73 const bool input2[] = {true, false, true, false};
74 const bool golden[] = {true, false, true, true};
75 bool output_data[4];
76 tflite::testing::TestLogicalOp(tflite::ops::micro::Register_LOGICAL_OR(),
77 shape, input1, shape, input2, shape, golden,
78 output_data);
79 }
80
TF_LITE_MICRO_TEST(BroadcastLogicalOr)81 TF_LITE_MICRO_TEST(BroadcastLogicalOr) {
82 const int input1_shape[] = {4, 1, 1, 1, 4};
83 const bool input1[] = {true, false, false, true};
84 const int input2_shape[] = {4, 1, 1, 1, 1};
85 const bool input2[] = {false};
86 const bool golden[] = {true, false, false, true};
87 bool output_data[4];
88 tflite::testing::TestLogicalOp(tflite::ops::micro::Register_LOGICAL_OR(),
89 input1_shape, input1, input2_shape, input2,
90 input1_shape, golden, output_data);
91 }
92
TF_LITE_MICRO_TEST(LogicalAnd)93 TF_LITE_MICRO_TEST(LogicalAnd) {
94 const int shape[] = {4, 1, 1, 1, 4};
95 const bool input1[] = {true, false, false, true};
96 const bool input2[] = {true, false, true, false};
97 const bool golden[] = {true, false, false, false};
98 bool output_data[4];
99 tflite::testing::TestLogicalOp(tflite::ops::micro::Register_LOGICAL_AND(),
100 shape, input1, shape, input2, shape, golden,
101 output_data);
102 }
103
TF_LITE_MICRO_TEST(BroadcastLogicalAnd)104 TF_LITE_MICRO_TEST(BroadcastLogicalAnd) {
105 const int input1_shape[] = {4, 1, 1, 1, 4};
106 const bool input1[] = {true, false, false, true};
107 const int input2_shape[] = {4, 1, 1, 1, 1};
108 const bool input2[] = {true};
109 const bool golden[] = {true, false, false, true};
110 bool output_data[4];
111 tflite::testing::TestLogicalOp(tflite::ops::micro::Register_LOGICAL_AND(),
112 input1_shape, input1, input2_shape, input2,
113 input1_shape, golden, output_data);
114 }
115
116 TF_LITE_MICRO_TESTS_END
117