tvl-depot/absl/random/mocking_bit_gen_test.cc

348 lines
12 KiB
C++
Raw Normal View History

//
// Copyright 2018 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/mocking_bit_gen.h"
#include <numeric>
#include <random>
#include "gmock/gmock.h"
#include "gtest/gtest-spi.h"
#include "gtest/gtest.h"
#include "absl/random/bit_gen_ref.h"
#include "absl/random/mock_distributions.h"
#include "absl/random/random.h"
namespace {
using ::testing::Ne;
using ::testing::Return;
TEST(BasicMocking, AllDistributionsAreOverridable) {
absl::MockingBitGen gen;
EXPECT_NE(absl::Uniform<int>(gen, 1, 1000000), 20);
EXPECT_CALL(absl::MockUniform<int>(), Call(gen, 1, 1000000))
.WillOnce(Return(20));
EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000000), 20);
EXPECT_NE(absl::Uniform<double>(gen, 0.0, 100.0), 5.0);
EXPECT_CALL(absl::MockUniform<double>(), Call(gen, 0.0, 100.0))
.WillOnce(Return(5.0));
EXPECT_EQ(absl::Uniform<double>(gen, 0.0, 100.0), 5.0);
EXPECT_NE(absl::Exponential<double>(gen, 1.0), 42);
EXPECT_CALL(absl::MockExponential<double>(), Call(gen, 1.0))
.WillOnce(Return(42));
EXPECT_EQ(absl::Exponential<double>(gen, 1.0), 42);
EXPECT_NE(absl::Poisson<int>(gen, 1.0), 500);
EXPECT_CALL(absl::MockPoisson<int>(), Call(gen, 1.0)).WillOnce(Return(500));
EXPECT_EQ(absl::Poisson<int>(gen, 1.0), 500);
EXPECT_NE(absl::Bernoulli(gen, 0.000001), true);
EXPECT_CALL(absl::MockBernoulli(), Call(gen, 0.000001))
.WillOnce(Return(true));
EXPECT_EQ(absl::Bernoulli(gen, 0.000001), true);
EXPECT_NE(absl::Zipf<int>(gen, 1000000, 2.0, 1.0), 1221);
EXPECT_CALL(absl::MockZipf<int>(), Call(gen, 1000000, 2.0, 1.0))
.WillOnce(Return(1221));
EXPECT_EQ(absl::Zipf<int>(gen, 1000000, 2.0, 1.0), 1221);
EXPECT_NE(absl::Gaussian<double>(gen, 0.0, 1.0), 0.001);
EXPECT_CALL(absl::MockGaussian<double>(), Call(gen, 0.0, 1.0))
.WillOnce(Return(0.001));
EXPECT_EQ(absl::Gaussian<double>(gen, 0.0, 1.0), 0.001);
Export of internal Abseil changes -- 7f6c15aadc4d97e217dd446518dbb4fdc86b36a3 by Derek Mauro <dmauro@google.com>: Upgrade GCC automated testing to use GCC 9.2 and Cmake 3.16.2 PiperOrigin-RevId: 288488783 -- a978cee848d3cf65b0826c981bfd81022fc36660 by Abseil Team <absl-team@google.com>: Removing formatting traits that were only used internally. ON_CALL/EXPECT_CALL do a sufficient job here. PiperOrigin-RevId: 288386509 -- fdec6f40293d5883220f1f0ea1261f7c5b60a66e by Derek Mauro <dmauro@google.com>: Upgrade MacOS tests to use Bazel 2.0.0 PiperOrigin-RevId: 288373298 -- 465865c4123e9481ab50ea0527e92b39519704dd by Derek Mauro <dmauro@google.com>: Changes to support GCC 9 * Fix several -Wredundant-move warnings * Remove FlatHashMap.Any test, which basically doesn't work on any platform any more (see https://cplusplus.github.io/LWG/lwg-active.html#3121) * Fix a constant sign-compare warning * Conditionally compile out the PoisonHash test which doesn't build PiperOrigin-RevId: 288360204 -- 57c4bb07fc58e7dd2a04f3c45027aab5ecaccf25 by Andy Soffer <asoffer@google.com>: Deflaking MockingBitGen test. Because MockingBitGen can return random values, it is inherently flaky. For log-unifrom, 2040 is a common enough value that tests failed unreasonably frequently. Replacing it with a significantly larger value so as to be much less common. 50000 is a good choice because it is (tied for) the least likely to occur randomly from this distribution, but is still in the distribution. PiperOrigin-RevId: 288360112 -- 86f38e4109899d972de353b1c556c018cfe37956 by Matt Calabrese <calabrese@google.com>: Remove construction tests for the internal `CompressedTuple<std::any>` instantiation. This was not guaranteed to work for the reasons that `std::tuple<std::any>` copy construction does not actually work by standard specification (some implementations introduce workarounds for this). In GCC9, `CompressedTuple<std::any>` and `std::tuple<std::any>` both fail for the same reasons, and a proper "fix" requires updating `std::any`, which is out of our control. PiperOrigin-RevId: 288351977 GitOrigin-RevId: 7f6c15aadc4d97e217dd446518dbb4fdc86b36a3 Change-Id: I5d5c62bd297dc0ff1f2970ff076bb5cd088a7e4c
2020-01-07 15:56:49 +01:00
EXPECT_NE(absl::LogUniform<int>(gen, 0, 1000000, 2), 500000);
EXPECT_CALL(absl::MockLogUniform<int>(), Call(gen, 0, 1000000, 2))
Export of internal Abseil changes -- 7f6c15aadc4d97e217dd446518dbb4fdc86b36a3 by Derek Mauro <dmauro@google.com>: Upgrade GCC automated testing to use GCC 9.2 and Cmake 3.16.2 PiperOrigin-RevId: 288488783 -- a978cee848d3cf65b0826c981bfd81022fc36660 by Abseil Team <absl-team@google.com>: Removing formatting traits that were only used internally. ON_CALL/EXPECT_CALL do a sufficient job here. PiperOrigin-RevId: 288386509 -- fdec6f40293d5883220f1f0ea1261f7c5b60a66e by Derek Mauro <dmauro@google.com>: Upgrade MacOS tests to use Bazel 2.0.0 PiperOrigin-RevId: 288373298 -- 465865c4123e9481ab50ea0527e92b39519704dd by Derek Mauro <dmauro@google.com>: Changes to support GCC 9 * Fix several -Wredundant-move warnings * Remove FlatHashMap.Any test, which basically doesn't work on any platform any more (see https://cplusplus.github.io/LWG/lwg-active.html#3121) * Fix a constant sign-compare warning * Conditionally compile out the PoisonHash test which doesn't build PiperOrigin-RevId: 288360204 -- 57c4bb07fc58e7dd2a04f3c45027aab5ecaccf25 by Andy Soffer <asoffer@google.com>: Deflaking MockingBitGen test. Because MockingBitGen can return random values, it is inherently flaky. For log-unifrom, 2040 is a common enough value that tests failed unreasonably frequently. Replacing it with a significantly larger value so as to be much less common. 50000 is a good choice because it is (tied for) the least likely to occur randomly from this distribution, but is still in the distribution. PiperOrigin-RevId: 288360112 -- 86f38e4109899d972de353b1c556c018cfe37956 by Matt Calabrese <calabrese@google.com>: Remove construction tests for the internal `CompressedTuple<std::any>` instantiation. This was not guaranteed to work for the reasons that `std::tuple<std::any>` copy construction does not actually work by standard specification (some implementations introduce workarounds for this). In GCC9, `CompressedTuple<std::any>` and `std::tuple<std::any>` both fail for the same reasons, and a proper "fix" requires updating `std::any`, which is out of our control. PiperOrigin-RevId: 288351977 GitOrigin-RevId: 7f6c15aadc4d97e217dd446518dbb4fdc86b36a3 Change-Id: I5d5c62bd297dc0ff1f2970ff076bb5cd088a7e4c
2020-01-07 15:56:49 +01:00
.WillOnce(Return(500000));
EXPECT_EQ(absl::LogUniform<int>(gen, 0, 1000000, 2), 500000);
}
TEST(BasicMocking, OnDistribution) {
absl::MockingBitGen gen;
EXPECT_NE(absl::Uniform<int>(gen, 1, 1000000), 20);
ON_CALL(absl::MockUniform<int>(), Call(gen, 1, 1000000))
.WillByDefault(Return(20));
EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000000), 20);
EXPECT_NE(absl::Uniform<double>(gen, 0.0, 100.0), 5.0);
ON_CALL(absl::MockUniform<double>(), Call(gen, 0.0, 100.0))
.WillByDefault(Return(5.0));
EXPECT_EQ(absl::Uniform<double>(gen, 0.0, 100.0), 5.0);
EXPECT_NE(absl::Exponential<double>(gen, 1.0), 42);
ON_CALL(absl::MockExponential<double>(), Call(gen, 1.0))
.WillByDefault(Return(42));
EXPECT_EQ(absl::Exponential<double>(gen, 1.0), 42);
EXPECT_NE(absl::Poisson<int>(gen, 1.0), 500);
ON_CALL(absl::MockPoisson<int>(), Call(gen, 1.0)).WillByDefault(Return(500));
EXPECT_EQ(absl::Poisson<int>(gen, 1.0), 500);
EXPECT_NE(absl::Bernoulli(gen, 0.000001), true);
ON_CALL(absl::MockBernoulli(), Call(gen, 0.000001))
.WillByDefault(Return(true));
EXPECT_EQ(absl::Bernoulli(gen, 0.000001), true);
EXPECT_NE(absl::Zipf<int>(gen, 1000000, 2.0, 1.0), 1221);
ON_CALL(absl::MockZipf<int>(), Call(gen, 1000000, 2.0, 1.0))
.WillByDefault(Return(1221));
EXPECT_EQ(absl::Zipf<int>(gen, 1000000, 2.0, 1.0), 1221);
EXPECT_NE(absl::Gaussian<double>(gen, 0.0, 1.0), 0.001);
ON_CALL(absl::MockGaussian<double>(), Call(gen, 0.0, 1.0))
.WillByDefault(Return(0.001));
EXPECT_EQ(absl::Gaussian<double>(gen, 0.0, 1.0), 0.001);
EXPECT_NE(absl::LogUniform<int>(gen, 0, 1000000, 2), 2040);
ON_CALL(absl::MockLogUniform<int>(), Call(gen, 0, 1000000, 2))
.WillByDefault(Return(2040));
EXPECT_EQ(absl::LogUniform<int>(gen, 0, 1000000, 2), 2040);
}
TEST(BasicMocking, GMockMatchers) {
absl::MockingBitGen gen;
EXPECT_NE(absl::Zipf<int>(gen, 1000000, 2.0, 1.0), 1221);
ON_CALL(absl::MockZipf<int>(), Call(gen, 1000000, 2.0, 1.0))
.WillByDefault(Return(1221));
EXPECT_EQ(absl::Zipf<int>(gen, 1000000, 2.0, 1.0), 1221);
}
TEST(BasicMocking, OverridesWithMultipleGMockExpectations) {
absl::MockingBitGen gen;
EXPECT_CALL(absl::MockUniform<int>(), Call(gen, 1, 10000))
.WillOnce(Return(20))
.WillOnce(Return(40))
.WillOnce(Return(60));
EXPECT_EQ(absl::Uniform(gen, 1, 10000), 20);
EXPECT_EQ(absl::Uniform(gen, 1, 10000), 40);
EXPECT_EQ(absl::Uniform(gen, 1, 10000), 60);
}
TEST(BasicMocking, DefaultArgument) {
absl::MockingBitGen gen;
ON_CALL(absl::MockExponential<double>(), Call(gen, 1.0))
.WillByDefault(Return(200));
EXPECT_EQ(absl::Exponential<double>(gen), 200);
EXPECT_EQ(absl::Exponential<double>(gen, 1.0), 200);
}
TEST(BasicMocking, MultipleGenerators) {
auto get_value = [](absl::BitGenRef gen_ref) {
return absl::Uniform(gen_ref, 1, 1000000);
};
absl::MockingBitGen unmocked_generator;
absl::MockingBitGen mocked_with_3;
absl::MockingBitGen mocked_with_11;
EXPECT_CALL(absl::MockUniform<int>(), Call(mocked_with_3, 1, 1000000))
.WillOnce(Return(3))
.WillRepeatedly(Return(17));
EXPECT_CALL(absl::MockUniform<int>(), Call(mocked_with_11, 1, 1000000))
.WillOnce(Return(11))
.WillRepeatedly(Return(17));
// Ensure that unmocked generator generates neither value.
int unmocked_value = get_value(unmocked_generator);
EXPECT_NE(unmocked_value, 3);
EXPECT_NE(unmocked_value, 11);
// Mocked generators should generate their mocked values.
EXPECT_EQ(get_value(mocked_with_3), 3);
EXPECT_EQ(get_value(mocked_with_11), 11);
// Ensure that the mocks have expired.
EXPECT_NE(get_value(mocked_with_3), 3);
EXPECT_NE(get_value(mocked_with_11), 11);
}
TEST(BasicMocking, MocksNotTrigeredForIncorrectTypes) {
absl::MockingBitGen gen;
EXPECT_CALL(absl::MockUniform<uint32_t>(), Call(gen)).WillOnce(Return(42));
EXPECT_NE(absl::Uniform<uint16_t>(gen), 42); // Not mocked
EXPECT_EQ(absl::Uniform<uint32_t>(gen), 42); // Mock triggered
}
TEST(BasicMocking, FailsOnUnsatisfiedMocks) {
EXPECT_NONFATAL_FAILURE(
[]() {
absl::MockingBitGen gen;
EXPECT_CALL(absl::MockExponential<double>(), Call(gen, 1.0))
.WillOnce(Return(3.0));
// Does not call absl::Exponential().
}(),
"unsatisfied and active");
}
TEST(OnUniform, RespectsUniformIntervalSemantics) {
absl::MockingBitGen gen;
EXPECT_CALL(absl::MockUniform<int>(),
Call(absl::IntervalClosed, gen, 1, 1000000))
.WillOnce(Return(301));
EXPECT_NE(absl::Uniform(gen, 1, 1000000), 301); // Not mocked
EXPECT_EQ(absl::Uniform(absl::IntervalClosed, gen, 1, 1000000), 301);
}
TEST(OnUniform, RespectsNoArgUnsignedShorthand) {
absl::MockingBitGen gen;
EXPECT_CALL(absl::MockUniform<uint32_t>(), Call(gen)).WillOnce(Return(42));
EXPECT_EQ(absl::Uniform<uint32_t>(gen), 42);
}
TEST(RepeatedlyModifier, ForceSnakeEyesForManyDice) {
auto roll_some_dice = [](absl::BitGenRef gen_ref) {
std::vector<int> results(16);
for (auto& r : results) {
r = absl::Uniform(absl::IntervalClosed, gen_ref, 1, 6);
}
return results;
};
std::vector<int> results;
absl::MockingBitGen gen;
// Without any mocked calls, not all dice roll a "6".
results = roll_some_dice(gen);
EXPECT_LT(std::accumulate(std::begin(results), std::end(results), 0),
results.size() * 6);
// Verify that we can force all "6"-rolls, with mocking.
ON_CALL(absl::MockUniform<int>(), Call(absl::IntervalClosed, gen, 1, 6))
.WillByDefault(Return(6));
results = roll_some_dice(gen);
EXPECT_EQ(std::accumulate(std::begin(results), std::end(results), 0),
results.size() * 6);
}
TEST(WillOnce, DistinctCounters) {
absl::MockingBitGen gen;
EXPECT_CALL(absl::MockUniform<int>(), Call(gen, 1, 1000000))
.Times(3)
.WillRepeatedly(Return(0));
EXPECT_CALL(absl::MockUniform<int>(), Call(gen, 1000001, 2000000))
.Times(3)
.WillRepeatedly(Return(1));
EXPECT_EQ(absl::Uniform(gen, 1000001, 2000000), 1);
EXPECT_EQ(absl::Uniform(gen, 1, 1000000), 0);
EXPECT_EQ(absl::Uniform(gen, 1000001, 2000000), 1);
EXPECT_EQ(absl::Uniform(gen, 1, 1000000), 0);
EXPECT_EQ(absl::Uniform(gen, 1000001, 2000000), 1);
EXPECT_EQ(absl::Uniform(gen, 1, 1000000), 0);
}
TEST(TimesModifier, ModifierSaturatesAndExpires) {
EXPECT_NONFATAL_FAILURE(
[]() {
absl::MockingBitGen gen;
EXPECT_CALL(absl::MockUniform<int>(), Call(gen, 1, 1000000))
.Times(3)
.WillRepeatedly(Return(15))
.RetiresOnSaturation();
EXPECT_EQ(absl::Uniform(gen, 1, 1000000), 15);
EXPECT_EQ(absl::Uniform(gen, 1, 1000000), 15);
EXPECT_EQ(absl::Uniform(gen, 1, 1000000), 15);
// Times(3) has expired - Should get a different value now.
EXPECT_NE(absl::Uniform(gen, 1, 1000000), 15);
}(),
"");
}
TEST(TimesModifier, Times0) {
absl::MockingBitGen gen;
EXPECT_CALL(absl::MockBernoulli(), Call(gen, 0.0)).Times(0);
EXPECT_CALL(absl::MockPoisson<int>(), Call(gen, 1.0)).Times(0);
}
TEST(AnythingMatcher, MatchesAnyArgument) {
using testing::_;
{
absl::MockingBitGen gen;
ON_CALL(absl::MockUniform<int>(), Call(absl::IntervalClosed, gen, _, 1000))
.WillByDefault(Return(11));
ON_CALL(absl::MockUniform<int>(),
Call(absl::IntervalClosed, gen, _, Ne(1000)))
.WillByDefault(Return(99));
EXPECT_EQ(absl::Uniform(absl::IntervalClosed, gen, 10, 1000000), 99);
EXPECT_EQ(absl::Uniform(absl::IntervalClosed, gen, 10, 1000), 11);
}
{
absl::MockingBitGen gen;
ON_CALL(absl::MockUniform<int>(), Call(gen, 1, _))
.WillByDefault(Return(25));
ON_CALL(absl::MockUniform<int>(), Call(gen, Ne(1), _))
.WillByDefault(Return(99));
EXPECT_EQ(absl::Uniform(gen, 3, 1000000), 99);
EXPECT_EQ(absl::Uniform(gen, 1, 1000000), 25);
}
{
absl::MockingBitGen gen;
ON_CALL(absl::MockUniform<int>(), Call(gen, _, _))
.WillByDefault(Return(145));
EXPECT_EQ(absl::Uniform(gen, 1, 1000), 145);
EXPECT_EQ(absl::Uniform(gen, 10, 1000), 145);
EXPECT_EQ(absl::Uniform(gen, 100, 1000), 145);
}
}
TEST(AnythingMatcher, WithWillByDefault) {
using testing::_;
absl::MockingBitGen gen;
std::vector<int> values = {11, 22, 33, 44, 55, 66, 77, 88, 99, 1010};
ON_CALL(absl::MockUniform<size_t>(), Call(gen, 0, _))
.WillByDefault(Return(0));
for (int i = 0; i < 100; i++) {
auto& elem = values[absl::Uniform(gen, 0u, values.size())];
EXPECT_EQ(elem, 11);
}
}
TEST(BasicMocking, WillByDefaultWithArgs) {
using testing::_;
absl::MockingBitGen gen;
ON_CALL(absl::MockPoisson<int>(), Call(gen, _))
.WillByDefault(
[](double lambda) { return static_cast<int>(lambda * 10); });
EXPECT_EQ(absl::Poisson<int>(gen, 1.7), 17);
EXPECT_EQ(absl::Poisson<int>(gen, 0.03), 0);
}
TEST(MockingBitGen, InSequenceSucceedsInOrder) {
absl::MockingBitGen gen;
testing::InSequence seq;
EXPECT_CALL(absl::MockPoisson<int>(), Call(gen, 1.0)).WillOnce(Return(3));
EXPECT_CALL(absl::MockPoisson<int>(), Call(gen, 2.0)).WillOnce(Return(4));
EXPECT_EQ(absl::Poisson<int>(gen, 1.0), 3);
EXPECT_EQ(absl::Poisson<int>(gen, 2.0), 4);
}
} // namespace