Merge -r 127928:132243 from trunk
[official-gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / search_n / iterator.cc
blob2a9b0f42a0929baf3d15333ae0c6faf2afee3410
1 // Copyright (C) 2004 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 2, or (at your option)
7 // any later version.
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
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING. If not, write to the Free
16 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 // USA.
19 // 25 algorithms, search_n
21 #include <algorithm>
22 #include <functional>
23 #include <testsuite_hooks.h>
24 #include <testsuite_iterators.h>
26 #define TEST_DEPTH 14
27 int array1[11] = {0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0};
28 int array2[TEST_DEPTH];
30 bool
31 pred(int i, int j)
33 return i == j;
36 bool
37 lexstep(int* start, int length)
39 int i = 0;
40 int carry = 1;
41 while(i < length && carry)
43 if(start[i] == 1)
44 start[i] = 0;
45 else
47 start[i] = 1;
48 carry = 0;
50 i++;
52 return !carry;
55 int main()
57 using __gnu_test::test_container;
58 using __gnu_test::random_access_iterator_wrapper;
59 using __gnu_test::bidirectional_iterator_wrapper;
60 using __gnu_test::forward_iterator_wrapper;
62 using std::search_n;
64 test_container<int,forward_iterator_wrapper> con(array1,array1 + 10);
65 VERIFY(search_n(con.end(), con.end(), 0, 1) == con.end());
66 VERIFY(search_n(con.end(), con.end(), 1, 1) == con.end());
67 VERIFY(search_n(con.begin(), con.end(), 1, 1).ptr == array1 + 1);
68 VERIFY(search_n(con.begin(), con.end(), 2, 1).ptr == array1 + 4);
69 VERIFY(search_n(con.begin(), con.end(), 3, 1).ptr == array1 + 7);
70 VERIFY(search_n(con.begin(), con.end(), 3, 0) == con.end());
72 // Now do a brute-force comparison of the different types
73 for(int i = 0; i < TEST_DEPTH; i++)
75 for(int j = 0; j < i; j++)
76 array2[i] = 0;
77 do {
78 for(int j = 0; j < i; j++)
80 test_container<int, forward_iterator_wrapper>
81 forwardcon(array2, array2 + i);
82 test_container<int, random_access_iterator_wrapper>
83 randomcon(array2, array2 + i);
84 test_container<int, bidirectional_iterator_wrapper>
85 bidircon(array2, array2 + i);
87 int* t1 = search_n(forwardcon.begin(),
88 forwardcon.end(), j, 1).ptr;
89 int* t2 = search_n(forwardcon.begin(),
90 forwardcon.end(), j, 1, pred).ptr;
91 int* t3 = search_n(bidircon.begin(),
92 bidircon.end(), j, 1).ptr;
93 int* t4 = search_n(bidircon.begin(),
94 bidircon.end(), j, 1, pred).ptr;
95 int* t5 = search_n(randomcon.begin(),
96 randomcon.end(), j, 1).ptr;
97 int* t6 = search_n(randomcon.begin(),
98 randomcon.end(), j, 1, pred).ptr;
99 VERIFY((t1 == t2) && (t2 == t3) && (t3 == t4) &&
100 (t4 == t5) && (t5 == t6));
103 while(lexstep(array2, i));
105 return 0;