dynamic_properties_test.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2005 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. // dynamic_properties_test.cpp - test cases for the dynamic property maps.
  7. //
  8. // Author: Ronald Garcia
  9. #include <boost/config.hpp>
  10. // For Borland, act like BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS is defined
  11. #if defined (__BORLANDC__) && (__BORLANDC__ <= 0x570) && !defined(BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS)
  12. # define BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS
  13. #endif
  14. #include <boost/test/minimal.hpp>
  15. #include <boost/smart_ptr.hpp>
  16. #include <boost/property_map/dynamic_property_map.hpp>
  17. #include <boost/property_map/property_map.hpp>
  18. #include <map>
  19. #include <iostream>
  20. #include <string>
  21. #include <memory>
  22. // generate a dynamic_property_map that maps strings to strings
  23. // WARNING: This code leaks memory. For testing purposes only!
  24. // WARNING: This code uses library internals. For testing purposes only!
  25. boost::shared_ptr<boost::dynamic_property_map>
  26. string2string_gen(const std::string& name,
  27. const boost::any&,
  28. const boost::any&) {
  29. typedef std::map<std::string,std::string> map_t;
  30. typedef
  31. boost::associative_property_map< std::map<std::string, std::string> >
  32. property_t;
  33. map_t* mymap = new map_t(); // hint: leaky memory here!
  34. property_t property_map(*mymap);
  35. boost::shared_ptr<boost::dynamic_property_map> pm(
  36. new
  37. boost::detail::dynamic_property_map_adaptor<property_t>(property_map));
  38. return pm;
  39. }
  40. int test_main(int,char**) {
  41. // build property maps using associative_property_map
  42. std::map<std::string, int> string2int;
  43. std::map<double,std::string> double2string;
  44. boost::associative_property_map< std::map<std::string, int> >
  45. int_map(string2int);
  46. boost::associative_property_map< std::map<double, std::string> >
  47. dbl_map(double2string);
  48. // add key-value information
  49. string2int["one"] = 1;
  50. string2int["five"] = 5;
  51. double2string[5.3] = "five point three";
  52. double2string[3.14] = "pi";
  53. // build and populate dynamic interface
  54. boost::dynamic_properties properties;
  55. properties.property("int",int_map);
  56. properties.property("double",dbl_map);
  57. using boost::get;
  58. using boost::put;
  59. using boost::type;
  60. // Get tests
  61. {
  62. BOOST_CHECK(get("int",properties,std::string("one")) == "1");
  63. #ifndef BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS
  64. BOOST_CHECK(boost::get<int>("int",properties,std::string("one")) == 1);
  65. #endif
  66. BOOST_CHECK(get("int",properties,std::string("one"), type<int>()) == 1);
  67. BOOST_CHECK(get("double",properties,5.3) == "five point three");
  68. }
  69. // Put tests
  70. {
  71. put("int",properties,std::string("five"),6);
  72. BOOST_CHECK(get("int",properties,std::string("five")) == "6");
  73. put("int",properties,std::string("five"),std::string("5"));
  74. #ifndef BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS
  75. BOOST_CHECK(get<int>("int",properties,std::string("five")) == 5);
  76. #endif
  77. BOOST_CHECK(get("int",properties,std::string("five"),type<int>()) == 5);
  78. put("double",properties,3.14,std::string("3.14159"));
  79. BOOST_CHECK(get("double",properties,3.14) == "3.14159");
  80. put("double",properties,3.14,std::string("pi"));
  81. #ifndef BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS
  82. BOOST_CHECK(get<std::string>("double",properties,3.14) == "pi");
  83. #endif
  84. BOOST_CHECK(get("double",properties,3.14,type<std::string>()) == "pi");
  85. }
  86. // Nonexistent property
  87. {
  88. try {
  89. get("nope",properties,3.14);
  90. BOOST_ERROR("No exception thrown.");
  91. } catch (boost::dynamic_get_failure&) { }
  92. try {
  93. put("nada",properties,3.14,std::string("3.14159"));
  94. BOOST_ERROR("No exception thrown.");
  95. } catch (boost::property_not_found&) { }
  96. }
  97. // Nonexistent property gets generated
  98. {
  99. boost::dynamic_properties props(&string2string_gen);
  100. put("nada",props,std::string("3.14"),std::string("pi"));
  101. BOOST_CHECK(get("nada",props,std::string("3.14")) == "pi");
  102. }
  103. // Use the ignore_other_properties generator
  104. {
  105. boost::dynamic_properties props(&boost::ignore_other_properties);
  106. bool value = put("nada",props,std::string("3.14"),std::string("pi"));
  107. BOOST_CHECK(value == false);
  108. }
  109. return boost::exit_success;
  110. }