1 /** 2 * Copyright 2020 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #include "common/common.h" 17 #include "minddata/dataset/text/kernels/sliding_window_op.h" 18 #include "utils/log_adapter.h" 19 20 using namespace mindspore::dataset; 21 using mindspore::MsLogLevel::INFO; 22 using mindspore::ExceptionType::NoExceptionType; 23 using mindspore::LogStream; 24 25 class MindDataTestSlidingWindowOp : public UT::Common { 26 protected: 27 MindDataTestSlidingWindowOp() {} 28 }; 29 30 TEST_F(MindDataTestSlidingWindowOp, Compute) { 31 MS_LOG(INFO) << "Doing MindDataTestSlidingWindowOp->Compute."; 32 std::vector<std::string> strings = {"one", "two", "three", "four", "five", "six", "seven", "eight"}; 33 TensorShape shape({static_cast<dsize_t>(strings.size())}); 34 std::shared_ptr<Tensor> input; 35 Tensor::CreateFromVector(strings, shape, &input); 36 std::shared_ptr<Tensor> output; 37 38 std::unique_ptr<SlidingWindowOp> op(new SlidingWindowOp(3, 0)); 39 Status s = op->Compute(input, &output); 40 41 std::vector<std::string> out = {"one", "two", "three", "two", "three", "four", "three", "four", "five", 42 "four", "five", "six", "five", "six", "seven", "six", "seven", "eight"}; 43 std::shared_ptr<Tensor> expected; 44 Tensor::CreateFromVector(out, TensorShape({6, 3}), &expected); 45 46 ASSERT_TRUE(output->shape() == expected->shape()); 47 ASSERT_TRUE(output->type() == expected->type()); 48 MS_LOG(DEBUG) << *output << std::endl; 49 MS_LOG(DEBUG) << *expected << std::endl; 50 ASSERT_TRUE(*output == *expected); 51 52 MS_LOG(INFO) << "MindDataTestSlidingWindowOp end."; 53 } 54 55 TEST_F(MindDataTestSlidingWindowOp, OutputShape) { 56 MS_LOG(INFO) << "Doing MindDataTestSlidingWindowOp->OutputShape."; 57 std::vector<std::string> strings = {"one", "two", "three", "four", "five", "six", "seven", "eight"}; 58 TensorShape shape({static_cast<dsize_t>(strings.size())}); 59 std::shared_ptr<Tensor> input; 60 Tensor::CreateFromVector(strings, shape, &input); 61 std::vector<TensorShape> input_shape = {input->shape()}; 62 std::vector<TensorShape> output_shape = {TensorShape({})}; 63 64 std::unique_ptr<SlidingWindowOp> op(new SlidingWindowOp(3, 0)); 65 Status s = op->OutputShape(input_shape, output_shape); 66 67 MS_LOG(DEBUG) << "input_shape" << input_shape[0]; 68 MS_LOG(DEBUG) << "output_shape" << output_shape[0]; 69 ASSERT_TRUE(output_shape[0] == TensorShape({6, 3})); 70 71 MS_LOG(INFO) << "MindDataTestSlidingWindowOp end."; 72 } 73