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.

csv_op_test.cc 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/datasetops/source/csv_op.h"
  25. #include "minddata/dataset/util/status.h"
  26. namespace common = mindspore::common;
  27. using namespace mindspore::dataset;
  28. using mindspore::MsLogLevel::INFO;
  29. using mindspore::ExceptionType::NoExceptionType;
  30. using mindspore::LogStream;
  31. class MindDataTestCSVOp : public UT::DatasetOpTesting {
  32. };
  33. TEST_F(MindDataTestCSVOp, TestCSVBasic) {
  34. // Start with an empty execution tree
  35. auto tree = std::make_shared<ExecutionTree>();
  36. std::string dataset_path;
  37. dataset_path = datasets_root_path_ + "/testCSV/1.csv";
  38. std::vector<std::shared_ptr<CsvOp::BaseRecord>> column_default_list;
  39. column_default_list.push_back(std::make_shared<CsvOp::Record<int>>(CsvOp::INT, 0));
  40. column_default_list.push_back(std::make_shared<CsvOp::Record<int>>(CsvOp::INT, 0));
  41. column_default_list.push_back(std::make_shared<CsvOp::Record<int>>(CsvOp::INT, 0));
  42. column_default_list.push_back(std::make_shared<CsvOp::Record<int>>(CsvOp::INT, 0));
  43. std::shared_ptr<CsvOp> op;
  44. CsvOp::Builder builder;
  45. builder.SetCsvFilesList({dataset_path})
  46. .SetRowsPerBuffer(16)
  47. .SetShuffleFiles(false)
  48. .SetOpConnectorSize(2)
  49. .SetFieldDelim(',')
  50. .SetColumDefault(column_default_list)
  51. .SetColumName({"col1", "col2", "col3", "col4"});
  52. Status rc = builder.Build(&op);
  53. ASSERT_TRUE(rc.IsOk());
  54. rc = tree->AssociateNode(op);
  55. ASSERT_TRUE(rc.IsOk());
  56. rc = tree->AssignRoot(op);
  57. ASSERT_TRUE(rc.IsOk());
  58. MS_LOG(INFO) << "Launching tree and begin iteration.";
  59. rc = tree->Prepare();
  60. ASSERT_TRUE(rc.IsOk());
  61. rc = tree->Launch();
  62. ASSERT_TRUE(rc.IsOk());
  63. // Start the loop of reading tensors from our pipeline
  64. DatasetIterator di(tree);
  65. TensorRow tensor_list;
  66. rc = di.FetchNextTensorRow(&tensor_list);
  67. ASSERT_TRUE(rc.IsOk());
  68. int row_count = 0;
  69. while (!tensor_list.empty()) {
  70. // Display the tensor by calling the printer on it
  71. for (int i = 0; i < tensor_list.size(); i++) {
  72. std::ostringstream ss;
  73. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  74. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  75. }
  76. rc = di.FetchNextTensorRow(&tensor_list);
  77. ASSERT_TRUE(rc.IsOk());
  78. row_count++;
  79. }
  80. ASSERT_EQ(row_count, 3);
  81. }
  82. TEST_F(MindDataTestCSVOp, TestTotalRows) {
  83. std::string csv_file1 = datasets_root_path_ + "/testCSV/1.csv";
  84. std::string csv_file2 = datasets_root_path_ + "/testCSV/size.csv";
  85. std::vector<std::string> files;
  86. files.push_back(csv_file1);
  87. int64_t total_rows = 0;
  88. CsvOp::CountAllFileRows(files, false, &total_rows);
  89. ASSERT_EQ(total_rows, 3);
  90. files.clear();
  91. files.push_back(csv_file2);
  92. CsvOp::CountAllFileRows(files, false, &total_rows);
  93. ASSERT_EQ(total_rows, 5);
  94. files.clear();
  95. files.push_back(csv_file1);
  96. files.push_back(csv_file2);
  97. CsvOp::CountAllFileRows(files, false, &total_rows);
  98. ASSERT_EQ(total_rows, 8);
  99. files.clear();
  100. }