summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/26_numerics/random/random_device/entropy.cc
blob: 63b7043bf9b77230072f7c1377951fa453351135 (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
// { dg-do run { target c++11 } }

#include <random>
#include <testsuite_hooks.h>
#include <testsuite_random.h>

void
test01()
{
  for (auto token : { "mt19937", "prng", "rand_s" })
    if (__gnu_test::random_device_available(token))
      VERIFY( std::random_device(token).entropy() == 0.0 );

  using result_type = std::random_device::result_type;
  const double max = std::log2(std::numeric_limits<result_type>::max() + 1.0);

  for (auto token : { "/dev/random", "/dev/urandom" })
    if (__gnu_test::random_device_available(token))
    {
      const double entropy = std::random_device(token).entropy();
      VERIFY( entropy >= 0.0 );
      VERIFY( entropy <= max );
    }

  for (auto token : { "rdrand", "rdseed", "darn", "hw" })
    if (__gnu_test::random_device_available(token))
    {
      const double entropy = std::random_device(token).entropy();
      VERIFY( entropy == max );
    }

    for (auto token : { "getentropy", "arc4random" })
    if (__gnu_test::random_device_available(token))
    {
      const double entropy = std::random_device(token).entropy();
      VERIFY( entropy == max );
    }
}

int
main()
{
  test01();
}