/** * Copyright 2020 Huawei Technologies Co., Ltd * * 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 "common/common.h" #include "minddata/dataset/kernels/data/fill_op.h" #include "utils/log_adapter.h" using namespace mindspore::dataset; using mindspore::LogStream; using mindspore::ExceptionType::NoExceptionType; using mindspore::MsLogLevel::INFO; class MindDataTestFillOp : public UT::Common { protected: MindDataTestFillOp() {} }; TEST_F(MindDataTestFillOp, TestOp) { MS_LOG(INFO) << "Doing MindDataTestFillOp-TestOp."; std::vector labels = {1, 1, 2}; std::shared_ptr input; Tensor::CreateFromVector(labels, &input); std::shared_ptr fill_tensor; Tensor::CreateScalar(4, &fill_tensor); std::shared_ptr output; std::unique_ptr op(new FillOp(fill_tensor)); Status s = op->Compute(input, &output); std::vector out = {4, 4, 4}; std::shared_ptr expected; Tensor::CreateFromVector(out, &expected); EXPECT_TRUE(s.IsOk()); ASSERT_TRUE(output->shape() == expected->shape()); ASSERT_TRUE(output->type() == expected->type()); MS_LOG(DEBUG) << *output << std::endl; MS_LOG(DEBUG) << *expected << std::endl; ASSERT_TRUE(*output == *expected); MS_LOG(INFO) << "MindDataTestFillOp-TestOp end."; } TEST_F(MindDataTestFillOp, TestCasting) { MS_LOG(INFO) << "Doing MindDataTestFillOp-TestCasting."; std::vector labels = {0, 1, 2}; std::shared_ptr input; Tensor::CreateFromVector(labels, &input); std::shared_ptr fill_tensor; Tensor::CreateScalar(2.0, &fill_tensor); std::shared_ptr output; std::unique_ptr op(new FillOp(fill_tensor)); Status s = op->Compute(input, &output); std::vector out = {2, 2, 2}; std::shared_ptr expected; Tensor::CreateFromVector(out, &expected); ASSERT_TRUE(output->shape() == expected->shape()); ASSERT_TRUE(output->type() == expected->type()); EXPECT_TRUE(s.IsOk()); MS_LOG(DEBUG) << *output << std::endl; MS_LOG(DEBUG) << *expected << std::endl; ASSERT_TRUE(*output == *expected); MS_LOG(INFO) << "MindDataTestFillOp-TestCasting end."; } TEST_F(MindDataTestFillOp, ScalarFill) { MS_LOG(INFO) << "Doing MindDataTestFillOp-ScalarFill."; std::vector labels = {0, 1, 2}; std::shared_ptr input; Tensor::CreateFromVector(labels, &input); TensorShape fill_shape({2}); std::vector fill_labels = {0, 1}; std::shared_ptr fill_tensor; Tensor::CreateFromVector(fill_labels, &fill_tensor); std::shared_ptr output; std::unique_ptr op(new FillOp(fill_tensor)); Status s = op->Compute(input, &output); EXPECT_TRUE(s.IsError()); ASSERT_TRUE(s.StatusCode() == StatusCode::kMDUnexpectedError); MS_LOG(INFO) << "MindDataTestFillOp-ScalarFill end."; } TEST_F(MindDataTestFillOp, StringFill) { MS_LOG(INFO) << "Doing MindDataTestFillOp-StringFill."; std::vector strings = {"xyzzy", "plugh", "abracadabra"}; std::shared_ptr input; Tensor::CreateFromVector(strings, &input); std::shared_ptr fill_tensor; Tensor::CreateScalar("hello", &fill_tensor); std::shared_ptr output; std::unique_ptr op(new FillOp(fill_tensor)); Status s = op->Compute(input, &output); std::vector expected_strings = {"hello", "hello", "hello"}; std::shared_ptr expected; Tensor::CreateFromVector(expected_strings, &expected); EXPECT_TRUE(s.IsOk()); ASSERT_TRUE(output->shape() == expected->shape()); ASSERT_TRUE(output->type() == expected->type()); MS_LOG(DEBUG) << *output << std::endl; MS_LOG(DEBUG) << *expected << std::endl; ASSERT_TRUE(*output == *expected); MS_LOG(INFO) << "MindDataTestFillOp-StringFill end."; } TEST_F(MindDataTestFillOp, NumericToString) { MS_LOG(INFO) << "Doing MindDataTestFillOp-NumericToString."; std::vector strings = {"xyzzy", "plugh", "abracadabra"}; std::shared_ptr input; Tensor::CreateFromVector(strings, &input); std::shared_ptr fill_tensor; Tensor::CreateScalar(2.0, &fill_tensor); std::shared_ptr output; std::unique_ptr op(new FillOp(fill_tensor)); Status s = op->Compute(input, &output); EXPECT_TRUE(s.IsError()); ASSERT_TRUE(s.StatusCode() == StatusCode::kMDUnexpectedError); MS_LOG(INFO) << "MindDataTestFillOp-NumericToString end."; } TEST_F(MindDataTestFillOp, StringToNumeric) { MS_LOG(INFO) << "Doing MindDataTestFillOp-StringToNumeric."; std::vector labels = {0, 1, 2}; std::shared_ptr input; Tensor::CreateFromVector(labels, &input); std::shared_ptr fill_tensor; Tensor::CreateScalar("hello", &fill_tensor); std::shared_ptr output; std::unique_ptr op(new FillOp(fill_tensor)); Status s = op->Compute(input, &output); EXPECT_TRUE(s.IsError()); ASSERT_TRUE(s.StatusCode() == StatusCode::kMDUnexpectedError); MS_LOG(INFO) << "MindDataTestFillOp-StringToNumeric end."; }