compressed_pair_test.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // boost::compressed_pair test program
  2. // (C) Copyright John Maddock 2000.
  3. // Use, modification and distribution are subject to the Boost Software License,
  4. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt).
  6. // standalone test program for <boost/compressed_pair.hpp>
  7. // Revised 03 Oct 2000:
  8. // Enabled tests for VC6.
  9. #include <iostream>
  10. #include <typeinfo>
  11. #include <cassert>
  12. #include <boost/compressed_pair.hpp>
  13. #include <boost/core/lightweight_test.hpp>
  14. using namespace boost;
  15. struct empty_UDT
  16. {
  17. ~empty_UDT(){};
  18. empty_UDT& operator=(const empty_UDT&){ return *this; }
  19. bool operator==(const empty_UDT&)const
  20. { return true; }
  21. };
  22. struct empty_POD_UDT
  23. {
  24. empty_POD_UDT& operator=(const empty_POD_UDT&){ return *this; }
  25. bool operator==(const empty_POD_UDT&)const
  26. { return true; }
  27. };
  28. struct non_empty1
  29. {
  30. int i;
  31. non_empty1() : i(1){}
  32. non_empty1(int v) : i(v){}
  33. friend bool operator==(const non_empty1& a, const non_empty1& b)
  34. { return a.i == b.i; }
  35. };
  36. struct non_empty2
  37. {
  38. int i;
  39. non_empty2() : i(3){}
  40. non_empty2(int v) : i(v){}
  41. friend bool operator==(const non_empty2& a, const non_empty2& b)
  42. { return a.i == b.i; }
  43. };
  44. #ifdef __GNUC__
  45. using std::swap;
  46. #endif
  47. template <class T1, class T2>
  48. struct compressed_pair_tester
  49. {
  50. // define the types we need:
  51. typedef T1 first_type;
  52. typedef T2 second_type;
  53. typedef typename call_traits<first_type>::param_type first_param_type;
  54. typedef typename call_traits<second_type>::param_type second_param_type;
  55. // define our test proc:
  56. static void test(first_param_type p1, second_param_type p2, first_param_type p3, second_param_type p4);
  57. };
  58. template <class T1, class T2>
  59. void compressed_pair_tester<T1, T2>::test(first_param_type p1, second_param_type p2, first_param_type p3, second_param_type p4)
  60. {
  61. #ifndef __GNUC__
  62. // gcc 2.90 can't cope with function scope using
  63. // declarations, and generates an internal compiler error...
  64. using std::swap;
  65. #endif
  66. // default construct:
  67. boost::compressed_pair<T1,T2> cp1;
  68. // first param construct:
  69. boost::compressed_pair<T1,T2> cp2(p1);
  70. cp2.second() = p2;
  71. BOOST_TEST(cp2.first() == p1);
  72. BOOST_TEST(cp2.second() == p2);
  73. // second param construct:
  74. boost::compressed_pair<T1,T2> cp3(p2);
  75. cp3.first() = p1;
  76. BOOST_TEST(cp3.second() == p2);
  77. BOOST_TEST(cp3.first() == p1);
  78. // both param construct:
  79. boost::compressed_pair<T1,T2> cp4(p1, p2);
  80. BOOST_TEST(cp4.first() == p1);
  81. BOOST_TEST(cp4.second() == p2);
  82. boost::compressed_pair<T1,T2> cp5(p3, p4);
  83. BOOST_TEST(cp5.first() == p3);
  84. BOOST_TEST(cp5.second() == p4);
  85. // check const members:
  86. const boost::compressed_pair<T1,T2>& cpr1 = cp4;
  87. BOOST_TEST(cpr1.first() == p1);
  88. BOOST_TEST(cpr1.second() == p2);
  89. // copy construct:
  90. boost::compressed_pair<T1,T2> cp6(cp4);
  91. BOOST_TEST(cp6.first() == p1);
  92. BOOST_TEST(cp6.second() == p2);
  93. // assignment:
  94. cp1 = cp4;
  95. BOOST_TEST(cp1.first() == p1);
  96. BOOST_TEST(cp1.second() == p2);
  97. cp1 = cp5;
  98. BOOST_TEST(cp1.first() == p3);
  99. BOOST_TEST(cp1.second() == p4);
  100. // swap:
  101. cp4.swap(cp5);
  102. BOOST_TEST(cp4.first() == p3);
  103. BOOST_TEST(cp4.second() == p4);
  104. BOOST_TEST(cp5.first() == p1);
  105. BOOST_TEST(cp5.second() == p2);
  106. swap(cp4,cp5);
  107. BOOST_TEST(cp4.first() == p1);
  108. BOOST_TEST(cp4.second() == p2);
  109. BOOST_TEST(cp5.first() == p3);
  110. BOOST_TEST(cp5.second() == p4);
  111. }
  112. //
  113. // tests for case where one or both
  114. // parameters are reference types:
  115. //
  116. template <class T1, class T2>
  117. struct compressed_pair_reference_tester
  118. {
  119. // define the types we need:
  120. typedef T1 first_type;
  121. typedef T2 second_type;
  122. typedef typename call_traits<first_type>::param_type first_param_type;
  123. typedef typename call_traits<second_type>::param_type second_param_type;
  124. // define our test proc:
  125. static void test(first_param_type p1, second_param_type p2, first_param_type p3, second_param_type p4);
  126. };
  127. template <class T1, class T2>
  128. void compressed_pair_reference_tester<T1, T2>::test(first_param_type p1, second_param_type p2, first_param_type p3, second_param_type p4)
  129. {
  130. #ifndef __GNUC__
  131. // gcc 2.90 can't cope with function scope using
  132. // declarations, and generates an internal compiler error...
  133. using std::swap;
  134. #endif
  135. // both param construct:
  136. boost::compressed_pair<T1,T2> cp4(p1, p2);
  137. BOOST_TEST(cp4.first() == p1);
  138. BOOST_TEST(cp4.second() == p2);
  139. boost::compressed_pair<T1,T2> cp5(p3, p4);
  140. BOOST_TEST(cp5.first() == p3);
  141. BOOST_TEST(cp5.second() == p4);
  142. // check const members:
  143. const boost::compressed_pair<T1,T2>& cpr1 = cp4;
  144. BOOST_TEST(cpr1.first() == p1);
  145. BOOST_TEST(cpr1.second() == p2);
  146. // copy construct:
  147. boost::compressed_pair<T1,T2> cp6(cp4);
  148. BOOST_TEST(cp6.first() == p1);
  149. BOOST_TEST(cp6.second() == p2);
  150. // assignment:
  151. // VC6 bug:
  152. // When second() is an empty class, VC6 performs the
  153. // assignment by doing a memcpy - even though the empty
  154. // class is really a zero sized base class, the result
  155. // is that the memory of first() gets trampled over.
  156. // Similar arguments apply to the case that first() is
  157. // an empty base class.
  158. // Strangely the problem is dependent upon the compiler
  159. // settings - some generate the problem others do not.
  160. cp4.first() = p3;
  161. cp4.second() = p4;
  162. BOOST_TEST(cp4.first() == p3);
  163. BOOST_TEST(cp4.second() == p4);
  164. }
  165. //
  166. // supplimentary tests for case where first arg only is a reference type:
  167. //
  168. template <class T1, class T2>
  169. struct compressed_pair_reference1_tester
  170. {
  171. // define the types we need:
  172. typedef T1 first_type;
  173. typedef T2 second_type;
  174. typedef typename call_traits<first_type>::param_type first_param_type;
  175. typedef typename call_traits<second_type>::param_type second_param_type;
  176. // define our test proc:
  177. static void test(first_param_type p1, second_param_type p2, first_param_type p3, second_param_type p4);
  178. };
  179. template <class T1, class T2>
  180. void compressed_pair_reference1_tester<T1, T2>::test(first_param_type p1, second_param_type p2, first_param_type, second_param_type)
  181. {
  182. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  183. // first param construct:
  184. boost::compressed_pair<T1,T2> cp2(p1);
  185. cp2.second() = p2;
  186. BOOST_TEST(cp2.first() == p1);
  187. BOOST_TEST(cp2.second() == p2);
  188. #endif
  189. }
  190. //
  191. // supplimentary tests for case where second arg only is a reference type:
  192. //
  193. template <class T1, class T2>
  194. struct compressed_pair_reference2_tester
  195. {
  196. // define the types we need:
  197. typedef T1 first_type;
  198. typedef T2 second_type;
  199. typedef typename call_traits<first_type>::param_type first_param_type;
  200. typedef typename call_traits<second_type>::param_type second_param_type;
  201. // define our test proc:
  202. static void test(first_param_type p1, second_param_type p2, first_param_type p3, second_param_type p4);
  203. };
  204. template <class T1, class T2>
  205. void compressed_pair_reference2_tester<T1, T2>::test(first_param_type p1, second_param_type p2, first_param_type, second_param_type)
  206. {
  207. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  208. // second param construct:
  209. boost::compressed_pair<T1,T2> cp3(p2);
  210. cp3.first() = p1;
  211. BOOST_TEST(cp3.second() == p2);
  212. BOOST_TEST(cp3.first() == p1);
  213. #endif
  214. }
  215. //
  216. // tests for where one or the other parameter is an array:
  217. //
  218. template <class T1, class T2>
  219. struct compressed_pair_array1_tester
  220. {
  221. // define the types we need:
  222. typedef T1 first_type;
  223. typedef T2 second_type;
  224. typedef typename call_traits<first_type>::param_type first_param_type;
  225. typedef typename call_traits<second_type>::param_type second_param_type;
  226. // define our test proc:
  227. static void test(first_param_type p1, second_param_type p2, first_param_type p3, second_param_type p4);
  228. };
  229. template <class T1, class T2>
  230. void compressed_pair_array1_tester<T1, T2>::test(first_param_type p1, second_param_type p2, first_param_type, second_param_type)
  231. {
  232. // default construct:
  233. boost::compressed_pair<T1,T2> cp1;
  234. // second param construct:
  235. boost::compressed_pair<T1,T2> cp3(p2);
  236. cp3.first()[0] = p1[0];
  237. BOOST_TEST(cp3.second() == p2);
  238. BOOST_TEST(cp3.first()[0] == p1[0]);
  239. // check const members:
  240. const boost::compressed_pair<T1,T2>& cpr1 = cp3;
  241. BOOST_TEST(cpr1.first()[0] == p1[0]);
  242. BOOST_TEST(cpr1.second() == p2);
  243. BOOST_TEST(sizeof(T1) == sizeof(cp1.first()));
  244. }
  245. template <class T1, class T2>
  246. struct compressed_pair_array2_tester
  247. {
  248. // define the types we need:
  249. typedef T1 first_type;
  250. typedef T2 second_type;
  251. typedef typename call_traits<first_type>::param_type first_param_type;
  252. typedef typename call_traits<second_type>::param_type second_param_type;
  253. // define our test proc:
  254. static void test(first_param_type p1, second_param_type p2, first_param_type p3, second_param_type p4);
  255. };
  256. template <class T1, class T2>
  257. void compressed_pair_array2_tester<T1, T2>::test(first_param_type p1, second_param_type p2, first_param_type, second_param_type)
  258. {
  259. // default construct:
  260. boost::compressed_pair<T1,T2> cp1;
  261. // first param construct:
  262. boost::compressed_pair<T1,T2> cp2(p1);
  263. cp2.second()[0] = p2[0];
  264. BOOST_TEST(cp2.first() == p1);
  265. BOOST_TEST(cp2.second()[0] == p2[0]);
  266. // check const members:
  267. const boost::compressed_pair<T1,T2>& cpr1 = cp2;
  268. BOOST_TEST(cpr1.first() == p1);
  269. BOOST_TEST(cpr1.second()[0] == p2[0]);
  270. BOOST_TEST(sizeof(T2) == sizeof(cp1.second()));
  271. }
  272. template <class T1, class T2>
  273. struct compressed_pair_array_tester
  274. {
  275. // define the types we need:
  276. typedef T1 first_type;
  277. typedef T2 second_type;
  278. typedef typename call_traits<first_type>::param_type first_param_type;
  279. typedef typename call_traits<second_type>::param_type second_param_type;
  280. // define our test proc:
  281. static void test(first_param_type p1, second_param_type p2, first_param_type p3, second_param_type p4);
  282. };
  283. template <class T1, class T2>
  284. void compressed_pair_array_tester<T1, T2>::test(first_param_type p1, second_param_type p2, first_param_type, second_param_type)
  285. {
  286. // default construct:
  287. boost::compressed_pair<T1,T2> cp1;
  288. cp1.first()[0] = p1[0];
  289. cp1.second()[0] = p2[0];
  290. BOOST_TEST(cp1.first()[0] == p1[0]);
  291. BOOST_TEST(cp1.second()[0] == p2[0]);
  292. // check const members:
  293. const boost::compressed_pair<T1,T2>& cpr1 = cp1;
  294. BOOST_TEST(cpr1.first()[0] == p1[0]);
  295. BOOST_TEST(cpr1.second()[0] == p2[0]);
  296. BOOST_TEST(sizeof(T1) == sizeof(cp1.first()));
  297. BOOST_TEST(sizeof(T2) == sizeof(cp1.second()));
  298. }
  299. int main()
  300. {
  301. // declare some variables to pass to the tester:
  302. non_empty1 ne1(2);
  303. non_empty1 ne2(3);
  304. non_empty2 ne3(4);
  305. non_empty2 ne4(5);
  306. empty_POD_UDT e1;
  307. empty_UDT e2;
  308. // T1 != T2, both non-empty
  309. compressed_pair_tester<non_empty1,non_empty2>::test(ne1, ne3, ne2, ne4);
  310. // T1 != T2, T2 empty
  311. compressed_pair_tester<non_empty1,empty_POD_UDT>::test(ne1, e1, ne2, e1);
  312. // T1 != T2, T1 empty
  313. compressed_pair_tester<empty_POD_UDT,non_empty2>::test(e1, ne3, e1, ne4);
  314. // T1 != T2, both empty
  315. compressed_pair_tester<empty_POD_UDT,empty_UDT>::test(e1, e2, e1, e2);
  316. // T1 == T2, both non-empty
  317. compressed_pair_tester<non_empty1,non_empty1>::test(ne1, ne1, ne2, ne2);
  318. // T1 == T2, both empty
  319. compressed_pair_tester<empty_UDT,empty_UDT>::test(e2, e2, e2, e2);
  320. // test references:
  321. // T1 != T2, both non-empty
  322. compressed_pair_reference_tester<non_empty1&,non_empty2>::test(ne1, ne3, ne2, ne4);
  323. compressed_pair_reference_tester<non_empty1,non_empty2&>::test(ne1, ne3, ne2, ne4);
  324. compressed_pair_reference1_tester<non_empty1&,non_empty2>::test(ne1, ne3, ne2, ne4);
  325. compressed_pair_reference2_tester<non_empty1,non_empty2&>::test(ne1, ne3, ne2, ne4);
  326. // T1 != T2, T2 empty
  327. compressed_pair_reference_tester<non_empty1&,empty_POD_UDT>::test(ne1, e1, ne2, e1);
  328. compressed_pair_reference1_tester<non_empty1&,empty_POD_UDT>::test(ne1, e1, ne2, e1);
  329. // T1 != T2, T1 empty
  330. compressed_pair_reference_tester<empty_POD_UDT,non_empty2&>::test(e1, ne3, e1, ne4);
  331. compressed_pair_reference2_tester<empty_POD_UDT,non_empty2&>::test(e1, ne3, e1, ne4);
  332. // T1 == T2, both non-empty
  333. compressed_pair_reference_tester<non_empty1&,non_empty1&>::test(ne1, ne1, ne2, ne2);
  334. // tests arrays:
  335. non_empty1 nea1[2];
  336. non_empty1 nea2[2];
  337. non_empty2 nea3[2];
  338. non_empty2 nea4[2];
  339. nea1[0] = non_empty1(5);
  340. nea2[0] = non_empty1(6);
  341. nea3[0] = non_empty2(7);
  342. nea4[0] = non_empty2(8);
  343. // T1 != T2, both non-empty
  344. compressed_pair_array1_tester<non_empty1[2],non_empty2>::test(nea1, ne3, nea2, ne4);
  345. compressed_pair_array2_tester<non_empty1,non_empty2[2]>::test(ne1, nea3, ne2, nea4);
  346. compressed_pair_array_tester<non_empty1[2],non_empty2[2]>::test(nea1, nea3, nea2, nea4);
  347. // T1 != T2, T2 empty
  348. compressed_pair_array1_tester<non_empty1[2],empty_POD_UDT>::test(nea1, e1, nea2, e1);
  349. // T1 != T2, T1 empty
  350. compressed_pair_array2_tester<empty_POD_UDT,non_empty2[2]>::test(e1, nea3, e1, nea4);
  351. // T1 == T2, both non-empty
  352. compressed_pair_array_tester<non_empty1[2],non_empty1[2]>::test(nea1, nea1, nea2, nea2);
  353. return boost::report_errors();
  354. }