1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#include <gtest/gtest.h>
16#include "tensorflow/contrib/lite/interpreter.h"
17#include "tensorflow/contrib/lite/kernels/register.h"
18#include "tensorflow/contrib/lite/kernels/test_util.h"
19#include "tensorflow/contrib/lite/model.h"
20
21namespace tflite {
22namespace {
23
24using ::testing::ElementsAreArray;
25
26class ReshapeOpModel : public SingleOpModel {
27 public:
28  ReshapeOpModel(std::initializer_list<int> input_shape,
29                 std::initializer_list<int> new_shape) {
30    input_ = AddInput(TensorType_FLOAT32);
31    output_ = AddOutput(TensorType_FLOAT32);
32    SetBuiltinOp(
33        BuiltinOperator_RESHAPE, BuiltinOptions_ReshapeOptions,
34        CreateReshapeOptions(builder_, builder_.CreateVector<int>(new_shape))
35            .Union());
36    BuildInterpreter({input_shape});
37  }
38
39  void SetInput(std::initializer_list<float> data) {
40    PopulateTensor<float>(input_, data);
41  }
42  std::vector<float> GetOutput() { return ExtractVector<float>(output_); }
43  std::vector<int> GetOutputShape() { return GetTensorShape(output_); }
44
45 private:
46  int input_;
47  int output_;
48};
49
50TEST(ReshapeOpTest, MismatchedDimensions) {
51  EXPECT_DEATH(ReshapeOpModel({1, 2, 4, 1}, {2, 1}),
52               "num_input_elements != num_output_elements");
53}
54
55TEST(ReshapeOpTest, TooManyDimensions) {
56  EXPECT_DEATH(
57      ReshapeOpModel({1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9}),
58      "Found too many dimensions");
59}
60
61TEST(ReshapeOpTest, TooManySpecialDimensions) {
62  EXPECT_DEATH(ReshapeOpModel({1, 2, 4, 1}, {-1, -1, 2, 4}),
63               "strech_dim != -1");
64}
65
66TEST(ReshapeOpTest, SimpleTest) {
67  ReshapeOpModel m({1, 2, 4, 1}, {2, 2, 2});
68  m.SetInput({1, 2, 3, 4, 5, 6, 7, 8});
69  m.Invoke();
70  EXPECT_THAT(m.GetOutput(), ElementsAreArray({1, 2, 3, 4, 5, 6, 7, 8}));
71  EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({2, 2, 2}));
72}
73
74TEST(ReshapeOpTest, WithStretchDimension) {
75  ReshapeOpModel m({1, 2, 4, 1}, {2, 1, -1});
76  m.SetInput({1, 2, 3, 4, 5, 6, 7, 8});
77  m.Invoke();
78  EXPECT_THAT(m.GetOutput(), ElementsAreArray({1, 2, 3, 4, 5, 6, 7, 8}));
79  EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({2, 1, 4}));
80}
81
82}  // namespace
83}  // namespace tflite
84
85int main(int argc, char** argv) {
86  ::tflite::LogToStderr();
87  ::testing::InitGoogleTest(&argc, argv);
88  return RUN_ALL_TESTS();
89}
90