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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/client.h"
  22. #include "minddata/dataset/core/tensor_shape.h"
  23. #include "minddata/dataset/core/de_tensor.h"
  24. #include "minddata/dataset/core/type_id.h"
  25. #include "utils/log_adapter.h"
  26. #include "minddata/dataset/engine/datasetops/batch_op.h"
  27. #include "minddata/dataset/engine/datasetops/repeat_op.h"
  28. #include "minddata/dataset/engine/datasetops/source/tf_reader_op.h"
  29. using mindspore::Status;
  30. using mindspore::StatusCode;
  31. #define ASSERT_OK(_s) \
  32. do { \
  33. Status __rc = (_s); \
  34. if (__rc.IsError()) { \
  35. MS_LOG(ERROR) << __rc.ToString() << "."; \
  36. ASSERT_TRUE(false); \
  37. } \
  38. } while (false)
  39. #define EXPECT_OK(_s) \
  40. do { \
  41. Status __rc = (_s); \
  42. if (__rc.IsError()) { \
  43. MS_LOG(ERROR) << __rc.ToString() << "."; \
  44. EXPECT_TRUE(false); \
  45. } \
  46. } while (false)
  47. #define ASSERT_ERROR(_s) \
  48. do { \
  49. Status __rc = (_s); \
  50. if (__rc.IsOk()) { \
  51. MS_LOG(ERROR) << __rc.ToString() << "."; \
  52. ASSERT_TRUE(false); \
  53. } \
  54. } while (false)
  55. #define EXPECT_ERROR(_s) \
  56. do { \
  57. Status __rc = (_s); \
  58. if (__rc.IsOk()) { \
  59. MS_LOG(ERROR) << __rc.ToString() << "."; \
  60. EXPECT_TRUE(false); \
  61. } \
  62. } while (false)
  63. // Macro to compare 2 MSTensors; compare shape-size, shape and data
  64. #define EXPECT_MSTENSOR_EQ(_mstensor1, _mstensor2) \
  65. do { \
  66. EXPECT_EQ(_mstensor1.Shape().size(), _mstensor2.Shape().size()); \
  67. for (int i = 0; i < _mstensor1.Shape().size(); i++) { \
  68. EXPECT_EQ(_mstensor1.Shape()[i], _mstensor2.Shape()[i]); \
  69. } \
  70. EXPECT_EQ(_mstensor1.DataSize(), _mstensor2.DataSize()); \
  71. EXPECT_EQ(std::memcmp((const void *)_mstensor1.Data().get(), (const void *)_mstensor2.Data().get(), \
  72. _mstensor2.DataSize()), \
  73. 0); \
  74. } while (false)
  75. // Macro to invoke MS_LOG for MSTensor
  76. #define TEST_MS_LOG_MSTENSOR(_loglevel, _msg, _mstensor) \
  77. do { \
  78. std::shared_ptr<Tensor> _de_tensor; \
  79. ASSERT_OK(Tensor::CreateFromMSTensor(_mstensor, &_de_tensor)); \
  80. MS_LOG(_loglevel) << _msg << *_de_tensor; \
  81. } while (false)
  82. namespace UT {
  83. class Common : public testing::Test {
  84. public:
  85. // every TEST_F macro will enter one
  86. virtual void SetUp();
  87. virtual void TearDown();
  88. };
  89. class DatasetOpTesting : public Common {
  90. public:
  91. // Helper functions for creating datasets
  92. std::shared_ptr<mindspore::dataset::BatchOp> Batch(int32_t batch_size = 1, bool drop = false,
  93. mindspore::dataset::PadInfo = {});
  94. std::shared_ptr<mindspore::dataset::RepeatOp> Repeat(int repeat_cnt = 1);
  95. std::shared_ptr<mindspore::dataset::TFReaderOp> TFReader(std::string file, int num_works = 8);
  96. std::shared_ptr<mindspore::dataset::ExecutionTree> Build(
  97. std::vector<std::shared_ptr<mindspore::dataset::DatasetOp>> ops);
  98. std::vector<mindspore::dataset::TensorShape> ToTensorShapeVec(const std::vector<std::vector<int64_t>> &v);
  99. std::vector<mindspore::dataset::DataType> ToDETypes(const std::vector<mindspore::DataType> &t);
  100. mindspore::MSTensor ReadFileToTensor(const std::string &file);
  101. std::string datasets_root_path_;
  102. std::string mindrecord_root_path_;
  103. void SetUp() override;
  104. };
  105. } // namespace UT
  106. #endif // TESTS_UT_CPP_DATASET_COMMON_COMMON_H_