File tree Expand file tree Collapse file tree 5 files changed +119
-0
lines changed Expand file tree Collapse file tree 5 files changed +119
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* ******************************************************************\
2
+
3
+ Module: constructor_of
4
+
5
+ Author: Diffblue Ltd.
6
+
7
+ \*******************************************************************/
8
+
9
+ #ifndef CPROVER_UTIL_CONSTRUCTOR_OF_H
10
+ #define CPROVER_UTIL_CONSTRUCTOR_OF_H
11
+
12
+ // / A type of functor which wraps around the set of constructors of a type.
13
+ // / \tparam constructedt: The type which this functor constructs.
14
+ template <typename constructedt>
15
+ class constructort final
16
+ {
17
+ public:
18
+ template <typename ... argumentst>
19
+ constructedt operator ()(argumentst &&... arguments)
20
+ {
21
+ return constructedt{std::forward<argumentst>(arguments)...};
22
+ }
23
+ };
24
+
25
+ // / \tparam constructedt: A type for construction.
26
+ // / \brief Returns a functor which constructs type `constructedt`.
27
+ template <typename constructedt>
28
+ constexpr constructort<constructedt> constructor_of ()
29
+ {
30
+ return {};
31
+ }
32
+
33
+ #endif // CPROVER_UTIL_CONSTRUCTOR_OF_H
Original file line number Diff line number Diff line change @@ -170,6 +170,14 @@ class json_arrayt:public jsont
170
170
{
171
171
}
172
172
173
+ template <typename begin_iteratort, typename end_iteratort>
174
+ json_arrayt (begin_iteratort &&begin_iterator, end_iteratort &&end_iterator)
175
+ : json_arrayt(arrayt(
176
+ std::forward<begin_iteratort>(begin_iterator),
177
+ std::forward<end_iteratort>(end_iterator)))
178
+ {
179
+ }
180
+
173
181
void resize (std::size_t size)
174
182
{
175
183
array.resize (size);
@@ -294,6 +302,14 @@ class json_objectt:public jsont
294
302
{
295
303
}
296
304
305
+ template <typename begin_iteratort, typename end_iteratort>
306
+ json_objectt (begin_iteratort &&begin_iterator, end_iteratort &&end_iterator)
307
+ : json_objectt(objectt(
308
+ std::forward<begin_iteratort>(begin_iterator),
309
+ std::forward<end_iteratort>(end_iterator)))
310
+ {
311
+ }
312
+
297
313
jsont &operator [](const std::string &key)
298
314
{
299
315
return object[key];
Original file line number Diff line number Diff line change @@ -351,6 +351,14 @@ struct ranget final
351
351
return end_value;
352
352
}
353
353
354
+ // / Constructs a collection containing the values, which this range iterates
355
+ // / over.
356
+ template <typename containert>
357
+ containert collect () const
358
+ {
359
+ return containert (begin (), end ());
360
+ }
361
+
354
362
private:
355
363
iteratort begin_value;
356
364
iteratort end_value;
Original file line number Diff line number Diff line change @@ -7,7 +7,12 @@ Author: Diffblue Ltd.
7
7
\*******************************************************************/
8
8
9
9
#include < testing-utils/catch.hpp>
10
+ #include < util/constructor_of.h>
10
11
#include < util/json.h>
12
+ #include < util/range.h>
13
+
14
+ #include < string>
15
+ #include < vector>
11
16
12
17
SCENARIO (
13
18
" Test that json_arrayt can be constructed from an initializer list." ,
@@ -33,3 +38,29 @@ SCENARIO(
33
38
}
34
39
}
35
40
}
41
+
42
+ SCENARIO (
43
+ " Test that json_arrayt can be constructed using `ranget`" ,
44
+ " [core][util][json]" )
45
+ {
46
+ GIVEN (" A vector of strings." )
47
+ {
48
+ const std::vector<std::string> input{" foo" , " bar" };
49
+ THEN (
50
+ " A json_arrayt can be constructed from the vector of strings using range "
51
+ " and map." )
52
+ {
53
+ const json_arrayt array = make_range (input)
54
+ .map (constructor_of<json_stringt>())
55
+ .collect <json_arrayt>();
56
+ auto it = array.begin ();
57
+ REQUIRE (it->kind == jsont::kindt::J_STRING);
58
+ REQUIRE (it->value == " foo" );
59
+ ++it;
60
+ REQUIRE (it->kind == jsont::kindt::J_STRING);
61
+ REQUIRE (it->value == " bar" );
62
+ ++it;
63
+ REQUIRE (it == array.end ());
64
+ }
65
+ }
66
+ }
Original file line number Diff line number Diff line change @@ -10,9 +10,12 @@ Author: Diffblue Ltd.
10
10
#include < util/json.h>
11
11
#include < util/optional.h>
12
12
#include < util/optional_utils.h>
13
+ #include < util/range.h>
13
14
14
15
#include < algorithm>
15
16
#include < iterator>
17
+ #include < string>
18
+ #include < vector>
16
19
17
20
SCENARIO (
18
21
" Test that json_objectt is compatible with STL algorithms" ,
@@ -73,3 +76,31 @@ SCENARIO(
73
76
}
74
77
}
75
78
}
79
+
80
+ SCENARIO (
81
+ " Test that json_objectt can be constructed using `ranget`" ,
82
+ " [core][util][json]" )
83
+ {
84
+ GIVEN (" A vector of numbers." )
85
+ {
86
+ const std::vector<int > input{1 , 2 , 3 };
87
+ THEN (
88
+ " A json_objectt can be constructed from the vector of numbers using "
89
+ " range and map." )
90
+ {
91
+ const json_objectt output =
92
+ make_range (input)
93
+ .map ([](const int number) {
94
+ const std::string number_as_string = std::to_string (number);
95
+ return make_pair (number_as_string, json_stringt{number_as_string});
96
+ })
97
+ .collect <json_objectt>();
98
+ REQUIRE (output[" 1" ].kind == jsont::kindt::J_STRING);
99
+ REQUIRE (output[" 1" ].value == " 1" );
100
+ REQUIRE (output[" 2" ].kind == jsont::kindt::J_STRING);
101
+ REQUIRE (output[" 2" ].value == " 2" );
102
+ REQUIRE (output[" 3" ].kind == jsont::kindt::J_STRING);
103
+ REQUIRE (output[" 3" ].value == " 3" );
104
+ };
105
+ }
106
+ }
You can’t perform that action at this time.
0 commit comments