libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
iterators.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2002 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 #include "stdafx.h"
20 
21 #define BOOST_TEST_MODULE libjmmcg_tests
22 #include <boost/test/included/unit_test.hpp>
23 
24 #include "core/line_iterator.hpp"
25 #include "core/csv_iterator.hpp"
26 
27 #include <algorithm>
28 #include <sstream>
29 #include <vector>
30 
31 using namespace libjmmcg;
32 
33 constexpr char data[]="k, l, m, 10, 11, 12\nd, e, f, 4, 5, 6\nh, i, j, 7, 8, 9\na, b, c, 1, 2, 3\n";
34 
35 BOOST_AUTO_TEST_SUITE(iterator_tests)
36 
37 BOOST_AUTO_TEST_CASE(ctor_eof) {
38  BOOST_CHECK_NO_THROW(line_iterator eof);
39 }
40 
41 BOOST_AUTO_TEST_CASE(ctor_eol) {
42  BOOST_CHECK_NO_THROW(line_iterator eof);
43 }
44 
45 BOOST_AUTO_TEST_CASE(basic) {
46  std::stringstream ss;
47  ss<<data;
48  line_iterator it(ss), eof;
49  std::vector<std::string> data(it, eof);
50 
51  // Sort it to ensure that we are not just pretending.
52  std::sort(data.begin(), data.end());
53 
54  for (auto const &line : data) {
55  BOOST_CHECK(!line.empty());
56  const csv_iterator eol;
57  for (csv_iterator csv_it(line); csv_it!=eol; ++csv_it) {
58  BOOST_CHECK(!csv_it->empty());
59  }
60  }
61 }
62 
63 BOOST_AUTO_TEST_CASE(stream) {
64  std::stringstream ss;
65  ss<<data;
66  line_iterator it(ss), eof;
67  while (it!=eof) {
68  std::string const line=*it++;
69  BOOST_CHECK(!line.empty());
70  const csv_iterator eol;
71  for (csv_iterator csv_it(line); csv_it!=eol; ++csv_it) {
72  BOOST_CHECK(!csv_it->empty());
73  }
74  }
75 }
76 
77 BOOST_AUTO_TEST_CASE(select_2nd_element) {
78  std::stringstream ss;
79  ss<<data;
80  line_iterator it(ss), eof;
81  std::string const line=*it;
82  BOOST_CHECK(!line.empty());
83  const csv_iterator eol;
84  csv_iterator csv_it(line);
85  auto val_iter=std::next(csv_it);
86  BOOST_CHECK_EQUAL(*val_iter, " l");
87 }
88 
89 BOOST_AUTO_TEST_SUITE_END()