stdcpp/tsrc/Boost_test/property_map/src/dynamic_properties_test.cpp
changeset 31 ce057bb09d0b
child 45 4b03adbd26ca
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     1 
       
     2 // Copyright 2005 The Trustees of Indiana University.
       
     3 
       
     4 // Use, modification and distribution is subject to the Boost Software 
       
     5 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
       
     6 // http://www.boost.org/LICENSE_1_0.txt)
       
     7 
       
     8 //
       
     9 // dynamic_properties_test.cpp - test cases for the dynamic property maps.
       
    10 //
       
    11 /*
       
    12  * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
       
    13 */
       
    14 
       
    15 //  Author: Ronald Garcia
       
    16 #include <boost/config.hpp>
       
    17 
       
    18 // For Borland, act like BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS is defined
       
    19 #if defined (__BORLANDC__) && (__BORLANDC__ <= 0x570) && !defined(BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS)
       
    20 #  define BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS
       
    21 #endif
       
    22 
       
    23 #include <boost/test/minimal.hpp>
       
    24 #include <boost/dynamic_property_map.hpp>
       
    25 #include <boost/property_map.hpp>
       
    26 #include <map>
       
    27 #include <iostream>
       
    28 #include <string>
       
    29 #ifdef __SYMBIAN32__
       
    30 #include "std_log_result.h"
       
    31 #define LOG_FILENAME_LINE __FILE__, __LINE__
       
    32 #endif
       
    33 // generate a dynamic_property_map that maps strings to strings
       
    34 // WARNING: This code leaks memory.  For testing purposes only!
       
    35 // WARNING: This code uses library internals. For testing purposes only!
       
    36 std::auto_ptr<boost::dynamic_property_map>
       
    37 string2string_gen(const std::string& name,
       
    38                   const boost::any&,
       
    39                   const boost::any&) {
       
    40   typedef std::map<std::string,std::string> map_t;
       
    41   typedef
       
    42     boost::associative_property_map< std::map<std::string, std::string> >
       
    43     property_t;
       
    44 
       
    45 
       
    46   map_t* mymap = new map_t(); // hint: leaky memory here!
       
    47 
       
    48   property_t property_map(*mymap);
       
    49 
       
    50   std::auto_ptr<boost::dynamic_property_map> pm(
       
    51     new
       
    52     boost::detail::dynamic_property_map_adaptor<property_t>(property_map));
       
    53 
       
    54   return pm;
       
    55 }
       
    56 
       
    57 
       
    58 int test_main(int,char**) {
       
    59 
       
    60   // build property maps using associative_property_map
       
    61 
       
    62   std::map<std::string, int> string2int;
       
    63   std::map<double,std::string> double2string;
       
    64   boost::associative_property_map< std::map<std::string, int> >
       
    65     int_map(string2int);
       
    66   boost::associative_property_map< std::map<double, std::string> >
       
    67     dbl_map(double2string);
       
    68 
       
    69 
       
    70   // add key-value information
       
    71   string2int["one"] = 1;
       
    72   string2int["five"] = 5;
       
    73   
       
    74   double2string[5.3] = "five point three";
       
    75   double2string[3.14] = "pi";
       
    76  
       
    77  
       
    78   // build and populate dynamic interface
       
    79   boost::dynamic_properties properties;
       
    80   properties.property("int",int_map);
       
    81   properties.property("double",dbl_map);
       
    82   
       
    83   using boost::get;
       
    84   using boost::put;
       
    85   using boost::type;
       
    86   // Get tests
       
    87   {
       
    88     BOOST_CHECK(get("int",properties,std::string("one")) == "1");
       
    89 #ifndef BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS
       
    90     BOOST_CHECK(boost::get<int>("int",properties,std::string("one")) == 1);
       
    91 #endif
       
    92     BOOST_CHECK(get("int",properties,std::string("one"), type<int>()) == 1);
       
    93     BOOST_CHECK(get("double",properties,5.3) == "five point three");
       
    94   }
       
    95 
       
    96   // Put tests
       
    97   {
       
    98     put("int",properties,std::string("five"),6);
       
    99     BOOST_CHECK(get("int",properties,std::string("five")) == "6");
       
   100     put("int",properties,std::string("five"),std::string("5"));
       
   101 #ifndef BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS
       
   102     BOOST_CHECK(get<int>("int",properties,std::string("five")) == 5);
       
   103 #endif
       
   104     BOOST_CHECK(get("int",properties,std::string("five"),type<int>()) == 5);
       
   105     put("double",properties,3.14,std::string("3.14159"));
       
   106     BOOST_CHECK(get("double",properties,3.14) == "3.14159");
       
   107     put("double",properties,3.14,std::string("pi"));
       
   108 #ifndef BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS
       
   109     BOOST_CHECK(get<std::string>("double",properties,3.14) == "pi");
       
   110 #endif
       
   111     BOOST_CHECK(get("double",properties,3.14,type<std::string>()) == "pi");
       
   112   }
       
   113 
       
   114   // Nonexistent property
       
   115   {
       
   116     try {
       
   117       get("nope",properties,3.14);
       
   118       BOOST_ERROR("No exception thrown.");
       
   119     } catch (boost::dynamic_get_failure&) { }
       
   120 
       
   121     try {
       
   122       put("nada",properties,3.14,std::string("3.14159"));
       
   123       BOOST_ERROR("No exception thrown.");
       
   124     } catch (boost::property_not_found&) { }
       
   125   }
       
   126 
       
   127   // Nonexistent property gets generated
       
   128   {
       
   129     boost::dynamic_properties props(&string2string_gen);
       
   130     put("nada",props,std::string("3.14"),std::string("pi"));
       
   131     BOOST_CHECK(get("nada",props,std::string("3.14"))  == "pi");
       
   132   }
       
   133 
       
   134 #ifdef __SYMBIAN32__
       
   135 
       
   136 
       
   137 	std_log(LOG_FILENAME_LINE,"[End Test Case ]");
       
   138  
       
   139 	testResultXml("dynamic_properties_test");
       
   140 	close_log_file();
       
   141 #endif
       
   142      return 0;
       
   143 //  return boost::exit_success;
       
   144 }