]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/25_algorithms/random_shuffle/moveable.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / random_shuffle / moveable.cc
CommitLineData
123c59a6 1// { dg-options "-std=gnu++11" }
18dddd09 2
f1717362 3// Copyright (C) 2009-2016 Free Software Foundation, Inc.
18dddd09 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.2.11 random_shuffle()
21
a8bcef69 22// XXX FIXME: parallel-mode should deal correctly with moveable-only types
23// per C++0x, at minimum smoothly fall back to serial.
24#undef _GLIBCXX_PARALLEL
25
18dddd09 26#include <algorithm>
27#include <testsuite_hooks.h>
28#include <testsuite_iterators.h>
29#include <testsuite_rvalref.h>
30
31using __gnu_test::test_container;
32using __gnu_test::random_access_iterator_wrapper;
33using __gnu_test::rvalstruct;
34
35typedef test_container<rvalstruct, random_access_iterator_wrapper> Container;
36
c46e4fef 37const unsigned int N = 10000;
38int A[N]; // This is made global because we don't want it on the stack
18dddd09 39
40void fill_ascending()
41{
42 for (int i = 0; i < N; ++i)
43 A[i] = i;
44}
45
46void
47test01()
48{
49 bool test __attribute__((unused)) = true;
50
51 fill_ascending();
52 rvalstruct rv[N];
53 std::copy(A, A + N, rv);
54 Container con(rv, rv + N);
55 std::random_shuffle(con.begin(), con.end());
56
57 // The chance that random_shuffle leaves the order as is by coincidence
58 // is negligible, so we expect it to be permuted
59 VERIFY( !std::equal(rv, rv + N, A) );
60
61 std::sort(con.begin(), con.end());
62 VERIFY( std::equal(rv, rv + N, A) );
63}
64
65int random_generator(int)
66{ return 0; }
67
68void
69test02()
70{
71 bool test __attribute__((unused)) = true;
72
18dddd09 73 rvalstruct rv[10] = {1,2,3,4,5,6,7,8,9,10};
74 int result[10] = {10,1,2,3,4,5,6,7,8,9};
75 Container con(rv, rv + 10);
76 std::random_shuffle(con.begin(), con.end(), random_generator);
77 // The above answer was generated by hand. It is not required by the standard,
78 // but is produced by the current algorithm.
79 VERIFY( std::equal(rv, rv + 10, result) );
80}
81
82int
83main()
84{
85 test01();
86 test02();
87 return 0;
88}