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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 and data
  60. #define EXPECT_MSTENSOR_EQ(_mstensor1, _mstensor2) \
  61. do { \
  62. EXPECT_EQ(_mstensor1.Shape().size(), _mstensor2.Shape().size()); \
  63. EXPECT_EQ(_mstensor1.DataSize(), _mstensor2.DataSize()); \
  64. EXPECT_EQ(std::memcmp((const void *)_mstensor1.Data().get(), (const void *)_mstensor2.Data().get(), \
  65. _mstensor2.DataSize()), 0); \
  66. } while (false)
  67. // Macro to invoke MS_LOG for MSTensor
  68. #define TEST_MS_LOG_MSTENSOR(_loglevel, _msg, _mstensor) \
  69. do { \
  70. std::shared_ptr<Tensor> _de_tensor; \
  71. ASSERT_OK(Tensor::CreateFromMSTensor(_mstensor, &_de_tensor)); \
  72. MS_LOG(_loglevel) << _msg << *_de_tensor; \
  73. } while (false)
  74. namespace UT {
  75. class Common : public testing::Test {
  76. public:
  77. // every TEST_F macro will enter one
  78. virtual void SetUp();
  79. virtual void TearDown();
  80. };
  81. class DatasetOpTesting : public Common {
  82. public:
  83. std::vector<mindspore::dataset::TensorShape> ToTensorShapeVec(const std::vector<std::vector<int64_t>> &v);
  84. std::vector<mindspore::dataset::DataType> ToDETypes(const std::vector<mindspore::DataType> &t);
  85. mindspore::MSTensor ReadFileToTensor(const std::string &file);
  86. std::string datasets_root_path_;
  87. std::string mindrecord_root_path_;
  88. void SetUp() override;
  89. };
  90. } // namespace UT
  91. #endif // TESTS_UT_CPP_DATASET_COMMON_COMMON_H_