Merge -r 127928:132243 from trunk
[official-gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / search / 1.cc
blobef00b58d010501e88642ca17479dc0d27059a4a5
1 // Copyright (C) 2005 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.1.5 [lib.alg.search]
21 #include <algorithm>
22 #include <testsuite_hooks.h>
23 #include <testsuite_iterators.h>
25 using __gnu_test::test_container;
26 using __gnu_test::forward_iterator_wrapper;
27 using __gnu_test::random_access_iterator_wrapper;
28 using std::search;
30 typedef test_container<int, forward_iterator_wrapper> Container;
31 typedef test_container<int, random_access_iterator_wrapper> RAcontainer;
32 int array1[] = {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1};
33 int array2[] = {0, 0, 0};
35 void
36 test1()
38 bool test __attribute__((unused)) = true;
39 Container con1(array1, array1);
40 Container con2(array1, array1 + 1);
41 VERIFY(search(con1.begin(), con1.end(), con2.begin(), con2.end()).ptr == array1);
42 VERIFY(search(con2.begin(), con2.end(), con1.begin(), con1.end()).ptr == array1);
45 void
46 test2()
48 bool test __attribute__((unused)) = true;
49 Container con1(array1, array1 + 3);
50 Container con2(array2, array2 + 3);
51 VERIFY(search(con1.begin(), con1.end(), con2.begin(), con2.end()).ptr
52 == array1);
55 void
56 test3()
58 bool test __attribute__((unused)) = true;
59 Container con1(array1 + 3, array1 + 10);
60 Container con2(array2, array2 + 3);
61 VERIFY(search(con1.begin(), con1.end(), con2.begin(), con2.end()).ptr
62 == array1 + 10);
65 void
66 test4()
68 bool test __attribute__((unused)) = true;
69 Container con1(array1, array1 + 10);
70 Container con2(array2, array2 + 1);
71 VERIFY(search(con1.begin(), con1.end(), con2.begin(), con2.end()).ptr
72 == array1);
75 void
76 test5()
78 bool test __attribute__((unused)) = true;
79 Container con1(array1 + 6, array1 + 10);
80 Container con2(array2, array2 + 1);
81 VERIFY(search(con1.begin(), con1.end(), con2.begin(), con2.end()).ptr
82 == array1 + 10);
85 void
86 test6()
88 bool test __attribute__((unused)) = true;
89 int array3[]={2, 2, 1, 2, 3, 5};
90 int array4[]={1, 2, 3, 4};
91 Container con1(array3, array3 + 3);
92 Container con2(array3, array3 + 4);
93 Container con3(array3, array3 + 5);
94 Container con4(array3, array3 + 6);
95 Container endcon(array4, array4 + 4);
96 VERIFY(search(con1.begin(), con1.end(), endcon.begin(), endcon.end()).ptr
97 == array3 + 3);
98 VERIFY(search(con2.begin(), con2.end(), endcon.begin(), endcon.end()).ptr
99 == array3 + 4);
100 VERIFY(search(con3.begin(), con3.end(), endcon.begin(), endcon.end()).ptr
101 == array3 + 5);
102 VERIFY(search(con4.begin(), con4.end(), endcon.begin(), endcon.end()).ptr
103 == array3 + 6);
106 bool
107 lexstep(int* start, int length)
109 int i = 0;
110 int carry = 1;
111 while(i < length && carry)
113 if(start[i] == 1)
114 start[i] = 0;
115 else
117 start[i] = 1;
118 carry = 0;
120 i++;
122 return !carry;
125 void test7()
127 int array1[6];
128 int array2[6];
129 for(int length1 = 0; length1 < 6; length1++)
131 for(int length2 = 0; length2 < 6; length2++)
133 std::fill_n(array1, length1, 0);
134 while(lexstep(array1, length1))
136 std::fill_n(array2, length2, 0);
137 while(lexstep(array2, length2))
139 Container con1(array1, array1 + length1);
140 Container con2(array2, array2 + length2);
141 RAcontainer rcon1(array1, array1 + length1);
142 RAcontainer rcon2(array2, array2 + length2);
143 VERIFY(search(con1.begin(), con1.end(), con2.begin(),
144 con2.end()).ptr ==
145 search(rcon1.begin(), rcon1.end(), rcon2.begin(),
146 rcon2.end()).ptr);
153 int
154 main()
156 test1();
157 test2();
158 test3();
159 test4();
160 test5();
161 test6();
162 test7();