Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / libstdc++-v3 / testsuite / 25_algorithms / search / 1.cc
1 // Copyright (C) 2005-2013 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   bool test __attribute__((unused)) = true;
38   Container con1(array1, array1);
39   Container con2(array1, array1 + 1);
40   VERIFY(search(con1.begin(), con1.end(), con2.begin(), con2.end()).ptr == array1);
41   VERIFY(search(con2.begin(), con2.end(), con1.begin(), con1.end()).ptr == array1);
42 }
43
44 void
45 test2()
46 {
47   bool test __attribute__((unused)) = true;
48   Container con1(array1, array1 + 3);
49   Container con2(array2, array2 + 3);
50   VERIFY(search(con1.begin(), con1.end(), con2.begin(), con2.end()).ptr 
51          == array1);
52 }
53
54 void
55 test3()
56 {
57   bool test __attribute__((unused)) = true;
58   Container con1(array1 + 3, array1 + 10);
59   Container con2(array2, array2 + 3);
60   VERIFY(search(con1.begin(), con1.end(), con2.begin(), con2.end()).ptr 
61          == array1 + 10);
62 }
63
64 void
65 test4()
66 {
67   bool test __attribute__((unused)) = true;
68   Container con1(array1, array1 + 10);
69   Container con2(array2, array2 + 1);
70   VERIFY(search(con1.begin(), con1.end(), con2.begin(), con2.end()).ptr
71          == array1);
72 }
73
74 void
75 test5()
76 {
77   bool test __attribute__((unused)) = true;
78   Container con1(array1 + 6, array1 + 10);
79   Container con2(array2, array2 + 1);
80   VERIFY(search(con1.begin(), con1.end(), con2.begin(), con2.end()).ptr
81          == array1 + 10);
82 }
83
84 void
85 test6()
86 {
87   bool test __attribute__((unused)) = true;
88   int array3[]={2, 2, 1, 2, 3, 5};
89   int array4[]={1, 2, 3, 4};
90   Container con1(array3, array3 + 3);
91   Container con2(array3, array3 + 4);
92   Container con3(array3, array3 + 5);
93   Container con4(array3, array3 + 6);
94   Container endcon(array4, array4 + 4);
95   VERIFY(search(con1.begin(), con1.end(), endcon.begin(), endcon.end()).ptr
96          == array3 + 3);
97   VERIFY(search(con2.begin(), con2.end(), endcon.begin(), endcon.end()).ptr
98          == array3 + 4);
99   VERIFY(search(con3.begin(), con3.end(), endcon.begin(), endcon.end()).ptr
100          == array3 + 5);
101   VERIFY(search(con4.begin(), con4.end(), endcon.begin(), endcon.end()).ptr
102          == array3 + 6);
103 }
104
105 bool
106 lexstep(int* start, int length) 
107 {
108   int i = 0;
109   int carry = 1;
110   while(i < length && carry) 
111     {
112       if(start[i] == 1)
113         start[i] = 0;
114       else 
115         {
116           start[i] = 1;
117           carry = 0;
118         }
119       i++;
120     }
121   return !carry;
122 }
123
124 void test7()
125 {
126   int array1[6];
127   int array2[6];
128   for(int length1 = 0; length1 < 6; length1++)
129   {
130     for(int length2 = 0; length2 < 6; length2++)
131     {
132       std::fill_n(array1, length1, 0);
133       while(lexstep(array1, length1))
134       {
135         std::fill_n(array2, length2, 0);
136         while(lexstep(array2, length2))
137         {
138           Container con1(array1, array1 + length1);
139           Container con2(array2, array2 + length2);
140           RAcontainer rcon1(array1, array1 + length1);
141           RAcontainer rcon2(array2, array2 + length2);
142           VERIFY(search(con1.begin(), con1.end(), con2.begin(), 
143                         con2.end()).ptr ==
144                  search(rcon1.begin(), rcon1.end(), rcon2.begin(),
145                  rcon2.end()).ptr);
146         }
147       } 
148     }
149   }
150 }
151
152 int 
153 main()
154 {
155   test1();
156   test2();
157   test3();
158   test4();
159   test5();
160   test6();
161   test7();
162 }