intrusive_ptr_ut.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. #include <library/cpp/testing/gtest/gtest.h>
  2. #include <library/cpp/yt/memory/new.h>
  3. #include <library/cpp/yt/memory/ref_counted.h>
  4. #include <library/cpp/yt/memory/serialize.h>
  5. #include <util/generic/buffer.h>
  6. #include <util/stream/buffer.h>
  7. #include <util/ysaveload.h>
  8. namespace NYT {
  9. namespace {
  10. ////////////////////////////////////////////////////////////////////////////////
  11. using ::testing::IsNull;
  12. using ::testing::NotNull;
  13. using ::testing::InSequence;
  14. using ::testing::MockFunction;
  15. using ::testing::StrictMock;
  16. ////////////////////////////////////////////////////////////////////////////////
  17. // Auxiliary types and functions.
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // This object tracks number of increments and decrements
  20. // to the reference counter (see traits specialization below).
  21. struct TIntricateObject
  22. : private TNonCopyable
  23. {
  24. int Increments = 0;
  25. int Decrements = 0;
  26. int Zeros = 0;
  27. void Ref()
  28. {
  29. ++Increments;
  30. }
  31. void Unref()
  32. {
  33. ++Decrements;
  34. if (Increments == Decrements) {
  35. ++Zeros;
  36. }
  37. }
  38. };
  39. using TIntricateObjectPtr = TIntrusivePtr<TIntricateObject>;
  40. void Ref(TIntricateObject* obj, int /*n*/ = 1)
  41. {
  42. obj->Ref();
  43. }
  44. void Unref(TIntricateObject* obj, int /*n*/ = 1)
  45. {
  46. obj->Unref();
  47. }
  48. MATCHER_P3(HasRefCounts, increments, decrements, zeros,
  49. "Reference counter " \
  50. "was incremented " + ::testing::PrintToString(increments) + " times, " +
  51. "was decremented " + ::testing::PrintToString(decrements) + " times, " +
  52. "vanished to zero " + ::testing::PrintToString(zeros) + " times")
  53. {
  54. Y_UNUSED(result_listener);
  55. return
  56. arg.Increments == increments &&
  57. arg.Decrements == decrements &&
  58. arg.Zeros == zeros;
  59. }
  60. void PrintTo(const TIntricateObject& arg, ::std::ostream* os)
  61. {
  62. *os << arg.Increments << " increments, "
  63. << arg.Decrements << " decrements and "
  64. << arg.Zeros << " times vanished";
  65. }
  66. // This is an object which creates intrusive pointers to the self
  67. // during its construction.
  68. class TObjectWithSelfPointers
  69. : public TRefCounted
  70. {
  71. public:
  72. explicit TObjectWithSelfPointers(IOutputStream* output)
  73. : Output_(output)
  74. {
  75. *Output_ << "Cb";
  76. for (int i = 0; i < 3; ++i) {
  77. *Output_ << '!';
  78. TIntrusivePtr<TObjectWithSelfPointers> ptr(this);
  79. }
  80. *Output_ << "Ca";
  81. }
  82. virtual ~TObjectWithSelfPointers()
  83. {
  84. *Output_ << 'D';
  85. }
  86. private:
  87. IOutputStream* const Output_;
  88. };
  89. // This is a simple object with simple reference counting.
  90. class TObjectWithSimpleRC
  91. : public TRefCounted
  92. {
  93. public:
  94. explicit TObjectWithSimpleRC(IOutputStream* output)
  95. : Output_(output)
  96. {
  97. *Output_ << 'C';
  98. }
  99. virtual ~TObjectWithSimpleRC()
  100. {
  101. *Output_ << 'D';
  102. }
  103. void DoSomething()
  104. {
  105. *Output_ << '!';
  106. }
  107. private:
  108. IOutputStream* const Output_;
  109. };
  110. // This is a simple object with full-fledged reference counting.
  111. class TObjectWithFullRC
  112. : public TRefCounted
  113. {
  114. public:
  115. explicit TObjectWithFullRC(IOutputStream* output)
  116. : Output_(output)
  117. {
  118. *Output_ << 'C';
  119. }
  120. virtual ~TObjectWithFullRC()
  121. {
  122. *Output_ << 'D';
  123. }
  124. void DoSomething()
  125. {
  126. *Output_ << '!';
  127. }
  128. private:
  129. IOutputStream* const Output_;
  130. };
  131. class TObjectWithExceptionInConstructor
  132. : public TRefCounted
  133. {
  134. public:
  135. TObjectWithExceptionInConstructor()
  136. {
  137. throw int(1);
  138. }
  139. volatile int Number = 0;
  140. };
  141. ////////////////////////////////////////////////////////////////////////////////
  142. TEST(TIntrusivePtrTest, Empty)
  143. {
  144. TIntricateObjectPtr emptyPointer;
  145. EXPECT_EQ(nullptr, emptyPointer.Get());
  146. }
  147. TEST(TIntrusivePtrTest, Basic)
  148. {
  149. TIntricateObject object;
  150. EXPECT_THAT(object, HasRefCounts(0, 0, 0));
  151. {
  152. TIntricateObjectPtr owningPointer(&object);
  153. EXPECT_THAT(object, HasRefCounts(1, 0, 0));
  154. EXPECT_EQ(&object, owningPointer.Get());
  155. }
  156. EXPECT_THAT(object, HasRefCounts(1, 1, 1));
  157. {
  158. TIntricateObjectPtr nonOwningPointer(&object, false);
  159. EXPECT_THAT(object, HasRefCounts(1, 1, 1));
  160. EXPECT_EQ(&object, nonOwningPointer.Get());
  161. }
  162. EXPECT_THAT(object, HasRefCounts(1, 2, 1));
  163. }
  164. TEST(TIntrusivePtrTest, ResetToNull)
  165. {
  166. TIntricateObject object;
  167. TIntricateObjectPtr ptr(&object);
  168. EXPECT_THAT(object, HasRefCounts(1, 0, 0));
  169. EXPECT_EQ(&object, ptr.Get());
  170. ptr.Reset();
  171. EXPECT_THAT(object, HasRefCounts(1, 1, 1));
  172. EXPECT_EQ(nullptr, ptr.Get());
  173. }
  174. TEST(TIntrusivePtrTest, ResetToOtherObject)
  175. {
  176. TIntricateObject firstObject;
  177. TIntricateObject secondObject;
  178. TIntricateObjectPtr ptr(&firstObject);
  179. EXPECT_THAT(firstObject, HasRefCounts(1, 0, 0));
  180. EXPECT_THAT(secondObject, HasRefCounts(0, 0, 0));
  181. EXPECT_EQ(&firstObject, ptr.Get());
  182. ptr.Reset(&secondObject);
  183. EXPECT_THAT(firstObject, HasRefCounts(1, 1, 1));
  184. EXPECT_THAT(secondObject, HasRefCounts(1, 0, 0));
  185. EXPECT_EQ(&secondObject, ptr.Get());
  186. }
  187. TEST(TIntrusivePtrTest, CopySemantics)
  188. {
  189. TIntricateObject object;
  190. TIntricateObjectPtr foo(&object);
  191. EXPECT_THAT(object, HasRefCounts(1, 0, 0));
  192. {
  193. TIntricateObjectPtr bar(foo);
  194. EXPECT_THAT(object, HasRefCounts(2, 0, 0));
  195. EXPECT_EQ(&object, foo.Get());
  196. EXPECT_EQ(&object, bar.Get());
  197. }
  198. EXPECT_THAT(object, HasRefCounts(2, 1, 0));
  199. {
  200. TIntricateObjectPtr bar;
  201. bar = foo;
  202. EXPECT_THAT(object, HasRefCounts(3, 1, 0));
  203. EXPECT_EQ(&object, foo.Get());
  204. EXPECT_EQ(&object, bar.Get());
  205. }
  206. EXPECT_THAT(object, HasRefCounts(3, 2, 0));
  207. }
  208. TEST(TIntrusivePtrTest, MoveSemantics)
  209. {
  210. TIntricateObject object;
  211. TIntricateObjectPtr foo(&object);
  212. EXPECT_THAT(object, HasRefCounts(1, 0, 0));
  213. {
  214. TIntricateObjectPtr bar(std::move(foo));
  215. EXPECT_THAT(object, HasRefCounts(1, 0, 0));
  216. EXPECT_THAT(foo.Get(), IsNull());
  217. EXPECT_EQ(&object, bar.Get());
  218. }
  219. EXPECT_THAT(object, HasRefCounts(1, 1, 1));
  220. foo.Reset(&object);
  221. EXPECT_THAT(object, HasRefCounts(2, 1, 1));
  222. {
  223. TIntricateObjectPtr bar;
  224. bar = std::move(foo);
  225. EXPECT_THAT(object, HasRefCounts(2, 1, 1));
  226. EXPECT_THAT(foo.Get(), IsNull());
  227. EXPECT_EQ(&object, bar.Get());
  228. }
  229. }
  230. TEST(TIntrusivePtrTest, Swap)
  231. {
  232. TIntricateObject object;
  233. TIntricateObjectPtr foo(&object);
  234. TIntricateObjectPtr bar;
  235. EXPECT_THAT(object, HasRefCounts(1, 0, 0));
  236. EXPECT_THAT(foo.Get(), NotNull());
  237. EXPECT_THAT(bar.Get(), IsNull());
  238. foo.Swap(bar);
  239. EXPECT_THAT(object, HasRefCounts(1, 0, 0));
  240. EXPECT_THAT(foo.Get(), IsNull());
  241. EXPECT_THAT(bar.Get(), NotNull());
  242. foo.Swap(bar);
  243. EXPECT_THAT(object, HasRefCounts(1, 0, 0));
  244. EXPECT_THAT(foo.Get(), NotNull());
  245. EXPECT_THAT(bar.Get(), IsNull());
  246. }
  247. TEST(TIntrusivePtrTest, UpCast)
  248. {
  249. //! This is a simple typical reference-counted object.
  250. class TSimpleObject
  251. : public TRefCounted
  252. { };
  253. //! This is a simple inherited reference-counted object.
  254. class TAnotherObject
  255. : public TSimpleObject
  256. { };
  257. auto foo = New<TSimpleObject>();
  258. auto bar = New<TAnotherObject>();
  259. auto baz = New<TAnotherObject>();
  260. foo = baz;
  261. EXPECT_TRUE(foo == baz);
  262. }
  263. TEST(TIntrusivePtrTest, DownCast)
  264. {
  265. class TBaseObject
  266. : public TRefCounted
  267. { };
  268. class TDerivedObject
  269. : public TBaseObject
  270. { };
  271. //! This is a simple inherited reference-counted object.
  272. class TAnotherObject
  273. : public TBaseObject
  274. { };
  275. TIntrusivePtr<TBaseObject> foo = New<TDerivedObject>();
  276. TIntrusivePtr<TBaseObject> bar = New<TAnotherObject>();
  277. {
  278. auto baz = StaticPointerCast<TDerivedObject>(foo);
  279. EXPECT_TRUE(foo == baz);
  280. }
  281. {
  282. auto baz = StaticPointerCast<TDerivedObject>(TIntrusivePtr<TBaseObject>{foo});
  283. EXPECT_TRUE(foo == baz);
  284. }
  285. {
  286. auto baz = DynamicPointerCast<TDerivedObject>(foo);
  287. EXPECT_TRUE(foo == baz);
  288. }
  289. {
  290. auto baz = DynamicPointerCast<TDerivedObject>(bar);
  291. EXPECT_TRUE(nullptr == baz);
  292. }
  293. {
  294. auto baz = ConstPointerCast<const TBaseObject>(foo);
  295. EXPECT_TRUE(foo.Get() == baz.Get());
  296. }
  297. {
  298. auto baz = ConstPointerCast<const TBaseObject>(TIntrusivePtr<TBaseObject>{foo});
  299. EXPECT_TRUE(foo.Get() == baz.Get());
  300. }
  301. }
  302. TEST(TIntrusivePtrTest, UnspecifiedBoolType)
  303. {
  304. TIntricateObject object;
  305. TIntricateObjectPtr foo;
  306. TIntricateObjectPtr bar(&object);
  307. EXPECT_FALSE(foo);
  308. EXPECT_TRUE(bar);
  309. }
  310. TEST(TIntrusivePtrTest, ObjectIsNotDestroyedPrematurely)
  311. {
  312. TStringStream output;
  313. New<TObjectWithSelfPointers>(&output);
  314. // TObject... appends symbols to the output; see definitions.
  315. EXPECT_STREQ("Cb!!!CaD", output.Str().c_str());
  316. }
  317. TEST(TIntrusivePtrTest, EqualityOperator)
  318. {
  319. TIntricateObject object, anotherObject;
  320. TIntricateObjectPtr emptyPointer;
  321. TIntricateObjectPtr somePointer(&object);
  322. TIntricateObjectPtr samePointer(&object);
  323. TIntricateObjectPtr anotherPointer(&anotherObject);
  324. EXPECT_FALSE(somePointer == emptyPointer);
  325. EXPECT_FALSE(samePointer == emptyPointer);
  326. EXPECT_TRUE(somePointer != emptyPointer);
  327. EXPECT_TRUE(samePointer != emptyPointer);
  328. EXPECT_TRUE(somePointer == samePointer);
  329. EXPECT_TRUE(&object == somePointer);
  330. EXPECT_TRUE(&object == samePointer);
  331. EXPECT_FALSE(somePointer == anotherPointer);
  332. EXPECT_TRUE(somePointer != anotherPointer);
  333. EXPECT_TRUE(&anotherObject == anotherPointer);
  334. }
  335. TEST(TIntrusivePtrTest, Reset)
  336. {
  337. TIntricateObject object;
  338. TIntricateObjectPtr pointer(&object);
  339. EXPECT_THAT(object, HasRefCounts(1, 0, 0));
  340. EXPECT_EQ(&object, pointer.Release());
  341. EXPECT_THAT(object, HasRefCounts(1, 0, 0));
  342. }
  343. TEST(TIntrusivePtrTest, CompareWithNullptr)
  344. {
  345. TIntricateObjectPtr pointer1;
  346. EXPECT_TRUE(nullptr == pointer1);
  347. EXPECT_FALSE(nullptr != pointer1);
  348. TIntricateObject object;
  349. TIntricateObjectPtr pointer2(&object);
  350. EXPECT_TRUE(pointer2 != nullptr);
  351. EXPECT_FALSE(pointer2 == nullptr);
  352. }
  353. template <class T>
  354. void TestIntrusivePtrBehavior()
  355. {
  356. using TMyPtr = TIntrusivePtr<T>;
  357. TStringStream output;
  358. {
  359. TMyPtr ptr(New<T>(&output));
  360. {
  361. TMyPtr anotherPtr(ptr);
  362. anotherPtr->DoSomething();
  363. }
  364. {
  365. TMyPtr anotherPtr(ptr);
  366. anotherPtr->DoSomething();
  367. }
  368. ptr->DoSomething();
  369. }
  370. // TObject... appends symbols to the output; see definitions.
  371. EXPECT_STREQ("C!!!D", output.Str().c_str());
  372. }
  373. TEST(TIntrusivePtrTest, SimpleRCBehaviour)
  374. {
  375. TestIntrusivePtrBehavior<TObjectWithSimpleRC>();
  376. }
  377. TEST(TIntrusivePtrTest, FullRCBehaviour)
  378. {
  379. TestIntrusivePtrBehavior<TObjectWithFullRC>();
  380. }
  381. TEST(TIntrusivePtrTest, ObjectAlignment)
  382. {
  383. struct TObject
  384. : public TRefCounted
  385. {
  386. alignas(64) ui64 Data;
  387. };
  388. struct TPODObject final
  389. {
  390. alignas(64) ui64 Data;
  391. };
  392. auto foo = New<TObject>();
  393. auto bar = New<TPODObject>();
  394. EXPECT_TRUE(reinterpret_cast<uintptr_t>(foo.Get()) % 64 == 0);
  395. EXPECT_TRUE(reinterpret_cast<uintptr_t>(bar.Get()) % 64 == 0);
  396. }
  397. TEST(TIntrusivePtrTest, InitStruct)
  398. {
  399. struct TObj1 final
  400. {
  401. const int A;
  402. const int B;
  403. };
  404. New<TObj1>(1, 2);
  405. struct TExplicitObj final
  406. {
  407. explicit TExplicitObj(int a = 0)
  408. : A(a)
  409. { }
  410. const int A;
  411. };
  412. New<TExplicitObj>();
  413. New<TExplicitObj>(1);
  414. struct TObj2 final
  415. {
  416. TObj2(i64 a = 0)
  417. : A(a)
  418. { }
  419. const i64 A;
  420. };
  421. New<TObj2>(123);
  422. struct TObj3 final
  423. {
  424. TObj3(ui64 a = 0)
  425. : A(a)
  426. { }
  427. const ui64 A;
  428. };
  429. New<TObj3>(123);
  430. struct TObj4 final
  431. {
  432. TObj4(int a, ui64 b = 0)
  433. : A(a)
  434. , B(b)
  435. { }
  436. int A;
  437. const ui64 B;
  438. };
  439. New<TObj4>(123);
  440. New<TObj4>(123, 123);
  441. struct TObj5 final
  442. {
  443. TExplicitObj E;
  444. int B;
  445. };
  446. New<TObj5>();
  447. struct TObj6 final
  448. {
  449. TObj2 O;
  450. int B;
  451. };
  452. New<TObj6>();
  453. New<TObj6>(1, 2);
  454. }
  455. TEST(TIntrusivePtrTest, Serialize)
  456. {
  457. TBufferStream stream;
  458. struct TObject
  459. : public TRefCounted
  460. {
  461. ui64 Data;
  462. TObject()
  463. : Data(0)
  464. { }
  465. TObject(ui64 value)
  466. : Data(value)
  467. { }
  468. inline void Save(IOutputStream* out) const
  469. {
  470. ::Save(out, Data);
  471. }
  472. inline void Load(IInputStream* in)
  473. {
  474. ::Load(in, Data);
  475. }
  476. };
  477. ::Save(&stream, TIntrusivePtr<TObject>(nullptr));
  478. bool hasValue = true;
  479. ::Load(&stream, hasValue);
  480. EXPECT_FALSE(hasValue);
  481. EXPECT_EQ(stream.Buffer().Size(), 1ull);
  482. auto data = New<TObject>(42ull);
  483. ::Save(&stream, data);
  484. ::Load(&stream, hasValue);
  485. EXPECT_TRUE(hasValue);
  486. data->Data = 0;
  487. ::Load(&stream, data->Data);
  488. EXPECT_EQ(data->Data, 42ull);
  489. ::Save(&stream, data);
  490. TIntrusivePtr<TObject> ptr;
  491. ::Load(&stream, ptr);
  492. EXPECT_TRUE(ptr);
  493. EXPECT_EQ(data->Data, ptr->Data);
  494. ptr = nullptr;
  495. ::Save(&stream, TIntrusivePtr<TObject>(nullptr));
  496. ::Load(&stream, ptr);
  497. EXPECT_FALSE(ptr);
  498. }
  499. TEST(TIntrusivePtrTest, TestObjectConstructionFail)
  500. {
  501. ASSERT_THROW(New<TObjectWithExceptionInConstructor>(), int);
  502. }
  503. ////////////////////////////////////////////////////////////////////////////////
  504. } // namespace
  505. } // namespace NYT