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