]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/25_algorithms/search_n/iterator.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / search_n / iterator.cc
CommitLineData
99dee823 1// Copyright (C) 2004-2021 Free Software Foundation, Inc.
278d4cc4
CJ
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
748086b7 6// Free Software Foundation; either version 3, or (at your option)
278d4cc4
CJ
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
748086b7
JJ
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
278d4cc4 17
8ef20cae
MM
18// { dg-options "-DTEST_DEPTH=10" { target simulator } }
19
278d4cc4
CJ
20// 25 algorithms, search_n
21
22#include <algorithm>
23#include <functional>
24#include <testsuite_hooks.h>
25#include <testsuite_iterators.h>
26
8ef20cae 27#ifndef TEST_DEPTH
278d4cc4 28#define TEST_DEPTH 14
8ef20cae
MM
29#endif
30
278d4cc4
CJ
31int array1[11] = {0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0};
32int array2[TEST_DEPTH];
33
4b47d655 34int pred_count;
278d4cc4
CJ
35bool
36pred(int i, int j)
37{
4b47d655 38 ++pred_count;
278d4cc4
CJ
39 return i == j;
40}
41
42bool
43lexstep(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
bd1a56a0
BK
61int 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;
278d4cc4 69
278d4cc4
CJ
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);
0e994557 88 test_container<int, random_access_iterator_wrapper>
278d4cc4
CJ
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;
4b47d655 95 pred_count = 0;
278d4cc4
CJ
96 int* t2 = search_n(forwardcon.begin(),
97 forwardcon.end(), j, 1, pred).ptr;
4b47d655 98 VERIFY(pred_count <= i);
278d4cc4
CJ
99 int* t3 = search_n(bidircon.begin(),
100 bidircon.end(), j, 1).ptr;
4b47d655 101 pred_count = 0;
278d4cc4
CJ
102 int* t4 = search_n(bidircon.begin(),
103 bidircon.end(), j, 1, pred).ptr;
4b47d655 104 VERIFY(pred_count <= i);
278d4cc4
CJ
105 int* t5 = search_n(randomcon.begin(),
106 randomcon.end(), j, 1).ptr;
4b47d655 107 pred_count = 0;
278d4cc4
CJ
108 int* t6 = search_n(randomcon.begin(),
109 randomcon.end(), j, 1, pred).ptr;
4b47d655 110 VERIFY(pred_count <= i);
278d4cc4
CJ
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}