null_iterators_test.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2014-2014. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #include <boost/container/vector.hpp>
  11. #include <boost/container/deque.hpp>
  12. #include <boost/container/stable_vector.hpp>
  13. #include <boost/container/static_vector.hpp>
  14. #include <boost/container/string.hpp>
  15. #include <boost/container/list.hpp>
  16. #include <boost/container/slist.hpp>
  17. #include <boost/container/map.hpp>
  18. #include <boost/container/set.hpp>
  19. #include <boost/container/flat_set.hpp>
  20. #include <boost/container/flat_map.hpp>
  21. #include <boost/intrusive/detail/mpl.hpp>
  22. #include <boost/core/lightweight_test.hpp>
  23. #include <boost/static_assert.hpp>
  24. #include <cstring>
  25. #include <new>
  26. using namespace boost::container;
  27. typedef boost::container::dtl::aligned_storage<sizeof(void*)*4>::type buffer_t;
  28. static buffer_t buffer_0x00;
  29. static buffer_t buffer_0xFF;
  30. template<class Iterator>
  31. const Iterator &on_0x00_buffer()
  32. {
  33. BOOST_STATIC_ASSERT(sizeof(buffer_t) >= sizeof(Iterator));
  34. return * ::new(std::memset(&buffer_0x00, 0x00, sizeof(buffer_0x00))) Iterator();
  35. }
  36. template<class Iterator>
  37. const Iterator &on_0xFF_buffer()
  38. {
  39. BOOST_STATIC_ASSERT(sizeof(buffer_t) >= sizeof(Iterator));
  40. return * ::new(std::memset(&buffer_0xFF, 0xFF, sizeof(buffer_0xFF))) Iterator();
  41. }
  42. namespace boost {
  43. namespace container {
  44. namespace test {
  45. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(reverse_iterator)
  46. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(const_reverse_iterator)
  47. }}} //namespace boost::container::test {
  48. template<class Container>
  49. void check_null_iterators()
  50. {
  51. typedef typename Container::iterator iterator;
  52. typedef typename Container::const_iterator const_iterator;
  53. typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT
  54. (boost::container::test::, Container
  55. ,reverse_iterator, iterator) reverse_iterator;
  56. typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT
  57. (boost::container::test::, Container
  58. ,const_reverse_iterator, const_iterator) const_reverse_iterator;
  59. BOOST_TEST(on_0xFF_buffer<iterator>() == on_0x00_buffer<iterator>());
  60. BOOST_TEST(on_0xFF_buffer<const_iterator>() == on_0x00_buffer<const_iterator>());
  61. BOOST_TEST(on_0xFF_buffer<reverse_iterator>() == on_0x00_buffer<reverse_iterator>());
  62. BOOST_TEST(on_0xFF_buffer<const_reverse_iterator>() == on_0x00_buffer<const_reverse_iterator>());
  63. }
  64. int main()
  65. {
  66. check_null_iterators< vector<int> >();
  67. check_null_iterators< deque<int> >();
  68. check_null_iterators< stable_vector<int> >();
  69. check_null_iterators< static_vector<int, 1> >();
  70. check_null_iterators< string >();
  71. check_null_iterators< list<int> >();
  72. check_null_iterators< slist<int> >();
  73. check_null_iterators< map<int, int> >();
  74. check_null_iterators< multimap<int, int> >();
  75. check_null_iterators< set<int> >();
  76. check_null_iterators< multiset<int> >();
  77. check_null_iterators< flat_set<int> >();
  78. check_null_iterators< flat_multiset<int> >();
  79. check_null_iterators< flat_map<int, int> >();
  80. check_null_iterators< flat_multimap<int, int> >();
  81. return boost::report_errors();
  82. }