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