Generators.tests.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "catch.hpp"
  2. #include <cstring>
  3. // Generators and sections can be nested freely
  4. TEST_CASE("Generators -- simple", "[generators]") {
  5. auto i = GENERATE(1, 2, 3);
  6. SECTION("one") {
  7. auto j = GENERATE(values({ -3, -2, -1 }));
  8. REQUIRE(j < i);
  9. }
  10. SECTION("two") {
  11. // You can also explicitly set type for generators via Catch::Generators::as
  12. auto str = GENERATE(as<std::string>{}, "a", "bb", "ccc");
  13. REQUIRE(4u * i > str.size());
  14. }
  15. }
  16. // You can create a cartesian-product of generators by creating multiple ones
  17. TEST_CASE("3x3x3 ints", "[generators]") {
  18. auto x = GENERATE(1, 2, 3);
  19. auto y = GENERATE(4, 5, 6);
  20. auto z = GENERATE(7, 8, 9);
  21. // These assertions will be run 27 times (3x3x3)
  22. CHECK(x < y);
  23. CHECK(y < z);
  24. REQUIRE(x < z);
  25. }
  26. // You can also create data tuples
  27. TEST_CASE("tables", "[generators]") {
  28. // Note that this will not compile with libstdc++ older than libstdc++6
  29. // See https://stackoverflow.com/questions/12436586/tuple-vector-and-initializer-list
  30. // for possible workarounds
  31. // auto data = GENERATE(table<char const*, int>({
  32. // {"first", 5},
  33. // {"second", 6},
  34. // {"third", 5},
  35. // {"etc...", 6}
  36. // }));
  37. // Workaround for the libstdc++ bug mentioned above
  38. using tuple_type = std::tuple<char const*, int>;
  39. auto data = GENERATE(table<char const*, int>({
  40. tuple_type{"first", 5},
  41. tuple_type{"second", 6},
  42. tuple_type{"third", 5},
  43. tuple_type{"etc...", 6}
  44. }));
  45. REQUIRE(strlen(std::get<0>(data)) == static_cast<size_t>(std::get<1>(data)));
  46. }
  47. #ifdef __cpp_structured_bindings
  48. // Structured bindings make the table utility much nicer to use
  49. TEST_CASE( "strlen2", "[approvals][generators]" ) {
  50. auto [test_input, expected] = GENERATE( table<std::string, size_t>({
  51. {"one", 3},
  52. {"two", 3},
  53. {"three", 5},
  54. {"four", 4}
  55. }));
  56. REQUIRE( test_input.size() == expected );
  57. }
  58. #endif
  59. // An alternate way of doing data tables without structured bindings
  60. struct Data { std::string str; size_t len; };
  61. TEST_CASE( "strlen3", "[generators]" ) {
  62. auto data = GENERATE( values<Data>({
  63. {"one", 3},
  64. {"two", 3},
  65. {"three", 5},
  66. {"four", 4}
  67. }));
  68. REQUIRE( data.str.size() == data.len );
  69. }
  70. #ifdef __cpp_structured_bindings
  71. // Based on example from https://docs.cucumber.io/gherkin/reference/#scenario-outline
  72. // (thanks to https://github.com/catchorg/Catch2/issues/850#issuecomment-399504851)
  73. // Note that GIVEN, WHEN, and THEN now forward onto DYNAMIC_SECTION instead of SECTION.
  74. // DYNAMIC_SECTION takes its name as a stringstream-style expression, so can be formatted using
  75. // variables in scope - such as the generated variables here. This reads quite nicely in the
  76. // test name output (the full scenario description).
  77. static auto eatCucumbers( int start, int eat ) -> int { return start-eat; }
  78. SCENARIO("Eating cucumbers", "[generators][approvals]") {
  79. auto [start, eat, left] = GENERATE( table<int,int,int> ({
  80. { 12, 5, 7 },
  81. { 20, 5, 15 }
  82. }));
  83. GIVEN( "there are " << start << " cucumbers" )
  84. WHEN( "I eat " << eat << " cucumbers" )
  85. THEN( "I should have " << left << " cucumbers" ) {
  86. REQUIRE( eatCucumbers( start, eat ) == left );
  87. }
  88. }
  89. #endif
  90. // There are also some generic generator manipulators
  91. TEST_CASE("Generators -- adapters", "[generators][generic]") {
  92. // TODO: This won't work yet, introduce GENERATE_VAR?
  93. //auto numbers = Catch::Generators::values({ 1, 2, 3, 4, 5, 6 });
  94. SECTION("Filtering by predicate") {
  95. SECTION("Basic usage") {
  96. // This filters out all odd (false) numbers, giving [2, 4, 6]
  97. auto i = GENERATE(filter([] (int val) { return val % 2 == 0; }, values({ 1, 2, 3, 4, 5, 6 })));
  98. REQUIRE(i % 2 == 0);
  99. }
  100. SECTION("Throws if there are no matching values") {
  101. using namespace Catch::Generators;
  102. REQUIRE_THROWS_AS(filter([] (int) {return false; }, value(1)), Catch::GeneratorException);
  103. }
  104. }
  105. SECTION("Shortening a range") {
  106. // This takes the first 3 elements from the values, giving back [1, 2, 3]
  107. auto i = GENERATE(take(3, values({ 1, 2, 3, 4, 5, 6 })));
  108. REQUIRE(i < 4);
  109. }
  110. SECTION("Transforming elements") {
  111. SECTION("Same type") {
  112. // This doubles values [1, 2, 3] into [2, 4, 6]
  113. auto i = GENERATE(map([] (int val) { return val * 2; }, values({ 1, 2, 3 })));
  114. REQUIRE(i % 2 == 0);
  115. }
  116. SECTION("Different type") {
  117. // This takes a generator that returns ints and maps them into strings
  118. auto i = GENERATE(map<std::string>([] (int val) { return std::to_string(val); }, values({ 1, 2, 3 })));
  119. REQUIRE(i.size() == 1);
  120. }
  121. SECTION("Different deduced type") {
  122. // This takes a generator that returns ints and maps them into strings
  123. auto i = GENERATE(map([] (int val) { return std::to_string(val); }, values({ 1, 2, 3 })));
  124. REQUIRE(i.size() == 1);
  125. }
  126. }
  127. SECTION("Repeating a generator") {
  128. // This will return values [1, 2, 3, 1, 2, 3]
  129. auto j = GENERATE(repeat(2, values({ 1, 2, 3 })));
  130. REQUIRE(j > 0);
  131. }
  132. SECTION("Chunking a generator into sized pieces") {
  133. SECTION("Number of elements in source is divisible by chunk size") {
  134. auto chunk2 = GENERATE(chunk(2, values({ 1, 1, 2, 2, 3, 3 })));
  135. REQUIRE(chunk2.size() == 2);
  136. REQUIRE(chunk2.front() == chunk2.back());
  137. }
  138. SECTION("Number of elements in source is not divisible by chunk size") {
  139. auto chunk2 = GENERATE(chunk(2, values({ 1, 1, 2, 2, 3 })));
  140. REQUIRE(chunk2.size() == 2);
  141. REQUIRE(chunk2.front() == chunk2.back());
  142. REQUIRE(chunk2.front() < 3);
  143. }
  144. SECTION("Chunk size of zero") {
  145. auto chunk2 = GENERATE(take(3, chunk(0, value(1))));
  146. REQUIRE(chunk2.size() == 0);
  147. }
  148. SECTION("Throws on too small generators") {
  149. using namespace Catch::Generators;
  150. REQUIRE_THROWS_AS(chunk(2, value(1)), Catch::GeneratorException);
  151. }
  152. }
  153. }
  154. // Note that because of the non-reproducibility of distributions,
  155. // anything involving the random generators cannot be part of approvals
  156. TEST_CASE("Random generator", "[generators][approvals]") {
  157. SECTION("Infer int from integral arguments") {
  158. auto val = GENERATE(take(4, random(0, 1)));
  159. STATIC_REQUIRE(std::is_same<decltype(val), int>::value);
  160. REQUIRE(0 <= val);
  161. REQUIRE(val <= 1);
  162. }
  163. SECTION("Infer double from double arguments") {
  164. auto val = GENERATE(take(4, random(0., 1.)));
  165. STATIC_REQUIRE(std::is_same<decltype(val), double>::value);
  166. REQUIRE(0. <= val);
  167. REQUIRE(val < 1);
  168. }
  169. }
  170. TEST_CASE("Nested generators and captured variables", "[generators]") {
  171. // Workaround for old libstdc++
  172. using record = std::tuple<int, int>;
  173. // Set up 3 ranges to generate numbers from
  174. auto extent = GENERATE(table<int, int>({
  175. record{3, 7},
  176. record{-5, -3},
  177. record{90, 100}
  178. }));
  179. auto from = std::get<0>(extent);
  180. auto to = std::get<1>(extent);
  181. auto values = GENERATE_COPY(range(from, to));
  182. REQUIRE(values > -6);
  183. }
  184. namespace {
  185. size_t call_count = 0;
  186. size_t test_count = 0;
  187. std::vector<int> make_data() {
  188. return { 1, 3, 5, 7, 9, 11 };
  189. }
  190. std::vector<int> make_data_counted() {
  191. ++call_count;
  192. return make_data();
  193. }
  194. }
  195. #if defined(__clang__)
  196. #pragma clang diagnostic push
  197. #pragma clang diagnostic ignored "-Wexit-time-destructors"
  198. #endif
  199. TEST_CASE("Copy and then generate a range", "[generators]") {
  200. SECTION("from var and iterators") {
  201. static auto data = make_data();
  202. // It is important to notice that a generator is only initialized
  203. // **once** per run. What this means is that modifying data will not
  204. // modify the underlying generator.
  205. auto elem = GENERATE_REF(from_range(data.begin(), data.end()));
  206. REQUIRE(elem % 2 == 1);
  207. }
  208. SECTION("From a temporary container") {
  209. auto elem = GENERATE(from_range(make_data_counted()));
  210. ++test_count;
  211. REQUIRE(elem % 2 == 1);
  212. }
  213. SECTION("Final validation") {
  214. REQUIRE(call_count == 1);
  215. REQUIRE(make_data().size() == test_count);
  216. }
  217. }
  218. TEST_CASE("#1913 - GENERATE inside a for loop should not keep recreating the generator", "[regression][generators]") {
  219. static int counter = 0;
  220. for (int i = 0; i < 3; ++i) {
  221. int _ = GENERATE(1, 2);
  222. (void)_;
  223. ++counter;
  224. }
  225. // There should be at most 6 (3 * 2) counter increments
  226. REQUIRE(counter < 7);
  227. }
  228. TEST_CASE("#1913 - GENERATEs can share a line", "[regression][generators]") {
  229. int i = GENERATE(1, 2); int j = GENERATE(3, 4);
  230. REQUIRE(i != j);
  231. }
  232. #if defined(__clang__)
  233. #pragma clang diagnostic pop
  234. #endif