1 /*=============================================================================
2 Copyright (c) 2017 Paul Fultz II
3 construct.cpp
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #include <boost/hof/construct.hpp>
8 #include "test.hpp"
9
10 #include <boost/hof/first_of.hpp>
11 #include <boost/hof/proj.hpp>
12 #include <boost/hof/placeholders.hpp>
13
14 #include <tuple>
15 #include <type_traits>
16 #include <vector>
17
18 template<class T>
19 struct ac
20 {
21 T value;
acac22 constexpr ac(T i) : value(i)
23 {}
24 };
25
26 template<class... Ts>
27 struct tuple_meta
28 {
29 typedef std::tuple<Ts...> type;
30 };
31
32 struct tuple_meta_class
33 {
34 template<class... Ts>
35 struct apply
36 {
37 typedef std::tuple<Ts...> type;
38 };
39 };
40
41 struct implicit_default
42 {
43 int mem1;
44 std::string mem2;
45 };
46
47 struct user_default
48 {
49 int mem1;
50 std::string mem2;
user_defaultuser_default51 user_default() { }
52 };
53
54 struct user_construct
55 {
56 int mem1;
57 std::string mem2;
user_constructuser_construct58 user_construct(int) { }
59 };
60
61 template<class T>
62 struct template_user_construct
63 {
64 int mem1;
65 std::string mem2;
template_user_constructtemplate_user_construct66 template_user_construct(T) { }
67 };
68
69
BOOST_HOF_TEST_CASE()70 BOOST_HOF_TEST_CASE()
71 {
72 auto v = boost::hof::construct<std::vector<int>>()(5, 5);
73 BOOST_HOF_TEST_CHECK(v.size() == 5);
74 BOOST_HOF_TEST_CHECK(v == std::vector<int>{5, 5, 5, 5, 5});
75 }
76
BOOST_HOF_TEST_CASE()77 BOOST_HOF_TEST_CASE()
78 {
79 auto v = boost::hof::construct_basic<std::vector<int>>()(5, 5);
80 BOOST_HOF_TEST_CHECK(v.size() == 5);
81 BOOST_HOF_TEST_CHECK(v == std::vector<int>{5, 5, 5, 5, 5});
82 }
83
BOOST_HOF_TEST_CASE()84 BOOST_HOF_TEST_CASE()
85 {
86 auto v = boost::hof::construct_forward<std::vector<int>>()(5, 5);
87 BOOST_HOF_TEST_CHECK(v.size() == 5);
88 BOOST_HOF_TEST_CHECK(v == std::vector<int>{5, 5, 5, 5, 5});
89 }
90
BOOST_HOF_TEST_CASE()91 BOOST_HOF_TEST_CASE()
92 {
93 auto x = boost::hof::construct<implicit_default>()();
94 BOOST_HOF_TEST_CHECK(x.mem1 == 0);
95 BOOST_HOF_TEST_CHECK(x.mem2 == "");
96 }
97
BOOST_HOF_TEST_CASE()98 BOOST_HOF_TEST_CASE()
99 {
100 auto x = boost::hof::construct<user_default>()();
101 BOOST_HOF_TEST_CHECK(x.mem1 == 0);
102 BOOST_HOF_TEST_CHECK(x.mem2 == "");
103 }
104
BOOST_HOF_TEST_CASE()105 BOOST_HOF_TEST_CASE()
106 {
107 auto x = boost::hof::construct<user_construct>()(3);
108 BOOST_HOF_TEST_CHECK(x.mem1 == 0);
109 BOOST_HOF_TEST_CHECK(x.mem2 == "");
110 }
111
BOOST_HOF_TEST_CASE()112 BOOST_HOF_TEST_CASE()
113 {
114 auto x = boost::hof::construct<template_user_construct>()(3);
115 BOOST_HOF_TEST_CHECK(x.mem1 == 0);
116 BOOST_HOF_TEST_CHECK(x.mem2 == "");
117 }
118
BOOST_HOF_TEST_CASE()119 BOOST_HOF_TEST_CASE()
120 {
121 auto x = boost::hof::construct_forward<template_user_construct>()(3);
122 BOOST_HOF_TEST_CHECK(x.mem1 == 0);
123 BOOST_HOF_TEST_CHECK(x.mem2 == "");
124 }
125
BOOST_HOF_TEST_CASE()126 BOOST_HOF_TEST_CASE()
127 {
128 auto x = boost::hof::construct_basic<template_user_construct>()(3);
129 BOOST_HOF_TEST_CHECK(x.mem1 == 0);
130 BOOST_HOF_TEST_CHECK(x.mem2 == "");
131 }
132
BOOST_HOF_TEST_CASE()133 BOOST_HOF_TEST_CASE()
134 {
135 auto v = boost::hof::construct<std::vector<int>>()({5, 5, 5, 5, 5});
136 BOOST_HOF_TEST_CHECK(v.size() == 5);
137 BOOST_HOF_TEST_CHECK(v == std::vector<int>{5, 5, 5, 5, 5});
138 }
139
BOOST_HOF_TEST_CASE()140 BOOST_HOF_TEST_CASE()
141 {
142 auto t = boost::hof::construct<std::tuple>()(1, 2, 3);
143 static_assert(std::is_same<std::tuple<int, int, int>, decltype(t)>::value, "");
144 BOOST_HOF_TEST_CHECK(t == std::make_tuple(1, 2, 3));
145 // GCC 4.7 doesn't have fully constexpr tuple
146 #if BOOST_HOF_HAS_CONSTEXPR_TUPLE
147 BOOST_HOF_STATIC_TEST_CHECK(std::make_tuple(1, 2, 3) == boost::hof::construct<std::tuple>()(1, 2, 3));
148 #endif
149 }
150
BOOST_HOF_TEST_CASE()151 BOOST_HOF_TEST_CASE()
152 {
153 auto t = boost::hof::construct<std::pair>()(1, 2);
154 static_assert(std::is_same<std::pair<int, int>, decltype(t)>::value, "");
155 BOOST_HOF_TEST_CHECK(t == std::make_pair(1, 2));
156 // GCC 4.7 doesn't have fully constexpr pair
157 #if BOOST_HOF_HAS_CONSTEXPR_TUPLE
158 BOOST_HOF_STATIC_TEST_CHECK(std::make_pair(1, 2) == boost::hof::construct<std::pair>()(1, 2));
159 #endif
160 }
161
BOOST_HOF_TEST_CASE()162 BOOST_HOF_TEST_CASE()
163 {
164 auto f = boost::hof::first_of(boost::hof::construct<std::pair>(), boost::hof::identity);
165 BOOST_HOF_TEST_CHECK(f(1, 2) == std::make_pair(1, 2));
166 BOOST_HOF_TEST_CHECK(f(1) == 1);
167 }
168
BOOST_HOF_TEST_CASE()169 BOOST_HOF_TEST_CASE()
170 {
171 auto x = boost::hof::construct<ac>()(1);
172 static_assert(std::is_same<ac<int>, decltype(x)>::value, "");
173 BOOST_HOF_TEST_CHECK(x.value == ac<int>(1).value);
174 BOOST_HOF_STATIC_TEST_CHECK(ac<int>(1).value == boost::hof::construct<ac>()(1).value);
175 }
176
BOOST_HOF_TEST_CASE()177 BOOST_HOF_TEST_CASE()
178 {
179 auto x = boost::hof::construct_basic<ac>()(1);
180 static_assert(std::is_same<ac<int>, decltype(x)>::value, "");
181 BOOST_HOF_TEST_CHECK(x.value == ac<int>(1).value);
182 BOOST_HOF_STATIC_TEST_CHECK(ac<int>(1).value == boost::hof::construct<ac>()(1).value);
183 }
184
BOOST_HOF_TEST_CASE()185 BOOST_HOF_TEST_CASE()
186 {
187 int i = 1;
188 auto x = boost::hof::construct_forward<ac>()(i);
189 static_assert(std::is_same<ac<int&>, decltype(x)>::value, "");
190 BOOST_HOF_TEST_CHECK(&x.value == &i);
191 }
192
BOOST_HOF_TEST_CASE()193 BOOST_HOF_TEST_CASE()
194 {
195 int i = 1;
196 auto x = boost::hof::construct_basic<ac>()(i);
197 static_assert(std::is_same<ac<int&>, decltype(x)>::value, "");
198 BOOST_HOF_TEST_CHECK(&x.value == &i);
199 }
200
BOOST_HOF_TEST_CASE()201 BOOST_HOF_TEST_CASE()
202 {
203 auto t = boost::hof::construct_meta<tuple_meta>()(1, 2, 3);
204 static_assert(std::is_same<std::tuple<int, int, int>, decltype(t)>::value, "");
205 BOOST_HOF_TEST_CHECK(t == std::make_tuple(1, 2, 3));
206 // GCC 4.7 doesn't have fully constexpr tuple
207 #if BOOST_HOF_HAS_CONSTEXPR_TUPLE
208 BOOST_HOF_STATIC_TEST_CHECK(std::make_tuple(1, 2, 3) == boost::hof::construct_meta<tuple_meta>()(1, 2, 3));
209 #endif
210 }
211
BOOST_HOF_TEST_CASE()212 BOOST_HOF_TEST_CASE()
213 {
214 auto t = boost::hof::construct_meta<tuple_meta_class>()(1, 2, 3);
215 static_assert(std::is_same<std::tuple<int, int, int>, decltype(t)>::value, "");
216 BOOST_HOF_TEST_CHECK(t == std::make_tuple(1, 2, 3));
217 // GCC 4.7 doesn't have fully constexpr tuple
218 #if BOOST_HOF_HAS_CONSTEXPR_TUPLE
219 BOOST_HOF_STATIC_TEST_CHECK(std::make_tuple(1, 2, 3) == boost::hof::construct_meta<tuple_meta_class>()(1, 2, 3));
220 #endif
221 }
222
223