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 3.9 kB

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/type_id.h"
  23. #include "minddata/dataset/core/de_tensor.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. namespace UT {
  68. class Common : public testing::Test {
  69. public:
  70. // every TEST_F macro will enter one
  71. virtual void SetUp();
  72. virtual void TearDown();
  73. };
  74. class DatasetOpTesting : public Common {
  75. public:
  76. std::vector<mindspore::dataset::TensorShape> ToTensorShapeVec(const std::vector<std::vector<int64_t>> &v);
  77. std::vector<mindspore::dataset::DataType> ToDETypes(const std::vector<mindspore::DataType> &t);
  78. mindspore::MSTensor ReadFileToTensor(const std::string &file);
  79. std::string datasets_root_path_;
  80. std::string mindrecord_root_path_;
  81. void SetUp() override;
  82. };
  83. } // namespace UT
  84. #endif // TESTS_UT_CPP_DATASET_COMMON_COMMON_H_