]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/25_algorithms/search/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / search / 1.cc
CommitLineData
a945c346 1// Copyright (C) 2005-2024 Free Software Foundation, Inc.
0e994557
PC
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)
0e994557
PC
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/>.
0e994557
PC
17
18// 25.1.5 [lib.alg.search]
19
20#include <algorithm>
21#include <testsuite_hooks.h>
22#include <testsuite_iterators.h>
23
24using __gnu_test::test_container;
25using __gnu_test::forward_iterator_wrapper;
f5783e34 26using __gnu_test::random_access_iterator_wrapper;
0e994557
PC
27using std::search;
28
29typedef test_container<int, forward_iterator_wrapper> Container;
f5783e34 30typedef test_container<int, random_access_iterator_wrapper> RAcontainer;
0e994557
PC
31int array1[] = {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1};
32int array2[] = {0, 0, 0};
33
34void
35test1()
36{
0e994557
PC
37 Container con1(array1, array1);
38 Container con2(array1, array1 + 1);
39 VERIFY(search(con1.begin(), con1.end(), con2.begin(), con2.end()).ptr == array1);
40 VERIFY(search(con2.begin(), con2.end(), con1.begin(), con1.end()).ptr == array1);
41}
42
43void
44test2()
45{
0e994557
PC
46 Container con1(array1, array1 + 3);
47 Container con2(array2, array2 + 3);
48 VERIFY(search(con1.begin(), con1.end(), con2.begin(), con2.end()).ptr
49 == array1);
50}
51
52void
53test3()
54{
0e994557
PC
55 Container con1(array1 + 3, array1 + 10);
56 Container con2(array2, array2 + 3);
57 VERIFY(search(con1.begin(), con1.end(), con2.begin(), con2.end()).ptr
58 == array1 + 10);
59}
60
61void
62test4()
63{
0e994557
PC
64 Container con1(array1, array1 + 10);
65 Container con2(array2, array2 + 1);
66 VERIFY(search(con1.begin(), con1.end(), con2.begin(), con2.end()).ptr
67 == array1);
68}
69
70void
71test5()
72{
0e994557
PC
73 Container con1(array1 + 6, array1 + 10);
74 Container con2(array2, array2 + 1);
75 VERIFY(search(con1.begin(), con1.end(), con2.begin(), con2.end()).ptr
76 == array1 + 10);
77}
78
79void
80test6()
81{
0e994557
PC
82 int array3[]={2, 2, 1, 2, 3, 5};
83 int array4[]={1, 2, 3, 4};
84 Container con1(array3, array3 + 3);
85 Container con2(array3, array3 + 4);
86 Container con3(array3, array3 + 5);
87 Container con4(array3, array3 + 6);
88 Container endcon(array4, array4 + 4);
89 VERIFY(search(con1.begin(), con1.end(), endcon.begin(), endcon.end()).ptr
90 == array3 + 3);
91 VERIFY(search(con2.begin(), con2.end(), endcon.begin(), endcon.end()).ptr
92 == array3 + 4);
93 VERIFY(search(con3.begin(), con3.end(), endcon.begin(), endcon.end()).ptr
94 == array3 + 5);
95 VERIFY(search(con4.begin(), con4.end(), endcon.begin(), endcon.end()).ptr
96 == array3 + 6);
97}
98
f5783e34
CJ
99bool
100lexstep(int* start, int length)
101{
102 int i = 0;
103 int carry = 1;
104 while(i < length && carry)
105 {
106 if(start[i] == 1)
107 start[i] = 0;
108 else
109 {
110 start[i] = 1;
111 carry = 0;
112 }
113 i++;
114 }
115 return !carry;
116}
117
118void test7()
119{
120 int array1[6];
121 int array2[6];
122 for(int length1 = 0; length1 < 6; length1++)
123 {
124 for(int length2 = 0; length2 < 6; length2++)
125 {
126 std::fill_n(array1, length1, 0);
127 while(lexstep(array1, length1))
128 {
129 std::fill_n(array2, length2, 0);
130 while(lexstep(array2, length2))
131 {
132 Container con1(array1, array1 + length1);
133 Container con2(array2, array2 + length2);
134 RAcontainer rcon1(array1, array1 + length1);
135 RAcontainer rcon2(array2, array2 + length2);
136 VERIFY(search(con1.begin(), con1.end(), con2.begin(),
137 con2.end()).ptr ==
138 search(rcon1.begin(), rcon1.end(), rcon2.begin(),
139 rcon2.end()).ptr);
140 }
141 }
142 }
143 }
144}
145
0e994557
PC
146int
147main()
148{
149 test1();
150 test2();
151 test3();
152 test4();
153 test5();
154 test6();
f5783e34 155 test7();
0e994557 156}