• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <gtest/gtest.h>
16 #include "tensorflow/lite/interpreter.h"
17 #include "tensorflow/lite/kernels/register.h"
18 #include "tensorflow/lite/kernels/test_util.h"
19 #include "tensorflow/lite/model.h"
20 
21 namespace tflite {
22 namespace {
23 
24 using ::testing::ElementsAreArray;
25 
26 class ZerosLikeOpModel : public SingleOpModel {
27  public:
ZerosLikeOpModel(const TensorData & input)28   explicit ZerosLikeOpModel(const TensorData& input) {
29     input_ = AddInput(input);
30     output_ = AddOutput(input);
31     SetBuiltinOp(BuiltinOperator_ZEROS_LIKE, BuiltinOptions_ZerosLikeOptions,
32                  CreateZerosLikeOptions(builder_).Union());
33     BuildInterpreter({GetShape(input_)});
34   }
35 
input()36   int input() { return input_; }
output()37   int output() { return output_; }
38 
39  protected:
40   int input_;
41   int output_;
42 };
43 
TEST(ZerosLikeOpModel,ZerosLikeFloat)44 TEST(ZerosLikeOpModel, ZerosLikeFloat) {
45   ZerosLikeOpModel m({TensorType_FLOAT32, {2, 3}});
46   m.PopulateTensor<float>(m.input(), {-2.0, -1.0, 0.0, 1.0, 2.0, 3.0});
47   m.Invoke();
48   EXPECT_THAT(m.ExtractVector<float>(m.output()),
49               ElementsAreArray({0.0, 0.0, 0.0, 0.0, 0.0, 0.0}));
50   EXPECT_THAT(m.GetTensorShape(m.output()), ElementsAreArray({2, 3}));
51 }
52 
TEST(ZerosLikeOpModel,ZerosLikeInt32)53 TEST(ZerosLikeOpModel, ZerosLikeInt32) {
54   ZerosLikeOpModel m({TensorType_INT32, {1, 2, 2, 1}});
55   m.PopulateTensor<int32_t>(m.input(), {-2, -1, 0, 3});
56   m.Invoke();
57   EXPECT_THAT(m.ExtractVector<int32_t>(m.output()),
58               ElementsAreArray({0, 0, 0, 0}));
59   EXPECT_THAT(m.GetTensorShape(m.output()), ElementsAreArray({1, 2, 2, 1}));
60 }
61 
TEST(ZerosLikeOpModel,ZerosLikeInt64)62 TEST(ZerosLikeOpModel, ZerosLikeInt64) {
63   ZerosLikeOpModel m({TensorType_INT64, {1, 2, 2, 1}});
64   m.PopulateTensor<int64_t>(m.input(), {-2, -1, 0, 3});
65   m.Invoke();
66   EXPECT_THAT(m.ExtractVector<int64_t>(m.output()),
67               ElementsAreArray({0, 0, 0, 0}));
68   EXPECT_THAT(m.GetTensorShape(m.output()), ElementsAreArray({1, 2, 2, 1}));
69 }
70 
71 }  // namespace
72 }  // namespace tflite
73 
main(int argc,char ** argv)74 int main(int argc, char** argv) {
75   ::tflite::LogToStderr();
76   ::testing::InitGoogleTest(&argc, argv);
77   return RUN_ALL_TESTS();
78 }
79