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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #include "minddata/dataset/engine/ir/datasetops/dataset_node.h"
  30. using mindspore::Status;
  31. using mindspore::StatusCode;
  32. #define ASSERT_OK(_s) \
  33. do { \
  34. Status __rc = (_s); \
  35. if (__rc.IsError()) { \
  36. MS_LOG(ERROR) << __rc.ToString() << "."; \
  37. ASSERT_TRUE(false); \
  38. } \
  39. } while (false)
  40. #define EXPECT_OK(_s) \
  41. do { \
  42. Status __rc = (_s); \
  43. if (__rc.IsError()) { \
  44. MS_LOG(ERROR) << __rc.ToString() << "."; \
  45. EXPECT_TRUE(false); \
  46. } \
  47. } while (false)
  48. #define ASSERT_ERROR(_s) \
  49. do { \
  50. Status __rc = (_s); \
  51. if (__rc.IsOk()) { \
  52. MS_LOG(ERROR) << __rc.ToString() << "."; \
  53. ASSERT_TRUE(false); \
  54. } \
  55. } while (false)
  56. #define EXPECT_ERROR(_s) \
  57. do { \
  58. Status __rc = (_s); \
  59. if (__rc.IsOk()) { \
  60. MS_LOG(ERROR) << __rc.ToString() << "."; \
  61. EXPECT_TRUE(false); \
  62. } \
  63. } while (false)
  64. // Macro to compare 2 MSTensors; compare shape-size, shape and data
  65. #define EXPECT_MSTENSOR_EQ(_mstensor1, _mstensor2) \
  66. do { \
  67. EXPECT_EQ(_mstensor1.Shape().size(), _mstensor2.Shape().size()); \
  68. for (int i = 0; i < _mstensor1.Shape().size(); i++) { \
  69. EXPECT_EQ(_mstensor1.Shape()[i], _mstensor2.Shape()[i]); \
  70. } \
  71. EXPECT_EQ(_mstensor1.DataSize(), _mstensor2.DataSize()); \
  72. EXPECT_EQ(std::memcmp((const void *)_mstensor1.Data().get(), (const void *)_mstensor2.Data().get(), \
  73. _mstensor2.DataSize()), \
  74. 0); \
  75. } while (false)
  76. // Macro to invoke MS_LOG for MSTensor
  77. #define TEST_MS_LOG_MSTENSOR(_loglevel, _msg, _mstensor) \
  78. do { \
  79. std::shared_ptr<Tensor> _de_tensor; \
  80. ASSERT_OK(Tensor::CreateFromMSTensor(_mstensor, &_de_tensor)); \
  81. MS_LOG(_loglevel) << _msg << *_de_tensor; \
  82. } while (false)
  83. namespace UT {
  84. class Common : public testing::Test {
  85. public:
  86. // every TEST_F macro will enter one
  87. virtual void SetUp();
  88. virtual void TearDown();
  89. };
  90. class DatasetOpTesting : public Common {
  91. public:
  92. // Helper functions for creating datasets
  93. std::shared_ptr<mindspore::dataset::BatchOp> Batch(int32_t batch_size = 1, bool drop = false,
  94. mindspore::dataset::PadInfo = {});
  95. std::shared_ptr<mindspore::dataset::RepeatOp> Repeat(int repeat_cnt = 1);
  96. std::shared_ptr<mindspore::dataset::TFReaderOp> TFReader(std::string file, int num_works = 8);
  97. std::shared_ptr<mindspore::dataset::ExecutionTree> Build(
  98. std::vector<std::shared_ptr<mindspore::dataset::DatasetOp>> ops);
  99. std::vector<mindspore::dataset::TensorShape> ToTensorShapeVec(const std::vector<std::vector<int64_t>> &v);
  100. std::vector<mindspore::dataset::DataType> ToDETypes(const std::vector<mindspore::DataType> &t);
  101. mindspore::MSTensor ReadFileToTensor(const std::string &file);
  102. std::string datasets_root_path_;
  103. std::string mindrecord_root_path_;
  104. void SetUp() override;
  105. };
  106. } // namespace UT
  107. namespace mindspore {
  108. namespace dataset {
  109. // defined in datasets.cc code, and function prototypes added here for UT purposes
  110. // convert MSTensorVec to DE TensorRow, return empty if fails
  111. TensorRow VecToRow(const MSTensorVec &v);
  112. // defined in datasets.cc code, and function prototypes added here for UT purposes
  113. // convert DE TensorRow to MSTensorVec, won't fail
  114. MSTensorVec RowToVec(const TensorRow &v);
  115. MSTensorVec Predicate1(MSTensorVec in);
  116. MSTensorVec Predicate2(MSTensorVec in);
  117. } // namespace dataset
  118. } // namespace mindspore
  119. #endif // TESTS_UT_CPP_DATASET_COMMON_COMMON_H_