tvl-depot/absl/random/internal/seed_material_test.cc
Abseil Team 8efba58a3b Export of internal Abseil changes
--
38bc0644e17bf9fe4d78d3db92cd06f585b99ba7 by Andy Soffer <asoffer@google.com>:

Change benchmark to be cc_binary instead of cc_test, and fix a bug in the zipf_distribution benchmark in which arguments were passed in the wrong order.

PiperOrigin-RevId: 262227159

--
3b5411d8f285a758a1713f7ef0dbfa3518f2b38b by CJ Johnson <johnsoncj@google.com>:

Updates Simple<*>() overload to match the name schema of the others

PiperOrigin-RevId: 262211217

--
0cb6812cb8b6e3bf0386b9354189ffcf46c4c094 by Andy Soffer <asoffer@google.com>:

Removing period in trailing namespace comments.

PiperOrigin-RevId: 262210952

--
c903feae3a881be81adf37e9fccd558ee3ed1e64 by CJ Johnson <johnsoncj@google.com>:

This is a cleanup on the public header of InlinedVector to be more presentable

PiperOrigin-RevId: 262207691

--
9a94384dc79cdcf38f6153894f337ebb744e2d76 by Tom Manshreck <shreck@google.com>:

Fix incorrect doc on operator()[] for flat_hash_set

PiperOrigin-RevId: 262206962

--
17e88ee10b727af82c04f8150b6d246eaac836cb by Derek Mauro <dmauro@google.com>:

Fix gcc-5 build error

PiperOrigin-RevId: 262198236
GitOrigin-RevId: 38bc0644e17bf9fe4d78d3db92cd06f585b99ba7
Change-Id: I77cababa47ba3ee8b6cebb2c2cfc9f60a331f6b7
2019-08-08 11:25:03 -04:00

202 lines
7.2 KiB
C++

// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/random/internal/seed_material.h"
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <random>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#ifdef __ANDROID__
// Android assert messages only go to system log, so death tests cannot inspect
// the message for matching.
#define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
EXPECT_DEATH_IF_SUPPORTED(statement, ".*")
#else
#define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
EXPECT_DEATH_IF_SUPPORTED(statement, regex)
#endif
namespace {
using testing::Each;
using testing::ElementsAre;
using testing::Eq;
using testing::Ne;
using testing::Pointwise;
TEST(SeedBitsToBlocks, VerifyCases) {
EXPECT_EQ(0, absl::random_internal::SeedBitsToBlocks(0));
EXPECT_EQ(1, absl::random_internal::SeedBitsToBlocks(1));
EXPECT_EQ(1, absl::random_internal::SeedBitsToBlocks(31));
EXPECT_EQ(1, absl::random_internal::SeedBitsToBlocks(32));
EXPECT_EQ(2, absl::random_internal::SeedBitsToBlocks(33));
EXPECT_EQ(4, absl::random_internal::SeedBitsToBlocks(127));
EXPECT_EQ(4, absl::random_internal::SeedBitsToBlocks(128));
EXPECT_EQ(5, absl::random_internal::SeedBitsToBlocks(129));
}
TEST(ReadSeedMaterialFromOSEntropy, SuccessiveReadsAreDistinct) {
constexpr size_t kSeedMaterialSize = 64;
uint32_t seed_material_1[kSeedMaterialSize] = {};
uint32_t seed_material_2[kSeedMaterialSize] = {};
EXPECT_TRUE(absl::random_internal::ReadSeedMaterialFromOSEntropy(
absl::Span<uint32_t>(seed_material_1, kSeedMaterialSize)));
EXPECT_TRUE(absl::random_internal::ReadSeedMaterialFromOSEntropy(
absl::Span<uint32_t>(seed_material_2, kSeedMaterialSize)));
EXPECT_THAT(seed_material_1, Pointwise(Ne(), seed_material_2));
}
TEST(ReadSeedMaterialFromOSEntropy, ReadZeroBytesIsNoOp) {
uint32_t seed_material[32] = {};
std::memset(seed_material, 0xAA, sizeof(seed_material));
EXPECT_TRUE(absl::random_internal::ReadSeedMaterialFromOSEntropy(
absl::Span<uint32_t>(seed_material, 0)));
EXPECT_THAT(seed_material, Each(Eq(0xAAAAAAAA)));
}
TEST(ReadSeedMaterialFromOSEntropy, NullPtrVectorArgument) {
#ifdef NDEBUG
EXPECT_FALSE(absl::random_internal::ReadSeedMaterialFromOSEntropy(
absl::Span<uint32_t>(nullptr, 32)));
#else
bool result;
ABSL_EXPECT_DEATH_IF_SUPPORTED(
result = absl::random_internal::ReadSeedMaterialFromOSEntropy(
absl::Span<uint32_t>(nullptr, 32)),
"!= nullptr");
(void)result; // suppress unused-variable warning
#endif
}
TEST(ReadSeedMaterialFromURBG, SeedMaterialEqualsVariateSequence) {
// Two default-constructed instances of std::mt19937_64 are guaranteed to
// produce equal variate-sequences.
std::mt19937 urbg_1;
std::mt19937 urbg_2;
constexpr size_t kSeedMaterialSize = 1024;
uint32_t seed_material[kSeedMaterialSize] = {};
EXPECT_TRUE(absl::random_internal::ReadSeedMaterialFromURBG(
&urbg_1, absl::Span<uint32_t>(seed_material, kSeedMaterialSize)));
for (uint32_t seed : seed_material) {
EXPECT_EQ(seed, urbg_2());
}
}
TEST(ReadSeedMaterialFromURBG, ReadZeroBytesIsNoOp) {
std::mt19937_64 urbg;
uint32_t seed_material[32];
std::memset(seed_material, 0xAA, sizeof(seed_material));
EXPECT_TRUE(absl::random_internal::ReadSeedMaterialFromURBG(
&urbg, absl::Span<uint32_t>(seed_material, 0)));
EXPECT_THAT(seed_material, Each(Eq(0xAAAAAAAA)));
}
TEST(ReadSeedMaterialFromURBG, NullUrbgArgument) {
constexpr size_t kSeedMaterialSize = 32;
uint32_t seed_material[kSeedMaterialSize];
#ifdef NDEBUG
EXPECT_FALSE(absl::random_internal::ReadSeedMaterialFromURBG<std::mt19937_64>(
nullptr, absl::Span<uint32_t>(seed_material, kSeedMaterialSize)));
#else
bool result;
ABSL_EXPECT_DEATH_IF_SUPPORTED(
result = absl::random_internal::ReadSeedMaterialFromURBG<std::mt19937_64>(
nullptr, absl::Span<uint32_t>(seed_material, kSeedMaterialSize)),
"!= nullptr");
(void)result; // suppress unused-variable warning
#endif
}
TEST(ReadSeedMaterialFromURBG, NullPtrVectorArgument) {
std::mt19937_64 urbg;
#ifdef NDEBUG
EXPECT_FALSE(absl::random_internal::ReadSeedMaterialFromURBG(
&urbg, absl::Span<uint32_t>(nullptr, 32)));
#else
bool result;
ABSL_EXPECT_DEATH_IF_SUPPORTED(
result = absl::random_internal::ReadSeedMaterialFromURBG(
&urbg, absl::Span<uint32_t>(nullptr, 32)),
"!= nullptr");
(void)result; // suppress unused-variable warning
#endif
}
// The avalanche effect is a desirable cryptographic property of hashes in which
// changing a single bit in the input causes each bit of the output to be
// changed with probability near 50%.
//
// https://en.wikipedia.org/wiki/Avalanche_effect
TEST(MixSequenceIntoSeedMaterial, AvalancheEffectTestOneBitLong) {
std::vector<uint32_t> seed_material = {1, 2, 3, 4, 5, 6, 7, 8};
// For every 32-bit number with exactly one bit set, verify the avalanche
// effect holds. In order to reduce flakiness of tests, accept values
// anywhere in the range of 30%-70%.
for (uint32_t v = 1; v != 0; v <<= 1) {
std::vector<uint32_t> seed_material_copy = seed_material;
absl::random_internal::MixIntoSeedMaterial(
absl::Span<uint32_t>(&v, 1),
absl::Span<uint32_t>(seed_material_copy.data(),
seed_material_copy.size()));
uint32_t changed_bits = 0;
for (size_t i = 0; i < seed_material.size(); i++) {
std::bitset<sizeof(uint32_t) * 8> bitset(seed_material[i] ^
seed_material_copy[i]);
changed_bits += bitset.count();
}
EXPECT_LE(changed_bits, 0.7 * sizeof(uint32_t) * 8 * seed_material.size());
EXPECT_GE(changed_bits, 0.3 * sizeof(uint32_t) * 8 * seed_material.size());
}
}
TEST(MixSequenceIntoSeedMaterial, AvalancheEffectTestOneBitShort) {
std::vector<uint32_t> seed_material = {1};
// For every 32-bit number with exactly one bit set, verify the avalanche
// effect holds. In order to reduce flakiness of tests, accept values
// anywhere in the range of 30%-70%.
for (uint32_t v = 1; v != 0; v <<= 1) {
std::vector<uint32_t> seed_material_copy = seed_material;
absl::random_internal::MixIntoSeedMaterial(
absl::Span<uint32_t>(&v, 1),
absl::Span<uint32_t>(seed_material_copy.data(),
seed_material_copy.size()));
uint32_t changed_bits = 0;
for (size_t i = 0; i < seed_material.size(); i++) {
std::bitset<sizeof(uint32_t) * 8> bitset(seed_material[i] ^
seed_material_copy[i]);
changed_bits += bitset.count();
}
EXPECT_LE(changed_bits, 0.7 * sizeof(uint32_t) * 8 * seed_material.size());
EXPECT_GE(changed_bits, 0.3 * sizeof(uint32_t) * 8 * seed_material.size());
}
}
} // namespace