|
4 | 4 |
|
5 | 5 | STDSHARP_TEST_NAMESPACES; |
6 | 6 |
|
7 | | -SCENARIO("vector allocation value", "[memory][allocation_value]") |
| 7 | +struct vector_data |
8 | 8 | { |
| 9 | + using allocation_value_type = vector<int>; |
9 | 10 | using allocator_type = std::allocator<stdsharp::byte>; |
10 | 11 | using allocation_traits = allocation_traits<allocator_type>; |
11 | | - using allocator_traits = allocation_traits::allocator_traits; |
12 | | - using allocation_value_t = allocation_value<allocator_type, vector<int>>; |
| 12 | + using allocation_value_t = |
| 13 | + allocation_value<allocator_type, allocation_value_type>; |
13 | 14 |
|
14 | | - allocator_type allocator; |
15 | | - allocation_value_t allocation_value; |
16 | | - const auto allocation_1 = allocation_traits::allocate(allocator, sizeof(vector<int>)); |
17 | | - allocation_traits::callocation_result callocation_1 = allocation_1; |
18 | | - const auto allocation_2 = allocation_traits::allocate(allocator, sizeof(vector<int>)); |
19 | | - const vector<int> seq{1, 2, 3}; |
| 15 | + static constexpr array<int, 3> data{1, 2, 3}; |
| 16 | + static constexpr std::ranges::empty_view<int> default_value{}; |
| 17 | + static constexpr size_t allocation_size = sizeof(allocation_value_type); |
| 18 | + static constexpr allocation_value_t allocation_value{}; |
20 | 19 |
|
21 | | - WHEN("default construct") |
| 20 | + static constexpr void set_value(const auto& allocation_value, const auto& allocation) |
22 | 21 | { |
23 | | - allocation_value(allocator, allocation_1, {}); |
24 | | - |
25 | | - THEN("value is 0") |
26 | | - { |
27 | | - REQUIRE_THAT( |
28 | | - allocation_value.get(allocation_1), |
29 | | - Catch::Matchers::RangeEquals(vector<int>{}) |
30 | | - ); |
31 | | - } |
32 | | - |
33 | | - THEN("set the value to int sequence") |
34 | | - { |
35 | | - allocation_value.get(allocation_1) = seq; |
36 | | - REQUIRE_THAT(allocation_value.get(allocation_1), Catch::Matchers::RangeEquals(seq)); |
37 | | - } |
38 | | - |
39 | | - allocation_value(allocator, allocation_1); |
| 22 | + allocation_value.get(allocation) = {data.begin(), data.end()}; |
40 | 23 | } |
41 | 24 |
|
42 | | - WHEN("constructs and set value to seq") |
| 25 | + static constexpr void |
| 26 | + construct_value(auto& allocator, const auto& allocation_value, const auto& allocation) |
43 | 27 | { |
44 | | - allocation_value(allocator, allocation_1, {}); |
45 | | - allocation_value.get(allocation_1) = seq; |
46 | | - |
47 | | - THEN("copy construct") |
48 | | - { |
49 | | - allocation_value(allocator, callocation_1, allocation_2); |
50 | | - THEN("value is seq") |
51 | | - { |
52 | | - REQUIRE_THAT(allocation_value.get(allocation_2), Catch::Matchers::RangeEquals(seq)); |
53 | | - } |
54 | | - allocation_value(allocator, allocation_2); |
55 | | - } |
56 | | - |
57 | | - THEN("move construct") |
58 | | - { |
59 | | - allocation_value(allocator, allocation_1, allocation_2); |
60 | | - REQUIRE_THAT(allocation_value.get(allocation_2), Catch::Matchers::RangeEquals(seq)); |
61 | | - allocation_value(allocator, allocation_2); |
62 | | - } |
| 28 | + allocation_value(allocator, allocation, {}, data.begin(), data.end()); |
| 29 | + } |
| 30 | +}; |
63 | 31 |
|
64 | | - THEN("copy assign") |
65 | | - { |
66 | | - allocation_value(allocator, allocation_2, {}); |
67 | | - allocation_value(callocation_1, allocation_2); |
68 | | - REQUIRE_THAT(allocation_value.get(allocation_2), Catch::Matchers::RangeEquals(seq)); |
69 | | - allocation_value(allocator, allocation_2); |
70 | | - } |
| 32 | +struct array_data |
| 33 | +{ |
| 34 | + using allocation_value_type = int[]; // NOLINT(*-arrays) |
| 35 | + using allocator_type = std::allocator<stdsharp::byte>; |
| 36 | + using allocation_traits = allocation_traits<allocator_type>; |
| 37 | + using allocation_value_t = |
| 38 | + allocation_value<allocator_type, allocation_value_type>; |
71 | 39 |
|
72 | | - THEN("move assign") |
73 | | - { |
74 | | - allocation_value(allocator, allocation_2, {}); |
75 | | - allocation_value(allocation_1, allocation_2); |
76 | | - REQUIRE_THAT(allocation_value.get(allocation_2), Catch::Matchers::RangeEquals(seq)); |
77 | | - allocation_value(allocator, allocation_2); |
78 | | - } |
| 40 | + static constexpr array<int, 3> data{1, 2, 3}; |
| 41 | + static constexpr std::ranges::repeat_view<int, int> default_value{0, 3}; |
| 42 | + static constexpr size_t allocation_size = data.size() * sizeof(int); |
| 43 | + static constexpr allocation_value_t allocation_value{data.size()}; |
79 | 44 |
|
80 | | - allocation_value(allocator, allocation_1); |
| 45 | + static constexpr void set_value(const auto& allocation_value, const auto& allocation) |
| 46 | + { |
| 47 | + std::ranges::copy(data, allocation_value.data(allocation)); |
81 | 48 | } |
82 | 49 |
|
83 | | - allocation_traits::deallocate(allocator, array{allocation_1, allocation_2}); |
84 | | -} |
| 50 | + static constexpr void |
| 51 | + construct_value(auto& allocator, const auto& allocation_value, const auto& allocation) |
| 52 | + { |
| 53 | + allocation_value(allocator, allocation, {}, data.begin()); |
| 54 | + } |
| 55 | +}; |
85 | 56 |
|
86 | | -SCENARIO("int array allocation value", "[memory][allocation_value]") |
| 57 | +TEMPLATE_TEST_CASE("Scenario: allocation value", "[memory][allocation_value]", vector_data, array_data) |
87 | 58 | { |
88 | | - using allocator_type = std::allocator<int>; |
89 | | - using allocation_traits = allocation_traits<allocator_type>; |
90 | | - using allocator_traits = allocation_traits::allocator_traits; |
91 | | - using allocation_value_t = allocation_value<allocator_type, int[]>; // NOLINT(*-arrays) |
92 | | - |
93 | | - allocator_type allocator; |
94 | | - allocation_value_t allocation_value{3}; |
95 | | - const auto allocation_1 = |
96 | | - allocation_traits::allocate(allocator, allocation_value.size() * sizeof(int)); |
97 | | - allocation_traits::callocation_result callocation_1 = allocation_1; |
98 | | - const auto allocation_2 = |
99 | | - allocation_traits::allocate(allocator, allocation_value.size() * sizeof(int)); |
100 | | - const int seq[3]{1, 2, 3}; // NOLINT(*-arrays) |
| 59 | + using allocation_traits = TestType::allocation_traits; |
| 60 | + |
| 61 | + typename TestType::allocator_type allocator; |
| 62 | + auto allocation_value = TestType::allocation_value; |
| 63 | + const auto allocation_1 = allocation_traits::allocate(allocator, TestType::allocation_size); |
| 64 | + typename allocation_traits::callocation_result callocation_1 = allocation_1; |
| 65 | + const auto allocation_2 = allocation_traits::allocate(allocator, TestType::allocation_size); |
101 | 66 |
|
102 | 67 | WHEN("default construct") |
103 | 68 | { |
104 | 69 | allocation_value(allocator, allocation_1, {}); |
105 | 70 |
|
106 | | - THEN("value is 0") |
| 71 | + THEN("value is default") |
107 | 72 | { |
108 | 73 | REQUIRE_THAT( |
109 | 74 | allocation_value.get(allocation_1), |
110 | | - Catch::Matchers::RangeEquals(vector<int>{}) |
| 75 | + Catch::Matchers::RangeEquals(TestType::default_value) |
111 | 76 | ); |
112 | 77 | } |
113 | 78 |
|
114 | 79 | THEN("set the value to int sequence") |
115 | 80 | { |
116 | | - std::ranges::copy(seq, allocation_value.data(allocation_1)); |
117 | | - REQUIRE_THAT(allocation_value.get(allocation_1), Catch::Matchers::RangeEquals(seq)); |
| 81 | + TestType::set_value(allocation_value, allocation_1); |
| 82 | + REQUIRE_THAT(allocation_value.get(allocation_1), Catch::Matchers::RangeEquals(TestType::data)); |
118 | 83 | } |
119 | 84 |
|
120 | 85 | allocation_value(allocator, allocation_1); |
121 | 86 | } |
122 | 87 |
|
123 | 88 | WHEN("constructs and set value to seq") |
124 | 89 | { |
125 | | - allocation_value(allocator, allocation_1, {}); |
126 | | - std::ranges::copy(seq, allocation_value.data(allocation_1)); |
| 90 | + TestType::construct_value(allocator, allocation_value, allocation_1); |
127 | 91 |
|
128 | 92 | THEN("copy construct") |
129 | 93 | { |
130 | 94 | allocation_value(allocator, callocation_1, allocation_2); |
131 | 95 | THEN("value is seq") |
132 | 96 | { |
133 | | - REQUIRE_THAT(allocation_value.get(allocation_2), Catch::Matchers::RangeEquals(seq)); |
| 97 | + REQUIRE_THAT(allocation_value.get(allocation_2), Catch::Matchers::RangeEquals(TestType::data)); |
134 | 98 | } |
135 | 99 | allocation_value(allocator, allocation_2); |
136 | 100 | } |
137 | 101 |
|
138 | 102 | THEN("move construct") |
139 | 103 | { |
140 | 104 | allocation_value(allocator, allocation_1, allocation_2); |
141 | | - REQUIRE_THAT(allocation_value.get(allocation_2), Catch::Matchers::RangeEquals(seq)); |
| 105 | + REQUIRE_THAT(allocation_value.get(allocation_2), Catch::Matchers::RangeEquals(TestType::data)); |
142 | 106 | allocation_value(allocator, allocation_2); |
143 | 107 | } |
144 | 108 |
|
145 | 109 | THEN("copy assign") |
146 | 110 | { |
147 | 111 | allocation_value(allocator, allocation_2, {}); |
148 | 112 | allocation_value(callocation_1, allocation_2); |
149 | | - REQUIRE_THAT(allocation_value.get(allocation_2), Catch::Matchers::RangeEquals(seq)); |
| 113 | + REQUIRE_THAT(allocation_value.get(allocation_2), Catch::Matchers::RangeEquals(TestType::data)); |
150 | 114 | allocation_value(allocator, allocation_2); |
151 | 115 | } |
152 | 116 |
|
153 | 117 | THEN("move assign") |
154 | 118 | { |
155 | 119 | allocation_value(allocator, allocation_2, {}); |
156 | 120 | allocation_value(allocation_1, allocation_2); |
157 | | - REQUIRE_THAT(allocation_value.get(allocation_2), Catch::Matchers::RangeEquals(seq)); |
| 121 | + REQUIRE_THAT(allocation_value.get(allocation_2), Catch::Matchers::RangeEquals(TestType::data)); |
158 | 122 | allocation_value(allocator, allocation_2); |
159 | 123 | } |
160 | 124 |
|
|
0 commit comments