You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mock_distributions.h 11 kB

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