xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/kernels/ceil_test.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 <initializer_list>
17 #include <vector>
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include "tensorflow/lite/kernels/test_util.h"
22 #include "tensorflow/lite/schema/schema_generated.h"
23 
24 namespace tflite {
25 namespace {
26 
27 using ::testing::ElementsAreArray;
28 
29 class CeilOpModel : public SingleOpModel {
30  public:
CeilOpModel(std::initializer_list<int> input_shape,TensorType input_type)31   CeilOpModel(std::initializer_list<int> input_shape, TensorType input_type) {
32     input_ = AddInput(TensorType_FLOAT32);
33     output_ = AddOutput(TensorType_FLOAT32);
34     SetBuiltinOp(BuiltinOperator_CEIL, BuiltinOptions_NONE, 0);
35     BuildInterpreter({
36         input_shape,
37     });
38   }
39 
input()40   int input() { return input_; }
41 
GetOutput()42   std::vector<float> GetOutput() { return ExtractVector<float>(output_); }
GetOutputShape()43   std::vector<int> GetOutputShape() { return GetTensorShape(output_); }
44 
45  private:
46   int input_;
47   int output_;
48 };
49 
TEST(CeilOpTest,SingleDim)50 TEST(CeilOpTest, SingleDim) {
51   CeilOpModel model({2}, TensorType_FLOAT32);
52   model.PopulateTensor<float>(model.input(), {8.5, 0.0});
53   ASSERT_EQ(model.Invoke(), kTfLiteOk);
54   EXPECT_THAT(model.GetOutput(), ElementsAreArray({9, 0}));
55   EXPECT_THAT(model.GetOutputShape(), ElementsAreArray({2}));
56 }
57 
TEST(CeilOpTest,MultiDims)58 TEST(CeilOpTest, MultiDims) {
59   CeilOpModel model({2, 1, 1, 5}, TensorType_FLOAT32);
60   model.PopulateTensor<float>(model.input(), {
61                                                  0.0001,
62                                                  8.0001,
63                                                  0.9999,
64                                                  9.9999,
65                                                  0.5,
66                                                  -0.0001,
67                                                  -8.0001,
68                                                  -0.9999,
69                                                  -9.9999,
70                                                  -0.5,
71                                              });
72   ASSERT_EQ(model.Invoke(), kTfLiteOk);
73   EXPECT_THAT(model.GetOutput(),
74               ElementsAreArray({1, 9, 1, 10, 1, 0, -8, 0, -9, 0}));
75   EXPECT_THAT(model.GetOutputShape(), ElementsAreArray({2, 1, 1, 5}));
76 }
77 
78 }  // namespace
79 }  // namespace tflite
80