1 //
2 //=======================================================================
3 // Author: Jeremiah Willcock
4 //
5 // Copyright 2012, Trustees of Indiana University
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See
8 // accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //=======================================================================
11 //
12
13 #include <boost/property_map/function_property_map.hpp>
14 #include <boost/property_map/transform_value_property_map.hpp>
15 #include <boost/concept/assert.hpp>
16 #include <boost/property_map/property_map.hpp>
17 #include <boost/test/minimal.hpp>
18 #include <boost/static_assert.hpp>
19
20 // Ensure this is not default constructible
times2times221 struct times2 {typedef int result_type; times2(int) {}; int operator()(int x) const {return 2 * x;}};
22
23 template <typename T>
operator ()add124 struct add1 {typedef T result_type; T operator()(const T& x) const {return x + 1;}};
25
26 template <typename T>
operator ()add1_val27 struct add1_val {typedef T result_type; T operator()(T x) const {return x + 1;}};
28
29 template <typename T>
30 struct return_fixed_ref {
31 int* ptr;
return_fixed_refreturn_fixed_ref32 return_fixed_ref(int* ptr): ptr(ptr) {}
33 typedef int& result_type;
operator ()return_fixed_ref34 int& operator()(const T&) const {return *ptr;}
35 };
36
test_main(int,char **)37 int test_main(int, char**) {
38 using namespace boost;
39 typedef function_property_map<times2, int> PM;
40 PM orig_pm(times2(0));
41 BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept<transform_value_property_map<add1<int>, PM>, int>));
42 BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept<transform_value_property_map<add1<int>, PM, double>, int>));
43 BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept<transform_value_property_map<add1_val<int>, PM>, int>));
44 BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept<transform_value_property_map<add1_val<int>, PM, double>, int>));
45 BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept<transform_value_property_map<return_fixed_ref<int>, PM>, int>));
46 BOOST_CONCEPT_ASSERT((WritablePropertyMapConcept<transform_value_property_map<return_fixed_ref<int>, PM>, int>));
47 BOOST_CONCEPT_ASSERT((ReadWritePropertyMapConcept<transform_value_property_map<return_fixed_ref<int>, PM>, int>));
48 BOOST_CONCEPT_ASSERT((LvaluePropertyMapConcept<transform_value_property_map<return_fixed_ref<int>, PM>, int>));
49
50 BOOST_STATIC_ASSERT((boost::is_same<boost::property_traits<transform_value_property_map<add1<int>, PM> >::category, boost::readable_property_map_tag>::value));
51 BOOST_STATIC_ASSERT((boost::is_same<boost::property_traits<transform_value_property_map<add1_val<int>, PM> >::category, boost::readable_property_map_tag>::value));
52 BOOST_STATIC_ASSERT((boost::is_same<boost::property_traits<transform_value_property_map<return_fixed_ref<int>, PM> >::category, boost::lvalue_property_map_tag>::value));
53
54 BOOST_CHECK(get(transform_value_property_map<add1<int>, PM>(add1<int>(), orig_pm), 3) == 7);
55 BOOST_CHECK(get(transform_value_property_map<add1<int>, PM>(add1<int>(), orig_pm), 4) == 9);
56 BOOST_CHECK(get(make_transform_value_property_map(add1<int>(), orig_pm), 5) == 11);
57 BOOST_CHECK(get(transform_value_property_map<add1_val<int>, PM>(add1_val<int>(), orig_pm), 3) == 7);
58 BOOST_CHECK(get(transform_value_property_map<add1_val<int>, PM>(add1_val<int>(), orig_pm), 4) == 9);
59 BOOST_CHECK(get(make_transform_value_property_map<int>(add1_val<int>(), orig_pm), 5) == 11);
60 int val;
61 const transform_value_property_map<return_fixed_ref<int>, PM> pm(return_fixed_ref<int>((&val)), orig_pm);
62 put(pm, 1, 6);
63 BOOST_CHECK(get(pm, 2) == 6);
64 BOOST_CHECK((get(pm, 3) = 7) == 7);
65 BOOST_CHECK(get(pm, 4) == 7);
66 const transform_value_property_map<return_fixed_ref<int>, PM> pm2 = pm; // Check shallow copying
67 BOOST_CHECK(get(pm2, 5) == 7);
68 put(pm2, 3, 1);
69 BOOST_CHECK(get(pm, 1) == 1);
70
71 return 0;
72 }
73