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 NegOpModel : public SingleOpModel {
27  public:
NegOpModel(const TensorData & input,const TensorData & output)28   NegOpModel(const TensorData& input, const TensorData& output) {
29     input_ = AddInput(input);
30     output_ = AddOutput(output);
31     SetBuiltinOp(BuiltinOperator_NEG, BuiltinOptions_NegOptions,
32                  CreateNegOptions(builder_).Union());
33     BuildInterpreter({GetShape(input_)});
34   }
35 
36   template <class T>
SetInput(std::initializer_list<T> data)37   void SetInput(std::initializer_list<T> data) {
38     PopulateTensor<T>(input_, data);
39   }
40 
41   template <class T>
GetOutput()42   std::vector<T> GetOutput() {
43     return ExtractVector<T>(output_);
44   }
45 
46  protected:
47   int input_;
48   int output_;
49 };
50 
TEST(NegOpModel,NegFloat)51 TEST(NegOpModel, NegFloat) {
52   NegOpModel m({TensorType_FLOAT32, {2, 3}}, {TensorType_FLOAT32, {2, 3}});
53   m.SetInput<float>({-2.0f, -1.0f, 0.f, 1.0f, 2.0f, 3.0f});
54   m.Invoke();
55   EXPECT_THAT(m.GetOutput<float>(),
56               ElementsAreArray({2.0f, 1.0f, 0.f, -1.0f, -2.0f, -3.0f}));
57 }
58 
TEST(NegOpModel,NegInt32)59 TEST(NegOpModel, NegInt32) {
60   NegOpModel m({TensorType_INT32, {2, 3}}, {TensorType_INT32, {2, 3}});
61   m.SetInput<int32_t>({-2, -1, 0, 1, 2, 3});
62   m.Invoke();
63   EXPECT_THAT(m.GetOutput<int32_t>(), ElementsAreArray({2, 1, 0, -1, -2, -3}));
64 }
65 
TEST(NegOpModel,NegInt64)66 TEST(NegOpModel, NegInt64) {
67   NegOpModel m({TensorType_INT64, {2, 3}}, {TensorType_INT64, {2, 3}});
68   m.SetInput<int64_t>({-2, -1, 0, 1, 2, 3});
69   m.Invoke();
70   EXPECT_THAT(m.GetOutput<int64_t>(), ElementsAreArray({2, 1, 0, -1, -2, -3}));
71 }
72 
73 }  // namespace
74 }  // namespace tflite
75 
main(int argc,char ** argv)76 int main(int argc, char** argv) {
77   ::tflite::LogToStderr();
78   ::testing::InitGoogleTest(&argc, argv);
79   return RUN_ALL_TESTS();
80 }
81