seed_sequences_test.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/random/seed_sequences.h"
  15. #include <iterator>
  16. #include <random>
  17. #include "gmock/gmock.h"
  18. #include "gtest/gtest.h"
  19. #include "absl/random/internal/nonsecure_base.h"
  20. #include "absl/random/random.h"
  21. namespace {
  22. TEST(SeedSequences, Examples) {
  23. {
  24. absl::SeedSeq seed_seq({1, 2, 3});
  25. absl::BitGen bitgen(seed_seq);
  26. EXPECT_NE(0, bitgen());
  27. }
  28. {
  29. absl::BitGen engine;
  30. auto seed_seq = absl::CreateSeedSeqFrom(&engine);
  31. absl::BitGen bitgen(seed_seq);
  32. EXPECT_NE(engine(), bitgen());
  33. }
  34. {
  35. auto seed_seq = absl::MakeSeedSeq();
  36. std::mt19937 random(seed_seq);
  37. EXPECT_NE(0, random());
  38. }
  39. }
  40. TEST(CreateSeedSeqFrom, CompatibleWithStdTypes) {
  41. using ExampleNonsecureURBG =
  42. absl::random_internal::NonsecureURBGBase<std::minstd_rand0>;
  43. // Construct a URBG instance.
  44. ExampleNonsecureURBG rng;
  45. // Construct a Seed Sequence from its variates.
  46. auto seq_from_rng = absl::CreateSeedSeqFrom(&rng);
  47. // Ensure that another URBG can be validly constructed from the Seed Sequence.
  48. std::mt19937_64{seq_from_rng};
  49. }
  50. TEST(CreateSeedSeqFrom, CompatibleWithBitGenerator) {
  51. // Construct a URBG instance.
  52. absl::BitGen rng;
  53. // Construct a Seed Sequence from its variates.
  54. auto seq_from_rng = absl::CreateSeedSeqFrom(&rng);
  55. // Ensure that another URBG can be validly constructed from the Seed Sequence.
  56. std::mt19937_64{seq_from_rng};
  57. }
  58. TEST(CreateSeedSeqFrom, CompatibleWithInsecureBitGen) {
  59. // Construct a URBG instance.
  60. absl::InsecureBitGen rng;
  61. // Construct a Seed Sequence from its variates.
  62. auto seq_from_rng = absl::CreateSeedSeqFrom(&rng);
  63. // Ensure that another URBG can be validly constructed from the Seed Sequence.
  64. std::mt19937_64{seq_from_rng};
  65. }
  66. TEST(CreateSeedSeqFrom, CompatibleWithRawURBG) {
  67. // Construct a URBG instance.
  68. std::random_device urandom;
  69. // Construct a Seed Sequence from its variates, using 64b of seed-material.
  70. auto seq_from_rng = absl::CreateSeedSeqFrom(&urandom);
  71. // Ensure that another URBG can be validly constructed from the Seed Sequence.
  72. std::mt19937_64{seq_from_rng};
  73. }
  74. template <typename URBG>
  75. void TestReproducibleVariateSequencesForNonsecureURBG() {
  76. const size_t kNumVariates = 1000;
  77. URBG rng;
  78. // Reused for both RNG instances.
  79. auto reusable_seed = absl::CreateSeedSeqFrom(&rng);
  80. typename URBG::result_type variates[kNumVariates];
  81. {
  82. URBG child(reusable_seed);
  83. for (auto& variate : variates) {
  84. variate = child();
  85. }
  86. }
  87. // Ensure that variate-sequence can be "replayed" by identical RNG.
  88. {
  89. URBG child(reusable_seed);
  90. for (auto& variate : variates) {
  91. ASSERT_EQ(variate, child());
  92. }
  93. }
  94. }
  95. TEST(CreateSeedSeqFrom, ReproducesVariateSequencesForInsecureBitGen) {
  96. TestReproducibleVariateSequencesForNonsecureURBG<absl::InsecureBitGen>();
  97. }
  98. TEST(CreateSeedSeqFrom, ReproducesVariateSequencesForBitGenerator) {
  99. TestReproducibleVariateSequencesForNonsecureURBG<absl::BitGen>();
  100. }
  101. } // namespace