test_mutant.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Boost.Bimap
  2. //
  3. // Copyright (c) 2006-2007 Matias Capeletto
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. // VC++ 8.0 warns on usage of certain Standard Library and API functions that
  9. // can be cause buffer overruns or other possible security issues if misused.
  10. // See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
  11. // But the wording of the warning is misleading and unsettling, there are no
  12. // portable alternative functions, and VC++ 8.0's own libraries use the
  13. // functions in question. So turn off the warnings.
  14. #define _CRT_SECURE_NO_DEPRECATE
  15. #define _SCL_SECURE_NO_DEPRECATE
  16. #include <boost/config.hpp>
  17. // Boost.Test
  18. #include <boost/test/minimal.hpp>
  19. // Boost.MPL
  20. #include <boost/mpl/list.hpp>
  21. #include <boost/type_traits/is_same.hpp>
  22. // Boost.Bimap
  23. #include <boost/bimap/relation/detail/mutant.hpp>
  24. using namespace boost::bimaps::relation::detail;
  25. // The mutant idiom is standard if only POD types are used.
  26. typedef double type_a;
  27. typedef int type_b;
  28. const type_a value_a = 1.4;
  29. const type_b value_b = 3;
  30. struct Data
  31. {
  32. type_a a;
  33. type_b b;
  34. };
  35. struct StdPairView
  36. {
  37. typedef type_a first_type;
  38. typedef type_b second_type;
  39. type_a first;
  40. type_b second;
  41. };
  42. struct ReverseStdPairView
  43. {
  44. typedef type_a second_type;
  45. typedef type_b first_type;
  46. type_a second;
  47. type_b first;
  48. };
  49. struct MutantData
  50. {
  51. typedef boost::mpl::list< StdPairView, ReverseStdPairView > mutant_views;
  52. MutantData(type_a ap, type_b bp) : a(ap), b(bp) {}
  53. type_a a;
  54. type_b b;
  55. };
  56. void test_mutant_basic()
  57. {
  58. // mutant test
  59. {
  60. MutantData m(value_a,value_b);
  61. BOOST_CHECK( sizeof( MutantData ) == sizeof( StdPairView ) );
  62. BOOST_CHECK( mutate<StdPairView>(m).first == value_a );
  63. BOOST_CHECK( mutate<StdPairView>(m).second == value_b );
  64. BOOST_CHECK( mutate<ReverseStdPairView>(m).first == value_b );
  65. BOOST_CHECK( mutate<ReverseStdPairView>(m).second == value_a );
  66. ReverseStdPairView & rpair = mutate<ReverseStdPairView>(m);
  67. rpair.first = value_b;
  68. rpair.second = value_a;
  69. BOOST_CHECK( mutate<StdPairView>(m).first == value_a );
  70. BOOST_CHECK( mutate<StdPairView>(m).second == value_b );
  71. BOOST_CHECK( &mutate<StdPairView>(m).first == &m.a );
  72. BOOST_CHECK( &mutate<StdPairView>(m).second == &m.b );
  73. }
  74. }
  75. int test_main( int, char* [] )
  76. {
  77. test_mutant_basic();
  78. return 0;
  79. }