[libcxx-commits] [libcxx] [libc++] Improve the test coverage for std::vector::emplace (PR #132440)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Mar 27 09:16:00 PDT 2025
================
@@ -7,150 +7,341 @@
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03 && !stdlib=libc++
+// ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=9000000
// <vector>
// template <class... Args> iterator emplace(const_iterator pos, Args&&... args);
-#include <vector>
#include <cassert>
+#include <cstddef>
+#include <type_traits>
+#include <utility>
+#include <vector>
-#include "test_macros.h"
-#include "test_allocator.h"
-#include "min_allocator.h"
#include "asan_testing.h"
+#include "common.h"
+#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_macros.h"
-class A {
- int i_;
- double d_;
+struct MoveOnly {
+ int value;
- A(const A&);
- A& operator=(const A&);
+ TEST_CONSTEXPR_CXX14 explicit MoveOnly(int i) : value(i) {}
-public:
- TEST_CONSTEXPR_CXX14 A(int i, double d) : i_(i), d_(d) {}
+ MoveOnly(MoveOnly const&) = delete;
+ MoveOnly& operator=(MoveOnly const&) = delete;
- TEST_CONSTEXPR_CXX14 A(A&& a) : i_(a.i_), d_(a.d_) {
- a.i_ = 0;
- a.d_ = 0;
- }
+ TEST_CONSTEXPR_CXX14 MoveOnly(MoveOnly&& other) TEST_NOEXCEPT : value(other.value) { other.value = -1; }
- TEST_CONSTEXPR_CXX14 A& operator=(A&& a) {
- i_ = a.i_;
- d_ = a.d_;
- a.i_ = 0;
- a.d_ = 0;
+ TEST_CONSTEXPR_CXX14 MoveOnly& operator=(MoveOnly&& other) TEST_NOEXCEPT {
+ value = other.value;
+ other.value = -1;
return *this;
}
- TEST_CONSTEXPR_CXX14 int geti() const { return i_; }
- TEST_CONSTEXPR_CXX14 double getd() const { return d_; }
+ TEST_CONSTEXPR_CXX14 friend bool operator==(MoveOnly const& lhs, MoveOnly const& rhs) {
+ return lhs.value == rhs.value;
+ }
};
-TEST_CONSTEXPR_CXX20 bool tests() {
+template <class T>
+struct has_moved_from_sentinel : std::false_type {};
+
+template <>
+struct has_moved_from_sentinel<MoveOnly> : std::true_type {};
+
+template <template <class...> class Allocator, class T>
+TEST_CONSTEXPR_CXX20 void test() {
+ using Vector = std::vector<T, Allocator<T> >;
+ using Iterator = typename Vector::iterator;
+
+ // Check the return type
{
- std::vector<A> c;
- std::vector<A>::iterator i = c.emplace(c.cbegin(), 2, 3.5);
- assert(i == c.begin());
- assert(c.size() == 1);
- assert(c.front().geti() == 2);
- assert(c.front().getd() == 3.5);
- assert(is_contiguous_container_asan_correct(c));
- i = c.emplace(c.cend(), 3, 4.5);
- assert(i == c.end() - 1);
- assert(c.size() == 2);
- assert(c.front().geti() == 2);
- assert(c.front().getd() == 3.5);
- assert(c.back().geti() == 3);
- assert(c.back().getd() == 4.5);
- assert(is_contiguous_container_asan_correct(c));
- i = c.emplace(c.cbegin() + 1, 4, 6.5);
- assert(i == c.begin() + 1);
- assert(c.size() == 3);
- assert(c.front().geti() == 2);
- assert(c.front().getd() == 3.5);
- assert(c[1].geti() == 4);
- assert(c[1].getd() == 6.5);
- assert(c.back().geti() == 3);
- assert(c.back().getd() == 4.5);
- assert(is_contiguous_container_asan_correct(c));
+ Vector v;
+ ASSERT_SAME_TYPE(decltype(v.emplace(v.cbegin(), 1)), Iterator);
}
+
+ // Emplace at the end of a vector with increasing size
{
- std::vector<A, limited_allocator<A, 7> > c;
- std::vector<A, limited_allocator<A, 7> >::iterator i = c.emplace(c.cbegin(), 2, 3.5);
- assert(i == c.begin());
- assert(c.size() == 1);
- assert(c.front().geti() == 2);
- assert(c.front().getd() == 3.5);
- assert(is_contiguous_container_asan_correct(c));
- i = c.emplace(c.cend(), 3, 4.5);
- assert(i == c.end() - 1);
- assert(c.size() == 2);
- assert(c.front().geti() == 2);
- assert(c.front().getd() == 3.5);
- assert(c.back().geti() == 3);
- assert(c.back().getd() == 4.5);
- assert(is_contiguous_container_asan_correct(c));
- i = c.emplace(c.cbegin() + 1, 4, 6.5);
- assert(i == c.begin() + 1);
- assert(c.size() == 3);
- assert(c.front().geti() == 2);
- assert(c.front().getd() == 3.5);
- assert(c[1].geti() == 4);
- assert(c[1].getd() == 6.5);
- assert(c.back().geti() == 3);
- assert(c.back().getd() == 4.5);
- assert(is_contiguous_container_asan_correct(c));
+ Vector v;
+
+ // starts with size 0
+ {
+ Iterator it = v.emplace(v.cend(), 0);
+ assert(it == v.end() - 1);
+ assert(v.size() == 1);
+ assert(v[0] == T(0));
+ assert(is_contiguous_container_asan_correct(v));
+ }
+
+ // starts with size 1
+ {
+ Iterator it = v.emplace(v.cend(), 1);
+ assert(it == v.end() - 1);
+ assert(v.size() == 2);
+ assert(v[0] == T(0));
+ assert(v[1] == T(1));
+ assert(is_contiguous_container_asan_correct(v));
+ }
+
+ // starts with size 2
+ {
+ Iterator it = v.emplace(v.cend(), 2);
+ assert(it == v.end() - 1);
+ assert(v.size() == 3);
+ assert(v[0] == T(0));
+ assert(v[1] == T(1));
+ assert(v[2] == T(2));
+ assert(is_contiguous_container_asan_correct(v));
+ }
+
+ // starts with size n...
+ for (std::size_t n = 3; n != 100; ++n) {
+ Iterator it = v.emplace(v.cend(), n);
+ assert(it == v.end() - 1);
+ assert(v.size() == n + 1);
+ for (std::size_t i = 0; i != n + 1; ++i)
+ assert(v[i] == T(i));
+ assert(is_contiguous_container_asan_correct(v));
+ }
----------------
ldionne wrote:
I wanted to be super explicit about what I'm testing. I'd have a weak preference for keeping it the way it is.
https://github.com/llvm/llvm-project/pull/132440
More information about the libcxx-commits
mailing list