1// { dg-options "-std=gnu++0x" }
2
3// Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
4// Free Software Foundation, Inc.
5//
6// This file is part of the GNU ISO C++ Library.  This library is free
7// software; you can redistribute it and/or modify it under the
8// terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 3, or (at your option)
10// any later version.
11
12// This library is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16
17// You should have received a copy of the GNU General Public License along
18// with this library; see the file COPYING3.  If not see
19// <http://www.gnu.org/licenses/>.
20
21// 25.3.2 [lib.alg.nth.element]
22
23#undef _GLIBCXX_CONCEPT_CHECKS
24
25// XXX FIXME:  parallel-mode should deal correctly with moveable-only types
26// per C++0x, at minimum smoothly fall back to serial.
27#undef _GLIBCXX_PARALLEL
28
29#include <algorithm>
30#include <testsuite_hooks.h>
31#include <testsuite_iterators.h>
32#include <testsuite_rvalref.h>
33
34using __gnu_test::test_container;
35using __gnu_test::random_access_iterator_wrapper;
36using std::nth_element;
37using __gnu_test::rvalstruct;
38
39typedef test_container<rvalstruct, random_access_iterator_wrapper> Container;
40
41void
42test1()
43{
44  bool test __attribute__((unused)) = true;
45
46  int intarray[] = {6, 5, 4, 3, 2, 1, 0};
47  rvalstruct array[7];
48  std::copy(intarray, intarray + 7, array);
49  Container con(array, array + 7);
50  nth_element(con.begin(), con.it(3), con.end());
51  for(int i = 0; i < 3; ++i)
52    VERIFY( array[i].val < 3 );
53  for(int i = 4; i < 7; ++i)
54    VERIFY( array[i].val > 3 );
55  for(int i = 0; i < 7; ++i)
56    VERIFY( array[i].valid );
57}
58
59void
60test2()
61{
62  bool test __attribute__((unused)) = true;
63
64  int intarray[] = {0, 6, 1, 5, 2, 4, 3};
65  rvalstruct array[7];
66  std::copy(intarray, intarray + 7, array);
67  Container con(array,array + 7);
68  nth_element(con.begin(), con.it(3), con.end());
69  for(int i = 0; i < 3; ++i)
70    VERIFY( array[i].val < 3 );
71  for(int i = 4; i < 7; ++i)
72    VERIFY( array[i].val > 3 );
73  for(int i = 0; i < 7; ++i)
74    VERIFY( array[i].valid );
75}
76
77bool
78are_less(const rvalstruct& lhs, const rvalstruct& rhs)
79{ return lhs < rhs; }
80
81void
82test3()
83{
84  bool test __attribute__((unused)) = true;
85
86  int intarray[] = {0, 6, 1, 5, 2, 4, 3};
87  rvalstruct array[7];
88  std::copy(intarray, intarray + 7, array);
89  Container con(array,array + 7);
90  nth_element(con.begin(), con.it(3), con.end(), are_less);
91  for(int i = 0; i < 3; ++i)
92    VERIFY( array[i].val < 3 );
93  for(int i = 4; i < 7; ++i)
94    VERIFY( array[i].val > 3 );
95  for(int i = 0; i < 7; ++i)
96    VERIFY( array[i].valid );
97}
98
99int
100main()
101{
102  test1();
103  test2();
104  test3();
105  return 0;
106}
107