summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/ext/random
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2018-05-15 16:36:46 +0100
committerJonathan Wakely <redi@gcc.gnu.org>2018-05-15 16:36:46 +0100
commit5a7960da41fec497eb4203d71215bcb1077207a9 (patch)
tree459507cfc4f9ff9bc0b70712e1db9fd0ce037432 /libstdc++-v3/testsuite/ext/random
parentc3b61fdadd7ffc9ef5c613c2df0ff4edf83ad0fc (diff)
downloadgcc-5a7960da41fec497eb4203d71215bcb1077207a9.tar.gz
PR libstdc++/85749 constrain seed sequences for random number engines
Constrain constructors and member functions of random number engines so that functions taking seed sequences can only be called with types that meet the seed sequence requirements. PR libstdc++/85749 * include/bits/random.h (__detail::__is_seed_seq): New SFINAE helper. (linear_congruential_engine, mersenne_twister_engine) (subtract_with_carry_engine, discard_block_engine) (independent_bits_engine, shuffle_order_engine): Use __is_seed_seq to constrain function templates taking seed sequences. * include/bits/random.tcc (linear_congruential_engine::seed(_Sseq&)) (mersenne_twister_engine::seed(_Sseq&)) (subtract_with_carry_engine::seed(_Sseq&)): Change return types to match declarations. * include/ext/random (simd_fast_mersenne_twister_engine): Use __is_seed_seq to constrain function templates taking seed sequences. * include/ext/random.tcc (simd_fast_mersenne_twister_engine::seed): Change return type to match declaration. * testsuite/26_numerics/random/discard_block_engine/cons/seed_seq2.cc: New. * testsuite/26_numerics/random/independent_bits_engine/cons/ seed_seq2.cc: New. * testsuite/26_numerics/random/linear_congruential_engine/cons/ seed_seq2.cc: New. * testsuite/26_numerics/random/mersenne_twister_engine/cons/ seed_seq2.cc: New. * testsuite/26_numerics/random/pr60037-neg.cc: Adjust dg-error lineno. * testsuite/26_numerics/random/shuffle_order_engine/cons/seed_seq2.cc: New. * testsuite/26_numerics/random/subtract_with_carry_engine/cons/ seed_seq2.cc: New. * testsuite/ext/random/simd_fast_mersenne_twister_engine/cons/ seed_seq2.cc: New. From-SVN: r260263
Diffstat (limited to 'libstdc++-v3/testsuite/ext/random')
-rw-r--r--libstdc++-v3/testsuite/ext/random/simd_fast_mersenne_twister_engine/cons/seed_seq2.cc90
1 files changed, 90 insertions, 0 deletions
diff --git a/libstdc++-v3/testsuite/ext/random/simd_fast_mersenne_twister_engine/cons/seed_seq2.cc b/libstdc++-v3/testsuite/ext/random/simd_fast_mersenne_twister_engine/cons/seed_seq2.cc
new file mode 100644
index 00000000000..325e27517b1
--- /dev/null
+++ b/libstdc++-v3/testsuite/ext/random/simd_fast_mersenne_twister_engine/cons/seed_seq2.cc
@@ -0,0 +1,90 @@
+// 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++11 } }
+// { dg-require-cstdint "" }
+// { dg-require-little-endian "" }
+
+#include <ext/random>
+#include <testsuite_hooks.h>
+
+template<typename T>
+struct seed_seq
+{
+ using result_type = unsigned;
+
+ seed_seq() { }
+
+ template<class U>
+ seed_seq(std::initializer_list<U>) { }
+
+ template<class InputIterator>
+ seed_seq(InputIterator, InputIterator) { }
+
+ template<class RandomAccessIterator>
+ void generate(RandomAccessIterator first, RandomAccessIterator last)
+ {
+ called = true;
+ if (first != last)
+ *first = 42;
+ }
+
+ size_t size() const { called = true; return 1; }
+
+ template<class OutputIterator>
+ void param(OutputIterator dest) const { called = true; dest = 42; }
+
+ // Prevents this type being considered as a seed sequence when
+ // T is convertible to the engine's result_type:
+ operator T() const noexcept { return T(); }
+
+ bool called = false;
+};
+
+using engine_type
+ = __gnu_cxx::simd_fast_mersenne_twister_engine<
+ uint32_t, 607, 2,
+ 15, 3, 13, 3,
+ 0xfdff37ffU, 0xef7f3f7dU,
+ 0xff777b7dU, 0x7ff7fb2fU,
+ 0x00000001U, 0x00000000U,
+ 0x00000000U, 0x5986f054U>;
+
+void
+test01()
+{
+ seed_seq<unsigned> seed;
+ engine_type x(seed);
+ VERIFY( ! seed.called );
+}
+
+void
+test02()
+{
+ seed_seq<void*> seed;
+ engine_type x(seed);
+ VERIFY( seed.called );
+
+ static_assert(!std::is_constructible<engine_type, const seed_seq<void>&>(),
+ "Cannot construct from a const seed_seq");
+}
+
+int main()
+{
+ test01();
+ test02();
+}