]>
Commit | Line | Data |
---|---|---|
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 | ||
27 | using __gnu_test::test_container; | |
28 | using __gnu_test::random_access_iterator_wrapper; | |
29 | using __gnu_test::rvalstruct; | |
30 | ||
31 | typedef test_container<rvalstruct, random_access_iterator_wrapper> Container; | |
32 | ||
33 | const int N = 200000; | |
34 | int A[N]; | |
35 | ||
36 | void fill_ascending() | |
37 | { | |
38 | for (int i = 0; i < N; ++i) | |
39 | A[i] = i; | |
40 | } | |
41 | ||
42 | void | |
43 | test01() | |
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 | ||
61 | int random_generator(int) | |
62 | { return 0; } | |
63 | ||
64 | void | |
65 | test02() | |
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 | ||
79 | int | |
80 | main() | |
81 | { | |
82 | test01(); | |
83 | test02(); | |
84 | return 0; | |
85 | } |