1 | // $Id: merge_iterator.cc 3387 2015-03-16 01:47:55Z peter $ |
---|
2 | /* |
---|
3 | Copyright (C) 2013 Peter Johansson |
---|
4 | |
---|
5 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
6 | |
---|
7 | The yat library is free software; you can redistribute it and/or |
---|
8 | modify it under the terms of the GNU General Public License as |
---|
9 | published by the Free Software Foundation; either version 3 of the |
---|
10 | License, or (at your option) any later version. |
---|
11 | |
---|
12 | The yat library is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
15 | General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU General Public License |
---|
18 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | */ |
---|
20 | |
---|
21 | #include <config.h> |
---|
22 | |
---|
23 | #include "Suite.h" |
---|
24 | |
---|
25 | #include "yat/utility/MergeIterator.h" |
---|
26 | |
---|
27 | #include <boost/iterator/iterator_archetypes.hpp> |
---|
28 | |
---|
29 | #include <algorithm> |
---|
30 | #include <cassert> |
---|
31 | #include <iostream> |
---|
32 | #include <string> |
---|
33 | #include <vector> |
---|
34 | |
---|
35 | using namespace theplu::yat; |
---|
36 | |
---|
37 | bool test1(test::Suite& suite) |
---|
38 | { |
---|
39 | suite.out() << "test1\n"; |
---|
40 | std::vector<int> vec1; |
---|
41 | vec1.push_back(2); |
---|
42 | vec1.push_back(1); |
---|
43 | vec1.push_back(2); |
---|
44 | std::sort(vec1.begin(), vec1.end()); |
---|
45 | std::vector<int> vec2; |
---|
46 | vec2.push_back(0); |
---|
47 | vec2.push_back(3); |
---|
48 | std::sort(vec2.begin(), vec2.end()); |
---|
49 | std::vector<int> vec3; |
---|
50 | vec3.push_back(10); |
---|
51 | vec3.push_back(1); |
---|
52 | std::sort(vec3.begin(), vec3.end()); |
---|
53 | |
---|
54 | typedef std::vector<int>::const_iterator vec_iterator; |
---|
55 | std::vector<std::pair<vec_iterator, vec_iterator> > data; |
---|
56 | data.push_back(std::make_pair(vec1.begin(), vec1.end())); |
---|
57 | data.push_back(std::make_pair(vec2.begin(), vec2.end())); |
---|
58 | data.push_back(std::make_pair(vec3.begin(), vec3.end())); |
---|
59 | data.push_back(std::make_pair(vec3.end(), vec3.end())); |
---|
60 | typedef utility::MergeIterator<vec_iterator> miterator; |
---|
61 | miterator begin(data); |
---|
62 | miterator end; |
---|
63 | |
---|
64 | std::vector<int> result(100, 999); |
---|
65 | |
---|
66 | std::copy(begin, end, result.begin()); |
---|
67 | |
---|
68 | std::vector<int> correct; |
---|
69 | correct.push_back(0); |
---|
70 | correct.push_back(1); |
---|
71 | correct.push_back(1); |
---|
72 | correct.push_back(2); |
---|
73 | correct.push_back(2); |
---|
74 | correct.push_back(3); |
---|
75 | correct.push_back(10); |
---|
76 | |
---|
77 | correct.push_back(999); |
---|
78 | for (size_t i=0; i<correct.size(); ++i) |
---|
79 | if (correct[i] != result[i]) { |
---|
80 | suite.err() << "error: " << i << " " << result[i] << " expected " |
---|
81 | << correct[i] << "\n"; |
---|
82 | return false; |
---|
83 | } |
---|
84 | return true; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | bool test2(test::Suite& suite) |
---|
89 | { |
---|
90 | suite.out() << "test2\n"; |
---|
91 | // using constructor passing comparator |
---|
92 | typedef int* base; |
---|
93 | std::vector<std::pair<base, base> > data; |
---|
94 | utility::MergeIterator<base, std::greater<int> > miterator(data); |
---|
95 | std::greater<int> comp; |
---|
96 | utility::MergeIterator<base, std::greater<int> > miterator2(data, comp); |
---|
97 | return true; |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | bool test3(test::Suite& suite) |
---|
102 | { |
---|
103 | suite.out() << "test3\n"; |
---|
104 | // using make_merge_iterator functions |
---|
105 | typedef int* base; |
---|
106 | std::vector<std::pair<base, base> > data; |
---|
107 | utility::MergeIterator<base> end; |
---|
108 | std::count(utility::make_merge_iterator(data), end, 0); |
---|
109 | utility::MergeIterator<base, std::greater<int> > end2; |
---|
110 | std::greater<int> comp; |
---|
111 | std::count(utility::make_merge_iterator(data, comp), end2, 0); |
---|
112 | return true; |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | void test4(test::Suite& suite) |
---|
117 | { |
---|
118 | // avoid running compilation test |
---|
119 | if (false) { |
---|
120 | typedef boost::less_than_comparable_archetype |
---|
121 | <boost::copy_constructible_archetype<> > value_type; |
---|
122 | typedef boost::iterator_archetypes::readable_iterator_t access_type; |
---|
123 | typedef boost::single_pass_traversal_tag traversal_type; |
---|
124 | |
---|
125 | typedef boost::iterator_archetype<value_type, access_type, traversal_type> |
---|
126 | base; |
---|
127 | utility::MergeIterator<base> it; |
---|
128 | test::test_readable_iterator(it); |
---|
129 | test::test_single_pass_iterator(it); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | int main(int argc, char* argv[]) |
---|
135 | { |
---|
136 | test::Suite suite(argc, argv); |
---|
137 | test1(suite); |
---|
138 | test2(suite); |
---|
139 | test3(suite); |
---|
140 | test4(suite); |
---|
141 | return suite.return_value(); |
---|
142 | } |
---|