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

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/text_file_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 MindDataTestTextFileOp : public UT::DatasetOpTesting {
  32. };
  33. TEST_F(MindDataTestTextFileOp, TestTextFileBasic) {
  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_ + "/testTextFileDataset/1.txt";
  38. std::shared_ptr<TextFileOp> op;
  39. TextFileOp::Builder builder;
  40. builder.SetTextFilesList({dataset_path}).SetOpConnectorSize(2);
  41. Status rc = builder.Build(&op);
  42. ASSERT_TRUE(rc.IsOk());
  43. rc = tree->AssociateNode(op);
  44. ASSERT_TRUE(rc.IsOk());
  45. rc = tree->AssignRoot(op);
  46. ASSERT_TRUE(rc.IsOk());
  47. MS_LOG(INFO) << "Launching tree and begin iteration.";
  48. rc = tree->Prepare();
  49. ASSERT_TRUE(rc.IsOk());
  50. rc = tree->Launch();
  51. ASSERT_TRUE(rc.IsOk());
  52. // Start the loop of reading tensors from our pipeline
  53. DatasetIterator di(tree);
  54. TensorRow tensor_list;
  55. rc = di.FetchNextTensorRow(&tensor_list);
  56. ASSERT_TRUE(rc.IsOk());
  57. int row_count = 0;
  58. while (!tensor_list.empty()) {
  59. // Display the tensor by calling the printer on it
  60. for (int i = 0; i < tensor_list.size(); i++) {
  61. std::ostringstream ss;
  62. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  63. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  64. }
  65. rc = di.FetchNextTensorRow(&tensor_list);
  66. ASSERT_TRUE(rc.IsOk());
  67. row_count++;
  68. }
  69. ASSERT_EQ(row_count, 3);
  70. }
  71. TEST_F(MindDataTestTextFileOp, TestTextFileFileNotExist) {
  72. // Start with an empty execution tree
  73. auto tree = std::make_shared<ExecutionTree>();
  74. std::string dataset_path = datasets_root_path_ + "/does/not/exist/0.txt";
  75. std::shared_ptr<TextFileOp> op;
  76. TextFileOp::Builder builder;
  77. builder.SetTextFilesList({dataset_path}).SetOpConnectorSize(2);
  78. Status rc = builder.Build(&op);
  79. ASSERT_TRUE(rc.IsOk());
  80. }
  81. TEST_F(MindDataTestTextFileOp, TestTotalRows) {
  82. std::string tf_file1 = datasets_root_path_ + "/testTextFileDataset/1.txt";
  83. std::string tf_file2 = datasets_root_path_ + "/testTextFileDataset/2.txt";
  84. std::vector<std::string> files;
  85. files.push_back(tf_file1);
  86. int64_t total_rows = 0;
  87. TextFileOp::CountAllFileRows(files, &total_rows);
  88. ASSERT_EQ(total_rows, 3);
  89. files.clear();
  90. files.push_back(tf_file2);
  91. TextFileOp::CountAllFileRows(files, &total_rows);
  92. ASSERT_EQ(total_rows, 2);
  93. files.clear();
  94. files.push_back(tf_file1);
  95. files.push_back(tf_file2);
  96. TextFileOp::CountAllFileRows(files, &total_rows);
  97. ASSERT_EQ(total_rows, 5);
  98. files.clear();
  99. }
  100. TEST_F(MindDataTestTextFileOp, TestTotalRowsFileNotExist) {
  101. std::string tf_file1 = datasets_root_path_ + "/does/not/exist/0.txt";
  102. std::vector<std::string> files;
  103. files.push_back(tf_file1);
  104. int64_t total_rows = 0;
  105. TextFileOp::CountAllFileRows(files, &total_rows);
  106. ASSERT_EQ(total_rows, 0);
  107. }