test1.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #include "boost/config.hpp"
  13. #ifdef BOOST_MSVC
  14. #pragma warning(disable:4244) // conversion from const int to const short
  15. #endif
  16. #include "boost/core/lightweight_test.hpp"
  17. #include "boost/variant.hpp"
  18. #include "class_a.h"
  19. #include "jobs.h"
  20. #include <iostream>
  21. #include <string>
  22. #include <vector>
  23. void run()
  24. {
  25. using boost::apply_visitor;
  26. using boost::variant;
  27. using std::string;
  28. using std::vector;
  29. using std::cout;
  30. using std::endl;
  31. typedef variant< char*, string, short > t_var0;
  32. typedef variant< int, string, double > t_var1;
  33. typedef variant< short, const char* > t_var2;
  34. typedef variant< string, char > t_var3;
  35. typedef variant< unsigned short, const char* > t_var4;
  36. typedef variant< unsigned short, const char*, t_var2 > t_var5;
  37. typedef variant< unsigned short, const char*, t_var5 > t_var6;
  38. typedef variant< class_a, const void* > t_var7;
  39. typedef variant< t_var6, int > t_var8;
  40. typedef variant< t_var8, unsigned short > t_var9;
  41. typedef variant< char, unsigned char > t_var10;
  42. typedef variant< short, int, vector<int>, long> t_var11;
  43. t_var1 v1;
  44. t_var0 v0;
  45. t_var2 v2;
  46. t_var3 v3;
  47. t_var4 v4;
  48. t_var5 v5;
  49. t_var6 v6;
  50. t_var7 v7;
  51. t_var8 v8;
  52. t_var9 v9;
  53. t_var10 v10;
  54. t_var11 v11;
  55. //
  56. // Check assignment rules
  57. //
  58. v2 = 4;
  59. v4 = v2;
  60. verify(v4, spec<unsigned short>());
  61. v2 = "abc";
  62. v4 = v2;
  63. verify(v4, spec<const char*>(), "[V] abc");
  64. v5 = "def";
  65. verify(v5, spec<const char*>(), "[V] def");
  66. v5 = v2;
  67. verify(v5, spec<t_var2>(), "[V] [V] abc");
  68. v6 = 58;
  69. verify(v6, spec<unsigned short>(), "[V] 58");
  70. v6 = v5;
  71. verify(v6, spec<t_var5>(), "[V] [V] [V] abc");
  72. v8 = v2;
  73. verify(v8, spec<t_var6>(), "[V] [V] abc");
  74. v8 = v6;
  75. verify(v8, spec<t_var6>(), "[V] [V] [V] [V] abc");
  76. v7 = v2;
  77. verify(v7, spec<const void*>());
  78. v7 = 199;
  79. verify(v7, spec<class_a>(), "[V] class_a(199)");
  80. v2 = 200;
  81. v7 = v2;
  82. verify(v7, spec<class_a>(), "[V] class_a(200)");
  83. //
  84. // Check sizes of held values
  85. //
  86. total_sizeof ts;
  87. v1 = 5.9;
  88. apply_visitor(ts, v1);
  89. v1 = 'B';
  90. apply_visitor(ts, v1);
  91. v1 = 3.4f;
  92. apply_visitor(ts, v1);
  93. BOOST_TEST(ts.result() == sizeof(int) + sizeof(double)*2);
  94. v11 = 5;
  95. string res_s = apply_visitor(int_printer(), v11);
  96. BOOST_TEST(res_s == "5");
  97. //
  98. // A variant object holding an std::vector
  99. //
  100. vector<int> int_vec_1;
  101. int_vec_1.push_back(512);
  102. int_vec_1.push_back(256);
  103. int_vec_1.push_back(128);
  104. int_vec_1.push_back(64);
  105. v11 = int_vec_1;
  106. res_s = apply_visitor(int_printer(), v11);
  107. BOOST_TEST(res_s == ",512,256,128,64");
  108. }
  109. int main()
  110. {
  111. run();
  112. return boost::report_errors();
  113. }