1 | // Licensed to the Apache Software Foundation (ASF) under one |
2 | // or more contributor license agreements. See the NOTICE file |
3 | // distributed with this work for additional information |
4 | // regarding copyright ownership. The ASF licenses this file |
5 | // to you under the Apache License, Version 2.0 (the |
6 | // "License"); you may not use this file except in compliance |
7 | // with the License. You may obtain a copy of the License at |
8 | // |
9 | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | // |
11 | // Unless required by applicable law or agreed to in writing, |
12 | // software distributed under the License is distributed on an |
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | // KIND, either express or implied. See the License for the |
15 | // specific language governing permissions and limitations |
16 | // under the License. |
17 | |
18 | #ifndef ARROW_CSV_TEST_COMMON_H |
19 | #define ARROW_CSV_TEST_COMMON_H |
20 | |
21 | #include <memory> |
22 | #include <sstream> |
23 | #include <string> |
24 | #include <vector> |
25 | |
26 | #include "arrow/csv/parser.h" |
27 | #include "arrow/test-util.h" |
28 | |
29 | namespace arrow { |
30 | namespace csv { |
31 | |
32 | std::string MakeCSVData(std::vector<std::string> lines) { |
33 | std::stringstream ss; |
34 | for (const auto& line : lines) { |
35 | ss << line; |
36 | } |
37 | return ss.str(); |
38 | } |
39 | |
40 | // Make a BlockParser from a vector of lines representing a CSV file |
41 | void MakeCSVParser(std::vector<std::string> lines, ParseOptions options, |
42 | std::shared_ptr<BlockParser>* out) { |
43 | auto csv = MakeCSVData(lines); |
44 | auto parser = std::make_shared<BlockParser>(options); |
45 | uint32_t out_size; |
46 | ASSERT_OK(parser->Parse(csv.data(), static_cast<uint32_t>(csv.size()), &out_size)); |
47 | ASSERT_EQ(out_size, csv.size()) << "trailing CSV data not parsed" ; |
48 | *out = parser; |
49 | } |
50 | |
51 | void MakeCSVParser(std::vector<std::string> lines, std::shared_ptr<BlockParser>* out) { |
52 | MakeCSVParser(lines, ParseOptions::Defaults(), out); |
53 | } |
54 | |
55 | // Make a BlockParser from a vector of strings representing a single CSV column |
56 | void MakeColumnParser(std::vector<std::string> items, std::shared_ptr<BlockParser>* out) { |
57 | auto options = ParseOptions::Defaults(); |
58 | // Need this to test for null (empty) values |
59 | options.ignore_empty_lines = false; |
60 | std::vector<std::string> lines; |
61 | for (const auto& item : items) { |
62 | lines.push_back(item + '\n'); |
63 | } |
64 | MakeCSVParser(lines, options, out); |
65 | ASSERT_EQ((*out)->num_cols(), 1) << "Should have seen only 1 CSV column" ; |
66 | ASSERT_EQ((*out)->num_rows(), items.size()); |
67 | } |
68 | |
69 | } // namespace csv |
70 | } // namespace arrow |
71 | |
72 | #endif // ARROW_CSV_TEST_COMMON_H |
73 | |