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.

text_file_op_test.cc 4.2 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Copyright 2020 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. #include <iostream>
  17. #include <memory>
  18. #include <vector>
  19. #include "minddata/dataset/core/client.h"
  20. #include "common/common.h"
  21. #include "utils/ms_utils.h"
  22. #include "gtest/gtest.h"
  23. #include "utils/log_adapter.h"
  24. #include "minddata/dataset/engine/data_schema.h"
  25. #include "minddata/dataset/engine/datasetops/source/text_file_op.h"
  26. #include "minddata/dataset/util/status.h"
  27. namespace common = mindspore::common;
  28. using namespace mindspore::dataset;
  29. using mindspore::LogStream;
  30. using mindspore::ExceptionType::NoExceptionType;
  31. using mindspore::MsLogLevel::INFO;
  32. class MindDataTestTextFileOp : public UT::DatasetOpTesting {};
  33. TEST_F(MindDataTestTextFileOp, TestTextFileBasic) {
  34. // Start with an empty execution tree
  35. auto tree = std::make_shared<ExecutionTree>();
  36. Status rc;
  37. std::string dataset_path;
  38. dataset_path = datasets_root_path_ + "/testTextFileDataset/1.txt";
  39. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  40. int32_t num_workers = 1; // Only one file
  41. int32_t op_connector_size = 2;
  42. int32_t worker_connector_size = config_manager->worker_connector_size();
  43. int64_t total_rows = 0; // read all rows
  44. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  45. rc = schema->AddColumn(ColDescriptor("text", DataType(DataType::DE_UINT8), TensorImpl::kFlexible, 1));
  46. ASSERT_OK(rc);
  47. std::vector<std::string> files = {dataset_path};
  48. bool shuffle_files = false;
  49. int32_t num_devices = 1;
  50. int32_t device_id = 0;
  51. std::shared_ptr<TextFileOp> op =
  52. std::make_shared<TextFileOp>(num_workers, total_rows, worker_connector_size, std::move(schema), files,
  53. op_connector_size, shuffle_files, num_devices, device_id);
  54. rc = op->Init();
  55. ASSERT_OK(rc);
  56. rc = tree->AssociateNode(op);
  57. ASSERT_OK(rc);
  58. rc = tree->AssignRoot(op);
  59. ASSERT_OK(rc);
  60. MS_LOG(INFO) << "Launching tree and begin iteration.";
  61. rc = tree->Prepare();
  62. ASSERT_OK(rc);
  63. rc = tree->Launch();
  64. ASSERT_OK(rc);
  65. // Start the loop of reading tensors from our pipeline
  66. DatasetIterator di(tree);
  67. TensorRow tensor_list;
  68. rc = di.FetchNextTensorRow(&tensor_list);
  69. ASSERT_OK(rc);
  70. int row_count = 0;
  71. while (!tensor_list.empty()) {
  72. // Display the tensor by calling the printer on it
  73. for (int i = 0; i < tensor_list.size(); i++) {
  74. std::ostringstream ss;
  75. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  76. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  77. }
  78. rc = di.FetchNextTensorRow(&tensor_list);
  79. ASSERT_OK(rc);
  80. row_count++;
  81. }
  82. ASSERT_EQ(row_count, 3);
  83. }
  84. TEST_F(MindDataTestTextFileOp, TestTotalRows) {
  85. std::string tf_file1 = datasets_root_path_ + "/testTextFileDataset/1.txt";
  86. std::string tf_file2 = datasets_root_path_ + "/testTextFileDataset/2.txt";
  87. std::vector<std::string> files;
  88. files.push_back(tf_file1);
  89. int64_t total_rows = 0;
  90. TextFileOp::CountAllFileRows(files, &total_rows);
  91. ASSERT_EQ(total_rows, 3);
  92. files.clear();
  93. files.push_back(tf_file2);
  94. TextFileOp::CountAllFileRows(files, &total_rows);
  95. ASSERT_EQ(total_rows, 2);
  96. files.clear();
  97. files.push_back(tf_file1);
  98. files.push_back(tf_file2);
  99. TextFileOp::CountAllFileRows(files, &total_rows);
  100. ASSERT_EQ(total_rows, 5);
  101. files.clear();
  102. }
  103. TEST_F(MindDataTestTextFileOp, TestTotalRowsFileNotExist) {
  104. std::string tf_file1 = datasets_root_path_ + "/does/not/exist/0.txt";
  105. std::vector<std::string> files;
  106. files.push_back(tf_file1);
  107. int64_t total_rows = 0;
  108. TextFileOp::CountAllFileRows(files, &total_rows);
  109. ASSERT_EQ(total_rows, 0);
  110. }