]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/25_algorithms/search_n/iterator.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / search_n / iterator.cc
1 // Copyright (C) 2004-2020 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 3, or (at your option)
7 // any later version.
8
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.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-options "-DTEST_DEPTH=10" { target simulator } }
19
20 // 25 algorithms, search_n
21
22 #include <algorithm>
23 #include <functional>
24 #include <testsuite_hooks.h>
25 #include <testsuite_iterators.h>
26
27 #ifndef TEST_DEPTH
28 #define TEST_DEPTH 14
29 #endif
30
31 int array1[11] = {0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0};
32 int array2[TEST_DEPTH];
33
34 int pred_count;
35 bool
36 pred(int i, int j)
37 {
38 ++pred_count;
39 return i == j;
40 }
41
42 bool
43 lexstep(int* start, int length)
44 {
45 int i = 0;
46 int carry = 1;
47 while(i < length && carry)
48 {
49 if(start[i] == 1)
50 start[i] = 0;
51 else
52 {
53 start[i] = 1;
54 carry = 0;
55 }
56 i++;
57 }
58 return !carry;
59 }
60
61 int main()
62 {
63 using __gnu_test::test_container;
64 using __gnu_test::random_access_iterator_wrapper;
65 using __gnu_test::bidirectional_iterator_wrapper;
66 using __gnu_test::forward_iterator_wrapper;
67
68 using std::search_n;
69
70 test_container<int,forward_iterator_wrapper> con(array1,array1 + 10);
71 VERIFY(search_n(con.end(), con.end(), 0, 1) == con.end());
72 VERIFY(search_n(con.end(), con.end(), 1, 1) == con.end());
73 VERIFY(search_n(con.begin(), con.end(), 1, 1).ptr == array1 + 1);
74 VERIFY(search_n(con.begin(), con.end(), 2, 1).ptr == array1 + 4);
75 VERIFY(search_n(con.begin(), con.end(), 3, 1).ptr == array1 + 7);
76 VERIFY(search_n(con.begin(), con.end(), 3, 0) == con.end());
77
78 // Now do a brute-force comparison of the different types
79 for(int i = 0; i < TEST_DEPTH; i++)
80 {
81 for(int j = 0; j < i; j++)
82 array2[i] = 0;
83 do {
84 for(int j = 0; j < i; j++)
85 {
86 test_container<int, forward_iterator_wrapper>
87 forwardcon(array2, array2 + i);
88 test_container<int, random_access_iterator_wrapper>
89 randomcon(array2, array2 + i);
90 test_container<int, bidirectional_iterator_wrapper>
91 bidircon(array2, array2 + i);
92
93 int* t1 = search_n(forwardcon.begin(),
94 forwardcon.end(), j, 1).ptr;
95 pred_count = 0;
96 int* t2 = search_n(forwardcon.begin(),
97 forwardcon.end(), j, 1, pred).ptr;
98 VERIFY(pred_count <= i);
99 int* t3 = search_n(bidircon.begin(),
100 bidircon.end(), j, 1).ptr;
101 pred_count = 0;
102 int* t4 = search_n(bidircon.begin(),
103 bidircon.end(), j, 1, pred).ptr;
104 VERIFY(pred_count <= i);
105 int* t5 = search_n(randomcon.begin(),
106 randomcon.end(), j, 1).ptr;
107 pred_count = 0;
108 int* t6 = search_n(randomcon.begin(),
109 randomcon.end(), j, 1, pred).ptr;
110 VERIFY(pred_count <= i);
111 VERIFY((t1 == t2) && (t2 == t3) && (t3 == t4) &&
112 (t4 == t5) && (t5 == t6));
113 }
114 }
115 while(lexstep(array2, i));
116 }
117 return 0;
118 }