1 ////////////////////////////////////////////////////////////////////////////////
2 //  Copyright (c) 2013 Shuangyang Yang
3 //
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 
8 #include <hpx/hpx_init.hpp>
9 #include <hpx/util/any.hpp>
10 #include <hpx/util/lightweight_test.hpp>
11 
12 #include <hpx/util/storage/tuple.hpp>
13 
14 #include <boost/any.hpp>
15 
16 #include <iostream>
17 #include <sstream>
18 #include <string>
19 #include <unordered_map>
20 #include <utility>
21 
22 #include "small_big_object.hpp"
23 
24 using boost::program_options::variables_map;
25 using boost::program_options::options_description;
26 
27 using hpx::util::any;
28 using hpx::util::any_nonser;
29 using hpx::util::any_cast;
30 
31 using hpx::init;
32 using hpx::finalize;
33 
34 ///////////////////////////////////////////////////////////////////////////////
hpx_main(variables_map & vm)35 int hpx_main(variables_map& vm)
36 {
37     {
38         {
39             any any1(big_object(30, 40));
40             std::stringstream buffer;
41 
42             buffer << any1;
43 
44             HPX_TEST_EQ(buffer.str(), "3040");
45         }
46 
47         {
48             typedef uint64_t index_type;
49             typedef hpx::util::any elem_type;
50             typedef hpx::util::hash_any hash_elem_functor;
51 
52             typedef std::unordered_multimap<elem_type, index_type,
53                 hash_elem_functor> field_index_map_type;
54             typedef field_index_map_type::iterator field_index_map_iterator_type;
55 
56             field_index_map_type field_index_map_;
57             field_index_map_iterator_type it;
58             elem_type elem(std::string("first string"));
59             index_type id = 1;
60 
61             std::pair<elem_type, index_type> pp=std::make_pair(elem,id);
62             it = field_index_map_.insert(pp);
63         }
64 
65         // test equality
66         {
67             any any1(7), any2(7), any3(10), any4(std::string("seven"));
68 
69             HPX_TEST_EQ(any1, 7);
70             HPX_TEST_NEQ(any1, 10);
71             HPX_TEST_NEQ(any1, 10.0f);
72             HPX_TEST_EQ(any1, any1);
73             HPX_TEST_EQ(any1, any2);
74             HPX_TEST_NEQ(any1, any3);
75             HPX_TEST_NEQ(any1, any4);
76 
77             std::string long_str =
78                 std::string("This is a looooooooooooooooooooooooooong string");
79             std::string other_str = std::string("a different string");
80             any1 = long_str;
81             any2 = any1;
82             any3 = other_str;
83             any4 = 10.0f;
84 
85             HPX_TEST_EQ(any1, long_str);
86             HPX_TEST_NEQ(any1, other_str);
87             HPX_TEST_NEQ(any1, 10.0f);
88             HPX_TEST_EQ(any1, any1);
89             HPX_TEST_EQ(any1, any2);
90             HPX_TEST_NEQ(any1, any3);
91             HPX_TEST_NEQ(any1, any4);
92         }
93 
94         {
95             if (sizeof(small_object) <= sizeof(void*))
96                 std::cout << "object is small\n";
97             else
98                 std::cout << "object is large\n";
99 
100             small_object const f(17);
101 
102             any any1(f);
103             any any2(any1);
104             any any3;
105             any3 = any1;
106 
107             HPX_TEST_EQ((any_cast<small_object>(any1)) (7), uint64_t(17+7));
108             HPX_TEST_EQ((any_cast<small_object>(any2)) (9), uint64_t(17+9));
109             HPX_TEST_EQ((any_cast<small_object>(any3)) (11), uint64_t(17+11));
110         }
111 
112         {
113             if (sizeof(big_object) <= sizeof(void*))
114                 std::cout << "object is small\n";
115             else
116                 std::cout << "object is large\n";
117 
118             big_object const f(5, 12);
119 
120             any any1(f);
121             any any2(any1);
122             any any3 = any1;
123 
124             HPX_TEST_EQ((any_cast<big_object>(any1)) (0, 1), uint64_t(5+12+0+1));
125             HPX_TEST_EQ((any_cast<big_object>(any2)) (1, 0), uint64_t(5+12+1+0));
126             HPX_TEST_EQ((any_cast<big_object>(any3)) (1, 1), uint64_t(5+12+1+1));
127         }
128 
129         // move semantics
130         {
131             any any1(5);
132             HPX_TEST(!any1.empty());
133             any any2(std::move(any1));
134             HPX_TEST(!any2.empty());
135             HPX_TEST(any1.empty());
136         }
137 
138         {
139             any any1(5);
140             HPX_TEST(!any1.empty());
141             any any2;
142             HPX_TEST(any2.empty());
143 
144             any2 = std::move(any1);
145             HPX_TEST(!any2.empty());
146             HPX_TEST(any1.empty());
147         }
148     }
149     // non serializable version
150     {
151         // test equality
152         {
153             any_nonser any1_nonser(7), any2_nonser(7), any3_nonser(10),
154                 any4_nonser(std::string("seven"));
155 
156             HPX_TEST_EQ(any1_nonser, 7);
157             HPX_TEST_NEQ(any1_nonser, 10);
158             HPX_TEST_NEQ(any1_nonser, 10.0f);
159             HPX_TEST_EQ(any1_nonser, any1_nonser);
160             HPX_TEST_EQ(any1_nonser, any2_nonser);
161             HPX_TEST_NEQ(any1_nonser, any3_nonser);
162             HPX_TEST_NEQ(any1_nonser, any4_nonser);
163 
164             std::string long_str =
165                 std::string("This is a looooooooooooooooooooooooooong string");
166             std::string other_str = std::string("a different string");
167             any1_nonser = long_str;
168             any2_nonser = any1_nonser;
169             any3_nonser = other_str;
170             any4_nonser = 10.0f;
171 
172             HPX_TEST_EQ(any1_nonser, long_str);
173             HPX_TEST_NEQ(any1_nonser, other_str);
174             HPX_TEST_NEQ(any1_nonser, 10.0f);
175             HPX_TEST_EQ(any1_nonser, any1_nonser);
176             HPX_TEST_EQ(any1_nonser, any2_nonser);
177             HPX_TEST_NEQ(any1_nonser, any3_nonser);
178             HPX_TEST_NEQ(any1_nonser, any4_nonser);
179         }
180 
181         {
182             if (sizeof(small_object) <= sizeof(void*))
183                 std::cout << "object is small\n";
184             else
185                 std::cout << "object is large\n";
186 
187             small_object const f(17);
188 
189             any_nonser any1_nonser(f);
190             any_nonser any2_nonser(any1_nonser);
191             any_nonser any3_nonser = any1_nonser;
192 
193             HPX_TEST_EQ((any_cast<small_object>(any1_nonser)) (2), uint64_t(17+2));
194             HPX_TEST_EQ((any_cast<small_object>(any2_nonser)) (4), uint64_t(17+4));
195             HPX_TEST_EQ((any_cast<small_object>(any3_nonser)) (6), uint64_t(17+6));
196 
197         }
198 
199         {
200             if (sizeof(big_object) <= sizeof(void*))
201                 std::cout << "object is small\n";
202             else
203                 std::cout << "object is large\n";
204 
205             big_object const f(5, 12);
206 
207             any_nonser any1_nonser(f);
208             any_nonser any2_nonser(any1_nonser);
209             any_nonser any3_nonser = any1_nonser;
210 
211             HPX_TEST_EQ((any_cast<big_object>(any1_nonser)) (3,4), uint64_t(5+12+3+4));
212             HPX_TEST_EQ((any_cast<big_object>(any2_nonser)) (5,6), uint64_t(5+12+5+6));
213             HPX_TEST_EQ((any_cast<big_object>(any3_nonser)) (7,8), uint64_t(5+12+7+8));
214         }
215 
216         // move semantics
217         {
218             any_nonser any1(5);
219             HPX_TEST(!any1.empty());
220             any_nonser any2(std::move(any1));
221             HPX_TEST(!any2.empty());
222             HPX_TEST(any1.empty());
223         }
224 
225         {
226             any_nonser any1(5);
227             HPX_TEST(!any1.empty());
228             any_nonser any2;
229             HPX_TEST(any2.empty());
230 
231             any2 = std::move(any1);
232             HPX_TEST(!any2.empty());
233             HPX_TEST(any1.empty());
234         }
235     }
236 
237     finalize();
238     return hpx::util::report_errors();
239 }
240 
241 ///////////////////////////////////////////////////////////////////////////////
main(int argc,char * argv[])242 int main(int argc, char* argv[])
243 {
244     // Configure application-specific options
245     options_description cmdline("Usage: " HPX_APPLICATION_STRING " [options]");
246 
247     // Initialize and run HPX
248     return init(cmdline, argc, argv);
249 }
250 
251