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::ElementsAre; 25using ::testing::ElementsAreArray; 26 27class SpaceToDepthOpModel : public SingleOpModel { 28 public: 29 SpaceToDepthOpModel(const TensorData& tensor_data, int block_size) { 30 input_ = AddInput(tensor_data); 31 output_ = AddOutput(tensor_data); 32 SetBuiltinOp(BuiltinOperator_SPACE_TO_DEPTH, 33 BuiltinOptions_SpaceToDepthOptions, 34 CreateSpaceToDepthOptions(builder_, block_size).Union()); 35 BuildInterpreter({GetShape(input_)}); 36 } 37 38 template <typename T> 39 void SetInput(std::initializer_list<T> data) { 40 PopulateTensor<T>(input_, data); 41 } 42 template <typename T> 43 std::vector<T> GetOutput() { 44 return ExtractVector<T>(output_); 45 } 46 std::vector<int> GetOutputShape() { return GetTensorShape(output_); } 47 48 private: 49 int input_; 50 int output_; 51}; 52 53TEST(SpaceToDepthOpModel, BadBlockSize) { 54 EXPECT_DEATH(SpaceToDepthOpModel({TensorType_FLOAT32, {1, 2, 2, 1}}, 3), 55 "Cannot allocate tensors"); 56} 57 58TEST(SpaceToDepthOpModel, Float32) { 59 SpaceToDepthOpModel m({TensorType_FLOAT32, {1, 2, 2, 2}}, 2); 60 m.SetInput<float>({1.4, 2.3, 3.2, 4.1, 5.4, 6.3, 7.2, 8.1}); 61 m.Invoke(); 62 EXPECT_THAT(m.GetOutput<float>(), 63 ElementsAreArray({1.4, 2.3, 3.2, 4.1, 5.4, 6.3, 7.2, 8.1})); 64 EXPECT_THAT(m.GetOutputShape(), ElementsAre(1, 1, 1, 8)); 65} 66 67TEST(SpaceToDepthOpModel, Uint8) { 68 SpaceToDepthOpModel m({TensorType_UINT8, {1, 2, 2, 1}}, 2); 69 m.SetInput<uint8_t>({1, 2, 3, 4}); 70 m.Invoke(); 71 EXPECT_THAT(m.GetOutput<uint8_t>(), ElementsAreArray({1, 2, 3, 4})); 72 EXPECT_THAT(m.GetOutputShape(), ElementsAre(1, 1, 1, 4)); 73} 74 75TEST(SpaceToDepthOpModel, Int32) { 76 SpaceToDepthOpModel m({TensorType_INT32, {1, 2, 2, 3}}, 2); 77 m.SetInput<int32_t>({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}); 78 m.Invoke(); 79 EXPECT_THAT(m.GetOutput<int32_t>(), 80 ElementsAreArray({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})); 81 EXPECT_THAT(m.GetOutputShape(), ElementsAre(1, 1, 1, 12)); 82} 83 84TEST(SpaceToDepthOpModel, Int64) { 85 SpaceToDepthOpModel m({TensorType_INT64, {1, 4, 4, 1}}, 2); 86 m.SetInput<int64_t>({1, 2, 5, 6, 3, 4, 7, 8, 9, 10, 13, 14, 11, 12, 15, 16}); 87 m.Invoke(); 88 EXPECT_THAT(m.GetOutput<int64_t>(), 89 ElementsAreArray( 90 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})); 91 EXPECT_THAT(m.GetOutputShape(), ElementsAre(1, 2, 2, 4)); 92} 93 94} // namespace 95} // namespace tflite 96 97int main(int argc, char** argv) { 98 ::tflite::LogToStderr(); 99 ::testing::InitGoogleTest(&argc, argv); 100 return RUN_ALL_TESTS(); 101} 102