123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772 |
- // Copyright 2022 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "base/memory/raw_ref.h"
- #include <functional>
- #include <type_traits>
- #include "base/test/gtest_util.h"
- #include "testing/gtest/include/gtest/gtest.h"
- namespace {
- class BaseClass {};
- class SubClass : public BaseClass {};
- // raw_ref just defers to the superclass for implementations, so it
- // can't add more data types.
- static_assert(sizeof(raw_ref<int>) == sizeof(raw_ptr<int>));
- // Since it can't hold null, raw_ref is not default-constructible.
- static_assert(!std::is_default_constructible_v<raw_ref<int>>);
- static_assert(!std::is_default_constructible_v<raw_ref<const int>>);
- // A mutable reference can only be constructed from a mutable lvalue reference.
- static_assert(!std::is_constructible_v<raw_ref<int>, const int>);
- static_assert(!std::is_constructible_v<raw_ref<int>, int>);
- static_assert(!std::is_constructible_v<raw_ref<int>, const int&>);
- static_assert(std::is_constructible_v<raw_ref<int>, int&>);
- static_assert(!std::is_constructible_v<raw_ref<int>, const int*>);
- static_assert(!std::is_constructible_v<raw_ref<int>, int*>);
- static_assert(!std::is_constructible_v<raw_ref<int>, const int&&>);
- static_assert(!std::is_constructible_v<raw_ref<int>, int&&>);
- // Same for assignment.
- static_assert(!std::is_assignable_v<raw_ref<int>, const int>);
- static_assert(!std::is_assignable_v<raw_ref<int>, int>);
- static_assert(!std::is_assignable_v<raw_ref<int>, const int&>);
- static_assert(std::is_assignable_v<raw_ref<int>, int&>);
- static_assert(!std::is_assignable_v<raw_ref<int>, const int*>);
- static_assert(!std::is_assignable_v<raw_ref<int>, int*>);
- static_assert(!std::is_assignable_v<raw_ref<int>, const int&&>);
- static_assert(!std::is_assignable_v<raw_ref<int>, int&&>);
- // A const reference can be constructed from a const or mutable lvalue
- // reference.
- static_assert(!std::is_constructible_v<raw_ref<const int>, const int>);
- static_assert(!std::is_constructible_v<raw_ref<const int>, int>);
- static_assert(std::is_constructible_v<raw_ref<const int>, const int&>);
- static_assert(std::is_constructible_v<raw_ref<const int>, int&>);
- static_assert(!std::is_constructible_v<raw_ref<const int>, const int*>);
- static_assert(!std::is_constructible_v<raw_ref<const int>, int*>);
- static_assert(!std::is_constructible_v<raw_ref<const int>, const int&&>);
- static_assert(!std::is_constructible_v<raw_ref<const int>, int&&>);
- // Same for assignment.
- static_assert(!std::is_assignable_v<raw_ref<const int>, const int>);
- static_assert(!std::is_assignable_v<raw_ref<const int>, int>);
- static_assert(std::is_assignable_v<raw_ref<const int>, const int&>);
- static_assert(std::is_assignable_v<raw_ref<const int>, int&>);
- static_assert(!std::is_assignable_v<raw_ref<const int>, const int*>);
- static_assert(!std::is_assignable_v<raw_ref<const int>, int*>);
- static_assert(!std::is_assignable_v<raw_ref<const int>, const int&&>);
- static_assert(!std::is_assignable_v<raw_ref<const int>, int&&>);
- // Same trivial operations (or not) as raw_ptr<T>.
- static_assert(std::is_trivially_constructible_v<raw_ref<int>, const int&> ==
- std::is_trivially_constructible_v<raw_ptr<int>, const int&>);
- static_assert(std::is_trivially_destructible_v<raw_ref<int>> ==
- std::is_trivially_destructible_v<raw_ptr<int>>);
- // But constructing from another raw_ref must check if it's internally null
- // (which indicates use-after-move).
- static_assert(!std::is_trivially_move_constructible_v<raw_ref<int>>);
- static_assert(!std::is_trivially_move_assignable_v<raw_ref<int>>);
- static_assert(!std::is_trivially_copy_constructible_v<raw_ref<int>>);
- static_assert(!std::is_trivially_copy_assignable_v<raw_ref<int>>);
- // A raw_ref can be copied or moved.
- static_assert(std::is_move_constructible_v<raw_ref<int>>);
- static_assert(std::is_copy_constructible_v<raw_ref<int>>);
- static_assert(std::is_move_assignable_v<raw_ref<int>>);
- static_assert(std::is_copy_assignable_v<raw_ref<int>>);
- // A SubClass can be converted to a BaseClass.
- static_assert(std::is_constructible_v<raw_ref<BaseClass>, raw_ref<SubClass>>);
- static_assert(
- std::is_constructible_v<raw_ref<BaseClass>, const raw_ref<SubClass>&>);
- static_assert(std::is_constructible_v<raw_ref<BaseClass>, raw_ref<SubClass>&&>);
- static_assert(std::is_assignable_v<raw_ref<BaseClass>, raw_ref<SubClass>>);
- static_assert(
- std::is_assignable_v<raw_ref<BaseClass>, const raw_ref<SubClass>&>);
- static_assert(std::is_assignable_v<raw_ref<BaseClass>, raw_ref<SubClass>&&>);
- // A BaseClass can't be implicitly downcasted.
- static_assert(!std::is_constructible_v<raw_ref<SubClass>, raw_ref<BaseClass>>);
- static_assert(
- !std::is_constructible_v<raw_ref<SubClass>, const raw_ref<BaseClass>&>);
- static_assert(
- !std::is_constructible_v<raw_ref<SubClass>, raw_ref<BaseClass>&&>);
- static_assert(!std::is_assignable_v<raw_ref<SubClass>, raw_ref<BaseClass>>);
- static_assert(
- !std::is_assignable_v<raw_ref<SubClass>, const raw_ref<BaseClass>&>);
- static_assert(!std::is_assignable_v<raw_ref<SubClass>, raw_ref<BaseClass>&&>);
- // A raw_ref<BaseClass> can be constructed directly from a SubClass.
- static_assert(std::is_constructible_v<raw_ref<BaseClass>, SubClass&>);
- static_assert(std::is_assignable_v<raw_ref<BaseClass>, SubClass&>);
- static_assert(std::is_constructible_v<raw_ref<const BaseClass>, SubClass&>);
- static_assert(std::is_assignable_v<raw_ref<const BaseClass>, SubClass&>);
- static_assert(
- std::is_constructible_v<raw_ref<const BaseClass>, const SubClass&>);
- static_assert(std::is_assignable_v<raw_ref<const BaseClass>, const SubClass&>);
- // But a raw_ref<SubClass> can't be constructed from an implicit downcast from a
- // BaseClass.
- static_assert(!std::is_constructible_v<raw_ref<SubClass>, BaseClass&>);
- static_assert(!std::is_assignable_v<raw_ref<SubClass>, BaseClass&>);
- static_assert(!std::is_constructible_v<raw_ref<const SubClass>, BaseClass&>);
- static_assert(!std::is_assignable_v<raw_ref<const SubClass>, BaseClass&>);
- static_assert(
- !std::is_constructible_v<raw_ref<const SubClass>, const BaseClass&>);
- static_assert(!std::is_assignable_v<raw_ref<const SubClass>, const BaseClass&>);
- // A mutable reference can be converted to const reference.
- static_assert(std::is_constructible_v<raw_ref<const int>, raw_ref<int>>);
- static_assert(std::is_assignable_v<raw_ref<const int>, raw_ref<int>>);
- // A const reference can't be converted to mutable.
- static_assert(!std::is_constructible_v<raw_ref<int>, raw_ref<const int>>);
- static_assert(!std::is_assignable_v<raw_ref<int>, raw_ref<const int>>);
- // The deref operator gives the internal reference.
- static_assert(std::is_same_v<int&, decltype(*std::declval<raw_ref<int>>())>);
- static_assert(
- std::is_same_v<int&, decltype(*std::declval<const raw_ref<int>>())>);
- static_assert(std::is_same_v<int&, decltype(*std::declval<raw_ref<int>&>())>);
- static_assert(
- std::is_same_v<int&, decltype(*std::declval<const raw_ref<int>&>())>);
- static_assert(std::is_same_v<int&, decltype(*std::declval<raw_ref<int>&&>())>);
- static_assert(
- std::is_same_v<int&, decltype(*std::declval<const raw_ref<int>&&>())>);
- // A const T is always returned as const.
- static_assert(
- std::is_same_v<const int&, decltype(*std::declval<raw_ref<const int>>())>);
- // The arrow operator gives a (non-null) pointer to the internal reference.
- static_assert(
- std::is_same_v<int*, decltype(std::declval<raw_ref<int>>().operator->())>);
- static_assert(
- std::is_same_v<const int*,
- decltype(std::declval<raw_ref<const int>>().operator->())>);
- TEST(RawRef, Construct) {
- int i = 1;
- auto r = raw_ref<int>(i);
- EXPECT_EQ(&*r, &i);
- auto cr = raw_ref<const int>(i);
- EXPECT_EQ(&*cr, &i);
- const int ci = 1;
- auto cci = raw_ref<const int>(ci);
- EXPECT_EQ(&*cci, &ci);
- }
- TEST(RawRef, CopyConstruct) {
- {
- int i = 1;
- auto r = raw_ref<int>(i);
- EXPECT_EQ(&*r, &i);
- auto r2 = raw_ref<int>(r);
- EXPECT_EQ(&*r2, &i);
- }
- {
- int i = 1;
- auto r = raw_ref<const int>(i);
- EXPECT_EQ(&*r, &i);
- auto r2 = raw_ref<const int>(r);
- EXPECT_EQ(&*r2, &i);
- }
- }
- TEST(RawRefDeathTest, CopyConstructAfterMove) {
- int i = 1;
- auto r = raw_ref<int>(i);
- auto r2 = std::move(r);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] auto r3 = r; });
- }
- TEST(RawRef, MoveConstruct) {
- {
- int i = 1;
- auto r = raw_ref<int>(i);
- EXPECT_EQ(&*r, &i);
- auto r2 = raw_ref<int>(std::move(r));
- EXPECT_EQ(&*r2, &i);
- }
- {
- int i = 1;
- auto r = raw_ref<const int>(i);
- EXPECT_EQ(&*r, &i);
- auto r2 = raw_ref<const int>(std::move(r));
- EXPECT_EQ(&*r2, &i);
- }
- }
- TEST(RawRefDeathTest, MoveConstructAfterMove) {
- int i = 1;
- auto r = raw_ref<int>(i);
- auto r2 = std::move(r);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] auto r3 = std::move(r); });
- }
- TEST(RawRef, CopyAssign) {
- {
- int i = 1;
- auto r = raw_ref<int>(i);
- EXPECT_EQ(&*r, &i);
- int j = 2;
- auto rj = raw_ref<int>(j);
- r = rj;
- EXPECT_EQ(&*r, &j);
- }
- {
- int i = 1;
- auto r = raw_ref<const int>(i);
- EXPECT_EQ(&*r, &i);
- int j = 2;
- auto rj = raw_ref<const int>(j);
- r = rj;
- EXPECT_EQ(&*r, &j);
- }
- {
- int i = 1;
- auto r = raw_ref<const int>(i);
- EXPECT_EQ(&*r, &i);
- int j = 2;
- auto rj = raw_ref<int>(j);
- r = rj;
- EXPECT_EQ(&*r, &j);
- }
- }
- TEST(RawRefDeathTest, CopyAssignAfterMove) {
- int i = 1;
- auto r = raw_ref<int>(i);
- auto r2 = std::move(r);
- EXPECT_CHECK_DEATH({ r2 = r; });
- }
- TEST(RawRef, CopyReassignAfterMove) {
- int i = 1;
- auto r = raw_ref<int>(i);
- auto r2 = std::move(r);
- int j = 1;
- r2 = raw_ref<int>(j);
- // Reassign to the moved-from `r` so it can be used again.
- r = r2;
- EXPECT_EQ(&*r, &j);
- }
- TEST(RawRef, MoveAssign) {
- {
- int i = 1;
- auto r = raw_ref<int>(i);
- EXPECT_EQ(&*r, &i);
- int j = 2;
- r = raw_ref<int>(j);
- EXPECT_EQ(&*r, &j);
- }
- {
- int i = 1;
- auto r = raw_ref<const int>(i);
- EXPECT_EQ(&*r, &i);
- int j = 2;
- r = raw_ref<const int>(j);
- EXPECT_EQ(&*r, &j);
- }
- {
- int i = 1;
- auto r = raw_ref<const int>(i);
- EXPECT_EQ(&*r, &i);
- int j = 2;
- r = raw_ref<int>(j);
- EXPECT_EQ(&*r, &j);
- }
- }
- TEST(RawRefDeathTest, MoveAssignAfterMove) {
- int i = 1;
- auto r = raw_ref<int>(i);
- auto r2 = std::move(r);
- EXPECT_CHECK_DEATH({ r2 = std::move(r); });
- }
- TEST(RawRef, MoveReassignAfterMove) {
- int i = 1;
- auto r = raw_ref<int>(i);
- auto r2 = std::move(r);
- int j = 1;
- // Reassign to the moved-from `r` so it can be used again.
- r = raw_ref<int>(j);
- EXPECT_EQ(&*r, &j);
- }
- TEST(RawRef, CopyConstructUpCast) {
- {
- auto s = SubClass();
- auto r = raw_ref<SubClass>(s);
- EXPECT_EQ(&*r, &s);
- auto r2 = raw_ref<BaseClass>(r);
- EXPECT_EQ(&*r2, &s);
- }
- {
- auto s = SubClass();
- auto r = raw_ref<const SubClass>(s);
- EXPECT_EQ(&*r, &s);
- auto r2 = raw_ref<const BaseClass>(r);
- EXPECT_EQ(&*r2, &s);
- }
- }
- TEST(RawRefDeathTest, CopyConstructAfterMoveUpCast) {
- auto s = SubClass();
- auto r = raw_ref<SubClass>(s);
- auto moved = std::move(r);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] auto r2 = raw_ref<BaseClass>(r); });
- }
- TEST(RawRef, MoveConstructUpCast) {
- {
- auto s = SubClass();
- auto r = raw_ref<SubClass>(s);
- EXPECT_EQ(&*r, &s);
- auto r2 = raw_ref<BaseClass>(std::move(r));
- EXPECT_EQ(&*r2, &s);
- }
- {
- auto s = SubClass();
- auto r = raw_ref<const SubClass>(s);
- EXPECT_EQ(&*r, &s);
- auto r2 = raw_ref<const BaseClass>(std::move(r));
- EXPECT_EQ(&*r2, &s);
- }
- }
- TEST(RawRefDeathTest, MoveConstructAfterMoveUpCast) {
- auto s = SubClass();
- auto r = raw_ref<SubClass>(s);
- auto moved = std::move(r);
- EXPECT_CHECK_DEATH(
- { [[maybe_unused]] auto r2 = raw_ref<BaseClass>(std::move(r)); });
- }
- TEST(RawRef, CopyAssignUpCast) {
- {
- auto s = SubClass();
- auto r = raw_ref<SubClass>(s);
- auto t = BaseClass();
- auto rt = raw_ref<BaseClass>(t);
- rt = r;
- EXPECT_EQ(&*rt, &s);
- }
- {
- auto s = SubClass();
- auto r = raw_ref<const SubClass>(s);
- auto t = BaseClass();
- auto rt = raw_ref<const BaseClass>(t);
- rt = r;
- EXPECT_EQ(&*rt, &s);
- }
- {
- auto s = SubClass();
- auto r = raw_ref<SubClass>(s);
- auto t = BaseClass();
- auto rt = raw_ref<const BaseClass>(t);
- rt = r;
- EXPECT_EQ(&*rt, &s);
- }
- }
- TEST(RawRefDeathTest, CopyAssignAfterMoveUpCast) {
- auto s = SubClass();
- auto r = raw_ref<const SubClass>(s);
- auto t = BaseClass();
- auto rt = raw_ref<const BaseClass>(t);
- auto moved = std::move(r);
- EXPECT_CHECK_DEATH({ rt = r; });
- }
- TEST(RawRef, MoveAssignUpCast) {
- {
- auto s = SubClass();
- auto r = raw_ref<SubClass>(s);
- auto t = BaseClass();
- auto rt = raw_ref<BaseClass>(t);
- rt = std::move(r);
- EXPECT_EQ(&*rt, &s);
- }
- {
- auto s = SubClass();
- auto r = raw_ref<const SubClass>(s);
- auto t = BaseClass();
- auto rt = raw_ref<const BaseClass>(t);
- rt = std::move(r);
- EXPECT_EQ(&*rt, &s);
- }
- {
- auto s = SubClass();
- auto r = raw_ref<SubClass>(s);
- auto t = BaseClass();
- auto rt = raw_ref<const BaseClass>(t);
- rt = std::move(r);
- EXPECT_EQ(&*rt, &s);
- }
- }
- TEST(RawRefDeathTest, MoveAssignAfterMoveUpCast) {
- auto s = SubClass();
- auto r = raw_ref<const SubClass>(s);
- auto t = BaseClass();
- auto rt = raw_ref<const BaseClass>(t);
- auto moved = std::move(r);
- EXPECT_CHECK_DEATH({ rt = std::move(r); });
- }
- TEST(RawRef, Deref) {
- int i;
- auto r = raw_ref<int>(i);
- EXPECT_EQ(&*r, &i);
- }
- TEST(RawRefDeathTest, DerefAfterMove) {
- int i;
- auto r = raw_ref<int>(i);
- auto moved = std::move(r);
- EXPECT_CHECK_DEATH({ r.operator*(); });
- }
- TEST(RawRef, Arrow) {
- int i;
- auto r = raw_ref<int>(i);
- EXPECT_EQ(r.operator->(), &i);
- }
- TEST(RawRefDeathTest, ArrowAfterMove) {
- int i;
- auto r = raw_ref<int>(i);
- auto moved = std::move(r);
- EXPECT_CHECK_DEATH({ r.operator->(); });
- }
- TEST(RawRef, Swap) {
- int i;
- auto ri = raw_ref<int>(i);
- int j;
- auto rj = raw_ref<int>(j);
- swap(ri, rj);
- EXPECT_EQ(&*ri, &j);
- EXPECT_EQ(&*rj, &i);
- }
- TEST(RawRefDeathTest, SwapAfterMove) {
- {
- int i;
- auto ri = raw_ref<int>(i);
- int j;
- auto rj = raw_ref<int>(j);
- auto moved = std::move(ri);
- EXPECT_CHECK_DEATH({ swap(ri, rj); });
- }
- {
- int i;
- auto ri = raw_ref<int>(i);
- int j;
- auto rj = raw_ref<int>(j);
- auto moved = std::move(rj);
- EXPECT_CHECK_DEATH({ swap(ri, rj); });
- }
- }
- TEST(RawRef, Equals) {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto r2 = raw_ref<int>(i);
- EXPECT_TRUE(r1 == r1);
- EXPECT_TRUE(r1 == r2);
- EXPECT_TRUE(r1 == i);
- EXPECT_TRUE(i == r1);
- int j = 1;
- auto r3 = raw_ref<int>(j);
- EXPECT_FALSE(r1 == r3);
- EXPECT_FALSE(r1 == j);
- EXPECT_FALSE(j == r1);
- }
- TEST(RawRefDeathTest, EqualsAfterMove) {
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto r2 = raw_ref<int>(i);
- auto moved = std::move(r1);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 == r2; });
- }
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto r2 = raw_ref<int>(i);
- auto moved = std::move(r2);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 == r2; });
- }
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto moved = std::move(r1);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 == r1; });
- }
- }
- TEST(RawRef, NotEquals) {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- int j = 1;
- auto r2 = raw_ref<int>(j);
- EXPECT_TRUE(r1 != r2);
- EXPECT_TRUE(r1 != j);
- EXPECT_TRUE(j != r1);
- EXPECT_FALSE(r1 != r1);
- EXPECT_FALSE(r2 != j);
- EXPECT_FALSE(j != r2);
- }
- TEST(RawRefDeathTest, NotEqualsAfterMove) {
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto r2 = raw_ref<int>(i);
- auto moved = std::move(r1);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 != r2; });
- }
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto r2 = raw_ref<int>(i);
- auto moved = std::move(r2);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 != r2; });
- }
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto moved = std::move(r1);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 != r1; });
- }
- }
- TEST(RawRef, LessThan) {
- int i[] = {1, 1};
- auto r1 = raw_ref<int>(i[0]);
- auto r2 = raw_ref<int>(i[1]);
- EXPECT_TRUE(r1 < r2);
- EXPECT_TRUE(r1 < i[1]);
- EXPECT_FALSE(i[1] < r1);
- EXPECT_FALSE(r2 < r1);
- EXPECT_FALSE(r2 < i[0]);
- EXPECT_TRUE(i[0] < r2);
- EXPECT_FALSE(r1 < r1);
- EXPECT_FALSE(r1 < i[0]);
- EXPECT_FALSE(i[0] < r1);
- }
- TEST(RawRefDeathTest, LessThanAfterMove) {
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto r2 = raw_ref<int>(i);
- auto moved = std::move(r1);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 < r2; });
- }
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto r2 = raw_ref<int>(i);
- auto moved = std::move(r2);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 < r2; });
- }
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto moved = std::move(r1);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 < r1; });
- }
- }
- TEST(RawRef, GreaterThan) {
- int i[] = {1, 1};
- auto r1 = raw_ref<int>(i[0]);
- auto r2 = raw_ref<int>(i[1]);
- EXPECT_TRUE(r2 > r1);
- EXPECT_FALSE(r1 > r2);
- EXPECT_FALSE(r1 > i[1]);
- EXPECT_TRUE(i[1] > r1);
- EXPECT_FALSE(r2 > r2);
- EXPECT_FALSE(r2 > i[1]);
- EXPECT_FALSE(i[1] > r2);
- }
- TEST(RawRefDeathTest, GreaterThanAfterMove) {
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto r2 = raw_ref<int>(i);
- auto moved = std::move(r1);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 > r2; });
- }
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto r2 = raw_ref<int>(i);
- auto moved = std::move(r2);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 > r2; });
- }
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto moved = std::move(r1);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 > r1; });
- }
- }
- TEST(RawRef, LessThanOrEqual) {
- int i[] = {1, 1};
- auto r1 = raw_ref<int>(i[0]);
- auto r2 = raw_ref<int>(i[1]);
- EXPECT_TRUE(r1 <= r2);
- EXPECT_TRUE(r1 <= r1);
- EXPECT_TRUE(r2 <= r2);
- EXPECT_FALSE(r2 <= r1);
- EXPECT_TRUE(r1 <= i[1]);
- EXPECT_TRUE(r1 <= i[0]);
- EXPECT_TRUE(r2 <= i[1]);
- EXPECT_FALSE(r2 <= i[0]);
- EXPECT_FALSE(i[1] <= r1);
- EXPECT_TRUE(i[0] <= r1);
- EXPECT_TRUE(i[1] <= r2);
- EXPECT_TRUE(i[0] <= r2);
- }
- TEST(RawRefDeathTest, LessThanOrEqualAfterMove) {
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto r2 = raw_ref<int>(i);
- auto moved = std::move(r1);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 <= r2; });
- }
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto r2 = raw_ref<int>(i);
- auto moved = std::move(r2);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 <= r2; });
- }
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto moved = std::move(r1);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 <= r1; });
- }
- }
- TEST(RawRef, GreaterThanOrEqual) {
- int i[] = {1, 1};
- auto r1 = raw_ref<int>(i[0]);
- auto r2 = raw_ref<int>(i[1]);
- EXPECT_TRUE(r2 >= r1);
- EXPECT_TRUE(r1 >= r1);
- EXPECT_TRUE(r2 >= r2);
- EXPECT_FALSE(r1 >= r2);
- EXPECT_TRUE(r2 >= i[0]);
- EXPECT_TRUE(r1 >= i[0]);
- EXPECT_TRUE(r2 >= i[1]);
- EXPECT_FALSE(r1 >= i[1]);
- EXPECT_FALSE(i[0] >= r2);
- EXPECT_TRUE(i[0] >= r1);
- EXPECT_TRUE(i[1] >= r2);
- EXPECT_TRUE(i[1] >= r1);
- }
- TEST(RawRefDeathTest, GreaterThanOrEqualAfterMove) {
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto r2 = raw_ref<int>(i);
- auto moved = std::move(r1);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 >= r2; });
- }
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto r2 = raw_ref<int>(i);
- auto moved = std::move(r2);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 >= r2; });
- }
- {
- int i = 1;
- auto r1 = raw_ref<int>(i);
- auto moved = std::move(r1);
- EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 >= r1; });
- }
- }
- TEST(RawRef, CTAD) {
- int i = 1;
- auto r = raw_ref(i);
- EXPECT_EQ(&*r, &i);
- }
- using RawPtrCountingImpl =
- base::internal::RawPtrCountingImplWrapperForTest<base::DefaultRawPtrImpl>;
- template <typename T>
- using CountingRawRef = raw_ref<T, RawPtrCountingImpl>;
- TEST(RawRef, StdLess) {
- int i[] = {1, 1};
- {
- RawPtrCountingImpl::ClearCounters();
- auto r1 = CountingRawRef<int>(i[0]);
- auto r2 = CountingRawRef<int>(i[1]);
- EXPECT_TRUE(std::less<CountingRawRef<int>>()(r1, r2));
- EXPECT_FALSE(std::less<CountingRawRef<int>>()(r2, r1));
- EXPECT_EQ(2, RawPtrCountingImpl::wrapped_ptr_less_cnt);
- }
- {
- RawPtrCountingImpl::ClearCounters();
- const auto r1 = CountingRawRef<int>(i[0]);
- const auto r2 = CountingRawRef<int>(i[1]);
- EXPECT_TRUE(std::less<CountingRawRef<int>>()(r1, r2));
- EXPECT_FALSE(std::less<CountingRawRef<int>>()(r2, r1));
- EXPECT_EQ(2, RawPtrCountingImpl::wrapped_ptr_less_cnt);
- }
- {
- RawPtrCountingImpl::ClearCounters();
- auto r1 = CountingRawRef<const int>(i[0]);
- auto r2 = CountingRawRef<const int>(i[1]);
- EXPECT_TRUE(std::less<CountingRawRef<const int>>()(r1, r2));
- EXPECT_FALSE(std::less<CountingRawRef<const int>>()(r2, r1));
- EXPECT_EQ(2, RawPtrCountingImpl::wrapped_ptr_less_cnt);
- }
- {
- RawPtrCountingImpl::ClearCounters();
- auto r1 = CountingRawRef<int>(i[0]);
- auto r2 = CountingRawRef<int>(i[1]);
- EXPECT_TRUE(std::less<CountingRawRef<int>>()(r1, i[1]));
- EXPECT_FALSE(std::less<CountingRawRef<int>>()(r2, i[0]));
- EXPECT_EQ(2, RawPtrCountingImpl::wrapped_ptr_less_cnt);
- }
- {
- RawPtrCountingImpl::ClearCounters();
- const auto r1 = CountingRawRef<int>(i[0]);
- const auto r2 = CountingRawRef<int>(i[1]);
- EXPECT_TRUE(std::less<CountingRawRef<int>>()(r1, i[1]));
- EXPECT_FALSE(std::less<CountingRawRef<int>>()(r2, i[0]));
- EXPECT_EQ(2, RawPtrCountingImpl::wrapped_ptr_less_cnt);
- }
- {
- RawPtrCountingImpl::ClearCounters();
- auto r1 = CountingRawRef<const int>(i[0]);
- auto r2 = CountingRawRef<const int>(i[1]);
- EXPECT_TRUE(std::less<CountingRawRef<const int>>()(r1, i[1]));
- EXPECT_FALSE(std::less<CountingRawRef<const int>>()(r2, i[0]));
- EXPECT_EQ(2, RawPtrCountingImpl::wrapped_ptr_less_cnt);
- }
- }
- } // namespace
|