alloc.pass.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // dynarray.cons
  10. // template <class Alloc>
  11. // dynarray(size_type c, const Alloc& alloc);
  12. // template <class Alloc>
  13. // dynarray(size_type c, const T& v, const Alloc& alloc);
  14. // template <class Alloc>
  15. // dynarray(const dynarray& d, const Alloc& alloc);
  16. // template <class Alloc>
  17. // dynarray(initializer_list<T>, const Alloc& alloc);
  18. // ~dynarray();
  19. #include <__config>
  20. #if _LIBCPP_STD_VER > 11
  21. #include <experimental/dynarray>
  22. #include <cassert>
  23. #include <algorithm>
  24. #include <complex>
  25. #include <string>
  26. #include "test_allocator.h"
  27. using std::experimental::dynarray;
  28. template <class T, class Allocator>
  29. void check_allocator ( const dynarray<T> &dyn, const Allocator &alloc ) {
  30. for ( int i = 0; i < dyn.size (); ++i )
  31. assert ( dyn[i].get_allocator() == alloc );
  32. }
  33. template <class T, class Allocator>
  34. void test ( const std::initializer_list<T> &vals, const Allocator &alloc ) {
  35. typedef dynarray<T> dynA;
  36. dynA d1 ( vals, alloc );
  37. assert ( d1.size () == vals.size() );
  38. assert ( std::equal ( vals.begin (), vals.end (), d1.begin (), d1.end ()));
  39. check_allocator ( d1, alloc );
  40. }
  41. template <class T, class Allocator>
  42. void test ( const T &val, const Allocator &alloc1, const Allocator &alloc2 ) {
  43. typedef dynarray<T> dynA;
  44. dynA d1 ( 4, alloc1 );
  45. assert ( d1.size () == 4 );
  46. assert ( std::all_of ( d1.begin (), d1.end (), []( const T &item ){ return item == T(); } ));
  47. check_allocator ( d1, alloc1 );
  48. dynA d2 ( 7, val, alloc1 );
  49. assert ( d2.size () == 7 );
  50. assert ( std::all_of ( d2.begin (), d2.end (), [&val]( const T &item ){ return item == val; } ));
  51. check_allocator ( d2, alloc1 );
  52. dynA d3 ( d2, alloc2 );
  53. assert ( d3.size () == 7 );
  54. assert ( std::all_of ( d3.begin (), d3.end (), [&val]( const T &item ){ return item == val; } ));
  55. check_allocator ( d3, alloc2 );
  56. }
  57. int main()
  58. {
  59. // This test is waiting on the resolution of LWG issue #2235
  60. // typedef test_allocator<char> Alloc;
  61. // typedef std::basic_string<char, std::char_traits<char>, Alloc> nstr;
  62. //
  63. // test ( nstr("fourteen"), Alloc(3), Alloc(4) );
  64. // test ( { nstr("1"), nstr("1"), nstr("2"), nstr("3"), nstr("5"), nstr("8")}, Alloc(6));
  65. }
  66. #else
  67. int main() {}
  68. #endif