Please use GitHub pull requests for new patches. Avoid migrating existing patches. Phabricator shutdown timeline
Changeset View
Changeset View
Standalone View
Standalone View
libcxx/test/libcxx/ranges/range.nonprop.cache/assign.copy.pass.cpp
Show First 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | |||||
template <class T> | template <class T> | ||||
constexpr void test() { | constexpr void test() { | ||||
using Cache = std::ranges::__non_propagating_cache<T>; | using Cache = std::ranges::__non_propagating_cache<T>; | ||||
static_assert(std::is_nothrow_copy_assignable_v<Cache>); | static_assert(std::is_nothrow_copy_assignable_v<Cache>); | ||||
// Assign to an empty cache | // Assign to an empty cache | ||||
{ | { | ||||
Cache a; a.__set(T{3}); | Cache a; a.__emplace(3); | ||||
Cache b; | Cache b; | ||||
Cache& result = (b = a); | Cache& result = (b = a); | ||||
assert(&result == &b); | assert(&result == &b); | ||||
assert(!b.__has_value()); // make sure we don't propagate | assert(!b.__has_value()); // make sure we don't propagate | ||||
assert(a.__has_value()); // make sure we don't "steal" from the source | assert(a.__has_value()); // make sure we don't "steal" from the source | ||||
assert(*a == T{3}); // | assert(*a == T{3}); // | ||||
} | } | ||||
// Assign to a non-empty cache | // Assign to a non-empty cache | ||||
{ | { | ||||
Cache a; a.__set(T{3}); | Cache a; a.__emplace(3); | ||||
Cache b; b.__set(T{5}); | Cache b; b.__emplace(5); | ||||
Cache& result = (b = a); | Cache& result = (b = a); | ||||
assert(&result == &b); | assert(&result == &b); | ||||
assert(!b.__has_value()); // make sure we don't propagate | assert(!b.__has_value()); // make sure we don't propagate | ||||
assert(a.__has_value()); // make sure we don't "steal" from the source | assert(a.__has_value()); // make sure we don't "steal" from the source | ||||
assert(*a == T{3}); // | assert(*a == T{3}); // | ||||
} | } | ||||
// Self-assignment should not do anything (case with empty cache) | // Self-assignment should not do anything (case with empty cache) | ||||
{ | { | ||||
Cache b; | Cache b; | ||||
Cache& result = (b = b); | Cache& result = (b = b); | ||||
assert(&result == &b); | assert(&result == &b); | ||||
assert(!b.__has_value()); | assert(!b.__has_value()); | ||||
} | } | ||||
// Self-assignment should not do anything (case with non-empty cache) | // Self-assignment should not do anything (case with non-empty cache) | ||||
{ | { | ||||
Cache b; b.__set(T{5}); | Cache b; b.__emplace(5); | ||||
Cache& result = (b = b); | Cache& result = (b = b); | ||||
assert(&result == &b); | assert(&result == &b); | ||||
assert(b.__has_value()); | assert(b.__has_value()); | ||||
assert(*b == T{5}); | assert(*b == T{5}); | ||||
} | } | ||||
} | } | ||||
constexpr bool tests() { | constexpr bool tests() { | ||||
Show All 12 Lines |