| 1 | // { dg-do run { target c++11 } } |
| 2 | // { dg-options "-Wno-deprecated-declarations" } |
| 3 | // { dg-add-options using-deprecated } |
| 4 | // { dg-require-effective-target hosted } |
| 5 | |
| 6 | // Copyright (C) 2009-2025 Free Software Foundation, Inc. |
| 7 | // |
| 8 | // This file is part of the GNU ISO C++ Library. This library is free |
| 9 | // software; you can redistribute it and/or modify it under the |
| 10 | // terms of the GNU General Public License as published by the |
| 11 | // Free Software Foundation; either version 3, or (at your option) |
| 12 | // any later version. |
| 13 | |
| 14 | // This library is distributed in the hope that it will be useful, |
| 15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | // GNU General Public License for more details. |
| 18 | |
| 19 | // You should have received a copy of the GNU General Public License along |
| 20 | // with this library; see the file COPYING3. If not see |
| 21 | // <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | // 25.2.11 random_shuffle() |
| 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 | |
| 34 | using __gnu_test::test_container; |
| 35 | using __gnu_test::random_access_iterator_wrapper; |
| 36 | using __gnu_test::rvalstruct; |
| 37 | |
| 38 | typedef test_container<rvalstruct, random_access_iterator_wrapper> Container; |
| 39 | |
| 40 | const unsigned int N = 10000; |
| 41 | int A[N]; // This is made global because we don't want it on the stack |
| 42 | |
| 43 | void fill_ascending() |
| 44 | { |
| 45 | for (int i = 0; i < N; ++i) |
| 46 | A[i] = i; |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | test01() |
| 51 | { |
| 52 | fill_ascending(); |
| 53 | rvalstruct rv[N]; |
| 54 | std::copy(A, A + N, rv); |
| 55 | Container con(rv, rv + N); |
| 56 | std::random_shuffle(con.begin(), con.end()); |
| 57 | |
| 58 | // The chance that random_shuffle leaves the order as is by coincidence |
| 59 | // is negligible, so we expect it to be permuted |
| 60 | VERIFY( !std::equal(rv, rv + N, A) ); |
| 61 | |
| 62 | std::sort(con.begin(), con.end()); |
| 63 | VERIFY( std::equal(rv, rv + N, A) ); |
| 64 | } |
| 65 | |
| 66 | int random_generator(int) |
| 67 | { return 0; } |
| 68 | |
| 69 | void |
| 70 | test02() |
| 71 | { |
| 72 | rvalstruct rv[10] = {1,2,3,4,5,6,7,8,9,10}; |
| 73 | int result[10] = {10,1,2,3,4,5,6,7,8,9}; |
| 74 | Container con(rv, rv + 10); |
| 75 | std::random_shuffle(con.begin(), con.end(), random_generator); |
| 76 | // The above answer was generated by hand. It is not required by the standard, |
| 77 | // but is produced by the current algorithm. |
| 78 | VERIFY( std::equal(rv, rv + 10, result) ); |
| 79 | } |
| 80 | |
| 81 | int |
| 82 | main() |
| 83 | { |
| 84 | test01(); |
| 85 | test02(); |
| 86 | return 0; |
| 87 | } |