4349 |
19 Jul 23 |
peter |
// $Id$ |
4349 |
19 Jul 23 |
peter |
2 |
|
4349 |
19 Jul 23 |
peter |
3 |
/* |
4349 |
19 Jul 23 |
peter |
Copyright (C) 2023 Peter Johansson |
4349 |
19 Jul 23 |
peter |
5 |
|
4349 |
19 Jul 23 |
peter |
This file is part of the yat library, https://dev.thep.lu.se/yat |
4349 |
19 Jul 23 |
peter |
7 |
|
4349 |
19 Jul 23 |
peter |
The yat library is free software; you can redistribute it and/or |
4349 |
19 Jul 23 |
peter |
modify it under the terms of the GNU General Public License as |
4349 |
19 Jul 23 |
peter |
published by the Free Software Foundation; either version 3 of the |
4349 |
19 Jul 23 |
peter |
License, or (at your option) any later version. |
4349 |
19 Jul 23 |
peter |
12 |
|
4349 |
19 Jul 23 |
peter |
The yat library is distributed in the hope that it will be useful, |
4349 |
19 Jul 23 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4349 |
19 Jul 23 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
4349 |
19 Jul 23 |
peter |
General Public License for more details. |
4349 |
19 Jul 23 |
peter |
17 |
|
4349 |
19 Jul 23 |
peter |
You should have received a copy of the GNU General Public License |
4349 |
19 Jul 23 |
peter |
along with yat. If not, see <https://www.gnu.org/licenses/>. |
4349 |
19 Jul 23 |
peter |
20 |
*/ |
4349 |
19 Jul 23 |
peter |
21 |
|
4349 |
19 Jul 23 |
peter |
22 |
#include <config.h> |
4349 |
19 Jul 23 |
peter |
23 |
|
4349 |
19 Jul 23 |
peter |
24 |
#include "Suite.h" |
4349 |
19 Jul 23 |
peter |
25 |
|
4349 |
19 Jul 23 |
peter |
26 |
#include <yat/utility/GetlineIterator.h> |
4349 |
19 Jul 23 |
peter |
27 |
#include <yat/utility/split.h> |
4349 |
19 Jul 23 |
peter |
28 |
#include <yat/utility/stl_utility.h> |
4349 |
19 Jul 23 |
peter |
29 |
|
4349 |
19 Jul 23 |
peter |
30 |
#include <boost/concept_archetype.hpp> |
4349 |
19 Jul 23 |
peter |
31 |
|
4349 |
19 Jul 23 |
peter |
32 |
using namespace theplu::yat; |
4349 |
19 Jul 23 |
peter |
33 |
|
4349 |
19 Jul 23 |
peter |
34 |
int main(int argc, char* argv[]) |
4349 |
19 Jul 23 |
peter |
35 |
{ |
4349 |
19 Jul 23 |
peter |
36 |
test::Suite suite(argc, argv); |
4349 |
19 Jul 23 |
peter |
37 |
|
4349 |
19 Jul 23 |
peter |
// test actual behaviour |
4349 |
19 Jul 23 |
peter |
39 |
std::string str("alpha\nbeta\ngamma\nalpha"); |
4349 |
19 Jul 23 |
peter |
40 |
|
4349 |
19 Jul 23 |
peter |
41 |
suite.out() << "=== testing random access iterator ===\n"; |
4349 |
19 Jul 23 |
peter |
42 |
std::vector<std::string> vec; |
4349 |
19 Jul 23 |
peter |
43 |
utility::split(vec, str, '\n'); |
4349 |
19 Jul 23 |
peter |
44 |
auto it = utility::is_sorted_until(vec.begin(), vec.end()); |
4349 |
19 Jul 23 |
peter |
45 |
int d = std::distance(it, vec.end()); |
4349 |
19 Jul 23 |
peter |
46 |
if (d != 1) { |
4349 |
19 Jul 23 |
peter |
47 |
suite.add(false); |
4349 |
19 Jul 23 |
peter |
48 |
suite.err() << "error: it: distance: " << d << "\n"; |
4349 |
19 Jul 23 |
peter |
49 |
if (it != vec.end()) |
4349 |
19 Jul 23 |
peter |
50 |
suite.err() << "value: " << *it << "\n"; |
4349 |
19 Jul 23 |
peter |
51 |
} |
4349 |
19 Jul 23 |
peter |
52 |
|
4349 |
19 Jul 23 |
peter |
53 |
auto it2 = utility::is_sorted_until(vec.begin(), vec.end(), |
4349 |
19 Jul 23 |
peter |
54 |
std::greater<std::string>()); |
4349 |
19 Jul 23 |
peter |
55 |
int d2 = std::distance(it2, vec.end()); |
4349 |
19 Jul 23 |
peter |
56 |
if (d2 != 3) { |
4349 |
19 Jul 23 |
peter |
57 |
suite.add(false); |
4349 |
19 Jul 23 |
peter |
58 |
suite.err() << "error: it2: distance: " << d2 << "\n"; |
4349 |
19 Jul 23 |
peter |
59 |
if (it2 != vec.end()) |
4349 |
19 Jul 23 |
peter |
60 |
suite.err() << "value: " << *it2 << "\n"; |
4349 |
19 Jul 23 |
peter |
61 |
} |
4349 |
19 Jul 23 |
peter |
62 |
|
4349 |
19 Jul 23 |
peter |
63 |
suite.out() << "=== testingh input iterator ===\n"; |
4349 |
19 Jul 23 |
peter |
64 |
std::stringstream ss(str); |
4349 |
19 Jul 23 |
peter |
65 |
utility::GetlineIterator begin(ss); |
4349 |
19 Jul 23 |
peter |
66 |
utility::GetlineIterator end; |
4349 |
19 Jul 23 |
peter |
67 |
auto it3 = utility::is_sorted_until(begin, end); |
4349 |
19 Jul 23 |
peter |
68 |
if (it3 != end) |
4349 |
19 Jul 23 |
peter |
69 |
suite.out() << "value 3: " << *it3 << "\n"; |
4349 |
19 Jul 23 |
peter |
70 |
int d3 = std::distance(it3, end); |
4349 |
19 Jul 23 |
peter |
71 |
if (d3 != 1) { |
4349 |
19 Jul 23 |
peter |
72 |
suite.add(false); |
4349 |
19 Jul 23 |
peter |
73 |
suite.err() << "error: it3: distance: " << d3 << "\n"; |
4349 |
19 Jul 23 |
peter |
74 |
} |
4349 |
19 Jul 23 |
peter |
75 |
|
4349 |
19 Jul 23 |
peter |
76 |
std::stringstream ss2(str); |
4349 |
19 Jul 23 |
peter |
77 |
utility::GetlineIterator begin2(ss2); |
4349 |
19 Jul 23 |
peter |
78 |
auto it4 = |
4349 |
19 Jul 23 |
peter |
79 |
utility::is_sorted_until(begin2, end, std::greater<std::string>()); |
4349 |
19 Jul 23 |
peter |
80 |
if (it4 != end) |
4349 |
19 Jul 23 |
peter |
81 |
suite.out() << "value 4: " << *it4 << "\n"; |
4349 |
19 Jul 23 |
peter |
82 |
int d4 = std::distance(it4, end); |
4349 |
19 Jul 23 |
peter |
83 |
if (d4 != 3) { |
4349 |
19 Jul 23 |
peter |
84 |
suite.add(false); |
4349 |
19 Jul 23 |
peter |
85 |
suite.err() << "error: it4: distance: " << d4 << "\n"; |
4349 |
19 Jul 23 |
peter |
86 |
} |
4349 |
19 Jul 23 |
peter |
87 |
|
4349 |
19 Jul 23 |
peter |
// test empty range |
4349 |
19 Jul 23 |
peter |
89 |
auto it5 = utility::is_sorted_until(end, end); |
4349 |
19 Jul 23 |
peter |
90 |
if (it5 != end) { |
4349 |
19 Jul 23 |
peter |
91 |
suite.add(false); |
4349 |
19 Jul 23 |
peter |
92 |
suite.err() << "it5 incorrect\n"; |
4349 |
19 Jul 23 |
peter |
93 |
} |
4349 |
19 Jul 23 |
peter |
94 |
|
4349 |
19 Jul 23 |
peter |
// avoid running pure compilation tests |
4349 |
19 Jul 23 |
peter |
96 |
if (false) { |
4349 |
19 Jul 23 |
peter |
97 |
typedef boost::copy_constructible_archetype<> ValueType; |
4349 |
19 Jul 23 |
peter |
98 |
typedef boost::input_iterator_archetype<ValueType> iterator; |
4349 |
19 Jul 23 |
peter |
99 |
typedef boost::binary_predicate_archetype<ValueType, ValueType> Compare; |
4349 |
19 Jul 23 |
peter |
100 |
boost::detail::dummy_constructor arg; |
4349 |
19 Jul 23 |
peter |
101 |
Compare comp(arg); |
4349 |
19 Jul 23 |
peter |
102 |
iterator first; |
4349 |
19 Jul 23 |
peter |
103 |
iterator last; |
4349 |
19 Jul 23 |
peter |
104 |
utility::is_sorted_until(first, last, comp); |
4349 |
19 Jul 23 |
peter |
105 |
|
4349 |
19 Jul 23 |
peter |
106 |
typedef boost::less_than_comparable_archetype<ValueType> ValueType2; |
4349 |
19 Jul 23 |
peter |
107 |
typedef boost::input_iterator_archetype<ValueType2> iterator2; |
4349 |
19 Jul 23 |
peter |
108 |
iterator2 first2; |
4349 |
19 Jul 23 |
peter |
109 |
iterator2 last2; |
4349 |
19 Jul 23 |
peter |
110 |
utility::is_sorted_until(first2, last2); |
4349 |
19 Jul 23 |
peter |
111 |
} |
4349 |
19 Jul 23 |
peter |
112 |
return suite.return_value(); |
4349 |
19 Jul 23 |
peter |
113 |
} |