You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.h 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * Copyright 2019-2021 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. #ifndef TESTS_UT_CPP_DATASET_COMMON_COMMON_H_
  17. #define TESTS_UT_CPP_DATASET_COMMON_COMMON_H_
  18. #include "gtest/gtest.h"
  19. #include "include/api/status.h"
  20. #include "include/api/types.h"
  21. #include "minddata/dataset/core/tensor_shape.h"
  22. #include "minddata/dataset/core/de_tensor.h"
  23. #include "minddata/dataset/core/type_id.h"
  24. #include "utils/log_adapter.h"
  25. using mindspore::Status;
  26. using mindspore::StatusCode;
  27. #define ASSERT_OK(_s) \
  28. do { \
  29. Status __rc = (_s); \
  30. if (__rc.IsError()) { \
  31. MS_LOG(ERROR) << __rc.ToString() << "."; \
  32. ASSERT_TRUE(false); \
  33. } \
  34. } while (false)
  35. #define EXPECT_OK(_s) \
  36. do { \
  37. Status __rc = (_s); \
  38. if (__rc.IsError()) { \
  39. MS_LOG(ERROR) << __rc.ToString() << "."; \
  40. EXPECT_TRUE(false); \
  41. } \
  42. } while (false)
  43. #define ASSERT_ERROR(_s) \
  44. do { \
  45. Status __rc = (_s); \
  46. if (__rc.IsOk()) { \
  47. MS_LOG(ERROR) << __rc.ToString() << "."; \
  48. ASSERT_TRUE(false); \
  49. } \
  50. } while (false)
  51. #define EXPECT_ERROR(_s) \
  52. do { \
  53. Status __rc = (_s); \
  54. if (__rc.IsOk()) { \
  55. MS_LOG(ERROR) << __rc.ToString() << "."; \
  56. EXPECT_TRUE(false); \
  57. } \
  58. } while (false)
  59. // Macro to compare 2 MSTensors; compare shape-size, shape and data
  60. #define EXPECT_MSTENSOR_EQ(_mstensor1, _mstensor2) \
  61. do { \
  62. EXPECT_EQ(_mstensor1.Shape().size(), _mstensor2.Shape().size()); \
  63. for (int i = 0; i < _mstensor1.Shape().size(); i++) { \
  64. EXPECT_EQ(_mstensor1.Shape()[i], _mstensor2.Shape()[i]); \
  65. } \
  66. EXPECT_EQ(_mstensor1.DataSize(), _mstensor2.DataSize()); \
  67. EXPECT_EQ(std::memcmp((const void *)_mstensor1.Data().get(), (const void *)_mstensor2.Data().get(), \
  68. _mstensor2.DataSize()), 0); \
  69. } while (false)
  70. // Macro to invoke MS_LOG for MSTensor
  71. #define TEST_MS_LOG_MSTENSOR(_loglevel, _msg, _mstensor) \
  72. do { \
  73. std::shared_ptr<Tensor> _de_tensor; \
  74. ASSERT_OK(Tensor::CreateFromMSTensor(_mstensor, &_de_tensor)); \
  75. MS_LOG(_loglevel) << _msg << *_de_tensor; \
  76. } while (false)
  77. namespace UT {
  78. class Common : public testing::Test {
  79. public:
  80. // every TEST_F macro will enter one
  81. virtual void SetUp();
  82. virtual void TearDown();
  83. };
  84. class DatasetOpTesting : public Common {
  85. public:
  86. std::vector<mindspore::dataset::TensorShape> ToTensorShapeVec(const std::vector<std::vector<int64_t>> &v);
  87. std::vector<mindspore::dataset::DataType> ToDETypes(const std::vector<mindspore::DataType> &t);
  88. mindspore::MSTensor ReadFileToTensor(const std::string &file);
  89. std::string datasets_root_path_;
  90. std::string mindrecord_root_path_;
  91. void SetUp() override;
  92. };
  93. } // namespace UT
  94. #endif // TESTS_UT_CPP_DATASET_COMMON_COMMON_H_