File: dynamic_properties_test.cpp

package info (click to toggle)
boost1.42 1.42.0-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 277,864 kB
  • ctags: 401,076
  • sloc: cpp: 1,235,659; xml: 74,142; ansic: 41,313; python: 26,756; sh: 11,840; cs: 2,118; makefile: 655; perl: 494; yacc: 456; asm: 353; csh: 6
file content (136 lines) | stat: -rw-r--r-- 4,327 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

// Copyright 2005 The Trustees of Indiana University.

// Use, modification and distribution is subject to the Boost Software 
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

//
// dynamic_properties_test.cpp - test cases for the dynamic property maps.
//

//  Author: Ronald Garcia
#include <boost/config.hpp>

// For Borland, act like BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS is defined
#if defined (__BORLANDC__) && (__BORLANDC__ <= 0x570) && !defined(BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS)
#  define BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS
#endif

#include <boost/test/minimal.hpp>
#include <boost/property_map/dynamic_property_map.hpp>
#include <boost/property_map/property_map.hpp>
#include <map>
#include <iostream>
#include <string>

// generate a dynamic_property_map that maps strings to strings
// WARNING: This code leaks memory.  For testing purposes only!
// WARNING: This code uses library internals. For testing purposes only!
std::auto_ptr<boost::dynamic_property_map>
string2string_gen(const std::string& name,
                  const boost::any&,
                  const boost::any&) {
  typedef std::map<std::string,std::string> map_t;
  typedef
    boost::associative_property_map< std::map<std::string, std::string> >
    property_t;


  map_t* mymap = new map_t(); // hint: leaky memory here!

  property_t property_map(*mymap);

  std::auto_ptr<boost::dynamic_property_map> pm(
    new
    boost::detail::dynamic_property_map_adaptor<property_t>(property_map));

  return pm;
}


int test_main(int,char**) {

  // build property maps using associative_property_map

  std::map<std::string, int> string2int;
  std::map<double,std::string> double2string;
  boost::associative_property_map< std::map<std::string, int> >
    int_map(string2int);
  boost::associative_property_map< std::map<double, std::string> >
    dbl_map(double2string);


  // add key-value information
  string2int["one"] = 1;
  string2int["five"] = 5;
  
  double2string[5.3] = "five point three";
  double2string[3.14] = "pi";
 
 
  // build and populate dynamic interface
  boost::dynamic_properties properties;
  properties.property("int",int_map);
  properties.property("double",dbl_map);
  
  using boost::get;
  using boost::put;
  using boost::type;
  // Get tests
  {
    BOOST_CHECK(get("int",properties,std::string("one")) == "1");
#ifndef BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS
    BOOST_CHECK(boost::get<int>("int",properties,std::string("one")) == 1);
#endif
    BOOST_CHECK(get("int",properties,std::string("one"), type<int>()) == 1);
    BOOST_CHECK(get("double",properties,5.3) == "five point three");
  }

  // Put tests
  {
    put("int",properties,std::string("five"),6);
    BOOST_CHECK(get("int",properties,std::string("five")) == "6");
    put("int",properties,std::string("five"),std::string("5"));
#ifndef BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS
    BOOST_CHECK(get<int>("int",properties,std::string("five")) == 5);
#endif
    BOOST_CHECK(get("int",properties,std::string("five"),type<int>()) == 5);
    put("double",properties,3.14,std::string("3.14159"));
    BOOST_CHECK(get("double",properties,3.14) == "3.14159");
    put("double",properties,3.14,std::string("pi"));
#ifndef BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS
    BOOST_CHECK(get<std::string>("double",properties,3.14) == "pi");
#endif
    BOOST_CHECK(get("double",properties,3.14,type<std::string>()) == "pi");
  }

  // Nonexistent property
  {
    try {
      get("nope",properties,3.14);
      BOOST_ERROR("No exception thrown.");
    } catch (boost::dynamic_get_failure&) { }

    try {
      put("nada",properties,3.14,std::string("3.14159"));
      BOOST_ERROR("No exception thrown.");
    } catch (boost::property_not_found&) { }
  }

  // Nonexistent property gets generated
  {
    boost::dynamic_properties props(&string2string_gen);
    put("nada",props,std::string("3.14"),std::string("pi"));
    BOOST_CHECK(get("nada",props,std::string("3.14"))  == "pi");
  }

  // Use the ignore_other_properties generator
  {
    boost::dynamic_properties props(&boost::ignore_other_properties);
    bool value = put("nada",props,std::string("3.14"),std::string("pi"));
    BOOST_CHECK(value == false);
  }
 
  return boost::exit_success;
}