raw_ref_unittest.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/memory/raw_ref.h"
  5. #include <functional>
  6. #include <type_traits>
  7. #include "base/test/gtest_util.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace {
  10. class BaseClass {};
  11. class SubClass : public BaseClass {};
  12. // raw_ref just defers to the superclass for implementations, so it
  13. // can't add more data types.
  14. static_assert(sizeof(raw_ref<int>) == sizeof(raw_ptr<int>));
  15. // Since it can't hold null, raw_ref is not default-constructible.
  16. static_assert(!std::is_default_constructible_v<raw_ref<int>>);
  17. static_assert(!std::is_default_constructible_v<raw_ref<const int>>);
  18. // A mutable reference can only be constructed from a mutable lvalue reference.
  19. static_assert(!std::is_constructible_v<raw_ref<int>, const int>);
  20. static_assert(!std::is_constructible_v<raw_ref<int>, int>);
  21. static_assert(!std::is_constructible_v<raw_ref<int>, const int&>);
  22. static_assert(std::is_constructible_v<raw_ref<int>, int&>);
  23. static_assert(!std::is_constructible_v<raw_ref<int>, const int*>);
  24. static_assert(!std::is_constructible_v<raw_ref<int>, int*>);
  25. static_assert(!std::is_constructible_v<raw_ref<int>, const int&&>);
  26. static_assert(!std::is_constructible_v<raw_ref<int>, int&&>);
  27. // Same for assignment.
  28. static_assert(!std::is_assignable_v<raw_ref<int>, const int>);
  29. static_assert(!std::is_assignable_v<raw_ref<int>, int>);
  30. static_assert(!std::is_assignable_v<raw_ref<int>, const int&>);
  31. static_assert(std::is_assignable_v<raw_ref<int>, int&>);
  32. static_assert(!std::is_assignable_v<raw_ref<int>, const int*>);
  33. static_assert(!std::is_assignable_v<raw_ref<int>, int*>);
  34. static_assert(!std::is_assignable_v<raw_ref<int>, const int&&>);
  35. static_assert(!std::is_assignable_v<raw_ref<int>, int&&>);
  36. // A const reference can be constructed from a const or mutable lvalue
  37. // reference.
  38. static_assert(!std::is_constructible_v<raw_ref<const int>, const int>);
  39. static_assert(!std::is_constructible_v<raw_ref<const int>, int>);
  40. static_assert(std::is_constructible_v<raw_ref<const int>, const int&>);
  41. static_assert(std::is_constructible_v<raw_ref<const int>, int&>);
  42. static_assert(!std::is_constructible_v<raw_ref<const int>, const int*>);
  43. static_assert(!std::is_constructible_v<raw_ref<const int>, int*>);
  44. static_assert(!std::is_constructible_v<raw_ref<const int>, const int&&>);
  45. static_assert(!std::is_constructible_v<raw_ref<const int>, int&&>);
  46. // Same for assignment.
  47. static_assert(!std::is_assignable_v<raw_ref<const int>, const int>);
  48. static_assert(!std::is_assignable_v<raw_ref<const int>, int>);
  49. static_assert(std::is_assignable_v<raw_ref<const int>, const int&>);
  50. static_assert(std::is_assignable_v<raw_ref<const int>, int&>);
  51. static_assert(!std::is_assignable_v<raw_ref<const int>, const int*>);
  52. static_assert(!std::is_assignable_v<raw_ref<const int>, int*>);
  53. static_assert(!std::is_assignable_v<raw_ref<const int>, const int&&>);
  54. static_assert(!std::is_assignable_v<raw_ref<const int>, int&&>);
  55. // Same trivial operations (or not) as raw_ptr<T>.
  56. static_assert(std::is_trivially_constructible_v<raw_ref<int>, const int&> ==
  57. std::is_trivially_constructible_v<raw_ptr<int>, const int&>);
  58. static_assert(std::is_trivially_destructible_v<raw_ref<int>> ==
  59. std::is_trivially_destructible_v<raw_ptr<int>>);
  60. // But constructing from another raw_ref must check if it's internally null
  61. // (which indicates use-after-move).
  62. static_assert(!std::is_trivially_move_constructible_v<raw_ref<int>>);
  63. static_assert(!std::is_trivially_move_assignable_v<raw_ref<int>>);
  64. static_assert(!std::is_trivially_copy_constructible_v<raw_ref<int>>);
  65. static_assert(!std::is_trivially_copy_assignable_v<raw_ref<int>>);
  66. // A raw_ref can be copied or moved.
  67. static_assert(std::is_move_constructible_v<raw_ref<int>>);
  68. static_assert(std::is_copy_constructible_v<raw_ref<int>>);
  69. static_assert(std::is_move_assignable_v<raw_ref<int>>);
  70. static_assert(std::is_copy_assignable_v<raw_ref<int>>);
  71. // A SubClass can be converted to a BaseClass.
  72. static_assert(std::is_constructible_v<raw_ref<BaseClass>, raw_ref<SubClass>>);
  73. static_assert(
  74. std::is_constructible_v<raw_ref<BaseClass>, const raw_ref<SubClass>&>);
  75. static_assert(std::is_constructible_v<raw_ref<BaseClass>, raw_ref<SubClass>&&>);
  76. static_assert(std::is_assignable_v<raw_ref<BaseClass>, raw_ref<SubClass>>);
  77. static_assert(
  78. std::is_assignable_v<raw_ref<BaseClass>, const raw_ref<SubClass>&>);
  79. static_assert(std::is_assignable_v<raw_ref<BaseClass>, raw_ref<SubClass>&&>);
  80. // A BaseClass can't be implicitly downcasted.
  81. static_assert(!std::is_constructible_v<raw_ref<SubClass>, raw_ref<BaseClass>>);
  82. static_assert(
  83. !std::is_constructible_v<raw_ref<SubClass>, const raw_ref<BaseClass>&>);
  84. static_assert(
  85. !std::is_constructible_v<raw_ref<SubClass>, raw_ref<BaseClass>&&>);
  86. static_assert(!std::is_assignable_v<raw_ref<SubClass>, raw_ref<BaseClass>>);
  87. static_assert(
  88. !std::is_assignable_v<raw_ref<SubClass>, const raw_ref<BaseClass>&>);
  89. static_assert(!std::is_assignable_v<raw_ref<SubClass>, raw_ref<BaseClass>&&>);
  90. // A raw_ref<BaseClass> can be constructed directly from a SubClass.
  91. static_assert(std::is_constructible_v<raw_ref<BaseClass>, SubClass&>);
  92. static_assert(std::is_assignable_v<raw_ref<BaseClass>, SubClass&>);
  93. static_assert(std::is_constructible_v<raw_ref<const BaseClass>, SubClass&>);
  94. static_assert(std::is_assignable_v<raw_ref<const BaseClass>, SubClass&>);
  95. static_assert(
  96. std::is_constructible_v<raw_ref<const BaseClass>, const SubClass&>);
  97. static_assert(std::is_assignable_v<raw_ref<const BaseClass>, const SubClass&>);
  98. // But a raw_ref<SubClass> can't be constructed from an implicit downcast from a
  99. // BaseClass.
  100. static_assert(!std::is_constructible_v<raw_ref<SubClass>, BaseClass&>);
  101. static_assert(!std::is_assignable_v<raw_ref<SubClass>, BaseClass&>);
  102. static_assert(!std::is_constructible_v<raw_ref<const SubClass>, BaseClass&>);
  103. static_assert(!std::is_assignable_v<raw_ref<const SubClass>, BaseClass&>);
  104. static_assert(
  105. !std::is_constructible_v<raw_ref<const SubClass>, const BaseClass&>);
  106. static_assert(!std::is_assignable_v<raw_ref<const SubClass>, const BaseClass&>);
  107. // A mutable reference can be converted to const reference.
  108. static_assert(std::is_constructible_v<raw_ref<const int>, raw_ref<int>>);
  109. static_assert(std::is_assignable_v<raw_ref<const int>, raw_ref<int>>);
  110. // A const reference can't be converted to mutable.
  111. static_assert(!std::is_constructible_v<raw_ref<int>, raw_ref<const int>>);
  112. static_assert(!std::is_assignable_v<raw_ref<int>, raw_ref<const int>>);
  113. // The deref operator gives the internal reference.
  114. static_assert(std::is_same_v<int&, decltype(*std::declval<raw_ref<int>>())>);
  115. static_assert(
  116. std::is_same_v<int&, decltype(*std::declval<const raw_ref<int>>())>);
  117. static_assert(std::is_same_v<int&, decltype(*std::declval<raw_ref<int>&>())>);
  118. static_assert(
  119. std::is_same_v<int&, decltype(*std::declval<const raw_ref<int>&>())>);
  120. static_assert(std::is_same_v<int&, decltype(*std::declval<raw_ref<int>&&>())>);
  121. static_assert(
  122. std::is_same_v<int&, decltype(*std::declval<const raw_ref<int>&&>())>);
  123. // A const T is always returned as const.
  124. static_assert(
  125. std::is_same_v<const int&, decltype(*std::declval<raw_ref<const int>>())>);
  126. // The arrow operator gives a (non-null) pointer to the internal reference.
  127. static_assert(
  128. std::is_same_v<int*, decltype(std::declval<raw_ref<int>>().operator->())>);
  129. static_assert(
  130. std::is_same_v<const int*,
  131. decltype(std::declval<raw_ref<const int>>().operator->())>);
  132. TEST(RawRef, Construct) {
  133. int i = 1;
  134. auto r = raw_ref<int>(i);
  135. EXPECT_EQ(&*r, &i);
  136. auto cr = raw_ref<const int>(i);
  137. EXPECT_EQ(&*cr, &i);
  138. const int ci = 1;
  139. auto cci = raw_ref<const int>(ci);
  140. EXPECT_EQ(&*cci, &ci);
  141. }
  142. TEST(RawRef, CopyConstruct) {
  143. {
  144. int i = 1;
  145. auto r = raw_ref<int>(i);
  146. EXPECT_EQ(&*r, &i);
  147. auto r2 = raw_ref<int>(r);
  148. EXPECT_EQ(&*r2, &i);
  149. }
  150. {
  151. int i = 1;
  152. auto r = raw_ref<const int>(i);
  153. EXPECT_EQ(&*r, &i);
  154. auto r2 = raw_ref<const int>(r);
  155. EXPECT_EQ(&*r2, &i);
  156. }
  157. }
  158. TEST(RawRefDeathTest, CopyConstructAfterMove) {
  159. int i = 1;
  160. auto r = raw_ref<int>(i);
  161. auto r2 = std::move(r);
  162. EXPECT_CHECK_DEATH({ [[maybe_unused]] auto r3 = r; });
  163. }
  164. TEST(RawRef, MoveConstruct) {
  165. {
  166. int i = 1;
  167. auto r = raw_ref<int>(i);
  168. EXPECT_EQ(&*r, &i);
  169. auto r2 = raw_ref<int>(std::move(r));
  170. EXPECT_EQ(&*r2, &i);
  171. }
  172. {
  173. int i = 1;
  174. auto r = raw_ref<const int>(i);
  175. EXPECT_EQ(&*r, &i);
  176. auto r2 = raw_ref<const int>(std::move(r));
  177. EXPECT_EQ(&*r2, &i);
  178. }
  179. }
  180. TEST(RawRefDeathTest, MoveConstructAfterMove) {
  181. int i = 1;
  182. auto r = raw_ref<int>(i);
  183. auto r2 = std::move(r);
  184. EXPECT_CHECK_DEATH({ [[maybe_unused]] auto r3 = std::move(r); });
  185. }
  186. TEST(RawRef, CopyAssign) {
  187. {
  188. int i = 1;
  189. auto r = raw_ref<int>(i);
  190. EXPECT_EQ(&*r, &i);
  191. int j = 2;
  192. auto rj = raw_ref<int>(j);
  193. r = rj;
  194. EXPECT_EQ(&*r, &j);
  195. }
  196. {
  197. int i = 1;
  198. auto r = raw_ref<const int>(i);
  199. EXPECT_EQ(&*r, &i);
  200. int j = 2;
  201. auto rj = raw_ref<const int>(j);
  202. r = rj;
  203. EXPECT_EQ(&*r, &j);
  204. }
  205. {
  206. int i = 1;
  207. auto r = raw_ref<const int>(i);
  208. EXPECT_EQ(&*r, &i);
  209. int j = 2;
  210. auto rj = raw_ref<int>(j);
  211. r = rj;
  212. EXPECT_EQ(&*r, &j);
  213. }
  214. }
  215. TEST(RawRefDeathTest, CopyAssignAfterMove) {
  216. int i = 1;
  217. auto r = raw_ref<int>(i);
  218. auto r2 = std::move(r);
  219. EXPECT_CHECK_DEATH({ r2 = r; });
  220. }
  221. TEST(RawRef, CopyReassignAfterMove) {
  222. int i = 1;
  223. auto r = raw_ref<int>(i);
  224. auto r2 = std::move(r);
  225. int j = 1;
  226. r2 = raw_ref<int>(j);
  227. // Reassign to the moved-from `r` so it can be used again.
  228. r = r2;
  229. EXPECT_EQ(&*r, &j);
  230. }
  231. TEST(RawRef, MoveAssign) {
  232. {
  233. int i = 1;
  234. auto r = raw_ref<int>(i);
  235. EXPECT_EQ(&*r, &i);
  236. int j = 2;
  237. r = raw_ref<int>(j);
  238. EXPECT_EQ(&*r, &j);
  239. }
  240. {
  241. int i = 1;
  242. auto r = raw_ref<const int>(i);
  243. EXPECT_EQ(&*r, &i);
  244. int j = 2;
  245. r = raw_ref<const int>(j);
  246. EXPECT_EQ(&*r, &j);
  247. }
  248. {
  249. int i = 1;
  250. auto r = raw_ref<const int>(i);
  251. EXPECT_EQ(&*r, &i);
  252. int j = 2;
  253. r = raw_ref<int>(j);
  254. EXPECT_EQ(&*r, &j);
  255. }
  256. }
  257. TEST(RawRefDeathTest, MoveAssignAfterMove) {
  258. int i = 1;
  259. auto r = raw_ref<int>(i);
  260. auto r2 = std::move(r);
  261. EXPECT_CHECK_DEATH({ r2 = std::move(r); });
  262. }
  263. TEST(RawRef, MoveReassignAfterMove) {
  264. int i = 1;
  265. auto r = raw_ref<int>(i);
  266. auto r2 = std::move(r);
  267. int j = 1;
  268. // Reassign to the moved-from `r` so it can be used again.
  269. r = raw_ref<int>(j);
  270. EXPECT_EQ(&*r, &j);
  271. }
  272. TEST(RawRef, CopyConstructUpCast) {
  273. {
  274. auto s = SubClass();
  275. auto r = raw_ref<SubClass>(s);
  276. EXPECT_EQ(&*r, &s);
  277. auto r2 = raw_ref<BaseClass>(r);
  278. EXPECT_EQ(&*r2, &s);
  279. }
  280. {
  281. auto s = SubClass();
  282. auto r = raw_ref<const SubClass>(s);
  283. EXPECT_EQ(&*r, &s);
  284. auto r2 = raw_ref<const BaseClass>(r);
  285. EXPECT_EQ(&*r2, &s);
  286. }
  287. }
  288. TEST(RawRefDeathTest, CopyConstructAfterMoveUpCast) {
  289. auto s = SubClass();
  290. auto r = raw_ref<SubClass>(s);
  291. auto moved = std::move(r);
  292. EXPECT_CHECK_DEATH({ [[maybe_unused]] auto r2 = raw_ref<BaseClass>(r); });
  293. }
  294. TEST(RawRef, MoveConstructUpCast) {
  295. {
  296. auto s = SubClass();
  297. auto r = raw_ref<SubClass>(s);
  298. EXPECT_EQ(&*r, &s);
  299. auto r2 = raw_ref<BaseClass>(std::move(r));
  300. EXPECT_EQ(&*r2, &s);
  301. }
  302. {
  303. auto s = SubClass();
  304. auto r = raw_ref<const SubClass>(s);
  305. EXPECT_EQ(&*r, &s);
  306. auto r2 = raw_ref<const BaseClass>(std::move(r));
  307. EXPECT_EQ(&*r2, &s);
  308. }
  309. }
  310. TEST(RawRefDeathTest, MoveConstructAfterMoveUpCast) {
  311. auto s = SubClass();
  312. auto r = raw_ref<SubClass>(s);
  313. auto moved = std::move(r);
  314. EXPECT_CHECK_DEATH(
  315. { [[maybe_unused]] auto r2 = raw_ref<BaseClass>(std::move(r)); });
  316. }
  317. TEST(RawRef, CopyAssignUpCast) {
  318. {
  319. auto s = SubClass();
  320. auto r = raw_ref<SubClass>(s);
  321. auto t = BaseClass();
  322. auto rt = raw_ref<BaseClass>(t);
  323. rt = r;
  324. EXPECT_EQ(&*rt, &s);
  325. }
  326. {
  327. auto s = SubClass();
  328. auto r = raw_ref<const SubClass>(s);
  329. auto t = BaseClass();
  330. auto rt = raw_ref<const BaseClass>(t);
  331. rt = r;
  332. EXPECT_EQ(&*rt, &s);
  333. }
  334. {
  335. auto s = SubClass();
  336. auto r = raw_ref<SubClass>(s);
  337. auto t = BaseClass();
  338. auto rt = raw_ref<const BaseClass>(t);
  339. rt = r;
  340. EXPECT_EQ(&*rt, &s);
  341. }
  342. }
  343. TEST(RawRefDeathTest, CopyAssignAfterMoveUpCast) {
  344. auto s = SubClass();
  345. auto r = raw_ref<const SubClass>(s);
  346. auto t = BaseClass();
  347. auto rt = raw_ref<const BaseClass>(t);
  348. auto moved = std::move(r);
  349. EXPECT_CHECK_DEATH({ rt = r; });
  350. }
  351. TEST(RawRef, MoveAssignUpCast) {
  352. {
  353. auto s = SubClass();
  354. auto r = raw_ref<SubClass>(s);
  355. auto t = BaseClass();
  356. auto rt = raw_ref<BaseClass>(t);
  357. rt = std::move(r);
  358. EXPECT_EQ(&*rt, &s);
  359. }
  360. {
  361. auto s = SubClass();
  362. auto r = raw_ref<const SubClass>(s);
  363. auto t = BaseClass();
  364. auto rt = raw_ref<const BaseClass>(t);
  365. rt = std::move(r);
  366. EXPECT_EQ(&*rt, &s);
  367. }
  368. {
  369. auto s = SubClass();
  370. auto r = raw_ref<SubClass>(s);
  371. auto t = BaseClass();
  372. auto rt = raw_ref<const BaseClass>(t);
  373. rt = std::move(r);
  374. EXPECT_EQ(&*rt, &s);
  375. }
  376. }
  377. TEST(RawRefDeathTest, MoveAssignAfterMoveUpCast) {
  378. auto s = SubClass();
  379. auto r = raw_ref<const SubClass>(s);
  380. auto t = BaseClass();
  381. auto rt = raw_ref<const BaseClass>(t);
  382. auto moved = std::move(r);
  383. EXPECT_CHECK_DEATH({ rt = std::move(r); });
  384. }
  385. TEST(RawRef, Deref) {
  386. int i;
  387. auto r = raw_ref<int>(i);
  388. EXPECT_EQ(&*r, &i);
  389. }
  390. TEST(RawRefDeathTest, DerefAfterMove) {
  391. int i;
  392. auto r = raw_ref<int>(i);
  393. auto moved = std::move(r);
  394. EXPECT_CHECK_DEATH({ r.operator*(); });
  395. }
  396. TEST(RawRef, Arrow) {
  397. int i;
  398. auto r = raw_ref<int>(i);
  399. EXPECT_EQ(r.operator->(), &i);
  400. }
  401. TEST(RawRefDeathTest, ArrowAfterMove) {
  402. int i;
  403. auto r = raw_ref<int>(i);
  404. auto moved = std::move(r);
  405. EXPECT_CHECK_DEATH({ r.operator->(); });
  406. }
  407. TEST(RawRef, Swap) {
  408. int i;
  409. auto ri = raw_ref<int>(i);
  410. int j;
  411. auto rj = raw_ref<int>(j);
  412. swap(ri, rj);
  413. EXPECT_EQ(&*ri, &j);
  414. EXPECT_EQ(&*rj, &i);
  415. }
  416. TEST(RawRefDeathTest, SwapAfterMove) {
  417. {
  418. int i;
  419. auto ri = raw_ref<int>(i);
  420. int j;
  421. auto rj = raw_ref<int>(j);
  422. auto moved = std::move(ri);
  423. EXPECT_CHECK_DEATH({ swap(ri, rj); });
  424. }
  425. {
  426. int i;
  427. auto ri = raw_ref<int>(i);
  428. int j;
  429. auto rj = raw_ref<int>(j);
  430. auto moved = std::move(rj);
  431. EXPECT_CHECK_DEATH({ swap(ri, rj); });
  432. }
  433. }
  434. TEST(RawRef, Equals) {
  435. int i = 1;
  436. auto r1 = raw_ref<int>(i);
  437. auto r2 = raw_ref<int>(i);
  438. EXPECT_TRUE(r1 == r1);
  439. EXPECT_TRUE(r1 == r2);
  440. EXPECT_TRUE(r1 == i);
  441. EXPECT_TRUE(i == r1);
  442. int j = 1;
  443. auto r3 = raw_ref<int>(j);
  444. EXPECT_FALSE(r1 == r3);
  445. EXPECT_FALSE(r1 == j);
  446. EXPECT_FALSE(j == r1);
  447. }
  448. TEST(RawRefDeathTest, EqualsAfterMove) {
  449. {
  450. int i = 1;
  451. auto r1 = raw_ref<int>(i);
  452. auto r2 = raw_ref<int>(i);
  453. auto moved = std::move(r1);
  454. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 == r2; });
  455. }
  456. {
  457. int i = 1;
  458. auto r1 = raw_ref<int>(i);
  459. auto r2 = raw_ref<int>(i);
  460. auto moved = std::move(r2);
  461. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 == r2; });
  462. }
  463. {
  464. int i = 1;
  465. auto r1 = raw_ref<int>(i);
  466. auto moved = std::move(r1);
  467. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 == r1; });
  468. }
  469. }
  470. TEST(RawRef, NotEquals) {
  471. int i = 1;
  472. auto r1 = raw_ref<int>(i);
  473. int j = 1;
  474. auto r2 = raw_ref<int>(j);
  475. EXPECT_TRUE(r1 != r2);
  476. EXPECT_TRUE(r1 != j);
  477. EXPECT_TRUE(j != r1);
  478. EXPECT_FALSE(r1 != r1);
  479. EXPECT_FALSE(r2 != j);
  480. EXPECT_FALSE(j != r2);
  481. }
  482. TEST(RawRefDeathTest, NotEqualsAfterMove) {
  483. {
  484. int i = 1;
  485. auto r1 = raw_ref<int>(i);
  486. auto r2 = raw_ref<int>(i);
  487. auto moved = std::move(r1);
  488. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 != r2; });
  489. }
  490. {
  491. int i = 1;
  492. auto r1 = raw_ref<int>(i);
  493. auto r2 = raw_ref<int>(i);
  494. auto moved = std::move(r2);
  495. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 != r2; });
  496. }
  497. {
  498. int i = 1;
  499. auto r1 = raw_ref<int>(i);
  500. auto moved = std::move(r1);
  501. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 != r1; });
  502. }
  503. }
  504. TEST(RawRef, LessThan) {
  505. int i[] = {1, 1};
  506. auto r1 = raw_ref<int>(i[0]);
  507. auto r2 = raw_ref<int>(i[1]);
  508. EXPECT_TRUE(r1 < r2);
  509. EXPECT_TRUE(r1 < i[1]);
  510. EXPECT_FALSE(i[1] < r1);
  511. EXPECT_FALSE(r2 < r1);
  512. EXPECT_FALSE(r2 < i[0]);
  513. EXPECT_TRUE(i[0] < r2);
  514. EXPECT_FALSE(r1 < r1);
  515. EXPECT_FALSE(r1 < i[0]);
  516. EXPECT_FALSE(i[0] < r1);
  517. }
  518. TEST(RawRefDeathTest, LessThanAfterMove) {
  519. {
  520. int i = 1;
  521. auto r1 = raw_ref<int>(i);
  522. auto r2 = raw_ref<int>(i);
  523. auto moved = std::move(r1);
  524. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 < r2; });
  525. }
  526. {
  527. int i = 1;
  528. auto r1 = raw_ref<int>(i);
  529. auto r2 = raw_ref<int>(i);
  530. auto moved = std::move(r2);
  531. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 < r2; });
  532. }
  533. {
  534. int i = 1;
  535. auto r1 = raw_ref<int>(i);
  536. auto moved = std::move(r1);
  537. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 < r1; });
  538. }
  539. }
  540. TEST(RawRef, GreaterThan) {
  541. int i[] = {1, 1};
  542. auto r1 = raw_ref<int>(i[0]);
  543. auto r2 = raw_ref<int>(i[1]);
  544. EXPECT_TRUE(r2 > r1);
  545. EXPECT_FALSE(r1 > r2);
  546. EXPECT_FALSE(r1 > i[1]);
  547. EXPECT_TRUE(i[1] > r1);
  548. EXPECT_FALSE(r2 > r2);
  549. EXPECT_FALSE(r2 > i[1]);
  550. EXPECT_FALSE(i[1] > r2);
  551. }
  552. TEST(RawRefDeathTest, GreaterThanAfterMove) {
  553. {
  554. int i = 1;
  555. auto r1 = raw_ref<int>(i);
  556. auto r2 = raw_ref<int>(i);
  557. auto moved = std::move(r1);
  558. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 > r2; });
  559. }
  560. {
  561. int i = 1;
  562. auto r1 = raw_ref<int>(i);
  563. auto r2 = raw_ref<int>(i);
  564. auto moved = std::move(r2);
  565. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 > r2; });
  566. }
  567. {
  568. int i = 1;
  569. auto r1 = raw_ref<int>(i);
  570. auto moved = std::move(r1);
  571. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 > r1; });
  572. }
  573. }
  574. TEST(RawRef, LessThanOrEqual) {
  575. int i[] = {1, 1};
  576. auto r1 = raw_ref<int>(i[0]);
  577. auto r2 = raw_ref<int>(i[1]);
  578. EXPECT_TRUE(r1 <= r2);
  579. EXPECT_TRUE(r1 <= r1);
  580. EXPECT_TRUE(r2 <= r2);
  581. EXPECT_FALSE(r2 <= r1);
  582. EXPECT_TRUE(r1 <= i[1]);
  583. EXPECT_TRUE(r1 <= i[0]);
  584. EXPECT_TRUE(r2 <= i[1]);
  585. EXPECT_FALSE(r2 <= i[0]);
  586. EXPECT_FALSE(i[1] <= r1);
  587. EXPECT_TRUE(i[0] <= r1);
  588. EXPECT_TRUE(i[1] <= r2);
  589. EXPECT_TRUE(i[0] <= r2);
  590. }
  591. TEST(RawRefDeathTest, LessThanOrEqualAfterMove) {
  592. {
  593. int i = 1;
  594. auto r1 = raw_ref<int>(i);
  595. auto r2 = raw_ref<int>(i);
  596. auto moved = std::move(r1);
  597. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 <= r2; });
  598. }
  599. {
  600. int i = 1;
  601. auto r1 = raw_ref<int>(i);
  602. auto r2 = raw_ref<int>(i);
  603. auto moved = std::move(r2);
  604. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 <= r2; });
  605. }
  606. {
  607. int i = 1;
  608. auto r1 = raw_ref<int>(i);
  609. auto moved = std::move(r1);
  610. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 <= r1; });
  611. }
  612. }
  613. TEST(RawRef, GreaterThanOrEqual) {
  614. int i[] = {1, 1};
  615. auto r1 = raw_ref<int>(i[0]);
  616. auto r2 = raw_ref<int>(i[1]);
  617. EXPECT_TRUE(r2 >= r1);
  618. EXPECT_TRUE(r1 >= r1);
  619. EXPECT_TRUE(r2 >= r2);
  620. EXPECT_FALSE(r1 >= r2);
  621. EXPECT_TRUE(r2 >= i[0]);
  622. EXPECT_TRUE(r1 >= i[0]);
  623. EXPECT_TRUE(r2 >= i[1]);
  624. EXPECT_FALSE(r1 >= i[1]);
  625. EXPECT_FALSE(i[0] >= r2);
  626. EXPECT_TRUE(i[0] >= r1);
  627. EXPECT_TRUE(i[1] >= r2);
  628. EXPECT_TRUE(i[1] >= r1);
  629. }
  630. TEST(RawRefDeathTest, GreaterThanOrEqualAfterMove) {
  631. {
  632. int i = 1;
  633. auto r1 = raw_ref<int>(i);
  634. auto r2 = raw_ref<int>(i);
  635. auto moved = std::move(r1);
  636. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 >= r2; });
  637. }
  638. {
  639. int i = 1;
  640. auto r1 = raw_ref<int>(i);
  641. auto r2 = raw_ref<int>(i);
  642. auto moved = std::move(r2);
  643. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 >= r2; });
  644. }
  645. {
  646. int i = 1;
  647. auto r1 = raw_ref<int>(i);
  648. auto moved = std::move(r1);
  649. EXPECT_CHECK_DEATH({ [[maybe_unused]] bool b = r1 >= r1; });
  650. }
  651. }
  652. TEST(RawRef, CTAD) {
  653. int i = 1;
  654. auto r = raw_ref(i);
  655. EXPECT_EQ(&*r, &i);
  656. }
  657. using RawPtrCountingImpl =
  658. base::internal::RawPtrCountingImplWrapperForTest<base::DefaultRawPtrImpl>;
  659. template <typename T>
  660. using CountingRawRef = raw_ref<T, RawPtrCountingImpl>;
  661. TEST(RawRef, StdLess) {
  662. int i[] = {1, 1};
  663. {
  664. RawPtrCountingImpl::ClearCounters();
  665. auto r1 = CountingRawRef<int>(i[0]);
  666. auto r2 = CountingRawRef<int>(i[1]);
  667. EXPECT_TRUE(std::less<CountingRawRef<int>>()(r1, r2));
  668. EXPECT_FALSE(std::less<CountingRawRef<int>>()(r2, r1));
  669. EXPECT_EQ(2, RawPtrCountingImpl::wrapped_ptr_less_cnt);
  670. }
  671. {
  672. RawPtrCountingImpl::ClearCounters();
  673. const auto r1 = CountingRawRef<int>(i[0]);
  674. const auto r2 = CountingRawRef<int>(i[1]);
  675. EXPECT_TRUE(std::less<CountingRawRef<int>>()(r1, r2));
  676. EXPECT_FALSE(std::less<CountingRawRef<int>>()(r2, r1));
  677. EXPECT_EQ(2, RawPtrCountingImpl::wrapped_ptr_less_cnt);
  678. }
  679. {
  680. RawPtrCountingImpl::ClearCounters();
  681. auto r1 = CountingRawRef<const int>(i[0]);
  682. auto r2 = CountingRawRef<const int>(i[1]);
  683. EXPECT_TRUE(std::less<CountingRawRef<const int>>()(r1, r2));
  684. EXPECT_FALSE(std::less<CountingRawRef<const int>>()(r2, r1));
  685. EXPECT_EQ(2, RawPtrCountingImpl::wrapped_ptr_less_cnt);
  686. }
  687. {
  688. RawPtrCountingImpl::ClearCounters();
  689. auto r1 = CountingRawRef<int>(i[0]);
  690. auto r2 = CountingRawRef<int>(i[1]);
  691. EXPECT_TRUE(std::less<CountingRawRef<int>>()(r1, i[1]));
  692. EXPECT_FALSE(std::less<CountingRawRef<int>>()(r2, i[0]));
  693. EXPECT_EQ(2, RawPtrCountingImpl::wrapped_ptr_less_cnt);
  694. }
  695. {
  696. RawPtrCountingImpl::ClearCounters();
  697. const auto r1 = CountingRawRef<int>(i[0]);
  698. const auto r2 = CountingRawRef<int>(i[1]);
  699. EXPECT_TRUE(std::less<CountingRawRef<int>>()(r1, i[1]));
  700. EXPECT_FALSE(std::less<CountingRawRef<int>>()(r2, i[0]));
  701. EXPECT_EQ(2, RawPtrCountingImpl::wrapped_ptr_less_cnt);
  702. }
  703. {
  704. RawPtrCountingImpl::ClearCounters();
  705. auto r1 = CountingRawRef<const int>(i[0]);
  706. auto r2 = CountingRawRef<const int>(i[1]);
  707. EXPECT_TRUE(std::less<CountingRawRef<const int>>()(r1, i[1]));
  708. EXPECT_FALSE(std::less<CountingRawRef<const int>>()(r2, i[0]));
  709. EXPECT_EQ(2, RawPtrCountingImpl::wrapped_ptr_less_cnt);
  710. }
  711. }
  712. } // namespace