Home | History | Annotate | Download | only in vector.cons
      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 
     10 // <vector>
     11 // UNSUPPORTED: c++98, c++03, c++11, c++14
     12 // UNSUPPORTED: libcpp-no-deduction-guides
     13 
     14 
     15 // template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
     16 //    deque(InputIterator, InputIterator, Allocator = Allocator())
     17 //    -> deque<typename iterator_traits<InputIterator>::value_type, Allocator>;
     18 //
     19 
     20 
     21 #include <vector>
     22 #include <iterator>
     23 #include <cassert>
     24 #include <cstddef>
     25 #include <climits> // INT_MAX
     26 
     27 #include "test_macros.h"
     28 #include "test_iterators.h"
     29 #include "test_allocator.h"
     30 
     31 struct A {};
     32 
     33 int main()
     34 {
     35 
     36 //  Test the explicit deduction guides
     37     {
     38     const int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     39     std::vector vec(std::begin(arr), std::end(arr));
     40 
     41     static_assert(std::is_same_v<decltype(vec), std::vector<int>>, "");
     42     assert(std::equal(vec.begin(), vec.end(), std::begin(arr), std::end(arr)));
     43     }
     44 
     45     {
     46     const long arr[] = {INT_MAX, 1L, 2L, 3L };
     47     std::vector vec(std::begin(arr), std::end(arr), std::allocator<long>());
     48     static_assert(std::is_same_v<decltype(vec)::value_type, long>, "");
     49     assert(vec.size() == 4);
     50     assert(vec[0] == INT_MAX);
     51     assert(vec[1] == 1L);
     52     assert(vec[2] == 2L);
     53     }
     54 
     55 //  Test the implicit deduction guides
     56 
     57     {
     58 //  We don't expect this one to work.
     59 //  std::vector vec(std::allocator<int>()); // vector (allocator &)
     60     }
     61 
     62     {
     63     std::vector vec(1, A{}); // vector (size_type, T)
     64     static_assert(std::is_same_v<decltype(vec)::value_type, A>, "");
     65     static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<A>>, "");
     66     assert(vec.size() == 1);
     67     }
     68 
     69     {
     70     std::vector vec(1, A{}, test_allocator<A>()); // vector (size_type, T, allocator)
     71     static_assert(std::is_same_v<decltype(vec)::value_type, A>, "");
     72     static_assert(std::is_same_v<decltype(vec)::allocator_type, test_allocator<A>>, "");
     73     assert(vec.size() == 1);
     74     }
     75 
     76     {
     77     std::vector vec{1U, 2U, 3U, 4U, 5U}; // vector(initializer-list)
     78     static_assert(std::is_same_v<decltype(vec)::value_type, unsigned>, "");
     79     assert(vec.size() == 5);
     80     assert(vec[2] == 3U);
     81     }
     82 
     83     {
     84     std::vector vec({1.0, 2.0, 3.0, 4.0}, test_allocator<double>()); // vector(initializer-list, allocator)
     85     static_assert(std::is_same_v<decltype(vec)::value_type, double>, "");
     86     static_assert(std::is_same_v<decltype(vec)::allocator_type, test_allocator<double>>, "");
     87     assert(vec.size() == 4);
     88     assert(vec[3] == 4.0);
     89     }
     90 
     91     {
     92     std::vector<long double> source;
     93     std::vector vec(source); // vector(vector &)
     94     static_assert(std::is_same_v<decltype(vec)::value_type, long double>, "");
     95     static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<long double>>, "");
     96     assert(vec.size() == 0);
     97     }
     98 
     99 
    100 //  A couple of vector<bool> tests, too!
    101     {
    102     std::vector vec(3, true); // vector(initializer-list)
    103     static_assert(std::is_same_v<decltype(vec)::value_type, bool>, "");
    104     static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<bool>>, "");
    105     assert(vec.size() == 3);
    106     assert(vec[0] && vec[1] && vec[2]);
    107     }
    108 
    109     {
    110     std::vector<bool> source;
    111     std::vector vec(source); // vector(vector &)
    112     static_assert(std::is_same_v<decltype(vec)::value_type, bool>, "");
    113     static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<bool>>, "");
    114     assert(vec.size() == 0);
    115     }
    116 }
    117