Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / libstdc++-v3 / testsuite / 25_algorithms / search_n / iterator.cc
1 // Copyright (C) 2004-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 // { dg-options "-DTEST_DEPTH=10" { target simulator } }
19
20 // 25 algorithms, search_n
21
22 #include <algorithm>
23 #include <functional>
24 #include <testsuite_hooks.h>
25 #include <testsuite_iterators.h>
26
27 #ifndef TEST_DEPTH
28 #define TEST_DEPTH 14
29 #endif
30
31 int array1[11] = {0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0};
32 int array2[TEST_DEPTH];
33
34 bool 
35 pred(int i, int j)
36 {
37   return i == j;
38 }
39
40 bool
41 lexstep(int* start, int length) 
42 {
43   int i = 0;
44   int carry = 1;
45   while(i < length && carry) 
46     {
47       if(start[i] == 1)
48         start[i] = 0;
49       else 
50         {
51           start[i] = 1;
52           carry = 0;
53         }
54       i++;
55     }
56   return !carry;
57 }
58
59 int main() 
60 {
61   using __gnu_test::test_container;
62   using __gnu_test::random_access_iterator_wrapper;
63   using __gnu_test::bidirectional_iterator_wrapper;
64   using __gnu_test::forward_iterator_wrapper;
65   
66   using std::search_n;
67
68   test_container<int,forward_iterator_wrapper> con(array1,array1 + 10);
69   VERIFY(search_n(con.end(), con.end(), 0, 1) == con.end());
70   VERIFY(search_n(con.end(), con.end(), 1, 1) == con.end());
71   VERIFY(search_n(con.begin(), con.end(), 1, 1).ptr == array1 + 1);
72   VERIFY(search_n(con.begin(), con.end(), 2, 1).ptr == array1 + 4);
73   VERIFY(search_n(con.begin(), con.end(), 3, 1).ptr == array1 + 7);
74   VERIFY(search_n(con.begin(), con.end(), 3, 0) == con.end());
75
76   // Now do a brute-force comparison of the different types
77   for(int i = 0; i < TEST_DEPTH; i++) 
78     {
79       for(int j = 0; j < i; j++)
80         array2[i] = 0;
81       do {
82         for(int j = 0; j < i; j++)
83           {
84             test_container<int, forward_iterator_wrapper>
85               forwardcon(array2, array2 + i);
86             test_container<int, random_access_iterator_wrapper>
87               randomcon(array2, array2 + i);
88             test_container<int, bidirectional_iterator_wrapper>
89               bidircon(array2, array2 + i);
90
91             int* t1 = search_n(forwardcon.begin(),
92                                forwardcon.end(), j, 1).ptr;
93             int* t2 = search_n(forwardcon.begin(),
94                                forwardcon.end(), j, 1, pred).ptr;
95             int* t3 = search_n(bidircon.begin(),
96                                bidircon.end(), j, 1).ptr;
97             int* t4 = search_n(bidircon.begin(),
98                                bidircon.end(), j, 1, pred).ptr;
99             int* t5 = search_n(randomcon.begin(),
100                                randomcon.end(), j, 1).ptr;
101             int* t6 = search_n(randomcon.begin(),
102                                randomcon.end(), j, 1, pred).ptr;
103             VERIFY((t1 == t2) && (t2 == t3) && (t3 == t4) &&
104                    (t4 == t5) && (t5 == t6));
105           }
106       } 
107       while(lexstep(array2, i));
108     }
109   return 0;
110 }