transform_value_property_map_test.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include <boost/property_map/function_property_map.hpp>
  13. #include <boost/property_map/transform_value_property_map.hpp>
  14. #include <boost/concept/assert.hpp>
  15. #include <boost/property_map/property_map.hpp>
  16. #include <boost/test/minimal.hpp>
  17. #include <boost/static_assert.hpp>
  18. // Ensure this is not default constructible
  19. struct times2 {typedef int result_type; times2(int) {}; int operator()(int x) const {return 2 * x;}};
  20. template <typename T>
  21. struct add1 {typedef T result_type; T operator()(const T& x) const {return x + 1;}};
  22. template <typename T>
  23. struct add1_val {typedef T result_type; T operator()(T x) const {return x + 1;}};
  24. template <typename T>
  25. struct return_fixed_ref {
  26. int* ptr;
  27. return_fixed_ref(int* ptr): ptr(ptr) {}
  28. typedef int& result_type;
  29. int& operator()(const T&) const {return *ptr;}
  30. };
  31. int test_main(int, char**) {
  32. using namespace boost;
  33. typedef function_property_map<times2, int> PM;
  34. PM orig_pm(times2(0));
  35. BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept<transform_value_property_map<add1<int>, PM>, int>));
  36. BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept<transform_value_property_map<add1<int>, PM, double>, int>));
  37. BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept<transform_value_property_map<add1_val<int>, PM>, int>));
  38. BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept<transform_value_property_map<add1_val<int>, PM, double>, int>));
  39. BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept<transform_value_property_map<return_fixed_ref<int>, PM>, int>));
  40. BOOST_CONCEPT_ASSERT((WritablePropertyMapConcept<transform_value_property_map<return_fixed_ref<int>, PM>, int>));
  41. BOOST_CONCEPT_ASSERT((ReadWritePropertyMapConcept<transform_value_property_map<return_fixed_ref<int>, PM>, int>));
  42. BOOST_CONCEPT_ASSERT((LvaluePropertyMapConcept<transform_value_property_map<return_fixed_ref<int>, PM>, int>));
  43. BOOST_STATIC_ASSERT((boost::is_same<boost::property_traits<transform_value_property_map<add1<int>, PM> >::category, boost::readable_property_map_tag>::value));
  44. BOOST_STATIC_ASSERT((boost::is_same<boost::property_traits<transform_value_property_map<add1_val<int>, PM> >::category, boost::readable_property_map_tag>::value));
  45. 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));
  46. BOOST_CHECK(get(transform_value_property_map<add1<int>, PM>(add1<int>(), orig_pm), 3) == 7);
  47. BOOST_CHECK(get(transform_value_property_map<add1<int>, PM>(add1<int>(), orig_pm), 4) == 9);
  48. BOOST_CHECK(get(make_transform_value_property_map(add1<int>(), orig_pm), 5) == 11);
  49. BOOST_CHECK(get(transform_value_property_map<add1_val<int>, PM>(add1_val<int>(), orig_pm), 3) == 7);
  50. BOOST_CHECK(get(transform_value_property_map<add1_val<int>, PM>(add1_val<int>(), orig_pm), 4) == 9);
  51. BOOST_CHECK(get(make_transform_value_property_map<int>(add1_val<int>(), orig_pm), 5) == 11);
  52. int val;
  53. const transform_value_property_map<return_fixed_ref<int>, PM> pm(return_fixed_ref<int>((&val)), orig_pm);
  54. put(pm, 1, 6);
  55. BOOST_CHECK(get(pm, 2) == 6);
  56. BOOST_CHECK((get(pm, 3) = 7) == 7);
  57. BOOST_CHECK(get(pm, 4) == 7);
  58. const transform_value_property_map<return_fixed_ref<int>, PM> pm2 = pm; // Check shallow copying
  59. BOOST_CHECK(get(pm2, 5) == 7);
  60. put(pm2, 3, 1);
  61. BOOST_CHECK(get(pm, 1) == 1);
  62. return 0;
  63. }