From 79445012bdc91d0844cbedddc49774349fafa38d Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 3 Jul 2020 15:28:11 -0400 Subject: [PATCH] Tensor ut works new test case Name change for empty check --- mindspore/ccsrc/dataset/core/tensor.cc | 9 +++++++++ mindspore/ccsrc/dataset/core/tensor.h | 4 ++++ tests/ut/cpp/dataset/tensor_test.cc | 14 ++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/mindspore/ccsrc/dataset/core/tensor.cc b/mindspore/ccsrc/dataset/core/tensor.cc index 8de3425c5b7..ce5aaa5d658 100644 --- a/mindspore/ccsrc/dataset/core/tensor.cc +++ b/mindspore/ccsrc/dataset/core/tensor.cc @@ -513,6 +513,15 @@ const unsigned char *Tensor::GetBuffer() const { return data_; } +// check for empty +bool Tensor::HasData() const { + if (data_ == nullptr) { + return true; + } else { + return false; + } +} + unsigned char *Tensor::GetMutableBuffer() { if (!shape_.known() || type_ == DataType::DE_UNKNOWN) { return nullptr; diff --git a/mindspore/ccsrc/dataset/core/tensor.h b/mindspore/ccsrc/dataset/core/tensor.h index 9fed0bbc975..899098faaf6 100644 --- a/mindspore/ccsrc/dataset/core/tensor.h +++ b/mindspore/ccsrc/dataset/core/tensor.h @@ -277,6 +277,10 @@ class Tensor { // @return const TensorShape &shape() const { return shape_; } + /// Check if tensor has data + /// \return bool - true if tensor is empty + bool HasData() const; + // Reshape the tensor. The given shape should have the same number of elements in the Tensor // @param shape virtual Status Reshape(const TensorShape &shape); diff --git a/tests/ut/cpp/dataset/tensor_test.cc b/tests/ut/cpp/dataset/tensor_test.cc index 1aa3cad2fa1..72181a0caf1 100644 --- a/tests/ut/cpp/dataset/tensor_test.cc +++ b/tests/ut/cpp/dataset/tensor_test.cc @@ -432,3 +432,17 @@ TEST_F(MindDataTestTensorDE, TensorConcatenate) { s = t1->Concatenate({5}, t2); EXPECT_FALSE(s.IsOk()); } + +TEST_F(MindDataTestTensorDE, TensorEmpty) { + std::shared_ptr t = std::make_shared(TensorShape({2, 3}), DataType(DataType::DE_UINT64)); + ASSERT_TRUE(t->HasData()); +} + +TEST_F(MindDataTestTensorDE, TensorEmptyInvalidate) { + std::vector values1 = {1, 2, 3, 0, 0, 0}; + std::shared_ptr t; + Tensor::CreateTensor(&t, values1); + t->Invalidate(); + ASSERT_TRUE(t->HasData()); +} +