noinit_adaptor_test.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright 2019 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <boost/core/noinit_adaptor.hpp>
  8. #include <boost/core/default_allocator.hpp>
  9. #include <boost/core/lightweight_test.hpp>
  10. #include <vector>
  11. template<class T>
  12. class creator
  13. : public boost::default_allocator<T> {
  14. public:
  15. template<class U>
  16. struct rebind {
  17. typedef creator<U> other;
  18. };
  19. creator(int state)
  20. : state_(state) { }
  21. template<class U>
  22. creator(const creator<U>& other)
  23. : state_(other.state()) { }
  24. template<class U, class V>
  25. void construct(U*, const V&) {
  26. BOOST_ERROR("construct");
  27. }
  28. template<class U>
  29. void destroy(U*) {
  30. BOOST_ERROR("destroy");
  31. }
  32. int state() const {
  33. return state_;
  34. }
  35. private:
  36. int state_;
  37. };
  38. template<class T, class U>
  39. inline bool
  40. operator==(const creator<T>& lhs, const creator<U>& rhs)
  41. {
  42. return lhs.state() == rhs.state();
  43. }
  44. template<class T, class U>
  45. inline bool
  46. operator!=(const creator<T>& lhs, const creator<U>& rhs)
  47. {
  48. return !(lhs == rhs);
  49. }
  50. class type {
  51. public:
  52. type() { }
  53. type(int value)
  54. : value_(value) { }
  55. int value() const {
  56. return value_;
  57. }
  58. private:
  59. int value_;
  60. };
  61. inline bool
  62. operator==(const type& lhs, const type& rhs)
  63. {
  64. return lhs.value() == rhs.value();
  65. }
  66. template<class A>
  67. void test(const A& allocator)
  68. {
  69. std::vector<typename A::value_type, A> v(allocator);
  70. v.push_back(1);
  71. BOOST_TEST(v.front() == 1);
  72. v.clear();
  73. v.resize(5);
  74. v.front() = 1;
  75. }
  76. int main()
  77. {
  78. test(boost::noinit_adaptor<creator<int> >(1));
  79. test(boost::noinit_adaptor<creator<type> >(2));
  80. test(boost::noinit_adapt(creator<int>(3)));
  81. test(boost::noinit_adapt(creator<type>(4)));
  82. return boost::report_errors();
  83. }