another_tuple_test_bench.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // For more information, see http://www.boost.org
  7. // another_test_bench.cpp --------------------------------
  8. // This file has various tests to see that things that shouldn't
  9. // compile, don't compile.
  10. // Defining any of E1 to E5 or E7 to E11 opens some illegal code that
  11. // should cause the compliation to fail.
  12. #define BOOST_INCLUDE_MAIN // for testing, include rather than link
  13. #include <boost/test/test_tools.hpp> // see "Header Implementation Option"
  14. #include "boost/tuple/tuple.hpp"
  15. #include <string>
  16. #include <utility>
  17. using namespace boost;
  18. using namespace boost::tuples;
  19. template<class T> void dummy(const T&) {}
  20. class A {}; class B {}; class C {};
  21. // A non-copyable class
  22. class no_copy {
  23. no_copy(const no_copy&) {}
  24. public:
  25. no_copy() {};
  26. };
  27. no_copy y;
  28. #ifdef E1
  29. tuple<no_copy> v1; // should faild
  30. #endif
  31. #ifdef E2
  32. char cs[10];
  33. tuple<char[10]> v3; // should fail, arrays must be stored as references
  34. #endif
  35. // a class without a public default constructor
  36. class no_def_constructor {
  37. no_def_constructor() {}
  38. public:
  39. no_def_constructor(std::string) {} // can be constructed with a string
  40. };
  41. void foo1() {
  42. #ifdef E3
  43. dummy(tuple<no_def_constructor, no_def_constructor, no_def_constructor>());
  44. // should fail
  45. #endif
  46. }
  47. void foo2() {
  48. // testing default values
  49. #ifdef E4
  50. dummy(tuple<double&>()); // should fail, not defaults for references
  51. dummy(tuple<const double&>()); // likewise
  52. #endif
  53. #ifdef E5
  54. double dd = 5;
  55. dummy(tuple<double&>(dd+3.14)); // should fail, temporary to non-const reference
  56. #endif
  57. }
  58. // make_tuple ------------------------------------------
  59. void foo3() {
  60. #ifdef E7
  61. std::make_pair("Doesn't","Work"); // fails
  62. #endif
  63. // make_tuple("Does", "Work"); // this should work
  64. }
  65. // - testing element access
  66. void foo4()
  67. {
  68. double d = 2.7;
  69. A a;
  70. tuple<int, double&, const A&> t(1, d, a);
  71. const tuple<int, double&, const A> ct = t;
  72. (void)ct;
  73. #ifdef E8
  74. get<0>(ct) = 5; // can't assign to const
  75. #endif
  76. #ifdef E9
  77. get<4>(t) = A(); // can't assign to const
  78. #endif
  79. #ifdef E10
  80. dummy(get<5>(ct)); // illegal index
  81. #endif
  82. }
  83. // testing copy and assignment with implicit conversions between elements
  84. // testing tie
  85. class AA {};
  86. class BB : public AA {};
  87. struct CC { CC() {} CC(const BB& b) {} };
  88. struct DD { operator CC() const { return CC(); }; };
  89. void foo5() {
  90. tuple<char, BB*, BB, DD> t;
  91. (void)t;
  92. tuple<char, char> aaa;
  93. tuple<int, int> bbb(aaa);
  94. (void)bbb;
  95. // tuple<int, AA*, CC, CC> a = t;
  96. // a = t;
  97. }
  98. // testing tie
  99. // testing assignment from std::pair
  100. void foo7() {
  101. tuple<int, int, float> a;
  102. #ifdef E11
  103. a = std::make_pair(1, 2); // should fail, tuple is of length 3, not 2
  104. #endif
  105. dummy(a);
  106. }
  107. // --------------------------------
  108. // ----------------------------
  109. int test_main(int, char *[]) {
  110. foo1();
  111. foo2();
  112. foo3();
  113. foo4();
  114. foo5();
  115. foo7();
  116. return 0;
  117. }