stdcpp/tsrc/Boost_test/variant/src/test1.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 //-----------------------------------------------------------------------------
       
     2 // boost-libs variant/test/test1.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 "class_a.h"
       
    20 #include "jobs.h"
       
    21 
       
    22 #include <iostream>
       
    23 #include <string>
       
    24 #include <vector>
       
    25 
       
    26 #ifdef __SYMBIAN32__
       
    27 #include "std_log_result.h"
       
    28 #define LOG_FILENAME_LINE __FILE__, __LINE__
       
    29 #endif
       
    30 
       
    31 void run()
       
    32 {
       
    33 
       
    34    using boost::apply_visitor;
       
    35    using boost::variant;
       
    36    using std::string;
       
    37    using std::vector;
       
    38 #ifndef __SYMBIAN32__
       
    39    using std::cout;
       
    40 #endif   
       
    41    using std::endl;
       
    42 
       
    43    typedef variant< char*, string, short > t_var0;
       
    44    typedef variant< int, string, double > t_var1;
       
    45    typedef variant< short, const char* > t_var2;
       
    46    typedef variant< string, char > t_var3;   
       
    47    typedef variant< unsigned short, const char* > t_var4;
       
    48    typedef variant< unsigned short, const char*, t_var2 > t_var5;
       
    49    typedef variant< unsigned short, const char*, t_var5 > t_var6;
       
    50    typedef variant< class_a, const void* > t_var7;
       
    51    typedef variant< t_var6, int > t_var8;
       
    52    typedef variant< t_var8, unsigned short > t_var9;
       
    53    typedef variant< char, unsigned char > t_var10;
       
    54    typedef variant< short, int, vector<int>, long> t_var11;
       
    55 
       
    56    t_var1 v1;
       
    57    t_var0 v0;
       
    58    t_var2 v2;
       
    59    t_var3 v3;
       
    60    t_var4 v4;
       
    61    t_var5 v5;
       
    62    t_var6 v6;
       
    63    t_var7 v7;
       
    64    t_var8 v8;
       
    65    t_var9 v9;
       
    66    t_var10 v10;
       
    67    t_var11 v11;
       
    68 
       
    69 
       
    70    //
       
    71    // Check assignment rules 
       
    72    //
       
    73 
       
    74    v2 = 4;
       
    75    v4 = v2;
       
    76    verify(v4, spec<unsigned short>());
       
    77 
       
    78    v2 = "abc";
       
    79    v4 = v2;
       
    80    verify(v4, spec<const char*>(), "[V] abc");
       
    81 
       
    82    v5 = "def";
       
    83    verify(v5, spec<const char*>(), "[V] def");
       
    84 
       
    85    v5 = v2;
       
    86    verify(v5, spec<t_var2>(), "[V] [V] abc");
       
    87 
       
    88    v6 = 58;
       
    89    verify(v6, spec<unsigned short>(), "[V] 58");
       
    90 
       
    91    v6 = v5;
       
    92    verify(v6, spec<t_var5>(), "[V] [V] [V] abc");
       
    93 
       
    94    v8 = v2;
       
    95    verify(v8, spec<t_var6>(), "[V] [V] abc");
       
    96 
       
    97    v8 = v6;
       
    98    verify(v8, spec<t_var6>(), "[V] [V] [V] [V] abc");
       
    99 
       
   100    v7 = v2;
       
   101    verify(v7, spec<const void*>());
       
   102 
       
   103    v7 = 199;
       
   104    verify(v7, spec<class_a>(), "[V] class_a(199)");
       
   105 
       
   106    v2 = 200;
       
   107    v7 = v2;
       
   108    verify(v7, spec<class_a>(), "[V] class_a(200)");
       
   109 
       
   110 
       
   111 
       
   112    //
       
   113    // Check sizes of held values
       
   114    //
       
   115    total_sizeof ts;
       
   116 
       
   117    v1 = 5.9;
       
   118    apply_visitor(ts, v1);
       
   119 
       
   120    v1 = 'B';
       
   121    apply_visitor(ts, v1);
       
   122 
       
   123    v1 = 3.4f;
       
   124    apply_visitor(ts, v1);
       
   125 
       
   126    BOOST_CHECK(ts.result() == sizeof(int) + sizeof(double)*2);
       
   127 
       
   128    v11 = 5;
       
   129    string res_s = apply_visitor(int_printer(), v11);
       
   130    BOOST_CHECK(res_s == "5");
       
   131 
       
   132    //
       
   133    // A variant object holding an std::vector 
       
   134    //
       
   135    vector<int> int_vec_1;
       
   136    int_vec_1.push_back(512);
       
   137    int_vec_1.push_back(256);
       
   138    int_vec_1.push_back(128);
       
   139    int_vec_1.push_back(64);
       
   140 
       
   141    v11 = int_vec_1;
       
   142    res_s = apply_visitor(int_printer(), v11);
       
   143    BOOST_CHECK(res_s == ",512,256,128,64");
       
   144 }
       
   145 
       
   146 
       
   147 
       
   148 int test_main(int , char* [])
       
   149 {
       
   150 std_log(LOG_FILENAME_LINE,"[Test Case for test1]");
       
   151    run();
       
   152 #ifdef __SYMBIAN32__
       
   153    	testResultXml("test1");
       
   154 	close_log_file();
       
   155 #endif
       
   156    return 0;
       
   157 }