]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/25_algorithms/search/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / search / 1.cc
1 // Copyright (C) 2005-2022 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 // 25.1.5 [lib.alg.search]
19
20 #include <algorithm>
21 #include <testsuite_hooks.h>
22 #include <testsuite_iterators.h>
23
24 using __gnu_test::test_container;
25 using __gnu_test::forward_iterator_wrapper;
26 using __gnu_test::random_access_iterator_wrapper;
27 using std::search;
28
29 typedef test_container<int, forward_iterator_wrapper> Container;
30 typedef test_container<int, random_access_iterator_wrapper> RAcontainer;
31 int array1[] = {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1};
32 int array2[] = {0, 0, 0};
33
34 void
35 test1()
36 {
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
43 void
44 test2()
45 {
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
52 void
53 test3()
54 {
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
61 void
62 test4()
63 {
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
70 void
71 test5()
72 {
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
79 void
80 test6()
81 {
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
99 bool
100 lexstep(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
118 void 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
146 int
147 main()
148 {
149 test1();
150 test2();
151 test3();
152 test4();
153 test5();
154 test6();
155 test7();
156 }