1 #include <catch2/catch.hpp>
2 #include <rapidcheck/catch.h>
3 
4 #include "Common.h"
5 
6 using namespace rc;
7 using namespace rc::test;
8 
9 namespace {
10 
11 template <typename Factory>
12 struct GenericFixedProperties {
13   template <typename T>
exec__anon4ec644680111::GenericFixedProperties14   static void exec() {
15     templatedProp<T>(
16         "generated value always has the requested number of elements",
17         [](const GenParams &params) {
18           const auto count = *gen::inRange<std::size_t>(0, 10);
19           const auto shrinkable = Factory::template makeGen<T>(
20               count, genCountdown())(params.random, params.size);
21           onAnyPath(
22               shrinkable,
23               [=](const Shrinkable<T> &value, const Shrinkable<T> &shrink) {
24                 RC_ASSERT(containerSize(shrink.value()) == count);
25               });
26         });
27 
28     templatedProp<T>("none of the shrinks equal the original value",
29                      [](const GenParams &params) {
30                        const auto count = *gen::inRange<std::size_t>(0, 10);
31                        const auto shrinkable = Factory::template makeGen<T>(
32                            count, genCountdown())(params.random, params.size);
33                        onAnyPath(shrinkable,
34                                  [](const Shrinkable<T> &value,
35                                     const Shrinkable<T> &shrink) {
36                                    RC_ASSERT(value.value() != shrink.value());
37                                  });
38                      });
39   }
40 };
41 
42 template <typename Factory>
43 struct ParamsFixedProperties {
44   template <typename T>
exec__anon4ec644680111::ParamsFixedProperties45   static void exec() {
46     using Element = typename T::value_type;
47 
48     templatedProp<T>("passes the correct size to the element generators",
49                      [](const GenParams &params) {
50                        const auto count = *gen::inRange<std::size_t>(0, 10);
51                        const auto value = Factory::template makeGen<T>(
52                                               count, genPassedParams())(
53                                               params.random, params.size)
54                                               .value();
55                        RC_ASSERT(std::all_of(begin(value),
56                                              end(value),
57                                              [&](const Element &x) {
58                                                return hasSize(params.size, x);
59                                              }));
60                      });
61 
62     templatedProp<T>(
63         "the random generators passed to element generators are unique",
64         [](const GenParams &params) {
65           const auto count = *gen::inRange<std::size_t>(0, 10);
66           const auto value =
67               Factory::template makeGen<T>(count, genPassedParams())(
68                   params.random, params.size)
69                   .value();
70           std::unordered_set<Random> randoms;
71           RC_ASSERT(std::all_of(
72               begin(value),
73               end(value),
74               [&](const Element &x) { return insertRandoms(randoms, x); }));
75         });
76   }
77 };
78 
79 } // namespace
80 
81 TEST_CASE("gen::container(std::size_t)") {
82   forEachType<GenericFixedProperties<ContainerFactory>,
83               RC_SEQUENCE_CONTAINERS(int),
84               RC_SET_CONTAINERS(int),
85               std::basic_string<int>>();
86   forEachType<GenericFixedProperties<MapFactory>, RC_MAP_CONTAINERS(int)>();
87 
88   forEachType<ParamsFixedProperties<ContainerFactory>,
89               RC_SEQUENCE_CONTAINERS(GenParams),
90               RC_SET_CONTAINERS(GenParams)>();
91   forEachType<ParamsFixedProperties<MapFactory>,
92               RC_MAP_CONTAINERS(GenParams)>();
93 
94   forEachType<RetrialProperties<FixedMapFactory<2, 15>>,
95               std::map<int, int>,
96               std::unordered_map<int, int>>();
97   forEachType<RetrialProperties<FixedContainerFactory<2, 15>>,
98               std::set<int>,
99               std::unordered_set<int>>();
100 
101   prop("throws GenerationFailure for std::array if count != N",
__anon4ec644680a02(const GenParams &params) 102        [](const GenParams &params) {
103          const auto count = *gen::distinctFrom(3);
104          const auto gen =
105              gen::container<std::array<int, 3>>(count, gen::arbitrary<int>());
106          const auto shrinkable = gen(params.random, params.size);
107          RC_ASSERT_THROWS_AS(shrinkable.value(), GenerationFailure);
108        });
109 
110   // TODO shrink tests?
111 }
112