os/ossrv/stdcpp/tsrc/Boost_test/variant/src/test8.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 //-----------------------------------------------------------------------------
     2 // boost-libs variant/test/test8.cpp header file
     3 // See http://www.boost.org for updates, documentation, and revision history.
     4 //-----------------------------------------------------------------------------
     5 //
     6 // Copyright (c) 2003
     7 // Eric Friedman, Itay Maman
     8 //
     9 // Distributed under the Boost Software License, Version 1.0. (See
    10 // accompanying file LICENSE_1_0.txt or copy at
    11 // http://www.boost.org/LICENSE_1_0.txt)
    12 /*
    13  * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
    14 */
    15 
    16 #include "boost/test/minimal.hpp"
    17 #include "boost/variant.hpp"
    18 
    19 #include <iostream>
    20 #include <vector>
    21 #include <string>
    22 
    23 using namespace std;
    24 using namespace boost;
    25 
    26 typedef variant<float, std::string, int, std::vector<std::string> > t_var1;
    27 
    28 struct int_sum : static_visitor<>
    29 {
    30    int_sum() : result_(0) { }
    31 
    32    void operator()(int t) 
    33    {
    34       result_ += t;
    35    }
    36 
    37    result_type operator()(float ) { }
    38    result_type operator()(const std::string& ) { }
    39    result_type operator()(const std::vector<std::string>& ) { }
    40 
    41    int result_;
    42 }; 
    43 
    44 template <typename T, typename Variant>
    45 T& check_pass(Variant& v, T value)
    46 {
    47     BOOST_CHECK(get<T>(&v));
    48 
    49     try
    50     {
    51         T& r = get<T>(v);
    52         BOOST_CHECK(r == value);
    53         return r;
    54     }
    55     catch(boost::bad_get&)
    56     {
    57         throw; // must never reach
    58     }
    59 }
    60 
    61 template <typename T, typename Variant>
    62 void check_fail(Variant& v)
    63 {
    64     BOOST_CHECK(!get<T>(&v));
    65 
    66     try
    67     {
    68         T& r = get<T>(v);
    69         BOOST_CHECK(false && &r); // should never reach
    70     }
    71     catch(boost::bad_get&)
    72     {
    73         // (do nothing here)
    74     }
    75 }
    76 
    77 int test_main(int , char* [])
    78 {
    79    int_sum acc;
    80    t_var1 v1 = 800;
    81 std_log(LOG_FILENAME_LINE,"[Test Case for test8]");
    82    // check get on non-const variant
    83    {
    84       int& r1 = check_pass<int>(v1, 800);
    85       const int& cr1 = check_pass<const int>(v1, 800);
    86 
    87       check_fail<float>(v1);
    88       check_fail<const float>(v1);
    89       check_fail<short>(v1);
    90       check_fail<const short>(v1);
    91 
    92       apply_visitor(acc, v1);
    93       BOOST_CHECK(acc.result_ == 800);
    94 
    95       r1 = 920; // NOTE: modifies content of v1
    96       apply_visitor(acc, v1);
    97       BOOST_CHECK(cr1 == 920);
    98       BOOST_CHECK(acc.result_ == 800 + 920);
    99    }
   100 
   101    // check const correctness:
   102    {
   103       const t_var1& c = v1;
   104 
   105       check_pass<const int>(c, 920);
   106 
   107       //check_fail<int>(c);
   108       check_fail<const float>(c);
   109       //check_fail<float>(c);
   110       check_fail<const short>(c);
   111       //check_fail<short>(c);
   112    }
   113 #ifdef __SYMBIAN32__
   114    	testResultXml("test8");
   115 	close_log_file();
   116 #endif
   117    return boost::exit_success;
   118 }