summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/26_numerics/random/mersenne_twister_engine/cons/seed_seq2.cc
blob: d900bc378d8663e814f3fff1eb2ba1b51f5c2ba5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 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 "" }

#include <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
  = std::mersenne_twister_engine<
    unsigned long, 32, 624, 397, 31,
    0x9908b0dful, 11,
    0xfffffffful, 7,
    0x9d2c5680ul, 15,
    0xefc60000ul, 18, 1812433253ul>;

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();
}