source: trunk/test/merge_iterator.cc @ 2995

Last change on this file since 2995 was 2995, checked in by Peter, 9 years ago

closes #741. New class MergeIterator?

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1// $Id: merge_iterator.cc 2995 2013-03-13 11:30:49Z 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 <algorithm>
28#include <cassert>
29#include <iostream>
30#include <string>
31#include <vector>
32
33using namespace theplu::yat;
34
35bool test1(test::Suite& suite)
36{
37  suite.out() << "test1\n";
38  std::vector<int> vec1;
39  vec1.push_back(2);
40  vec1.push_back(1);
41  vec1.push_back(2);
42  std::sort(vec1.begin(), vec1.end());
43  std::vector<int> vec2;
44  vec2.push_back(0);
45  vec2.push_back(3);
46  std::sort(vec2.begin(), vec2.end());
47  std::vector<int> vec3;
48  vec3.push_back(10);
49  vec3.push_back(1);
50  std::sort(vec3.begin(), vec3.end());
51
52  typedef std::vector<int>::const_iterator vec_iterator;
53  std::vector<std::pair<vec_iterator, vec_iterator> > data;
54  data.push_back(std::make_pair(vec1.begin(), vec1.end()));
55  data.push_back(std::make_pair(vec2.begin(), vec2.end()));
56  data.push_back(std::make_pair(vec3.begin(), vec3.end()));
57  data.push_back(std::make_pair(vec3.end(), vec3.end()));
58  typedef utility::MergeIterator<vec_iterator> miterator;
59  miterator begin(data);
60  miterator end;
61
62  std::vector<int> result(100, 999);
63
64  std::copy(begin, end, result.begin());
65
66  std::vector<int> correct;
67  correct.push_back(0);
68  correct.push_back(1);
69  correct.push_back(1);
70  correct.push_back(2);
71  correct.push_back(2);
72  correct.push_back(3);
73  correct.push_back(10);
74
75  correct.push_back(999);
76  for (size_t i=0; i<correct.size(); ++i)
77    if (correct[i] != result[i]) {
78      suite.err() << "error: " << i << " " << result[i] << " expected "
79                << correct[i] << "\n";
80      return false;
81    }
82  return true;
83}
84
85
86bool test2(test::Suite& suite)
87{
88  suite.out() << "test2\n";
89  // using constructor passing comparator
90  typedef int* base;
91  std::vector<std::pair<base, base> > data;
92  utility::MergeIterator<base, std::greater<int> > miterator(data);
93  std::greater<int> comp;
94  utility::MergeIterator<base, std::greater<int> > miterator2(data, comp);
95  return true;
96}
97
98
99bool test3(test::Suite& suite)
100{
101  suite.out() << "test3\n";
102  // using make_merge_iterator functions
103  typedef int* base;
104  std::vector<std::pair<base, base> > data;
105  utility::MergeIterator<base> end;
106  std::count(utility::make_merge_iterator(data), end, 0);
107  utility::MergeIterator<base, std::greater<int> > end2;
108  std::greater<int> comp;
109  std::count(utility::make_merge_iterator(data, comp), end2, 0);
110  return true;
111}
112
113
114int main(int argc, char* argv[])
115{
116  test::Suite suite(argc, argv);
117  test1(suite);
118  test2(suite);
119  test3(suite);
120  return suite.return_value();
121}
Note: See TracBrowser for help on using the repository browser.