function_property_map_test.cpp 3.2 KB

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