GeneratorsImpl.tests.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #include "catch.hpp"
  2. // Tests of generator implementation details
  3. TEST_CASE("Generators internals", "[generators][internals]") {
  4. using namespace Catch::Generators;
  5. SECTION("Single value") {
  6. auto gen = value(123);
  7. REQUIRE(gen.get() == 123);
  8. REQUIRE_FALSE(gen.next());
  9. }
  10. SECTION("Preset values") {
  11. auto gen = values({ 1, 3, 5 });
  12. REQUIRE(gen.get() == 1);
  13. REQUIRE(gen.next());
  14. REQUIRE(gen.get() == 3);
  15. REQUIRE(gen.next());
  16. REQUIRE(gen.get() == 5);
  17. REQUIRE_FALSE(gen.next());
  18. }
  19. SECTION("Generator combinator") {
  20. auto gen = makeGenerators(1, 5, values({ 2, 4 }), 0);
  21. REQUIRE(gen.get() == 1);
  22. REQUIRE(gen.next());
  23. REQUIRE(gen.get() == 5);
  24. REQUIRE(gen.next());
  25. REQUIRE(gen.get() == 2);
  26. REQUIRE(gen.next());
  27. REQUIRE(gen.get() == 4);
  28. REQUIRE(gen.next());
  29. REQUIRE(gen.get() == 0);
  30. REQUIRE_FALSE(gen.next());
  31. }
  32. SECTION("Explicitly typed generator sequence") {
  33. auto gen = makeGenerators(as<std::string>{}, "aa", "bb", "cc");
  34. // This just checks that the type is std::string:
  35. REQUIRE(gen.get().size() == 2);
  36. // Iterate over the generator
  37. REQUIRE(gen.get() == "aa");
  38. REQUIRE(gen.next());
  39. REQUIRE(gen.get() == "bb");
  40. REQUIRE(gen.next());
  41. REQUIRE(gen.get() == "cc");
  42. REQUIRE_FALSE(gen.next());
  43. }
  44. SECTION("Filter generator") {
  45. // Normal usage
  46. auto gen = filter([] (int i) { return i != 2; }, values({ 2, 1, 2, 3, 2, 2 }));
  47. REQUIRE(gen.get() == 1);
  48. REQUIRE(gen.next());
  49. REQUIRE(gen.get() == 3);
  50. REQUIRE_FALSE(gen.next());
  51. // Completely filtered-out generator should throw on construction
  52. REQUIRE_THROWS_AS(filter([] (int) { return false; }, value(1)), Catch::GeneratorException);
  53. }
  54. SECTION("Take generator") {
  55. SECTION("Take less") {
  56. auto gen = take(2, values({ 1, 2, 3 }));
  57. REQUIRE(gen.get() == 1);
  58. REQUIRE(gen.next());
  59. REQUIRE(gen.get() == 2);
  60. REQUIRE_FALSE(gen.next());
  61. }
  62. SECTION("Take more") {
  63. auto gen = take(2, value(1));
  64. REQUIRE(gen.get() == 1);
  65. REQUIRE_FALSE(gen.next());
  66. }
  67. }
  68. SECTION("Map with explicit return type") {
  69. auto gen = map<double>([] (int i) {return 2.0 * i; }, values({ 1, 2, 3 }));
  70. REQUIRE(gen.get() == 2.0);
  71. REQUIRE(gen.next());
  72. REQUIRE(gen.get() == 4.0);
  73. REQUIRE(gen.next());
  74. REQUIRE(gen.get() == 6.0);
  75. REQUIRE_FALSE(gen.next());
  76. }
  77. SECTION("Map with deduced return type") {
  78. auto gen = map([] (int i) {return 2.0 * i; }, values({ 1, 2, 3 }));
  79. REQUIRE(gen.get() == 2.0);
  80. REQUIRE(gen.next());
  81. REQUIRE(gen.get() == 4.0);
  82. REQUIRE(gen.next());
  83. REQUIRE(gen.get() == 6.0);
  84. REQUIRE_FALSE(gen.next());
  85. }
  86. SECTION("Repeat") {
  87. SECTION("Singular repeat") {
  88. auto gen = repeat(1, value(3));
  89. REQUIRE(gen.get() == 3);
  90. REQUIRE_FALSE(gen.next());
  91. }
  92. SECTION("Actual repeat") {
  93. auto gen = repeat(2, values({ 1, 2, 3 }));
  94. REQUIRE(gen.get() == 1);
  95. REQUIRE(gen.next());
  96. REQUIRE(gen.get() == 2);
  97. REQUIRE(gen.next());
  98. REQUIRE(gen.get() == 3);
  99. REQUIRE(gen.next());
  100. REQUIRE(gen.get() == 1);
  101. REQUIRE(gen.next());
  102. REQUIRE(gen.get() == 2);
  103. REQUIRE(gen.next());
  104. REQUIRE(gen.get() == 3);
  105. REQUIRE_FALSE(gen.next());
  106. }
  107. }
  108. SECTION("Range") {
  109. SECTION("Positive auto step") {
  110. SECTION("Integer") {
  111. auto gen = range(-2, 2);
  112. REQUIRE(gen.get() == -2);
  113. REQUIRE(gen.next());
  114. REQUIRE(gen.get() == -1);
  115. REQUIRE(gen.next());
  116. REQUIRE(gen.get() == 0);
  117. REQUIRE(gen.next());
  118. REQUIRE(gen.get() == 1);
  119. REQUIRE_FALSE(gen.next());
  120. }
  121. }
  122. SECTION("Negative auto step") {
  123. SECTION("Integer") {
  124. auto gen = range(2, -2);
  125. REQUIRE(gen.get() == 2);
  126. REQUIRE(gen.next());
  127. REQUIRE(gen.get() == 1);
  128. REQUIRE(gen.next());
  129. REQUIRE(gen.get() == 0);
  130. REQUIRE(gen.next());
  131. REQUIRE(gen.get() == -1);
  132. REQUIRE_FALSE(gen.next());
  133. }
  134. }
  135. SECTION("Positive manual step") {
  136. SECTION("Integer") {
  137. SECTION("Exact") {
  138. auto gen = range(-7, 5, 3);
  139. REQUIRE(gen.get() == -7);
  140. REQUIRE(gen.next());
  141. REQUIRE(gen.get() == -4);
  142. REQUIRE(gen.next());
  143. REQUIRE(gen.get() == -1);
  144. REQUIRE(gen.next());
  145. REQUIRE(gen.get() == 2);
  146. REQUIRE_FALSE(gen.next());
  147. }
  148. SECTION("Slightly over end") {
  149. auto gen = range(-7, 4, 3);
  150. REQUIRE(gen.get() == -7);
  151. REQUIRE(gen.next());
  152. REQUIRE(gen.get() == -4);
  153. REQUIRE(gen.next());
  154. REQUIRE(gen.get() == -1);
  155. REQUIRE(gen.next());
  156. REQUIRE(gen.get() == 2);
  157. REQUIRE_FALSE(gen.next());
  158. }
  159. SECTION("Slightly under end") {
  160. auto gen = range(-7, 6, 3);
  161. REQUIRE(gen.get() == -7);
  162. REQUIRE(gen.next());
  163. REQUIRE(gen.get() == -4);
  164. REQUIRE(gen.next());
  165. REQUIRE(gen.get() == -1);
  166. REQUIRE(gen.next());
  167. REQUIRE(gen.get() == 2);
  168. REQUIRE(gen.next());
  169. REQUIRE(gen.get() == 5);
  170. REQUIRE_FALSE(gen.next());
  171. }
  172. }
  173. SECTION("Floating Point") {
  174. SECTION("Exact") {
  175. const auto rangeStart = -1.;
  176. const auto rangeEnd = 1.;
  177. const auto step = .1;
  178. auto gen = range(rangeStart, rangeEnd, step);
  179. auto expected = rangeStart;
  180. while( (rangeEnd - expected) > step ) {
  181. INFO( "Current expected value is " << expected )
  182. REQUIRE(gen.get() == Approx(expected));
  183. REQUIRE(gen.next());
  184. expected += step;
  185. }
  186. REQUIRE(gen.get() == Approx( rangeEnd ) );
  187. REQUIRE_FALSE(gen.next());
  188. }
  189. SECTION("Slightly over end") {
  190. const auto rangeStart = -1.;
  191. const auto rangeEnd = 1.;
  192. const auto step = .3;
  193. auto gen = range(rangeStart, rangeEnd, step);
  194. auto expected = rangeStart;
  195. while( (rangeEnd - expected) > step ) {
  196. INFO( "Current expected value is " << expected )
  197. REQUIRE(gen.get() == Approx(expected));
  198. REQUIRE(gen.next());
  199. expected += step;
  200. }
  201. REQUIRE_FALSE(gen.next());
  202. }
  203. SECTION("Slightly under end") {
  204. const auto rangeStart = -1.;
  205. const auto rangeEnd = .9;
  206. const auto step = .3;
  207. auto gen = range(rangeStart, rangeEnd, step);
  208. auto expected = rangeStart;
  209. while( (rangeEnd - expected) > step ) {
  210. INFO( "Current expected value is " << expected )
  211. REQUIRE(gen.get() == Approx(expected));
  212. REQUIRE(gen.next());
  213. expected += step;
  214. }
  215. REQUIRE_FALSE(gen.next());
  216. }
  217. }
  218. }
  219. SECTION("Negative manual step") {
  220. SECTION("Integer") {
  221. SECTION("Exact") {
  222. auto gen = range(5, -7, -3);
  223. REQUIRE(gen.get() == 5);
  224. REQUIRE(gen.next());
  225. REQUIRE(gen.get() == 2);
  226. REQUIRE(gen.next());
  227. REQUIRE(gen.get() == -1);
  228. REQUIRE(gen.next());
  229. REQUIRE(gen.get() == -4);
  230. REQUIRE_FALSE(gen.next());
  231. }
  232. SECTION("Slightly over end") {
  233. auto gen = range(5, -6, -3);
  234. REQUIRE(gen.get() == 5);
  235. REQUIRE(gen.next());
  236. REQUIRE(gen.get() == 2);
  237. REQUIRE(gen.next());
  238. REQUIRE(gen.get() == -1);
  239. REQUIRE(gen.next());
  240. REQUIRE(gen.get() == -4);
  241. REQUIRE_FALSE(gen.next());
  242. }
  243. SECTION("Slightly under end") {
  244. auto gen = range(5, -8, -3);
  245. REQUIRE(gen.get() == 5);
  246. REQUIRE(gen.next());
  247. REQUIRE(gen.get() == 2);
  248. REQUIRE(gen.next());
  249. REQUIRE(gen.get() == -1);
  250. REQUIRE(gen.next());
  251. REQUIRE(gen.get() == -4);
  252. REQUIRE(gen.next());
  253. REQUIRE(gen.get() == -7);
  254. REQUIRE_FALSE(gen.next());
  255. }
  256. }
  257. }
  258. }
  259. }
  260. // todo: uncopyable type used in a generator
  261. // idea: uncopyable tag type for a stupid generator
  262. namespace {
  263. struct non_copyable {
  264. non_copyable() = default;
  265. non_copyable(non_copyable const&) = delete;
  266. non_copyable& operator=(non_copyable const&) = delete;
  267. int value = -1;
  268. };
  269. // This class shows how to implement a simple generator for Catch tests
  270. class TestGen : public Catch::Generators::IGenerator<int> {
  271. int current_number;
  272. public:
  273. TestGen(non_copyable const& nc):
  274. current_number(nc.value) {}
  275. int const& get() const override;
  276. bool next() override {
  277. return false;
  278. }
  279. };
  280. // Avoids -Wweak-vtables
  281. int const& TestGen::get() const {
  282. return current_number;
  283. }
  284. }
  285. TEST_CASE("GENERATE capture macros", "[generators][internals][approvals]") {
  286. auto value = GENERATE(take(10, random(0, 10)));
  287. non_copyable nc; nc.value = value;
  288. // neither `GENERATE_COPY` nor plain `GENERATE` would compile here
  289. auto value2 = GENERATE_REF(Catch::Generators::GeneratorWrapper<int>(std::unique_ptr<Catch::Generators::IGenerator<int>>(new TestGen(nc))));
  290. REQUIRE(value == value2);
  291. }
  292. TEST_CASE("#1809 - GENERATE_COPY and SingleValueGenerator does not compile", "[generators][compilation][approvals]") {
  293. // Verify Issue #1809 fix, only needs to compile.
  294. auto a = GENERATE_COPY(1, 2);
  295. (void)a;
  296. auto b = GENERATE_COPY(as<long>{}, 1, 2);
  297. (void)b;
  298. int i = 1;
  299. int j = 2;
  300. auto c = GENERATE_COPY(i, j);
  301. (void)c;
  302. auto d = GENERATE_COPY(as<long>{}, i, j);
  303. (void)d;
  304. SUCCEED();
  305. }
  306. TEST_CASE("Multiple random generators in one test case output different values", "[generators][internals][approvals]") {
  307. SECTION("Integer") {
  308. auto random1 = Catch::Generators::random(0, 1000);
  309. auto random2 = Catch::Generators::random(0, 1000);
  310. size_t same = 0;
  311. for (size_t i = 0; i < 1000; ++i) {
  312. same += random1.get() == random2.get();
  313. random1.next(); random2.next();
  314. }
  315. // 0.5% seems like a sane bound for random identical elements within 1000 runs
  316. REQUIRE(same < 5);
  317. }
  318. SECTION("Float") {
  319. auto random1 = Catch::Generators::random(0., 1000.);
  320. auto random2 = Catch::Generators::random(0., 1000.);
  321. size_t same = 0;
  322. for (size_t i = 0; i < 1000; ++i) {
  323. same += random1.get() == random2.get();
  324. random1.next(); random2.next();
  325. }
  326. // 0.5% seems like a sane bound for random identical elements within 1000 runs
  327. REQUIRE(same < 5);
  328. }
  329. }