]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/25_algorithms/random_shuffle/moveable.cc
testsuite_rvalref.h: Remove obsolete macro using _GLIBCXX_TESTSUITE_ALLOW_RVALREF_ALI...
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / random_shuffle / moveable.cc
CommitLineData
01bbe151
CJ
1// { dg-options "-std=gnu++0x" }
2
3// Copyright (C) 2009 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.2.11 random_shuffle()
21
22#include <algorithm>
23#include <testsuite_hooks.h>
24#include <testsuite_iterators.h>
25#include <testsuite_rvalref.h>
26
27using __gnu_test::test_container;
28using __gnu_test::random_access_iterator_wrapper;
29using __gnu_test::rvalstruct;
30
31typedef test_container<rvalstruct, random_access_iterator_wrapper> Container;
32
33const int N = 200000;
34int A[N];
35
36void fill_ascending()
37{
38 for (int i = 0; i < N; ++i)
39 A[i] = i;
40}
41
42void
43test01()
44{
45 bool test __attribute__((unused)) = true;
46
47 fill_ascending();
48 rvalstruct rv[N];
49 std::copy(A, A + N, rv);
50 Container con(rv, rv + N);
51 std::random_shuffle(con.begin(), con.end());
52
53 // The chance that random_shuffle leaves the order as is by coincidence
54 // is negligible, so we expect it to be permuted
55 VERIFY( !std::equal(rv, rv + N, A) );
56
57 std::sort(con.begin(), con.end());
58 VERIFY( std::equal(rv, rv + N, A) );
59}
60
61int random_generator(int)
62{ return 0; }
63
64void
65test02()
66{
67 bool test __attribute__((unused)) = true;
68
69 fill_ascending();
70 rvalstruct rv[10] = {1,2,3,4,5,6,7,8,9,10};
71 int result[10] = {10,1,2,3,4,5,6,7,8,9};
72 Container con(rv, rv + 10);
73 std::random_shuffle(con.begin(), con.end(), random_generator);
74 // The above answer was generated by hand. It is not required by the standard,
75 // but is produced by the current algorithm.
76 VERIFY( std::equal(rv, rv + 10, result) );
77}
78
79int
80main()
81{
82 test01();
83 test02();
84 return 0;
85}