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