/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ #include #include "tensorflow/contrib/lite/interpreter.h" #include "tensorflow/contrib/lite/kernels/register.h" #include "tensorflow/contrib/lite/kernels/test_util.h" #include "tensorflow/contrib/lite/model.h" namespace tflite { namespace { using ::testing::ElementsAreArray; template class SliceOpModel : public SingleOpModel { public: SliceOpModel(std::initializer_list input_shape, std::initializer_list begin_shape, std::initializer_list size_shape, TensorType tensor_index_type, TensorType tensor_input_type) { input_ = AddInput(tensor_input_type); begin_ = AddInput(tensor_index_type); size_ = AddInput(tensor_index_type); output_ = AddOutput(tensor_input_type); SetBuiltinOp(BuiltinOperator_SLICE, BuiltinOptions_SliceOptions, CreateSliceOptions(builder_).Union()); BuildInterpreter({input_shape, begin_shape, size_shape}); } void SetInput(std::initializer_list data) { PopulateTensor(input_, data); } void SetBegin(std::initializer_list data) { PopulateTensor(begin_, data); } void SetSize(std::initializer_list data) { PopulateTensor(size_, data); } std::vector GetOutput() { return ExtractVector(output_); } std::vector GetOutputShape() { return GetTensorShape(output_); } private: int input_; int begin_; int size_; int output_; }; TEST(SliceOpTest, In1D) { SliceOpModel m({4}, {1}, {1}, TensorType_INT32, TensorType_FLOAT32); m.SetInput({1, 2, 3, 4}); m.SetBegin({1}); m.SetSize({2}); m.Invoke(); EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({2})); EXPECT_THAT(m.GetOutput(), ElementsAreArray({2, 3})); } TEST(SliceOpTest, In2D) { SliceOpModel m({2, 3}, {2}, {2}, TensorType_INT32, TensorType_FLOAT32); m.SetInput({1, 2, 3, 4, 5, 6}); m.SetBegin({1, 0}); m.SetSize({1, 2}); m.Invoke(); EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({1, 2})); EXPECT_THAT(m.GetOutput(), ElementsAreArray({4, 5})); } TEST(SliceOpTest, In3D) { SliceOpModel m({2, 3, 2}, {3}, {4}, TensorType_INT32, TensorType_FLOAT32); m.SetInput({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}); m.SetBegin({0, 0, 0}); m.SetSize({2, 3, 2}); m.Invoke(); EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({2, 3, 2})); EXPECT_THAT(m.GetOutput(), ElementsAreArray({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})); } TEST(SliceOpTest, InputFloat) { SliceOpModel m({4, 1, 1, 1}, {4}, {4}, TensorType_INT32, TensorType_FLOAT32); m.SetInput({1, 2, 3, 4}); m.SetBegin({1, 0, 0, 0}); m.SetSize({3, 1, 1, 1}); m.Invoke(); EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({3, 1, 1, 1})); EXPECT_THAT(m.GetOutput(), ElementsAreArray({2, 3, 4})); } TEST(SliceOpTest, IndexInt64) { SliceOpModel m({4, 1, 1, 1}, {4}, {4}, TensorType_INT64, TensorType_FLOAT32); m.SetInput({1, 2, 3, 4}); m.SetBegin({1, 0, 0, 0}); m.SetSize({3, 1, 1, 1}); m.Invoke(); EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({3, 1, 1, 1})); EXPECT_THAT(m.GetOutput(), ElementsAreArray({2, 3, 4})); } // See these test cases under: // https://www.tensorflow.org/versions/master/api_docs/python/tf/slice TEST(SliceOpTest, InputInteger1) { SliceOpModel m({3, 2, 3, 1}, {4}, {4}, TensorType_INT32, TensorType_INT32); m.SetInput({1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6}); m.SetBegin({1, 0, 0, 0}); m.SetSize({1, 1, 3, 1}); m.Invoke(); EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({1, 1, 3, 1})); EXPECT_THAT(m.GetOutput(), ElementsAreArray({3, 3, 3})); } TEST(SliceOpTest, InputInteger2) { SliceOpModel m({3, 2, 3, 1}, {4}, {4}, TensorType_INT32, TensorType_INT32); m.SetInput({1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6}); m.SetBegin({1, 0, 0, 0}); m.SetSize({1, 2, 3, 1}); m.Invoke(); EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({1, 2, 3, 1})); EXPECT_THAT(m.GetOutput(), ElementsAreArray({3, 3, 3, 4, 4, 4})); } TEST(SliceOpTest, InputInteger3) { SliceOpModel m({3, 2, 3, 1}, {4}, {4}, TensorType_INT32, TensorType_INT32); m.SetInput({1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6}); m.SetBegin({1, 0, 0, 0}); m.SetSize({2, 1, 3, 1}); m.Invoke(); EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({2, 1, 3, 1})); EXPECT_THAT(m.GetOutput(), ElementsAreArray({3, 3, 3, 5, 5, 5})); } TEST(SliceOpTest, SizeMinus1) { SliceOpModel m({3, 2, 3, 1}, {4}, {4}, TensorType_INT32, TensorType_INT32); m.SetInput({1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6}); m.SetBegin({1, 0, 0, 0}); m.SetSize({2, 1, -1, 1}); m.Invoke(); EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({2, 1, 3, 1})); EXPECT_THAT(m.GetOutput(), ElementsAreArray({3, 3, 3, 5, 5, 5})); } } // namespace } // namespace tflite int main(int argc, char** argv) { ::tflite::LogToStderr(); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }