mirror of
https://github.com/abseil/abseil-cpp.git
synced 2026-06-04 12:07:05 +08:00
Minor consistency cleanups to absl::BitGen mocking.
PiperOrigin-RevId: 735546502 Change-Id: I7daacb4018cf3f2719db7043b868b4df34ddaf05
This commit is contained in:
committed by
Copybara-Service
parent
c52afac4f8
commit
48f9175aa6
@@ -447,6 +447,7 @@ cc_test(
|
||||
deps = [
|
||||
":bit_gen_ref",
|
||||
":random",
|
||||
"//absl/base:config",
|
||||
"//absl/base:fast_type_id",
|
||||
"//absl/random/internal:sequence_urbg",
|
||||
"@googletest//:gtest",
|
||||
|
||||
@@ -59,6 +59,7 @@ absl_cc_test(
|
||||
LINKOPTS
|
||||
${ABSL_DEFAULT_LINKOPTS}
|
||||
DEPS
|
||||
absl::config
|
||||
absl::random_bit_gen_ref
|
||||
absl::random_random
|
||||
absl::random_internal_sequence_urbg
|
||||
|
||||
@@ -88,7 +88,7 @@ class MockHelpers;
|
||||
//
|
||||
class BitGenRef {
|
||||
// SFINAE to detect whether the URBG type includes a member matching
|
||||
// bool InvokeMock(base_internal::FastTypeIdType, void*, void*).
|
||||
// bool InvokeMock(key_id, args_tuple*, result*).
|
||||
//
|
||||
// These live inside BitGenRef so that they have friend access
|
||||
// to MockingBitGen. (see similar methods in DistributionCaller).
|
||||
@@ -158,19 +158,19 @@ class BitGenRef {
|
||||
|
||||
// Get a type-erased InvokeMock pointer.
|
||||
template <typename URBG>
|
||||
static bool MockCall(uintptr_t gen_ptr, base_internal::FastTypeIdType type,
|
||||
static bool MockCall(uintptr_t gen_ptr, base_internal::FastTypeIdType key_id,
|
||||
void* result, void* arg_tuple) {
|
||||
return reinterpret_cast<URBG*>(gen_ptr)->InvokeMock(type, result,
|
||||
return reinterpret_cast<URBG*>(gen_ptr)->InvokeMock(key_id, result,
|
||||
arg_tuple);
|
||||
}
|
||||
static bool NotAMock(uintptr_t, base_internal::FastTypeIdType, void*, void*) {
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool InvokeMock(base_internal::FastTypeIdType type, void* args_tuple,
|
||||
inline bool InvokeMock(base_internal::FastTypeIdType key_id, void* args_tuple,
|
||||
void* result) {
|
||||
if (mock_call_ == NotAMock) return false; // avoids an indirect call.
|
||||
return mock_call_(t_erased_gen_ptr_, type, args_tuple, result);
|
||||
return mock_call_(t_erased_gen_ptr_, key_id, args_tuple, result);
|
||||
}
|
||||
|
||||
uintptr_t t_erased_gen_ptr_;
|
||||
|
||||
@@ -15,8 +15,13 @@
|
||||
//
|
||||
#include "absl/random/bit_gen_ref.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/base/config.h"
|
||||
#include "absl/base/internal/fast_type_id.h"
|
||||
#include "absl/random/internal/sequence_urbg.h"
|
||||
#include "absl/random/random.h"
|
||||
@@ -34,7 +39,7 @@ class ConstBitGen {
|
||||
result_type operator()() { return 1; }
|
||||
|
||||
// InvokeMock method
|
||||
bool InvokeMock(base_internal::FastTypeIdType index, void*, void* result) {
|
||||
bool InvokeMock(base_internal::FastTypeIdType, void*, void* result) {
|
||||
*static_cast<int*>(result) = 42;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ struct DistributionCaller {
|
||||
static_assert(!std::is_pointer<URBG>::value,
|
||||
"You must pass a reference, not a pointer.");
|
||||
// SFINAE to detect whether the URBG type includes a member matching
|
||||
// bool InvokeMock(base_internal::FastTypeIdType, void*, void*).
|
||||
// bool InvokeMock(key_id, args_tuple*, result*).
|
||||
//
|
||||
// These live inside BitGenRef so that they have friend access
|
||||
// to MockingBitGen. (see similar methods in DistributionCaller).
|
||||
@@ -50,8 +50,8 @@ struct DistributionCaller {
|
||||
|
||||
template <class T>
|
||||
using invoke_mock_t = decltype(std::declval<T*>()->InvokeMock(
|
||||
std::declval<::absl::base_internal::FastTypeIdType>(),
|
||||
std::declval<void*>(), std::declval<void*>()));
|
||||
std::declval<base_internal::FastTypeIdType>(), std::declval<void*>(),
|
||||
std::declval<void*>()));
|
||||
|
||||
using HasInvokeMock = typename detector<invoke_mock_t, void, URBG>::type;
|
||||
|
||||
@@ -74,7 +74,7 @@ struct DistributionCaller {
|
||||
|
||||
ArgTupleT arg_tuple(std::forward<Args>(args)...);
|
||||
ResultT result;
|
||||
if (!urbg->InvokeMock(::absl::base_internal::FastTypeId<KeyT>(), &arg_tuple,
|
||||
if (!urbg->InvokeMock(base_internal::FastTypeId<KeyT>(), &arg_tuple,
|
||||
&result)) {
|
||||
auto dist = absl::make_from_tuple<DistrT>(arg_tuple);
|
||||
result = dist(*urbg);
|
||||
|
||||
@@ -82,7 +82,7 @@ class MockHelpers {
|
||||
Args&&... args) {
|
||||
ArgTupleT arg_tuple(std::forward<Args>(args)...);
|
||||
ReturnT result;
|
||||
if (urbg->InvokeMock(::absl::base_internal::FastTypeId<KeyT>(), &arg_tuple,
|
||||
if (urbg->InvokeMock(base_internal::FastTypeId<KeyT>(), &arg_tuple,
|
||||
&result)) {
|
||||
return result;
|
||||
}
|
||||
@@ -92,9 +92,9 @@ class MockHelpers {
|
||||
public:
|
||||
// InvokeMock is private; this provides access for some specialized use cases.
|
||||
template <typename URBG>
|
||||
static inline bool PrivateInvokeMock(URBG* urbg, IdType type,
|
||||
static inline bool PrivateInvokeMock(URBG* urbg, IdType key_id,
|
||||
void* args_tuple, void* result) {
|
||||
return urbg->InvokeMock(type, args_tuple, result);
|
||||
return urbg->InvokeMock(key_id, args_tuple, result);
|
||||
}
|
||||
|
||||
// Invoke a mock for the KeyT (may or may not be a signature).
|
||||
|
||||
@@ -177,7 +177,7 @@ class MockingBitGen {
|
||||
typename ValidatorT>
|
||||
auto RegisterMock(SelfT&, base_internal::FastTypeIdType type, ValidatorT)
|
||||
-> decltype(GetMockFnType(std::declval<ResultT>(),
|
||||
std::declval<ArgTupleT>())) & {
|
||||
std::declval<ArgTupleT>()))& {
|
||||
using MockFnType = decltype(GetMockFnType(std::declval<ResultT>(),
|
||||
std::declval<ArgTupleT>()));
|
||||
|
||||
@@ -203,8 +203,8 @@ class MockingBitGen {
|
||||
|
||||
// MockingBitGen::InvokeMock
|
||||
//
|
||||
// InvokeMock(FastTypeIdType, args, result) is the entrypoint for invoking
|
||||
// mocks registered on MockingBitGen.
|
||||
// bool InvokeMock(key_id, args_tuple*, result*) is the entrypoint
|
||||
// for invoking mocks registered on MockingBitGen.
|
||||
//
|
||||
// When no mocks are registered on the provided FastTypeIdType, returns false.
|
||||
// Otherwise attempts to invoke the mock function ResultT(Args...) that
|
||||
@@ -212,10 +212,10 @@ class MockingBitGen {
|
||||
// Requires tuple_args to point to a ArgTupleT, which is a std::tuple<Args...>
|
||||
// used to invoke the mock function.
|
||||
// Requires result to point to a ResultT, which is the result of the call.
|
||||
inline bool InvokeMock(base_internal::FastTypeIdType type, void* args_tuple,
|
||||
inline bool InvokeMock(base_internal::FastTypeIdType key_id, void* args_tuple,
|
||||
void* result) {
|
||||
// Trigger a mock, if there exists one that matches `param`.
|
||||
auto it = mocks_.find(type);
|
||||
auto it = mocks_.find(key_id);
|
||||
if (it == mocks_.end()) return false;
|
||||
it->second->Apply(args_tuple, result);
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user