mock_distributions.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright 2018 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. //
  15. // -----------------------------------------------------------------------------
  16. // File: mock_distributions.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This file contains mock distribution functions for use alongside an
  20. // `absl::MockingBitGen` object within the Googletest testing framework. Such
  21. // mocks are useful to provide deterministic values as return values within
  22. // (otherwise random) Abseil distribution functions.
  23. //
  24. // The return type of each function is a mock expectation object which
  25. // is used to set the match result.
  26. //
  27. // More information about the Googletest testing framework is available at
  28. // https://github.com/google/googletest
  29. //
  30. // EXPECT_CALL and ON_CALL need to be made within the same DLL component as
  31. // the call to absl::Uniform and related methods, otherwise mocking will fail
  32. // since the underlying implementation creates a type-specific pointer which
  33. // will be distinct across different DLL boundaries.
  34. //
  35. // Example:
  36. //
  37. // absl::MockingBitGen mock;
  38. // EXPECT_CALL(absl::MockUniform<int>(), Call(mock, 1, 1000))
  39. // .WillRepeatedly(testing::ReturnRoundRobin({20, 40}));
  40. //
  41. // EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000), 20);
  42. // EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000), 40);
  43. // EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000), 20);
  44. // EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000), 40);
  45. #ifndef ABSL_RANDOM_MOCK_DISTRIBUTIONS_H_
  46. #define ABSL_RANDOM_MOCK_DISTRIBUTIONS_H_
  47. #include <limits>
  48. #include <type_traits>
  49. #include <utility>
  50. #include "gmock/gmock.h"
  51. #include "gtest/gtest.h"
  52. #include "absl/meta/type_traits.h"
  53. #include "absl/random/distributions.h"
  54. #include "absl/random/internal/mock_overload_set.h"
  55. #include "absl/random/mocking_bit_gen.h"
  56. namespace absl {
  57. ABSL_NAMESPACE_BEGIN
  58. // -----------------------------------------------------------------------------
  59. // absl::MockUniform
  60. // -----------------------------------------------------------------------------
  61. //
  62. // Matches calls to absl::Uniform.
  63. //
  64. // `absl::MockUniform` is a class template used in conjunction with Googletest's
  65. // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an
  66. // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the
  67. // same way one would define mocks on a Googletest `MockFunction()`.
  68. //
  69. // Example:
  70. //
  71. // absl::MockingBitGen mock;
  72. // EXPECT_CALL(absl::MockUniform<uint32_t>(), Call(mock))
  73. // .WillOnce(Return(123456));
  74. // auto x = absl::Uniform<uint32_t>(mock);
  75. // assert(x == 123456)
  76. //
  77. template <typename R>
  78. using MockUniform = random_internal::MockOverloadSet<
  79. random_internal::UniformDistributionWrapper<R>,
  80. R(IntervalClosedOpenTag, MockingBitGen&, R, R),
  81. R(IntervalClosedClosedTag, MockingBitGen&, R, R),
  82. R(IntervalOpenOpenTag, MockingBitGen&, R, R),
  83. R(IntervalOpenClosedTag, MockingBitGen&, R, R), R(MockingBitGen&, R, R),
  84. R(MockingBitGen&)>;
  85. // -----------------------------------------------------------------------------
  86. // absl::MockBernoulli
  87. // -----------------------------------------------------------------------------
  88. //
  89. // Matches calls to absl::Bernoulli.
  90. //
  91. // `absl::MockBernoulli` is a class used in conjunction with Googletest's
  92. // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an
  93. // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the
  94. // same way one would define mocks on a Googletest `MockFunction()`.
  95. //
  96. // Example:
  97. //
  98. // absl::MockingBitGen mock;
  99. // EXPECT_CALL(absl::MockBernoulli(), Call(mock, testing::_))
  100. // .WillOnce(Return(false));
  101. // assert(absl::Bernoulli(mock, 0.5) == false);
  102. //
  103. using MockBernoulli =
  104. random_internal::MockOverloadSet<absl::bernoulli_distribution,
  105. bool(MockingBitGen&, double)>;
  106. // -----------------------------------------------------------------------------
  107. // absl::MockBeta
  108. // -----------------------------------------------------------------------------
  109. //
  110. // Matches calls to absl::Beta.
  111. //
  112. // `absl::MockBeta` is a class used in conjunction with Googletest's `ON_CALL()`
  113. // and `EXPECT_CALL()` macros. To use it, default-construct an instance of it
  114. // inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the same way one
  115. // would define mocks on a Googletest `MockFunction()`.
  116. //
  117. // Example:
  118. //
  119. // absl::MockingBitGen mock;
  120. // EXPECT_CALL(absl::MockBeta(), Call(mock, 3.0, 2.0))
  121. // .WillOnce(Return(0.567));
  122. // auto x = absl::Beta<double>(mock, 3.0, 2.0);
  123. // assert(x == 0.567);
  124. //
  125. template <typename RealType>
  126. using MockBeta =
  127. random_internal::MockOverloadSet<absl::beta_distribution<RealType>,
  128. RealType(MockingBitGen&, RealType,
  129. RealType)>;
  130. // -----------------------------------------------------------------------------
  131. // absl::MockExponential
  132. // -----------------------------------------------------------------------------
  133. //
  134. // Matches calls to absl::Exponential.
  135. //
  136. // `absl::MockExponential` is a class template used in conjunction with
  137. // Googletest's `ON_CALL()` and `EXPECT_CALL()` macros. To use it,
  138. // default-construct an instance of it inside `ON_CALL()` or `EXPECT_CALL()`,
  139. // and use `Call(...)` the same way one would define mocks on a
  140. // Googletest `MockFunction()`.
  141. //
  142. // Example:
  143. //
  144. // absl::MockingBitGen mock;
  145. // EXPECT_CALL(absl::MockExponential<double>(), Call(mock, 0.5))
  146. // .WillOnce(Return(12.3456789));
  147. // auto x = absl::Exponential<double>(mock, 0.5);
  148. // assert(x == 12.3456789)
  149. //
  150. template <typename RealType>
  151. using MockExponential =
  152. random_internal::MockOverloadSet<absl::exponential_distribution<RealType>,
  153. RealType(MockingBitGen&, RealType)>;
  154. // -----------------------------------------------------------------------------
  155. // absl::MockGaussian
  156. // -----------------------------------------------------------------------------
  157. //
  158. // Matches calls to absl::Gaussian.
  159. //
  160. // `absl::MockGaussian` is a class template used in conjunction with
  161. // Googletest's `ON_CALL()` and `EXPECT_CALL()` macros. To use it,
  162. // default-construct an instance of it inside `ON_CALL()` or `EXPECT_CALL()`,
  163. // and use `Call(...)` the same way one would define mocks on a
  164. // Googletest `MockFunction()`.
  165. //
  166. // Example:
  167. //
  168. // absl::MockingBitGen mock;
  169. // EXPECT_CALL(absl::MockGaussian<double>(), Call(mock, 16.3, 3.3))
  170. // .WillOnce(Return(12.3456789));
  171. // auto x = absl::Gaussian<double>(mock, 16.3, 3.3);
  172. // assert(x == 12.3456789)
  173. //
  174. template <typename RealType>
  175. using MockGaussian =
  176. random_internal::MockOverloadSet<absl::gaussian_distribution<RealType>,
  177. RealType(MockingBitGen&, RealType,
  178. RealType)>;
  179. // -----------------------------------------------------------------------------
  180. // absl::MockLogUniform
  181. // -----------------------------------------------------------------------------
  182. //
  183. // Matches calls to absl::LogUniform.
  184. //
  185. // `absl::MockLogUniform` is a class template used in conjunction with
  186. // Googletest's `ON_CALL()` and `EXPECT_CALL()` macros. To use it,
  187. // default-construct an instance of it inside `ON_CALL()` or `EXPECT_CALL()`,
  188. // and use `Call(...)` the same way one would define mocks on a
  189. // Googletest `MockFunction()`.
  190. //
  191. // Example:
  192. //
  193. // absl::MockingBitGen mock;
  194. // EXPECT_CALL(absl::MockLogUniform<int>(), Call(mock, 10, 10000, 10))
  195. // .WillOnce(Return(1221));
  196. // auto x = absl::LogUniform<int>(mock, 10, 10000, 10);
  197. // assert(x == 1221)
  198. //
  199. template <typename IntType>
  200. using MockLogUniform = random_internal::MockOverloadSet<
  201. absl::log_uniform_int_distribution<IntType>,
  202. IntType(MockingBitGen&, IntType, IntType, IntType)>;
  203. // -----------------------------------------------------------------------------
  204. // absl::MockPoisson
  205. // -----------------------------------------------------------------------------
  206. //
  207. // Matches calls to absl::Poisson.
  208. //
  209. // `absl::MockPoisson` is a class template used in conjunction with Googletest's
  210. // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an
  211. // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the
  212. // same way one would define mocks on a Googletest `MockFunction()`.
  213. //
  214. // Example:
  215. //
  216. // absl::MockingBitGen mock;
  217. // EXPECT_CALL(absl::MockPoisson<int>(), Call(mock, 2.0))
  218. // .WillOnce(Return(1221));
  219. // auto x = absl::Poisson<int>(mock, 2.0);
  220. // assert(x == 1221)
  221. //
  222. template <typename IntType>
  223. using MockPoisson =
  224. random_internal::MockOverloadSet<absl::poisson_distribution<IntType>,
  225. IntType(MockingBitGen&, double)>;
  226. // -----------------------------------------------------------------------------
  227. // absl::MockZipf
  228. // -----------------------------------------------------------------------------
  229. //
  230. // Matches calls to absl::Zipf.
  231. //
  232. // `absl::MockZipf` is a class template used in conjunction with Googletest's
  233. // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an
  234. // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the
  235. // same way one would define mocks on a Googletest `MockFunction()`.
  236. //
  237. // Example:
  238. //
  239. // absl::MockingBitGen mock;
  240. // EXPECT_CALL(absl::MockZipf<int>(), Call(mock, 1000000, 2.0, 1.0))
  241. // .WillOnce(Return(1221));
  242. // auto x = absl::Zipf<int>(mock, 1000000, 2.0, 1.0);
  243. // assert(x == 1221)
  244. //
  245. template <typename IntType>
  246. using MockZipf =
  247. random_internal::MockOverloadSet<absl::zipf_distribution<IntType>,
  248. IntType(MockingBitGen&, IntType, double,
  249. double)>;
  250. ABSL_NAMESPACE_END
  251. } // namespace absl
  252. #endif // ABSL_RANDOM_MOCK_DISTRIBUTIONS_H_