Message ID | 20180625162338.GA15218@redhat.com |
---|---|
State | New |
Headers | show |
Series | Add experimental::sample and experimental::shuffle from N4531 | expand |
On 25/06/18 17:23 +0100, Jonathan Wakely wrote: >The additions to <experimental/random> were added in 2015 but the new >algorithms in <experimental/algorithm> were not. This adds them. > > * include/experimental/algorithm (sample, shuffle): Add new overloads > using per-thread random number engine. > * testsuite/experimental/algorithm/sample.cc: Simpify and reduce > dependencies by using __gnu_test::test_container. > * testsuite/experimental/algorithm/sample-2.cc: New. > * testsuite/experimental/algorithm/shuffle.cc: New. > >Tested x86_64-linux, committed to trunk. And this documents it in the manual. commit 0e350380ee69c6b719362fbd9fbb6a6d0854f6ec Author: Jonathan Wakely <jwakely@redhat.com> Date: Mon Jun 25 17:42:09 2018 +0100 * doc/xml/manual/status_cxx2017.xml: Document N4531 status. diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml index aa0914cff72..a77653a3ab4 100644 --- a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml +++ b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml @@ -954,6 +954,16 @@ and test for <code>__STDCPP_MATH_SPEC_FUNCS__ >= 201003L</code>. <entry>Library Fundamentals 2 TS</entry> </row> + <row> + <entry> + <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4531.html"> + N4531 + </link> + </entry> + <entry>std::rand replacement, revision 3</entry> + <entry>Y</entry> + <entry>Library Fundamentals 2 TS</entry> + </row> </tbody> </tgroup>
On Mon, 25 Jun 2018 at 18:23, Jonathan Wakely <jwakely@redhat.com> wrote: > > The additions to <experimental/random> were added in 2015 but the new > algorithms in <experimental/algorithm> were not. This adds them. > > * include/experimental/algorithm (sample, shuffle): Add new overloads > using per-thread random number engine. > * testsuite/experimental/algorithm/sample.cc: Simpify and reduce > dependencies by using __gnu_test::test_container. > * testsuite/experimental/algorithm/sample-2.cc: New. > * testsuite/experimental/algorithm/shuffle.cc: New. > > Tested x86_64-linux, committed to trunk. > > This would be safe to backport, but nobody has noticed the algos are > missing or complained, so it doesn't seem very important to backport. > > Hi, On bare-metal targets (aarch64 and arm + newlib), I've noticed that the two new tests fail: PASS: experimental/algorithm/shuffle.cc (test for excess errors) spawn /aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/utils/bin/qemu-wrapper.sh ./shuffle.exe terminate called after throwing an instance of 'std::runtime_error' what(): random_device::random_device(const std::string&) *** EXIT code 4242 FAIL: experimental/algorithm/shuffle.cc execution test PASS: experimental/algorithm/sample-2.cc (test for excess errors) spawn /aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/utils/bin/qemu-wrapper.sh ./sample-2.exe terminate called after throwing an instance of 'std::runtime_error' what(): random_device::random_device(const std::string&) *** EXIT code 4242 FAIL: experimental/algorithm/sample-2.cc execution test Does this ring a bell? Thanks, Christophe
On 29/06/18 08:55 +0200, Christophe Lyon wrote: >On Mon, 25 Jun 2018 at 18:23, Jonathan Wakely <jwakely@redhat.com> wrote: >> >> The additions to <experimental/random> were added in 2015 but the new >> algorithms in <experimental/algorithm> were not. This adds them. >> >> * include/experimental/algorithm (sample, shuffle): Add new overloads >> using per-thread random number engine. >> * testsuite/experimental/algorithm/sample.cc: Simpify and reduce >> dependencies by using __gnu_test::test_container. >> * testsuite/experimental/algorithm/sample-2.cc: New. >> * testsuite/experimental/algorithm/shuffle.cc: New. >> >> Tested x86_64-linux, committed to trunk. >> >> This would be safe to backport, but nobody has noticed the algos are >> missing or complained, so it doesn't seem very important to backport. >> >> > >Hi, > >On bare-metal targets (aarch64 and arm + newlib), I've noticed that >the two new tests fail: >PASS: experimental/algorithm/shuffle.cc (test for excess errors) >spawn /aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/utils/bin/qemu-wrapper.sh >./shuffle.exe >terminate called after throwing an instance of 'std::runtime_error' > what(): random_device::random_device(const std::string&) > >*** EXIT code 4242 >FAIL: experimental/algorithm/shuffle.cc execution test > >PASS: experimental/algorithm/sample-2.cc (test for excess errors) >spawn /aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/utils/bin/qemu-wrapper.sh >./sample-2.exe >terminate called after throwing an instance of 'std::runtime_error' > what(): random_device::random_device(const std::string&) > >*** EXIT code 4242 >FAIL: experimental/algorithm/sample-2.cc execution test > >Does this ring a bell? Does the existing testsuite/experimental/random/randint.cc file fail in the same way?
On Fri, 29 Jun 2018 at 09:21, Jonathan Wakely <jwakely@redhat.com> wrote: > > On 29/06/18 08:55 +0200, Christophe Lyon wrote: > >On Mon, 25 Jun 2018 at 18:23, Jonathan Wakely <jwakely@redhat.com> wrote: > >> > >> The additions to <experimental/random> were added in 2015 but the new > >> algorithms in <experimental/algorithm> were not. This adds them. > >> > >> * include/experimental/algorithm (sample, shuffle): Add new overloads > >> using per-thread random number engine. > >> * testsuite/experimental/algorithm/sample.cc: Simpify and reduce > >> dependencies by using __gnu_test::test_container. > >> * testsuite/experimental/algorithm/sample-2.cc: New. > >> * testsuite/experimental/algorithm/shuffle.cc: New. > >> > >> Tested x86_64-linux, committed to trunk. > >> > >> This would be safe to backport, but nobody has noticed the algos are > >> missing or complained, so it doesn't seem very important to backport. > >> > >> > > > >Hi, > > > >On bare-metal targets (aarch64 and arm + newlib), I've noticed that > >the two new tests fail: > >PASS: experimental/algorithm/shuffle.cc (test for excess errors) > >spawn /aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/utils/bin/qemu-wrapper.sh > >./shuffle.exe > >terminate called after throwing an instance of 'std::runtime_error' > > what(): random_device::random_device(const std::string&) > > > >*** EXIT code 4242 > >FAIL: experimental/algorithm/shuffle.cc execution test > > > >PASS: experimental/algorithm/sample-2.cc (test for excess errors) > >spawn /aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/utils/bin/qemu-wrapper.sh > >./sample-2.exe > >terminate called after throwing an instance of 'std::runtime_error' > > what(): random_device::random_device(const std::string&) > > > >*** EXIT code 4242 > >FAIL: experimental/algorithm/sample-2.cc execution test > > > >Does this ring a bell? > > Does the existing testsuite/experimental/random/randint.cc file fail > in the same way? > Yes it does. And so do: 25_algorithms/make_heap/complexity.cc 23_containers/array/element_access/at_neg.cc 26_numerics/random/random_device/cons/default.cc
On 29/06/18 09:39 +0200, Christophe Lyon wrote: >On Fri, 29 Jun 2018 at 09:21, Jonathan Wakely <jwakely@redhat.com> wrote: >> >> On 29/06/18 08:55 +0200, Christophe Lyon wrote: >> >On Mon, 25 Jun 2018 at 18:23, Jonathan Wakely <jwakely@redhat.com> wrote: >> >> >> >> The additions to <experimental/random> were added in 2015 but the new >> >> algorithms in <experimental/algorithm> were not. This adds them. >> >> >> >> * include/experimental/algorithm (sample, shuffle): Add new overloads >> >> using per-thread random number engine. >> >> * testsuite/experimental/algorithm/sample.cc: Simpify and reduce >> >> dependencies by using __gnu_test::test_container. >> >> * testsuite/experimental/algorithm/sample-2.cc: New. >> >> * testsuite/experimental/algorithm/shuffle.cc: New. >> >> >> >> Tested x86_64-linux, committed to trunk. >> >> >> >> This would be safe to backport, but nobody has noticed the algos are >> >> missing or complained, so it doesn't seem very important to backport. >> >> >> >> >> > >> >Hi, >> > >> >On bare-metal targets (aarch64 and arm + newlib), I've noticed that >> >the two new tests fail: >> >PASS: experimental/algorithm/shuffle.cc (test for excess errors) >> >spawn /aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/utils/bin/qemu-wrapper.sh >> >./shuffle.exe >> >terminate called after throwing an instance of 'std::runtime_error' >> > what(): random_device::random_device(const std::string&) >> > >> >*** EXIT code 4242 >> >FAIL: experimental/algorithm/shuffle.cc execution test >> > >> >PASS: experimental/algorithm/sample-2.cc (test for excess errors) >> >spawn /aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/utils/bin/qemu-wrapper.sh >> >./sample-2.exe >> >terminate called after throwing an instance of 'std::runtime_error' >> > what(): random_device::random_device(const std::string&) >> > >> >*** EXIT code 4242 >> >FAIL: experimental/algorithm/sample-2.cc execution test >> > >> >Does this ring a bell? >> >> Does the existing testsuite/experimental/random/randint.cc file fail >> in the same way? >> > >Yes it does. > >And so do: >25_algorithms/make_heap/complexity.cc This one also uses std::random_device. >23_containers/array/element_access/at_neg.cc Hmm, // Expected behavior is to either throw and have the uncaught // exception end up in a terminate handler which eventually exits, // or abort. (Depending on -fno-exceptions.) So this is expected to XFAIL. >26_numerics/random/random_device/cons/default.cc We should XFAIL the ones that use std::random_device, if we can identify an effective target to describe them.
On 29/06/18 10:45 +0100, Jonathan Wakely wrote: >On 29/06/18 09:39 +0200, Christophe Lyon wrote: >>On Fri, 29 Jun 2018 at 09:21, Jonathan Wakely <jwakely@redhat.com> wrote: >>> >>>On 29/06/18 08:55 +0200, Christophe Lyon wrote: >>>>On Mon, 25 Jun 2018 at 18:23, Jonathan Wakely <jwakely@redhat.com> wrote: >>>>> >>>>> The additions to <experimental/random> were added in 2015 but the new >>>>> algorithms in <experimental/algorithm> were not. This adds them. >>>>> >>>>> * include/experimental/algorithm (sample, shuffle): Add new overloads >>>>> using per-thread random number engine. >>>>> * testsuite/experimental/algorithm/sample.cc: Simpify and reduce >>>>> dependencies by using __gnu_test::test_container. >>>>> * testsuite/experimental/algorithm/sample-2.cc: New. >>>>> * testsuite/experimental/algorithm/shuffle.cc: New. >>>>> >>>>> Tested x86_64-linux, committed to trunk. >>>>> >>>>> This would be safe to backport, but nobody has noticed the algos are >>>>> missing or complained, so it doesn't seem very important to backport. >>>>> >>>>> >>>> >>>>Hi, >>>> >>>>On bare-metal targets (aarch64 and arm + newlib), I've noticed that >>>>the two new tests fail: >>>>PASS: experimental/algorithm/shuffle.cc (test for excess errors) >>>>spawn /aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/utils/bin/qemu-wrapper.sh >>>>./shuffle.exe >>>>terminate called after throwing an instance of 'std::runtime_error' >>>> what(): random_device::random_device(const std::string&) >>>> >>>>*** EXIT code 4242 >>>>FAIL: experimental/algorithm/shuffle.cc execution test >>>> >>>>PASS: experimental/algorithm/sample-2.cc (test for excess errors) >>>>spawn /aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/utils/bin/qemu-wrapper.sh >>>>./sample-2.exe >>>>terminate called after throwing an instance of 'std::runtime_error' >>>> what(): random_device::random_device(const std::string&) >>>> >>>>*** EXIT code 4242 >>>>FAIL: experimental/algorithm/sample-2.cc execution test >>>> >>>>Does this ring a bell? >>> >>>Does the existing testsuite/experimental/random/randint.cc file fail >>>in the same way? >>> >> >>Yes it does. >> >>And so do: >>25_algorithms/make_heap/complexity.cc > >This one also uses std::random_device. > >>23_containers/array/element_access/at_neg.cc > >Hmm, > > // Expected behavior is to either throw and have the uncaught > // exception end up in a terminate handler which eventually exits, > // or abort. (Depending on -fno-exceptions.) > >So this is expected to XFAIL. > >>26_numerics/random/random_device/cons/default.cc > >We should XFAIL the ones that use std::random_device, if we can >identify an effective target to describe them. This adds a new "random_device" effective-target, so the tests are disabled when the random_device isn't usable. Tested powerpc64le-linux, committed to trunk. If this works for Christophe's bare metal targets I'll backport it to gcc-8-branch too. commit b32bcdc1dc7ff6d483a4a7223d78198b7522cbe4 Author: Jonathan Wakely <jwakely@redhat.com> Date: Wed Jul 4 15:44:45 2018 +0100 Define "random_device" effective target Currently only matches targets where _GLIBCXX_USE_RANDOM_TR1 is defined, which means /dev/random and /dev/urandom are usable. * testsuite/25_algorithms/make_heap/complexity.cc: Require effective target for std::random_device. * testsuite/26_numerics/random/random_device/cons/default.cc: Likewise. * testsuite/experimental/algorithm/sample-2.cc: Likewise. * testsuite/experimental/algorithm/shuffle.cc: Likewise. * testsuite/experimental/random/randint.cc: Likewise. * testsuite/lib/libstdc++.exp (check_effective_target_random_device): New proc. diff --git a/libstdc++-v3/testsuite/25_algorithms/make_heap/complexity.cc b/libstdc++-v3/testsuite/25_algorithms/make_heap/complexity.cc index cca48f61e0a..069d2d0433d 100644 --- a/libstdc++-v3/testsuite/25_algorithms/make_heap/complexity.cc +++ b/libstdc++-v3/testsuite/25_algorithms/make_heap/complexity.cc @@ -16,6 +16,7 @@ // <http://www.gnu.org/licenses/>. // { dg-do run { target c++11 } } +// { dg-require-effective-target random_device } #include <random> #include <vector> diff --git a/libstdc++-v3/testsuite/26_numerics/random/random_device/cons/default.cc b/libstdc++-v3/testsuite/26_numerics/random/random_device/cons/default.cc index 38210963f7e..5a34526a5f7 100644 --- a/libstdc++-v3/testsuite/26_numerics/random/random_device/cons/default.cc +++ b/libstdc++-v3/testsuite/26_numerics/random/random_device/cons/default.cc @@ -1,4 +1,5 @@ // { dg-do run { target c++11 } } +// { dg-require-effective-target random_device } // { dg-require-cstdint "" } // // 2008-11-24 Edward M. Smith-Rowland <3dw4rd@verizon.net> diff --git a/libstdc++-v3/testsuite/experimental/algorithm/sample-2.cc b/libstdc++-v3/testsuite/experimental/algorithm/sample-2.cc index 541d17e08a0..ef3f7daa14c 100644 --- a/libstdc++-v3/testsuite/experimental/algorithm/sample-2.cc +++ b/libstdc++-v3/testsuite/experimental/algorithm/sample-2.cc @@ -16,6 +16,7 @@ // <http://www.gnu.org/licenses/>. // { dg-do run { target c++14 } } +// { dg-require-effective-target random_device } // { dg-require-effective-target tls_runtime } // { dg-add-options tls } diff --git a/libstdc++-v3/testsuite/experimental/algorithm/shuffle.cc b/libstdc++-v3/testsuite/experimental/algorithm/shuffle.cc index ee06fbd996a..db958f600d4 100644 --- a/libstdc++-v3/testsuite/experimental/algorithm/shuffle.cc +++ b/libstdc++-v3/testsuite/experimental/algorithm/shuffle.cc @@ -1,4 +1,5 @@ // { dg-do run { target c++14 } } +// { dg-require-effective-target random_device } // { dg-require-effective-target tls_runtime } // { dg-add-options tls } diff --git a/libstdc++-v3/testsuite/experimental/random/randint.cc b/libstdc++-v3/testsuite/experimental/random/randint.cc index e80bd858f29..90ba72ac2fc 100644 --- a/libstdc++-v3/testsuite/experimental/random/randint.cc +++ b/libstdc++-v3/testsuite/experimental/random/randint.cc @@ -1,4 +1,5 @@ // { dg-do run { target c++14 } } +// { dg-require-effective-target random_device } // { dg-require-effective-target tls_runtime } // { dg-add-options tls } diff --git a/libstdc++-v3/testsuite/lib/libstdc++.exp b/libstdc++-v3/testsuite/lib/libstdc++.exp index 7af3266f855..d8717e402df 100644 --- a/libstdc++-v3/testsuite/lib/libstdc++.exp +++ b/libstdc++-v3/testsuite/lib/libstdc++.exp @@ -2061,6 +2061,34 @@ proc check_effective_target_cxx11-abi { } { return 0 } +# Return 1 if std::random_device should be usable using the current flags, 0 otherwise. +proc check_effective_target_random_device { } { + global cxxflags + + # Set up and preprocess a C++ test program that depends + # on std::random_device being usable. + set src random_device[pid].cc + + set f [open $src "w"] + puts $f "#include <bits/c++config.h>" + puts $f "#if ! _GLIBCXX_USE_RANDOM_TR1" + puts $f "# error No working std::random_device available" + puts $f "#endif" + close $f + + set lines [v3_target_compile $src /dev/null preprocess ""] + file delete $src + + if [string match "" $lines] { + # No error message, preprocessing succeeded. + verbose "check_v3_random_device: `1'" 2 + return 1 + } + + verbose "check_v3_random_device: `0'" 2 + return 0 +} + set additional_prunes "" if { [info exists env(GCC_RUNTEST_PARALLELIZE_DIR)] \
On Wed, 4 Jul 2018 at 18:56, Jonathan Wakely <jwakely@redhat.com> wrote: > > On 29/06/18 10:45 +0100, Jonathan Wakely wrote: > >On 29/06/18 09:39 +0200, Christophe Lyon wrote: > >>On Fri, 29 Jun 2018 at 09:21, Jonathan Wakely <jwakely@redhat.com> wrote: > >>> > >>>On 29/06/18 08:55 +0200, Christophe Lyon wrote: > >>>>On Mon, 25 Jun 2018 at 18:23, Jonathan Wakely <jwakely@redhat.com> wrote: > >>>>> > >>>>> The additions to <experimental/random> were added in 2015 but the new > >>>>> algorithms in <experimental/algorithm> were not. This adds them. > >>>>> > >>>>> * include/experimental/algorithm (sample, shuffle): Add new overloads > >>>>> using per-thread random number engine. > >>>>> * testsuite/experimental/algorithm/sample.cc: Simpify and reduce > >>>>> dependencies by using __gnu_test::test_container. > >>>>> * testsuite/experimental/algorithm/sample-2.cc: New. > >>>>> * testsuite/experimental/algorithm/shuffle.cc: New. > >>>>> > >>>>> Tested x86_64-linux, committed to trunk. > >>>>> > >>>>> This would be safe to backport, but nobody has noticed the algos are > >>>>> missing or complained, so it doesn't seem very important to backport. > >>>>> > >>>>> > >>>> > >>>>Hi, > >>>> > >>>>On bare-metal targets (aarch64 and arm + newlib), I've noticed that > >>>>the two new tests fail: > >>>>PASS: experimental/algorithm/shuffle.cc (test for excess errors) > >>>>spawn /aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/utils/bin/qemu-wrapper.sh > >>>>./shuffle.exe > >>>>terminate called after throwing an instance of 'std::runtime_error' > >>>> what(): random_device::random_device(const std::string&) > >>>> > >>>>*** EXIT code 4242 > >>>>FAIL: experimental/algorithm/shuffle.cc execution test > >>>> > >>>>PASS: experimental/algorithm/sample-2.cc (test for excess errors) > >>>>spawn /aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/utils/bin/qemu-wrapper.sh > >>>>./sample-2.exe > >>>>terminate called after throwing an instance of 'std::runtime_error' > >>>> what(): random_device::random_device(const std::string&) > >>>> > >>>>*** EXIT code 4242 > >>>>FAIL: experimental/algorithm/sample-2.cc execution test > >>>> > >>>>Does this ring a bell? > >>> > >>>Does the existing testsuite/experimental/random/randint.cc file fail > >>>in the same way? > >>> > >> > >>Yes it does. > >> > >>And so do: > >>25_algorithms/make_heap/complexity.cc > > > >This one also uses std::random_device. > > > >>23_containers/array/element_access/at_neg.cc > > > >Hmm, > > > > // Expected behavior is to either throw and have the uncaught > > // exception end up in a terminate handler which eventually exits, > > // or abort. (Depending on -fno-exceptions.) > > > >So this is expected to XFAIL. > > > >>26_numerics/random/random_device/cons/default.cc > > > >We should XFAIL the ones that use std::random_device, if we can > >identify an effective target to describe them. > > This adds a new "random_device" effective-target, so the tests are > disabled when the random_device isn't usable. > > Tested powerpc64le-linux, committed to trunk. If this works for > Christophe's bare metal targets I'll backport it to gcc-8-branch too. > Yes, that works for me: the tests are now UNSUPPORTED on aarch64*-elf and arm-none-eabi. Thanks!
diff --git a/libstdc++-v3/include/experimental/algorithm b/libstdc++-v3/include/experimental/algorithm index fde4f347f88..4c51efb1c97 100644 --- a/libstdc++-v3/include/experimental/algorithm +++ b/libstdc++-v3/include/experimental/algorithm @@ -35,6 +35,7 @@ #include <algorithm> #include <experimental/bits/lfts_config.h> +#include <experimental/random> namespace std _GLIBCXX_VISIBILITY(default) { @@ -42,7 +43,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION namespace experimental { -inline namespace fundamentals_v1 +inline namespace fundamentals_v2 { template<typename _ForwardIterator, typename _Searcher> inline _ForwardIterator @@ -79,7 +80,23 @@ inline namespace fundamentals_v1 __d, std::forward<_UniformRandomNumberGenerator>(__g)); } -} // namespace fundamentals_v1 + + template<typename _PopulationIterator, typename _SampleIterator, + typename _Distance> + inline _SampleIterator + sample(_PopulationIterator __first, _PopulationIterator __last, + _SampleIterator __out, _Distance __n) + { + return experimental::sample(__first, __last, __out, __n, + _S_randint_engine()); + } + + template<typename _RandomAccessIterator> + inline void + shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last) + { return std::shuffle(__first, __last, _S_randint_engine()); } + +} // namespace fundamentals_v2 } // namespace experimental _GLIBCXX_END_NAMESPACE_VERSION diff --git a/libstdc++-v3/testsuite/experimental/algorithm/sample-2.cc b/libstdc++-v3/testsuite/experimental/algorithm/sample-2.cc new file mode 100644 index 00000000000..4ef9a7c77e4 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/algorithm/sample-2.cc @@ -0,0 +1,98 @@ +// Copyright (C) 2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +// { dg-do run { target c++14 } } + +#include <experimental/algorithm> +#include <algorithm> +#include <testsuite_hooks.h> +#include <testsuite_iterators.h> + +using __gnu_test::test_container; +using __gnu_test::input_iterator_wrapper; +using __gnu_test::output_iterator_wrapper; +using __gnu_test::forward_iterator_wrapper; + +void +test01() +{ + const int pop[] = { 1, 2 }; + int samp[10] = { }; + + // population smaller than desired sample size + auto it = std::experimental::sample(pop, pop + 2, samp, 10); + VERIFY( it == samp + 2 ); + VERIFY( std::accumulate(samp, samp + 10, 0) == 3 ); +} + +void +test02() +{ + const int pop[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; + int samp[10] = { }; + + auto it = std::experimental::sample(pop, std::end(pop), samp, 10); + VERIFY( it == samp + 10 ); + + std::sort(samp, it); + auto it2 = std::unique(samp, it); + VERIFY( it2 == it ); +} + +void +test03() +{ + const int pop[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, }; + int samp[5] = { }; + + // input iterator for population + test_container<const int, input_iterator_wrapper> pop_in{pop}; + auto it = std::experimental::sample(pop_in.begin(), pop_in.end(), samp, 5); + VERIFY( it == samp + 5 ); + + std::sort(samp, it); + auto it2 = std::unique(samp, it); + VERIFY( it2 == it ); +} + +void +test04() +{ + const int pop[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + int samp[5] = { }; + + // forward iterator for population and output iterator for result + test_container<const int, forward_iterator_wrapper> pop_fwd{pop}; + test_container<int, output_iterator_wrapper> samp_out{samp}; + auto it = std::experimental::sample(pop_fwd.begin(), pop_fwd.end(), + samp_out.begin(), 5); + + VERIFY( std::distance(samp, it.ptr) == 5 ); + + std::sort(samp, it.ptr); + auto it2 = std::unique(samp, it.ptr); + VERIFY( it2 == it.ptr ); +} + +int +main() +{ + test01(); + test02(); + test03(); + test04(); +} diff --git a/libstdc++-v3/testsuite/experimental/algorithm/sample.cc b/libstdc++-v3/testsuite/experimental/algorithm/sample.cc index 543a6efc461..b2373706f04 100644 --- a/libstdc++-v3/testsuite/experimental/algorithm/sample.cc +++ b/libstdc++-v3/testsuite/experimental/algorithm/sample.cc @@ -18,19 +18,17 @@ // { dg-do run { target c++14 } } #include <experimental/algorithm> -#include <iterator> -#include <sstream> -#include <forward_list> -#include <vector> #include <random> -#include <algorithm> #include <testsuite_hooks.h> +#include <testsuite_iterators.h> + +using __gnu_test::test_container; +using __gnu_test::input_iterator_wrapper; +using __gnu_test::output_iterator_wrapper; +using __gnu_test::forward_iterator_wrapper; std::mt19937 rng; -using std::istream_iterator; -using std::ostream_iterator; - void test01() { @@ -60,11 +58,12 @@ test02() void test03() { - std::istringstream pop("0 1 2 3 4 5 6 7 8 9"); + const int pop[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, }; int samp[5] = { }; // input iterator for population - auto it = std::experimental::sample(istream_iterator<int>{pop}, {}, + test_container<const int, input_iterator_wrapper> pop_in{pop}; + auto it = std::experimental::sample(pop_in.begin(), pop_in.end(), samp, 5, rng); VERIFY( it == samp + 5 ); @@ -77,21 +76,20 @@ test03() void test04() { - std::forward_list<int> pop{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; - std::stringstream samp; + const int pop[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + int samp[5] = { }; // forward iterator for population and output iterator for result - std::experimental::sample(pop.begin(), pop.end(), - ostream_iterator<int>{samp, " "}, - 5, rng); + test_container<const int, forward_iterator_wrapper> pop_fwd{pop}; + test_container<int, output_iterator_wrapper> samp_out{samp}; + auto it = std::experimental::sample(pop_fwd.begin(), pop_fwd.end(), + samp_out.begin(), 5, rng); - // samp.rdbuf()->pubseekoff(0, std::ios::beg); - std::vector<int> v(istream_iterator<int>{samp}, {}); - VERIFY( v.size() == 5 ); + VERIFY( std::distance(samp, it.ptr) == 5 ); - std::sort(v.begin(), v.end()); - auto it = std::unique(v.begin(), v.end()); - VERIFY( it == v.end() ); + std::sort(samp, it.ptr); + auto it2 = std::unique(samp, it.ptr); + VERIFY( it2 == it.ptr ); } int diff --git a/libstdc++-v3/testsuite/experimental/algorithm/shuffle.cc b/libstdc++-v3/testsuite/experimental/algorithm/shuffle.cc new file mode 100644 index 00000000000..5c4e7fbd433 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/algorithm/shuffle.cc @@ -0,0 +1,61 @@ +// { dg-do run { target c++14 } } + +// Derived from: 2010-03-19 Paolo Carlini <paolo.carlini@oracle.com> + +// Copyright (C) 2010-2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <experimental/algorithm> +#include <vector> +#include <testsuite_hooks.h> + +void test01() +{ + for (unsigned size = 0; size < 50; ++size) + { + std::vector<int> vref(size); + std::iota(vref.begin(), vref.end(), 0); + std::vector<int> v1(vref), v2(vref); + + std::experimental::shuffle(v1.begin(), v1.end()); + std::experimental::shuffle(v2.begin(), v2.end()); + + if (size >= 10) + { + VERIFY( !std::equal(v1.begin(), v1.end(), vref.begin()) ); + VERIFY( !std::equal(v2.begin(), v2.end(), vref.begin()) ); + VERIFY( !std::equal(v1.begin(), v1.end(), v2.begin()) ); + } + + for (unsigned ind = 0; ind < size; ++ind) + { + auto it1 = std::find(v1.begin(), v1.end(), vref[ind]); + auto it2 = std::find(v2.begin(), v2.end(), vref[ind]); + VERIFY( it1 != v1.end() ); + VERIFY( it2 != v2.end() ); + v1.erase(it1); + v2.erase(it2); + } + VERIFY( v1.empty() ); + VERIFY( v2.empty() ); + } +} + +int main() +{ + test01(); +}