mock_distributions.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. // `y_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 y_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. // y_absl::MockingBitGen mock;
  38. // EXPECT_CALL(y_absl::MockUniform<int>(), Call(mock, 1, 1000))
  39. // .WillRepeatedly(testing::ReturnRoundRobin({20, 40}));
  40. //
  41. // EXPECT_EQ(y_absl::Uniform<int>(gen, 1, 1000), 20);
  42. // EXPECT_EQ(y_absl::Uniform<int>(gen, 1, 1000), 40);
  43. // EXPECT_EQ(y_absl::Uniform<int>(gen, 1, 1000), 20);
  44. // EXPECT_EQ(y_absl::Uniform<int>(gen, 1, 1000), 40);
  45. #ifndef Y_ABSL_RANDOM_MOCK_DISTRIBUTIONS_H_
  46. #define Y_ABSL_RANDOM_MOCK_DISTRIBUTIONS_H_
  47. #include "y_absl/base/config.h"
  48. #include "y_absl/random/bernoulli_distribution.h"
  49. #include "y_absl/random/beta_distribution.h"
  50. #include "y_absl/random/distributions.h"
  51. #include "y_absl/random/exponential_distribution.h"
  52. #include "y_absl/random/gaussian_distribution.h"
  53. #include "y_absl/random/internal/mock_overload_set.h"
  54. #include "y_absl/random/internal/mock_validators.h"
  55. #include "y_absl/random/log_uniform_int_distribution.h"
  56. #include "y_absl/random/mocking_bit_gen.h"
  57. #include "y_absl/random/poisson_distribution.h"
  58. #include "y_absl/random/zipf_distribution.h"
  59. namespace y_absl {
  60. Y_ABSL_NAMESPACE_BEGIN
  61. // -----------------------------------------------------------------------------
  62. // y_absl::MockUniform
  63. // -----------------------------------------------------------------------------
  64. //
  65. // Matches calls to y_absl::Uniform.
  66. //
  67. // `y_absl::MockUniform` is a class template used in conjunction with Googletest's
  68. // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an
  69. // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the
  70. // same way one would define mocks on a Googletest `MockFunction()`.
  71. //
  72. // Example:
  73. //
  74. // y_absl::MockingBitGen mock;
  75. // EXPECT_CALL(y_absl::MockUniform<uint32_t>(), Call(mock))
  76. // .WillOnce(Return(123456));
  77. // auto x = y_absl::Uniform<uint32_t>(mock);
  78. // assert(x == 123456)
  79. //
  80. template <typename R>
  81. using MockUniform = random_internal::MockOverloadSetWithValidator<
  82. random_internal::UniformDistributionWrapper<R>,
  83. random_internal::UniformDistributionValidator<R>,
  84. R(IntervalClosedOpenTag, MockingBitGen&, R, R),
  85. R(IntervalClosedClosedTag, MockingBitGen&, R, R),
  86. R(IntervalOpenOpenTag, MockingBitGen&, R, R),
  87. R(IntervalOpenClosedTag, MockingBitGen&, R, R), R(MockingBitGen&, R, R),
  88. R(MockingBitGen&)>;
  89. // -----------------------------------------------------------------------------
  90. // y_absl::MockBernoulli
  91. // -----------------------------------------------------------------------------
  92. //
  93. // Matches calls to y_absl::Bernoulli.
  94. //
  95. // `y_absl::MockBernoulli` is a class used in conjunction with Googletest's
  96. // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an
  97. // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the
  98. // same way one would define mocks on a Googletest `MockFunction()`.
  99. //
  100. // Example:
  101. //
  102. // y_absl::MockingBitGen mock;
  103. // EXPECT_CALL(y_absl::MockBernoulli(), Call(mock, testing::_))
  104. // .WillOnce(Return(false));
  105. // assert(y_absl::Bernoulli(mock, 0.5) == false);
  106. //
  107. using MockBernoulli =
  108. random_internal::MockOverloadSet<y_absl::bernoulli_distribution,
  109. bool(MockingBitGen&, double)>;
  110. // -----------------------------------------------------------------------------
  111. // y_absl::MockBeta
  112. // -----------------------------------------------------------------------------
  113. //
  114. // Matches calls to y_absl::Beta.
  115. //
  116. // `y_absl::MockBeta` is a class used in conjunction with Googletest's `ON_CALL()`
  117. // and `EXPECT_CALL()` macros. To use it, default-construct an instance of it
  118. // inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the same way one
  119. // would define mocks on a Googletest `MockFunction()`.
  120. //
  121. // Example:
  122. //
  123. // y_absl::MockingBitGen mock;
  124. // EXPECT_CALL(y_absl::MockBeta(), Call(mock, 3.0, 2.0))
  125. // .WillOnce(Return(0.567));
  126. // auto x = y_absl::Beta<double>(mock, 3.0, 2.0);
  127. // assert(x == 0.567);
  128. //
  129. template <typename RealType>
  130. using MockBeta =
  131. random_internal::MockOverloadSet<y_absl::beta_distribution<RealType>,
  132. RealType(MockingBitGen&, RealType,
  133. RealType)>;
  134. // -----------------------------------------------------------------------------
  135. // y_absl::MockExponential
  136. // -----------------------------------------------------------------------------
  137. //
  138. // Matches calls to y_absl::Exponential.
  139. //
  140. // `y_absl::MockExponential` is a class template used in conjunction with
  141. // Googletest's `ON_CALL()` and `EXPECT_CALL()` macros. To use it,
  142. // default-construct an instance of it inside `ON_CALL()` or `EXPECT_CALL()`,
  143. // and use `Call(...)` the same way one would define mocks on a
  144. // Googletest `MockFunction()`.
  145. //
  146. // Example:
  147. //
  148. // y_absl::MockingBitGen mock;
  149. // EXPECT_CALL(y_absl::MockExponential<double>(), Call(mock, 0.5))
  150. // .WillOnce(Return(12.3456789));
  151. // auto x = y_absl::Exponential<double>(mock, 0.5);
  152. // assert(x == 12.3456789)
  153. //
  154. template <typename RealType>
  155. using MockExponential =
  156. random_internal::MockOverloadSet<y_absl::exponential_distribution<RealType>,
  157. RealType(MockingBitGen&, RealType)>;
  158. // -----------------------------------------------------------------------------
  159. // y_absl::MockGaussian
  160. // -----------------------------------------------------------------------------
  161. //
  162. // Matches calls to y_absl::Gaussian.
  163. //
  164. // `y_absl::MockGaussian` is a class template used in conjunction with
  165. // Googletest's `ON_CALL()` and `EXPECT_CALL()` macros. To use it,
  166. // default-construct an instance of it inside `ON_CALL()` or `EXPECT_CALL()`,
  167. // and use `Call(...)` the same way one would define mocks on a
  168. // Googletest `MockFunction()`.
  169. //
  170. // Example:
  171. //
  172. // y_absl::MockingBitGen mock;
  173. // EXPECT_CALL(y_absl::MockGaussian<double>(), Call(mock, 16.3, 3.3))
  174. // .WillOnce(Return(12.3456789));
  175. // auto x = y_absl::Gaussian<double>(mock, 16.3, 3.3);
  176. // assert(x == 12.3456789)
  177. //
  178. template <typename RealType>
  179. using MockGaussian =
  180. random_internal::MockOverloadSet<y_absl::gaussian_distribution<RealType>,
  181. RealType(MockingBitGen&, RealType,
  182. RealType)>;
  183. // -----------------------------------------------------------------------------
  184. // y_absl::MockLogUniform
  185. // -----------------------------------------------------------------------------
  186. //
  187. // Matches calls to y_absl::LogUniform.
  188. //
  189. // `y_absl::MockLogUniform` is a class template used in conjunction with
  190. // Googletest's `ON_CALL()` and `EXPECT_CALL()` macros. To use it,
  191. // default-construct an instance of it inside `ON_CALL()` or `EXPECT_CALL()`,
  192. // and use `Call(...)` the same way one would define mocks on a
  193. // Googletest `MockFunction()`.
  194. //
  195. // Example:
  196. //
  197. // y_absl::MockingBitGen mock;
  198. // EXPECT_CALL(y_absl::MockLogUniform<int>(), Call(mock, 10, 10000, 10))
  199. // .WillOnce(Return(1221));
  200. // auto x = y_absl::LogUniform<int>(mock, 10, 10000, 10);
  201. // assert(x == 1221)
  202. //
  203. template <typename IntType>
  204. using MockLogUniform = random_internal::MockOverloadSet<
  205. y_absl::log_uniform_int_distribution<IntType>,
  206. IntType(MockingBitGen&, IntType, IntType, IntType)>;
  207. // -----------------------------------------------------------------------------
  208. // y_absl::MockPoisson
  209. // -----------------------------------------------------------------------------
  210. //
  211. // Matches calls to y_absl::Poisson.
  212. //
  213. // `y_absl::MockPoisson` is a class template used in conjunction with Googletest's
  214. // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an
  215. // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the
  216. // same way one would define mocks on a Googletest `MockFunction()`.
  217. //
  218. // Example:
  219. //
  220. // y_absl::MockingBitGen mock;
  221. // EXPECT_CALL(y_absl::MockPoisson<int>(), Call(mock, 2.0))
  222. // .WillOnce(Return(1221));
  223. // auto x = y_absl::Poisson<int>(mock, 2.0);
  224. // assert(x == 1221)
  225. //
  226. template <typename IntType>
  227. using MockPoisson =
  228. random_internal::MockOverloadSet<y_absl::poisson_distribution<IntType>,
  229. IntType(MockingBitGen&, double)>;
  230. // -----------------------------------------------------------------------------
  231. // y_absl::MockZipf
  232. // -----------------------------------------------------------------------------
  233. //
  234. // Matches calls to y_absl::Zipf.
  235. //
  236. // `y_absl::MockZipf` is a class template used in conjunction with Googletest's
  237. // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an
  238. // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the
  239. // same way one would define mocks on a Googletest `MockFunction()`.
  240. //
  241. // Example:
  242. //
  243. // y_absl::MockingBitGen mock;
  244. // EXPECT_CALL(y_absl::MockZipf<int>(), Call(mock, 1000000, 2.0, 1.0))
  245. // .WillOnce(Return(1221));
  246. // auto x = y_absl::Zipf<int>(mock, 1000000, 2.0, 1.0);
  247. // assert(x == 1221)
  248. //
  249. template <typename IntType>
  250. using MockZipf =
  251. random_internal::MockOverloadSet<y_absl::zipf_distribution<IntType>,
  252. IntType(MockingBitGen&, IntType, double,
  253. double)>;
  254. Y_ABSL_NAMESPACE_END
  255. } // namespace y_absl
  256. #endif // Y_ABSL_RANDOM_MOCK_DISTRIBUTIONS_H_