os/ossrv/stdcpp/tsrc/Boost_test/variant/src/test3.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
//-----------------------------------------------------------------------------
sl@0
     2
// boost-libs variant/test/test3.cpp source 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 <sstream>
sl@0
    21
#include <string>
sl@0
    22
#ifdef __SYMBIAN32__
sl@0
    23
#include "std_log_result.h"
sl@0
    24
#define LOG_FILENAME_LINE __FILE__, __LINE__
sl@0
    25
#endif
sl@0
    26
/////////////////////////////////////////////////////////////////////
sl@0
    27
sl@0
    28
using boost::variant;
sl@0
    29
using boost::recursive_wrapper;
sl@0
    30
#ifndef __SYMBIAN32__
sl@0
    31
using std::cout;
sl@0
    32
#endif
sl@0
    33
using std::endl;
sl@0
    34
sl@0
    35
/////////////////////////////////////////////////////////////////////
sl@0
    36
/////////////////////////////////////////////////////////////////////
sl@0
    37
sl@0
    38
struct Add;
sl@0
    39
struct Sub;
sl@0
    40
sl@0
    41
typedef variant<int, recursive_wrapper<Add>, recursive_wrapper<Sub> > Expr;
sl@0
    42
sl@0
    43
struct Sub
sl@0
    44
{
sl@0
    45
   Sub();
sl@0
    46
   Sub(const Expr& l, const Expr& r);
sl@0
    47
   Sub(const Sub& other);
sl@0
    48
sl@0
    49
   Expr lhs_;
sl@0
    50
   Expr rhs_;
sl@0
    51
};
sl@0
    52
sl@0
    53
struct Add
sl@0
    54
{
sl@0
    55
   Add() { }
sl@0
    56
   Add(const Expr& l, const Expr& r) : lhs_(l), rhs_(r) { }
sl@0
    57
   Add(const Add& other) : lhs_(other.lhs_), rhs_(other.rhs_) { }
sl@0
    58
sl@0
    59
   Expr lhs_;
sl@0
    60
   Expr rhs_;
sl@0
    61
};
sl@0
    62
sl@0
    63
Sub::Sub() { }
sl@0
    64
Sub::Sub(const Expr& l, const Expr& r) : lhs_(l), rhs_(r) { }
sl@0
    65
Sub::Sub(const Sub& other) : lhs_(other.lhs_), rhs_(other.rhs_) { }
sl@0
    66
sl@0
    67
sl@0
    68
//
sl@0
    69
// insert-to operators
sl@0
    70
//
sl@0
    71
std::ostream& operator<<(std::ostream& out, const Sub& a);
sl@0
    72
sl@0
    73
std::ostream& operator<<(std::ostream& out, const Add& a)
sl@0
    74
{
sl@0
    75
   out << '(' << a.lhs_ << '+' << a.rhs_ << ')';
sl@0
    76
   return out;
sl@0
    77
}
sl@0
    78
sl@0
    79
std::ostream& operator<<(std::ostream& out, const Sub& a)
sl@0
    80
{
sl@0
    81
   out << '(' << a.lhs_ << '-' << a.rhs_ << ')';
sl@0
    82
   return out;
sl@0
    83
}
sl@0
    84
sl@0
    85
//
sl@0
    86
// Expression evaluation visitor
sl@0
    87
//
sl@0
    88
struct Calculator : boost::static_visitor<int>
sl@0
    89
{
sl@0
    90
   Calculator()  { }
sl@0
    91
sl@0
    92
   int operator()(Add& x) const
sl@0
    93
   {
sl@0
    94
      Calculator calc;
sl@0
    95
      int n1 = boost::apply_visitor(calc, x.lhs_);
sl@0
    96
      int n2 = boost::apply_visitor(calc, x.rhs_);
sl@0
    97
      
sl@0
    98
      return n1 + n2;
sl@0
    99
   }
sl@0
   100
sl@0
   101
   int operator()(Sub& x) const
sl@0
   102
   {
sl@0
   103
      return boost::apply_visitor(Calculator(), x.lhs_) 
sl@0
   104
         - boost::apply_visitor(Calculator(), x.rhs_);
sl@0
   105
   }
sl@0
   106
sl@0
   107
   int operator()(Expr& x) const
sl@0
   108
   {
sl@0
   109
      Calculator calc;
sl@0
   110
      return boost::apply_visitor(calc, x);
sl@0
   111
   }
sl@0
   112
sl@0
   113
   int operator()(int x) const
sl@0
   114
   {
sl@0
   115
      return x;
sl@0
   116
   }
sl@0
   117
sl@0
   118
}; // Calculator
sl@0
   119
sl@0
   120
sl@0
   121
/////////////////////////////////////////////////////////////////////
sl@0
   122
sl@0
   123
sl@0
   124
int test_main(int, char* [])
sl@0
   125
{
sl@0
   126
std_log(LOG_FILENAME_LINE,"[Test Case for test3]");
sl@0
   127
   int n = 13;
sl@0
   128
   Expr e1( Add(n, Sub(Add(40,2),Add(10,4))) ); //n + (40+2)-(10+14) = n+28
sl@0
   129
sl@0
   130
   std::ostringstream e1_str;
sl@0
   131
   e1_str << e1;
sl@0
   132
sl@0
   133
   BOOST_CHECK(e1.type() == typeid(Add));
sl@0
   134
   BOOST_CHECK(e1_str.str() == "(13+((40+2)-(10+4)))");
sl@0
   135
sl@0
   136
   //Evaluate expression
sl@0
   137
   int res = boost::apply_visitor(Calculator(), e1);
sl@0
   138
   BOOST_CHECK(res == n + 28);
sl@0
   139
sl@0
   140
#ifdef __SYMBIAN32__
sl@0
   141
   	testResultXml("test3");
sl@0
   142
	close_log_file();
sl@0
   143
#endif
sl@0
   144
   return 0;
sl@0
   145
}
sl@0
   146