1 // Copyright (C) 2013 Eurodecision
2 // Authors: Guillaume Pinot
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7
8 #include <boost/property_map/compose_property_map.hpp>
9
10 #include <boost/property_map/function_property_map.hpp>
11 #include <boost/test/minimal.hpp>
12
concept_checks()13 void concept_checks()
14 {
15 using namespace boost;
16 {
17 typedef null_archetype<> Key;
18 //typedef assignable_archetype<copy_constructible_archetype<> > Value;
19 typedef copy_constructible_archetype<assignable_archetype<> > Value;
20 typedef readable_property_map_archetype<Key, Key> GPMap;
21 typedef readable_property_map_archetype<Key, Value> FPMap;
22 typedef compose_property_map<FPMap, GPMap> CPM;
23 BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept<CPM, Key>));
24 }
25 {
26 typedef null_archetype<> Key;
27 typedef copy_constructible_archetype<assignable_archetype<> > Value;
28 typedef readable_property_map_archetype<Key, Key> GPMap;
29 typedef writable_property_map_archetype<Key, Value> FPMap;
30 typedef compose_property_map<FPMap, GPMap> CPM;
31 BOOST_CONCEPT_ASSERT((WritablePropertyMapConcept<CPM, Key>));
32 }
33 {
34 typedef null_archetype<> Key;
35 typedef copy_constructible_archetype<assignable_archetype<> > Value;
36 typedef readable_property_map_archetype<Key, Key> GPMap;
37 typedef read_write_property_map_archetype<Key, Value> FPMap;
38 typedef compose_property_map<FPMap, GPMap> CPM;
39 BOOST_CONCEPT_ASSERT((ReadWritePropertyMapConcept<CPM, Key>));
40 }
41 {
42 typedef null_archetype<> Key;
43 typedef copy_constructible_archetype<assignable_archetype<> > Value;
44 typedef readable_property_map_archetype<Key, Key> GPMap;
45 typedef lvalue_property_map_archetype<Key, Value> FPMap;
46 typedef compose_property_map<FPMap, GPMap> CPM;
47 BOOST_CONCEPT_ASSERT((LvaluePropertyMapConcept<CPM, Key>));
48 }
49 {
50 typedef null_archetype<> Key;
51 typedef copy_constructible_archetype<assignable_archetype<> > Value;
52 typedef readable_property_map_archetype<Key, Key> GPMap;
53 typedef mutable_lvalue_property_map_archetype<Key, Value> FPMap;
54 typedef compose_property_map<FPMap, GPMap> CPM;
55 BOOST_CONCEPT_ASSERT((Mutable_LvaluePropertyMapConcept<CPM, Key>));
56 }
57 }
58
pointer_pmap_check()59 void pointer_pmap_check()
60 {
61 const int idx[] = {2, 0, 4, 1, 3};
62 double v[] = {1., 3., 0., 4., 2.};
63 boost::compose_property_map<double*, const int*> cpm(v, idx);
64
65 for (int i = 0; i < 5; ++i) {
66 BOOST_CHECK(get(cpm, i) == static_cast<double>(i));
67 ++cpm[i];
68 BOOST_CHECK(cpm[i] == static_cast<double>(i + 1));
69 put(cpm, i, 42.);
70 BOOST_CHECK(cpm[i] == 42.);
71 }
72 }
73
74 struct modulo_add_one {
75 typedef int result_type;
modulo_add_onemodulo_add_one76 modulo_add_one(int m): modulo(m) {}
operator ()modulo_add_one77 int operator()(int i) const {return (i + 1) % modulo;}
78 int modulo;
79 };
80
readable_pmap_checks()81 void readable_pmap_checks()
82 {
83 using namespace boost;
84 typedef function_property_map<modulo_add_one, int> modulo_add_one_pmap;
85
86 compose_property_map<modulo_add_one_pmap, modulo_add_one_pmap>
87 cpm(modulo_add_one(5), modulo_add_one(5));
88
89 for (int i = 0; i < 10; ++i)
90 BOOST_CHECK(get(cpm, i) == (i + 2) % 5);
91 }
92
93 int
test_main(int,char **)94 test_main(int, char**)
95 {
96 concept_checks();
97 pointer_pmap_check();
98 readable_pmap_checks();
99
100 return 0;
101 }
102