[libcxx] r315994 - [libc++] Fix PR34898 - vector iterator constructors and assign method perform push_back instead of emplace_back.
Eric Fiselier via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 17 06:03:17 PDT 2017
Author: ericwf
Date: Tue Oct 17 06:03:17 2017
New Revision: 315994
URL: http://llvm.org/viewvc/llvm-project?rev=315994&view=rev
Log:
[libc++] Fix PR34898 - vector iterator constructors and assign method perform push_back instead of emplace_back.
Summary:
The constructors `vector(Iter, Iter, Alloc = Alloc{})` and `assign(Iter, Iter)` don't correctly perform EmplaceConstruction from the result of dereferencing the iterator. This results in them performing an additional and unneeded copy.
This patch addresses the issue by correctly using `emplace_back` in C++11 and newer.
There are also some bugs in our `insert` implementation, but those will be handled separately.
@mclow.lists We should probably merge this into 5.1, agreed?
Reviewers: mclow.lists, dlj, EricWF
Reviewed By: mclow.lists, EricWF
Subscribers: cfe-commits, mclow.lists
Differential Revision: https://reviews.llvm.org/D38757
Added:
libcxx/trunk/test/std/containers/sequences/vector/vector.cons/assign_iter_iter.pass.cpp
libcxx/trunk/test/support/emplace_constructible.h
Modified:
libcxx/trunk/include/deque
libcxx/trunk/include/list
libcxx/trunk/include/vector
libcxx/trunk/test/std/containers/sequences/deque/deque.cons/assign_iter_iter.pass.cpp
libcxx/trunk/test/std/containers/sequences/deque/deque.cons/iter_iter.pass.cpp
libcxx/trunk/test/std/containers/sequences/deque/deque.cons/iter_iter_alloc.pass.cpp
libcxx/trunk/test/std/containers/sequences/list/list.cons/input_iterator.pass.cpp
libcxx/trunk/test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp
libcxx/trunk/test/std/containers/sequences/vector/vector.cons/construct_iter_iter_alloc.pass.cpp
libcxx/trunk/test/support/container_test_types.h
Modified: libcxx/trunk/include/deque
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/deque?rev=315994&r1=315993&r2=315994&view=diff
==============================================================================
--- libcxx/trunk/include/deque (original)
+++ libcxx/trunk/include/deque Tue Oct 17 06:03:17 2017
@@ -1356,7 +1356,6 @@ public:
iterator insert(const_iterator __p, initializer_list<value_type> __il)
{return insert(__p, __il.begin(), __il.end());}
#endif // _LIBCPP_CXX03_LANG
-
iterator insert(const_iterator __p, const value_type& __v);
iterator insert(const_iterator __p, size_type __n, const value_type& __v);
template <class _InputIter>
@@ -2224,7 +2223,11 @@ deque<_Tp, _Allocator>::__append(_InpIte
!__is_forward_iterator<_InpIter>::value>::type*)
{
for (; __f != __l; ++__f)
+#ifdef _LIBCPP_CXX03_LANG
push_back(*__f);
+#else
+ emplace_back(*__f);
+#endif
}
template <class _Tp, class _Allocator>
Modified: libcxx/trunk/include/list
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/list?rev=315994&r1=315993&r2=315994&view=diff
==============================================================================
--- libcxx/trunk/include/list (original)
+++ libcxx/trunk/include/list Tue Oct 17 06:03:17 2017
@@ -992,6 +992,15 @@ public:
void push_front(const value_type& __x);
void push_back(const value_type& __x);
+#ifndef _LIBCPP_CXX03_LANG
+ template <class _Arg>
+ _LIBCPP_INLINE_VISIBILITY
+ void __emplace_back(_Arg&& __arg) { emplace_back(_VSTD::forward<_Arg>(__arg)); }
+#else
+ _LIBCPP_INLINE_VISIBILITY
+ void __emplace_back(value_type const& __arg) { push_back(__arg); }
+#endif
+
iterator insert(const_iterator __p, const value_type& __x);
iterator insert(const_iterator __p, size_type __n, const value_type& __x);
template <class _InpIter>
@@ -1189,7 +1198,7 @@ list<_Tp, _Alloc>::list(_InpIter __f, _I
__get_db()->__insert_c(this);
#endif
for (; __f != __l; ++__f)
- push_back(*__f);
+ __emplace_back(*__f);
}
template <class _Tp, class _Alloc>
@@ -1202,7 +1211,7 @@ list<_Tp, _Alloc>::list(_InpIter __f, _I
__get_db()->__insert_c(this);
#endif
for (; __f != __l; ++__f)
- push_back(*__f);
+ __emplace_back(*__f);
}
template <class _Tp, class _Alloc>
Modified: libcxx/trunk/include/vector
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/vector?rev=315994&r1=315993&r2=315994&view=diff
==============================================================================
--- libcxx/trunk/include/vector (original)
+++ libcxx/trunk/include/vector Tue Oct 17 06:03:17 2017
@@ -674,6 +674,17 @@ public:
const value_type* data() const _NOEXCEPT
{return _VSTD::__to_raw_pointer(this->__begin_);}
+#ifdef _LIBCPP_CXX03_LANG
+ _LIBCPP_INLINE_VISIBILITY
+ void __emplace_back(const value_type& __x) { push_back(__x); }
+#else
+ template <class _Arg>
+ _LIBCPP_INLINE_VISIBILITY
+ void __emplace_back(_Arg&& __arg) {
+ emplace_back(_VSTD::forward<_Arg>(__arg));
+ }
+#endif
+
_LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
#ifndef _LIBCPP_CXX03_LANG
@@ -1128,7 +1139,7 @@ vector<_Tp, _Allocator>::vector(_InputIt
__get_db()->__insert_c(this);
#endif
for (; __first != __last; ++__first)
- push_back(*__first);
+ __emplace_back(*__first);
}
template <class _Tp, class _Allocator>
@@ -1145,7 +1156,7 @@ vector<_Tp, _Allocator>::vector(_InputIt
__get_db()->__insert_c(this);
#endif
for (; __first != __last; ++__first)
- push_back(*__first);
+ __emplace_back(*__first);
}
template <class _Tp, class _Allocator>
@@ -1365,7 +1376,7 @@ vector<_Tp, _Allocator>::assign(_InputIt
{
clear();
for (; __first != __last; ++__first)
- push_back(*__first);
+ __emplace_back(*__first);
}
template <class _Tp, class _Allocator>
Modified: libcxx/trunk/test/std/containers/sequences/deque/deque.cons/assign_iter_iter.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/sequences/deque/deque.cons/assign_iter_iter.pass.cpp?rev=315994&r1=315993&r2=315994&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/sequences/deque/deque.cons/assign_iter_iter.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/sequences/deque/deque.cons/assign_iter_iter.pass.cpp Tue Oct 17 06:03:17 2017
@@ -19,6 +19,9 @@
#include "test_macros.h"
#include "test_iterators.h"
#include "min_allocator.h"
+#if TEST_STD_VER >= 11
+#include "emplace_constructible.h"
+#endif
template <class C>
C
@@ -80,7 +83,7 @@ testNI(int start, int N, int M)
testI(c1, c2);
}
-int main()
+void basic_test()
{
{
int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
@@ -103,3 +106,51 @@ int main()
}
#endif
}
+
+void test_emplacable_concept() {
+#if TEST_STD_VER >= 11
+ int arr1[] = {42};
+ int arr2[] = {1, 101, 42};
+ {
+ using T = EmplaceConstructibleMoveableAndAssignable<int>;
+ using It = random_access_iterator<int*>;
+ {
+ std::deque<T> v;
+ v.assign(It(arr1), It(std::end(arr1)));
+ assert(v[0].value == 42);
+ }
+ {
+ std::deque<T> v;
+ v.assign(It(arr2), It(std::end(arr2)));
+ assert(v[0].value == 1);
+ assert(v[1].value == 101);
+ assert(v[2].value == 42);
+ }
+ }
+ {
+ using T = EmplaceConstructibleMoveableAndAssignable<int>;
+ using It = input_iterator<int*>;
+ {
+ std::deque<T> v;
+ v.assign(It(arr1), It(std::end(arr1)));
+ assert(v[0].copied == 0);
+ assert(v[0].value == 42);
+ }
+ {
+ std::deque<T> v;
+ v.assign(It(arr2), It(std::end(arr2)));
+ //assert(v[0].copied == 0);
+ assert(v[0].value == 1);
+ //assert(v[1].copied == 0);
+ assert(v[1].value == 101);
+ assert(v[2].copied == 0);
+ assert(v[2].value == 42);
+ }
+ }
+#endif
+}
+
+int main() {
+ basic_test();
+ test_emplacable_concept();
+}
Modified: libcxx/trunk/test/std/containers/sequences/deque/deque.cons/iter_iter.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/sequences/deque/deque.cons/iter_iter.pass.cpp?rev=315994&r1=315993&r2=315994&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/sequences/deque/deque.cons/iter_iter.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/sequences/deque/deque.cons/iter_iter.pass.cpp Tue Oct 17 06:03:17 2017
@@ -15,9 +15,13 @@
#include <cassert>
#include <cstddef>
+#include "test_macros.h"
#include "test_allocator.h"
#include "test_iterators.h"
#include "min_allocator.h"
+#if TEST_STD_VER >= 11
+#include "emplace_constructible.h"
+#endif
template <class InputIterator>
void
@@ -48,7 +52,7 @@ test(InputIterator f, InputIterator l)
assert(*i == *f);
}
-int main()
+void basic_test()
{
int ab[] = {3, 4, 2, 8, 0, 1, 44, 34, 45, 96, 80, 1, 13, 31, 45};
int* an = ab + sizeof(ab)/sizeof(ab[0]);
@@ -61,3 +65,48 @@ int main()
test<min_allocator<int> >(ab, an);
#endif
}
+
+
+void test_emplacable_concept() {
+#if TEST_STD_VER >= 11
+ int arr1[] = {42};
+ int arr2[] = {1, 101, 42};
+ {
+ using T = EmplaceConstructibleAndMoveable<int>;
+ using It = random_access_iterator<int*>;
+ {
+ std::deque<T> v(It(arr1), It(std::end(arr1)));
+ assert(v[0].value == 42);
+ }
+ {
+ std::deque<T> v(It(arr2), It(std::end(arr2)));
+ assert(v[0].value == 1);
+ assert(v[1].value == 101);
+ assert(v[2].value == 42);
+ }
+ }
+ {
+ using T = EmplaceConstructibleAndMoveable<int>;
+ using It = input_iterator<int*>;
+ {
+ std::deque<T> v(It(arr1), It(std::end(arr1)));
+ assert(v[0].copied == 0);
+ assert(v[0].value == 42);
+ }
+ {
+ std::deque<T> v(It(arr2), It(std::end(arr2)));
+ //assert(v[0].copied == 0);
+ assert(v[0].value == 1);
+ //assert(v[1].copied == 0);
+ assert(v[1].value == 101);
+ assert(v[2].copied == 0);
+ assert(v[2].value == 42);
+ }
+ }
+#endif
+}
+
+int main() {
+ basic_test();
+ test_emplacable_concept();
+}
Modified: libcxx/trunk/test/std/containers/sequences/deque/deque.cons/iter_iter_alloc.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/sequences/deque/deque.cons/iter_iter_alloc.pass.cpp?rev=315994&r1=315993&r2=315994&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/sequences/deque/deque.cons/iter_iter_alloc.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/sequences/deque/deque.cons/iter_iter_alloc.pass.cpp Tue Oct 17 06:03:17 2017
@@ -16,9 +16,13 @@
#include <cassert>
#include <cstddef>
+#include "test_macros.h"
#include "test_iterators.h"
#include "test_allocator.h"
#include "min_allocator.h"
+#if TEST_STD_VER >= 11
+#include "emplace_constructible.h"
+#endif
template <class InputIterator, class Allocator>
void
@@ -35,7 +39,7 @@ test(InputIterator f, InputIterator l, c
assert(*i == *f);
}
-int main()
+void basic_test()
{
int ab[] = {3, 4, 2, 8, 0, 1, 44, 34, 45, 96, 80, 1, 13, 31, 45};
int* an = ab + sizeof(ab)/sizeof(ab[0]);
@@ -50,3 +54,50 @@ int main()
test(random_access_iterator<const int*>(ab), random_access_iterator<const int*>(an), min_allocator<int>());
#endif
}
+
+
+void test_emplacable_concept() {
+#if TEST_STD_VER >= 11
+ int arr1[] = {42};
+ int arr2[] = {1, 101, 42};
+ {
+ using T = EmplaceConstructibleAndMoveable<int>;
+ using It = random_access_iterator<int*>;
+ std::allocator<T> a;
+ {
+ std::deque<T> v(It(arr1), It(std::end(arr1)), a);
+ assert(v[0].value == 42);
+ }
+ {
+ std::deque<T> v(It(arr2), It(std::end(arr2)), a);
+ assert(v[0].value == 1);
+ assert(v[1].value == 101);
+ assert(v[2].value == 42);
+ }
+ }
+ {
+ using T = EmplaceConstructibleAndMoveable<int>;
+ using It = input_iterator<int*>;
+ std::allocator<T> a;
+ {
+ std::deque<T> v(It(arr1), It(std::end(arr1)), a);
+ assert(v[0].copied == 0);
+ assert(v[0].value == 42);
+ }
+ {
+ std::deque<T> v(It(arr2), It(std::end(arr2)), a);
+ //assert(v[0].copied == 0);
+ assert(v[0].value == 1);
+ //assert(v[1].copied == 0);
+ assert(v[1].value == 101);
+ assert(v[2].copied == 0);
+ assert(v[2].value == 42);
+ }
+ }
+#endif
+}
+
+int main() {
+ basic_test();
+ test_emplacable_concept();
+}
Modified: libcxx/trunk/test/std/containers/sequences/list/list.cons/input_iterator.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/sequences/list/list.cons/input_iterator.pass.cpp?rev=315994&r1=315993&r2=315994&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/sequences/list/list.cons/input_iterator.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/sequences/list/list.cons/input_iterator.pass.cpp Tue Oct 17 06:03:17 2017
@@ -17,8 +17,12 @@
#include "test_iterators.h"
#include "test_allocator.h"
#include "min_allocator.h"
+#if TEST_STD_VER >= 11
+#include "emplace_constructible.h"
+#include "container_test_types.h"
+#endif
-int main()
+void basic_test()
{
{
int a[] = {0, 1, 2, 3};
@@ -76,3 +80,179 @@ int main()
}
#endif
}
+
+
+
+void test_emplacable_concept() {
+#if TEST_STD_VER >= 11
+ int arr1[] = {42};
+ int arr2[] = {1, 101, 42};
+ {
+ using T = EmplaceConstructible<int>;
+ using It = random_access_iterator<int*>;
+ {
+ std::list<T> v(It(arr1), It(std::end(arr1)));
+ auto I = v.begin();
+ assert(I->value == 42);
+ }
+ {
+ std::list<T> v(It(arr2), It(std::end(arr2)));
+ auto I = v.begin();
+ assert(I->value == 1);
+ ++I;
+ assert(I->value == 101);
+ ++I;
+ assert(I->value == 42);
+ }
+ }
+ {
+ using T = EmplaceConstructible<int>;
+ using It = input_iterator<int*>;
+ {
+ std::list<T> v(It(arr1), It(std::end(arr1)));
+ auto I = v.begin();
+ assert(I->value == 42);
+ }
+ {
+ std::list<T> v(It(arr2), It(std::end(arr2)));
+ auto I = v.begin();
+ //assert(v[0].copied == 0);
+ assert(I->value == 1);
+ //assert(v[1].copied == 0);
+ ++I;
+ assert(I->value == 101);
+ ++I;
+ assert(I->value == 42);
+ }
+ }
+#endif
+}
+
+
+
+void test_emplacable_concept_with_alloc() {
+#if TEST_STD_VER >= 11
+ int arr1[] = {42};
+ int arr2[] = {1, 101, 42};
+ {
+ using T = EmplaceConstructible<int>;
+ using It = random_access_iterator<int*>;
+ std::allocator<T> a;
+ {
+ std::list<T> v(It(arr1), It(std::end(arr1)), a);
+ auto I = v.begin();
+ assert(I->value == 42);
+ }
+ {
+ std::list<T> v(It(arr2), It(std::end(arr2)), a);
+ auto I = v.begin();
+ assert(I->value == 1);
+ ++I;
+ assert(I->value == 101);
+ ++I;
+ assert(I->value == 42);
+ }
+ }
+ {
+ using T = EmplaceConstructible<int>;
+ using It = input_iterator<int*>;
+ std::allocator<T> a;
+ {
+ std::list<T> v(It(arr1), It(std::end(arr1)), a);
+ auto I = v.begin();
+ assert(I->value == 42);
+ }
+ {
+ std::list<T> v(It(arr2), It(std::end(arr2)), a);
+ auto I = v.begin();
+ //assert(v[0].copied == 0);
+ assert(I->value == 1);
+ //assert(v[1].copied == 0);
+ ++I;
+ assert(I->value == 101);
+ ++I;
+ assert(I->value == 42);
+ }
+ }
+#endif
+}
+
+void test_ctor_under_alloc() {
+#if TEST_STD_VER >= 11
+ int arr1[] = {42};
+ int arr2[] = {1, 101, 42};
+ {
+ using C = TCT::list<>;
+ using T = typename C::value_type;
+ using It = forward_iterator<int*>;
+ {
+ ExpectConstructGuard<int&> G(1);
+ C v(It(arr1), It(std::end(arr1)));
+ }
+ {
+ ExpectConstructGuard<int&> G(3);
+ C v(It(arr2), It(std::end(arr2)));
+ }
+ }
+ {
+ using C = TCT::list<>;
+ using T = typename C::value_type;
+ using It = input_iterator<int*>;
+ {
+ ExpectConstructGuard<int&> G(1);
+ C v(It(arr1), It(std::end(arr1)));
+ }
+ {
+ ExpectConstructGuard<int&> G(3);
+ C v(It(arr2), It(std::end(arr2)));
+ }
+ }
+#endif
+}
+
+void test_ctor_under_alloc_with_alloc() {
+#if TEST_STD_VER >= 11
+ int arr1[] = {42};
+ int arr2[] = {1, 101, 42};
+ {
+ using C = TCT::list<>;
+ using T = typename C::value_type;
+ using It = forward_iterator<int*>;
+ using Alloc = typename C::allocator_type;
+ Alloc a;
+ {
+ ExpectConstructGuard<int&> G(1);
+ C v(It(arr1), It(std::end(arr1)), a);
+ }
+ {
+ ExpectConstructGuard<int&> G(3);
+ C v(It(arr2), It(std::end(arr2)), a);
+ }
+ }
+ {
+ using C = TCT::list<>;
+ using T = typename C::value_type;
+ using It = input_iterator<int*>;
+ using Alloc = typename C::allocator_type;
+ Alloc a;
+ {
+ ExpectConstructGuard<int&> G(1);
+ C v(It(arr1), It(std::end(arr1)), a);
+ }
+ {
+ ExpectConstructGuard<int&> G(3);
+ C v(It(arr2), It(std::end(arr2)), a);
+ }
+ }
+#endif
+}
+
+
+
+int main() {
+ basic_test();
+ test_emplacable_concept();
+ test_emplacable_concept_with_alloc();
+ test_ctor_under_alloc();
+ test_ctor_under_alloc_with_alloc();
+}
Added: libcxx/trunk/test/std/containers/sequences/vector/vector.cons/assign_iter_iter.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/sequences/vector/vector.cons/assign_iter_iter.pass.cpp?rev=315994&view=auto
==============================================================================
--- libcxx/trunk/test/std/containers/sequences/vector/vector.cons/assign_iter_iter.pass.cpp (added)
+++ libcxx/trunk/test/std/containers/sequences/vector/vector.cons/assign_iter_iter.pass.cpp Tue Oct 17 06:03:17 2017
@@ -0,0 +1,76 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <vector>
+
+// void assign(size_type n, const_reference v);
+
+#include <vector>
+#include <algorithm>
+#include <cassert>
+#include <iostream>
+#include "test_macros.h"
+#include "min_allocator.h"
+#include "asan_testing.h"
+#include "test_iterators.h"
+#if TEST_STD_VER >= 11
+#include "emplace_constructible.h"
+#include "container_test_types.h"
+#endif
+
+
+void test_emplaceable_concept() {
+#if TEST_STD_VER >= 11
+ int arr1[] = {42};
+ int arr2[] = {1, 101, 42};
+ {
+ using T = EmplaceConstructibleMoveableAndAssignable<int>;
+ using It = forward_iterator<int*>;
+ {
+ std::vector<T> v;
+ v.assign(It(arr1), It(std::end(arr1)));
+ assert(v[0].value == 42);
+ }
+ {
+ std::vector<T> v;
+ v.assign(It(arr2), It(std::end(arr2)));
+ assert(v[0].value == 1);
+ assert(v[1].value == 101);
+ assert(v[2].value == 42);
+ }
+ }
+ {
+ using T = EmplaceConstructibleMoveableAndAssignable<int>;
+ using It = input_iterator<int*>;
+ {
+ std::vector<T> v;
+ v.assign(It(arr1), It(std::end(arr1)));
+ assert(v[0].copied == 0);
+ assert(v[0].value == 42);
+ }
+ {
+ std::vector<T> v;
+ v.assign(It(arr2), It(std::end(arr2)));
+ //assert(v[0].copied == 0);
+ assert(v[0].value == 1);
+ //assert(v[1].copied == 0);
+ assert(v[1].value == 101);
+ assert(v[2].copied == 0);
+ assert(v[2].value == 42);
+ }
+ }
+#endif
+}
+
+
+
+int main()
+{
+ test_emplaceable_concept();
+}
Modified: libcxx/trunk/test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp?rev=315994&r1=315993&r2=315994&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp Tue Oct 17 06:03:17 2017
@@ -20,40 +20,137 @@
#include "test_allocator.h"
#include "min_allocator.h"
#include "asan_testing.h"
+#if TEST_STD_VER >= 11
+#include "emplace_constructible.h"
+#include "container_test_types.h"
+#endif
template <class C, class Iterator>
-void
-test(Iterator first, Iterator last)
-{
- C c(first, last);
- LIBCPP_ASSERT(c.__invariants());
- assert(c.size() == static_cast<std::size_t>(std::distance(first, last)));
- LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
- for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i, ++first)
- assert(*i == *first);
+void test(Iterator first, Iterator last) {
+ C c(first, last);
+ LIBCPP_ASSERT(c.__invariants());
+ assert(c.size() == static_cast<std::size_t>(std::distance(first, last)));
+ LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
+ for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e;
+ ++i, ++first)
+ assert(*i == *first);
+}
+
+static void basic_test_cases() {
+ int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 1, 0};
+ int* an = a + sizeof(a) / sizeof(a[0]);
+ test<std::vector<int> >(input_iterator<const int*>(a),
+ input_iterator<const int*>(an));
+ test<std::vector<int> >(forward_iterator<const int*>(a),
+ forward_iterator<const int*>(an));
+ test<std::vector<int> >(bidirectional_iterator<const int*>(a),
+ bidirectional_iterator<const int*>(an));
+ test<std::vector<int> >(random_access_iterator<const int*>(a),
+ random_access_iterator<const int*>(an));
+ test<std::vector<int> >(a, an);
+
+ test<std::vector<int, limited_allocator<int, 63> > >(
+ input_iterator<const int*>(a), input_iterator<const int*>(an));
+ // Add 1 for implementations that dynamically allocate a container proxy.
+ test<std::vector<int, limited_allocator<int, 18 + 1> > >(
+ forward_iterator<const int*>(a), forward_iterator<const int*>(an));
+ test<std::vector<int, limited_allocator<int, 18 + 1> > >(
+ bidirectional_iterator<const int*>(a),
+ bidirectional_iterator<const int*>(an));
+ test<std::vector<int, limited_allocator<int, 18 + 1> > >(
+ random_access_iterator<const int*>(a),
+ random_access_iterator<const int*>(an));
+ test<std::vector<int, limited_allocator<int, 18 + 1> > >(a, an);
+#if TEST_STD_VER >= 11
+ test<std::vector<int, min_allocator<int> > >(input_iterator<const int*>(a),
+ input_iterator<const int*>(an));
+ test<std::vector<int, min_allocator<int> > >(
+ forward_iterator<const int*>(a), forward_iterator<const int*>(an));
+ test<std::vector<int, min_allocator<int> > >(
+ bidirectional_iterator<const int*>(a),
+ bidirectional_iterator<const int*>(an));
+ test<std::vector<int, min_allocator<int> > >(
+ random_access_iterator<const int*>(a),
+ random_access_iterator<const int*>(an));
+ test<std::vector<int> >(a, an);
+#endif
}
-int main()
-{
- int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 1, 0};
- int* an = a + sizeof(a)/sizeof(a[0]);
- test<std::vector<int> >(input_iterator<const int*>(a), input_iterator<const int*>(an));
- test<std::vector<int> >(forward_iterator<const int*>(a), forward_iterator<const int*>(an));
- test<std::vector<int> >(bidirectional_iterator<const int*>(a), bidirectional_iterator<const int*>(an));
- test<std::vector<int> >(random_access_iterator<const int*>(a), random_access_iterator<const int*>(an));
- test<std::vector<int> >(a, an);
-
- test<std::vector<int, limited_allocator<int, 63> > >(input_iterator<const int*>(a), input_iterator<const int*>(an));
- // Add 1 for implementations that dynamically allocate a container proxy.
- test<std::vector<int, limited_allocator<int, 18 + 1> > >(forward_iterator<const int*>(a), forward_iterator<const int*>(an));
- test<std::vector<int, limited_allocator<int, 18 + 1> > >(bidirectional_iterator<const int*>(a), bidirectional_iterator<const int*>(an));
- test<std::vector<int, limited_allocator<int, 18 + 1> > >(random_access_iterator<const int*>(a), random_access_iterator<const int*>(an));
- test<std::vector<int, limited_allocator<int, 18 + 1> > >(a, an);
+void emplaceable_concept_tests() {
#if TEST_STD_VER >= 11
- test<std::vector<int, min_allocator<int>> >(input_iterator<const int*>(a), input_iterator<const int*>(an));
- test<std::vector<int, min_allocator<int>> >(forward_iterator<const int*>(a), forward_iterator<const int*>(an));
- test<std::vector<int, min_allocator<int>> >(bidirectional_iterator<const int*>(a), bidirectional_iterator<const int*>(an));
- test<std::vector<int, min_allocator<int>> >(random_access_iterator<const int*>(a), random_access_iterator<const int*>(an));
- test<std::vector<int> >(a, an);
+ int arr1[] = {42};
+ int arr2[] = {1, 101, 42};
+ {
+ using T = EmplaceConstructible<int>;
+ using It = forward_iterator<int*>;
+ {
+ std::vector<T> v(It(arr1), It(std::end(arr1)));
+ assert(v[0].value == 42);
+ }
+ {
+ std::vector<T> v(It(arr2), It(std::end(arr2)));
+ assert(v[0].value == 1);
+ assert(v[1].value == 101);
+ assert(v[2].value == 42);
+ }
+ }
+ {
+ using T = EmplaceConstructibleAndMoveInsertable<int>;
+ using It = input_iterator<int*>;
+ {
+ std::vector<T> v(It(arr1), It(std::end(arr1)));
+ assert(v[0].copied == 0);
+ assert(v[0].value == 42);
+ }
+ {
+ std::vector<T> v(It(arr2), It(std::end(arr2)));
+ //assert(v[0].copied == 0);
+ assert(v[0].value == 1);
+ //assert(v[1].copied == 0);
+ assert(v[1].value == 101);
+ assert(v[2].copied == 0);
+ assert(v[2].value == 42);
+ }
+ }
#endif
}
+
+void test_ctor_under_alloc() {
+#if TEST_STD_VER >= 11
+ int arr1[] = {42};
+ int arr2[] = {1, 101, 42};
+ {
+ using C = TCT::vector<>;
+ using T = typename C::value_type;
+ using It = forward_iterator<int*>;
+ {
+ ExpectConstructGuard<int&> G(1);
+ C v(It(arr1), It(std::end(arr1)));
+ }
+ {
+ ExpectConstructGuard<int&> G(3);
+ C v(It(arr2), It(std::end(arr2)));
+ }
+ }
+ {
+ using C = TCT::vector<>;
+ using T = typename C::value_type;
+ using It = input_iterator<int*>;
+ {
+ ExpectConstructGuard<int&> G(1);
+ C v(It(arr1), It(std::end(arr1)));
+ }
+ {
+ //ExpectConstructGuard<int&> G(3);
+ //C v(It(arr2), It(std::end(arr2)), a);
+ }
+ }
+#endif
+}
+
+
+int main() {
+ basic_test_cases();
+ emplaceable_concept_tests(); // See PR34898
+ test_ctor_under_alloc();
+}
Modified: libcxx/trunk/test/std/containers/sequences/vector/vector.cons/construct_iter_iter_alloc.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/sequences/vector/vector.cons/construct_iter_iter_alloc.pass.cpp?rev=315994&r1=315993&r2=315994&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/sequences/vector/vector.cons/construct_iter_iter_alloc.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/sequences/vector/vector.cons/construct_iter_iter_alloc.pass.cpp Tue Oct 17 06:03:17 2017
@@ -21,56 +21,152 @@
#include "test_allocator.h"
#include "min_allocator.h"
#include "asan_testing.h"
+#if TEST_STD_VER >= 11
+#include "emplace_constructible.h"
+#include "container_test_types.h"
+#endif
template <class C, class Iterator, class A>
-void
-test(Iterator first, Iterator last, const A& a)
-{
- C c(first, last, a);
- LIBCPP_ASSERT(c.__invariants());
- assert(c.size() == static_cast<std::size_t>(std::distance(first, last)));
- LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
- for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i, ++first)
- assert(*i == *first);
+void test(Iterator first, Iterator last, const A& a) {
+ C c(first, last, a);
+ LIBCPP_ASSERT(c.__invariants());
+ assert(c.size() == static_cast<std::size_t>(std::distance(first, last)));
+ LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
+ for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e;
+ ++i, ++first)
+ assert(*i == *first);
}
#if TEST_STD_VER >= 11
template <class T>
-struct implicit_conv_allocator : min_allocator<T>
-{
- implicit_conv_allocator(void*) {}
- implicit_conv_allocator(const implicit_conv_allocator&) = default;
+struct implicit_conv_allocator : min_allocator<T> {
+ implicit_conv_allocator(void*) {}
+ implicit_conv_allocator(const implicit_conv_allocator&) = default;
- template <class U>
- implicit_conv_allocator(implicit_conv_allocator<U>) {}
+ template <class U>
+ implicit_conv_allocator(implicit_conv_allocator<U>) {}
};
#endif
-int main()
-{
- {
+void basic_tests() {
+ {
int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 1, 0};
- int* an = a + sizeof(a)/sizeof(a[0]);
+ int* an = a + sizeof(a) / sizeof(a[0]);
std::allocator<int> alloc;
- test<std::vector<int> >(input_iterator<const int*>(a), input_iterator<const int*>(an), alloc);
- test<std::vector<int> >(forward_iterator<const int*>(a), forward_iterator<const int*>(an), alloc);
- test<std::vector<int> >(bidirectional_iterator<const int*>(a), bidirectional_iterator<const int*>(an), alloc);
- test<std::vector<int> >(random_access_iterator<const int*>(a), random_access_iterator<const int*>(an), alloc);
+ test<std::vector<int> >(input_iterator<const int*>(a),
+ input_iterator<const int*>(an), alloc);
+ test<std::vector<int> >(forward_iterator<const int*>(a),
+ forward_iterator<const int*>(an), alloc);
+ test<std::vector<int> >(bidirectional_iterator<const int*>(a),
+ bidirectional_iterator<const int*>(an), alloc);
+ test<std::vector<int> >(random_access_iterator<const int*>(a),
+ random_access_iterator<const int*>(an), alloc);
test<std::vector<int> >(a, an, alloc);
- }
+ }
#if TEST_STD_VER >= 11
- {
+ {
int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 1, 0};
- int* an = a + sizeof(a)/sizeof(a[0]);
+ int* an = a + sizeof(a) / sizeof(a[0]);
min_allocator<int> alloc;
- test<std::vector<int, min_allocator<int>> >(input_iterator<const int*>(a), input_iterator<const int*>(an), alloc);
- test<std::vector<int, min_allocator<int>> >(forward_iterator<const int*>(a), forward_iterator<const int*>(an), alloc);
- test<std::vector<int, min_allocator<int>> >(bidirectional_iterator<const int*>(a), bidirectional_iterator<const int*>(an), alloc);
- test<std::vector<int, min_allocator<int>> >(random_access_iterator<const int*>(a), random_access_iterator<const int*>(an), alloc);
- test<std::vector<int, min_allocator<int>> >(a, an, alloc);
- test<std::vector<int, implicit_conv_allocator<int>> >(a, an, nullptr);
+ test<std::vector<int, min_allocator<int> > >(
+ input_iterator<const int*>(a), input_iterator<const int*>(an), alloc);
+ test<std::vector<int, min_allocator<int> > >(
+ forward_iterator<const int*>(a), forward_iterator<const int*>(an),
+ alloc);
+ test<std::vector<int, min_allocator<int> > >(
+ bidirectional_iterator<const int*>(a),
+ bidirectional_iterator<const int*>(an), alloc);
+ test<std::vector<int, min_allocator<int> > >(
+ random_access_iterator<const int*>(a),
+ random_access_iterator<const int*>(an), alloc);
+ test<std::vector<int, min_allocator<int> > >(a, an, alloc);
+ test<std::vector<int, implicit_conv_allocator<int> > >(a, an, nullptr);
+ }
+#endif
+}
+
+void emplaceable_concept_tests() {
+#if TEST_STD_VER >= 11
+ int arr1[] = {42};
+ int arr2[] = {1, 101, 42};
+ {
+ using T = EmplaceConstructible<int>;
+ using It = forward_iterator<int*>;
+ using Alloc = std::allocator<T>;
+ Alloc a;
+ {
+ std::vector<T> v(It(arr1), It(std::end(arr1)), a);
+ assert(v[0].value == 42);
+ }
+ {
+ std::vector<T> v(It(arr2), It(std::end(arr2)), a);
+ assert(v[0].value == 1);
+ assert(v[1].value == 101);
+ assert(v[2].value == 42);
+ }
+ }
+ {
+ using T = EmplaceConstructibleAndMoveInsertable<int>;
+ using It = input_iterator<int*>;
+ using Alloc = std::allocator<T>;
+ Alloc a;
+ {
+ std::vector<T> v(It(arr1), It(std::end(arr1)), a);
+ assert(v[0].copied == 0);
+ assert(v[0].value == 42);
+ }
+ {
+ std::vector<T> v(It(arr2), It(std::end(arr2)), a);
+ assert(v[0].value == 1);
+ assert(v[1].value == 101);
+ assert(v[2].copied == 0);
+ assert(v[2].value == 42);
}
+ }
#endif
}
+
+void test_ctor_under_alloc() {
+#if TEST_STD_VER >= 11
+ int arr1[] = {42};
+ int arr2[] = {1, 101, 42};
+ {
+ using C = TCT::vector<>;
+ using T = typename C::value_type;
+ using It = forward_iterator<int*>;
+ using Alloc = typename C::allocator_type;
+ Alloc a;
+ {
+ ExpectConstructGuard<int&> G(1);
+ C v(It(arr1), It(std::end(arr1)), a);
+ }
+ {
+ ExpectConstructGuard<int&> G(3);
+ C v(It(arr2), It(std::end(arr2)), a);
+ }
+ }
+ {
+ using C = TCT::vector<>;
+ using T = typename C::value_type;
+ using It = input_iterator<int*>;
+ using Alloc = typename C::allocator_type;
+ Alloc a;
+ {
+ ExpectConstructGuard<int&> G(1);
+ C v(It(arr1), It(std::end(arr1)), a);
+ }
+ {
+ //ExpectConstructGuard<int&> G(3);
+ //C v(It(arr2), It(std::end(arr2)), a);
+ }
+ }
+#endif
+}
+
+int main() {
+ basic_tests();
+ emplaceable_concept_tests(); // See PR34898
+ test_ctor_under_alloc();
+}
Modified: libcxx/trunk/test/support/container_test_types.h
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/support/container_test_types.h?rev=315994&r1=315993&r2=315994&view=diff
==============================================================================
--- libcxx/trunk/test/support/container_test_types.h (original)
+++ libcxx/trunk/test/support/container_test_types.h Tue Oct 17 06:03:17 2017
@@ -234,6 +234,19 @@ inline ConstructController* getConstruct
return &c;
}
+template <class ...Args>
+struct ExpectConstructGuard {
+ ExpectConstructGuard(int N) {
+ auto CC = getConstructController();
+ assert(!CC->unchecked());
+ CC->expect<Args...>(N);
+ }
+
+ ~ExpectConstructGuard() {
+ assert(!getConstructController()->unchecked());
+ }
+};
+
//===----------------------------------------------------------------------===//
// ContainerTestAllocator
//===----------------------------------------------------------------------===//
@@ -417,7 +430,12 @@ namespace std {
return arg.data;
}
};
-
+ template <class T, class Alloc>
+ class vector;
+ template <class T, class Alloc>
+ class deque;
+ template <class T, class Alloc>
+ class list;
template <class _Key, class _Value, class _Less, class _Alloc>
class map;
template <class _Key, class _Value, class _Less, class _Alloc>
@@ -444,6 +462,13 @@ _LIBCPP_END_NAMESPACE_STD
// TCT - Test container type
namespace TCT {
+template <class T = CopyInsertable<1>>
+using vector = std::vector<T, ContainerTestAllocator<T, T> >;
+template <class T = CopyInsertable<1>>
+using deque = std::deque<T, ContainerTestAllocator<T, T> >;
+template <class T = CopyInsertable<1>>
+using list = std::list<T, ContainerTestAllocator<T, T> >;
+
template <class Key = CopyInsertable<1>, class Value = CopyInsertable<2>,
class ValueTp = std::pair<const Key, Value> >
using unordered_map =
@@ -488,5 +513,4 @@ using multiset =
} // end namespace TCT
-
#endif // SUPPORT_CONTAINER_TEST_TYPES_H
Added: libcxx/trunk/test/support/emplace_constructible.h
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/support/emplace_constructible.h?rev=315994&view=auto
==============================================================================
--- libcxx/trunk/test/support/emplace_constructible.h (added)
+++ libcxx/trunk/test/support/emplace_constructible.h Tue Oct 17 06:03:17 2017
@@ -0,0 +1,74 @@
+#ifndef TEST_SUPPORT_EMPLACE_CONSTRUCTIBLE_H
+#define TEST_SUPPORT_EMPLACE_CONSTRUCTIBLE_H
+
+#include "test_macros.h"
+
+#if TEST_STD_VER >= 11
+template <class T>
+struct EmplaceConstructible {
+ T value;
+ explicit EmplaceConstructible(T value) : value(value) {}
+ EmplaceConstructible(EmplaceConstructible const&) = delete;
+};
+
+template <class T>
+struct EmplaceConstructibleAndMoveInsertable {
+ int copied = 0;
+ T value;
+ explicit EmplaceConstructibleAndMoveInsertable(T value) : value(value) {}
+
+ EmplaceConstructibleAndMoveInsertable(
+ EmplaceConstructibleAndMoveInsertable&& Other)
+ : copied(Other.copied + 1), value(std::move(Other.value)) {}
+};
+
+template <class T>
+struct EmplaceConstructibleAndMoveable {
+ int copied = 0;
+ int assigned = 0;
+ T value;
+ explicit EmplaceConstructibleAndMoveable(T value) noexcept : value(value) {}
+
+ EmplaceConstructibleAndMoveable(EmplaceConstructibleAndMoveable&& Other)
+ noexcept : copied(Other.copied + 1),
+ value(std::move(Other.value)) {}
+
+ EmplaceConstructibleAndMoveable&
+ operator=(EmplaceConstructibleAndMoveable&& Other) noexcept {
+ copied = Other.copied;
+ assigned = Other.assigned + 1;
+ value = std::move(Other.value);
+ return *this;
+ }
+};
+
+template <class T>
+struct EmplaceConstructibleMoveableAndAssignable {
+ int copied = 0;
+ int assigned = 0;
+ T value;
+ explicit EmplaceConstructibleMoveableAndAssignable(T value) noexcept
+ : value(value) {}
+
+ EmplaceConstructibleMoveableAndAssignable(
+ EmplaceConstructibleMoveableAndAssignable&& Other) noexcept
+ : copied(Other.copied + 1),
+ value(std::move(Other.value)) {}
+
+ EmplaceConstructibleMoveableAndAssignable&
+ operator=(EmplaceConstructibleMoveableAndAssignable&& Other) noexcept {
+ copied = Other.copied;
+ assigned = Other.assigned + 1;
+ value = std::move(Other.value);
+ return *this;
+ }
+
+ EmplaceConstructibleMoveableAndAssignable& operator=(T xvalue) {
+ value = std::move(xvalue);
+ ++assigned;
+ return *this;
+ }
+};
+#endif
+
+#endif // TEST_SUPPORT_EMPLACE_CONSTRUCTIBLE_H
More information about the cfe-commits
mailing list