| 8 | |
| 9 | Y_UNIT_TEST_SUITE(TSmartPtr) { |
| 10 | Y_UNIT_TEST(TUsageTest) { |
| 11 | //validate smart ptr |
| 12 | typedef TSharedPtrB<int> TSharedInt; |
| 13 | typedef TWeakPtrB<int> TWeakInt; |
| 14 | |
| 15 | TWeakInt w01; |
| 16 | TWeakInt w02; |
| 17 | |
| 18 | UNIT_ASSERT(w01.UseCount() == 0); |
| 19 | |
| 20 | { |
| 21 | TSharedInt s1(new int(42)); |
| 22 | |
| 23 | UNIT_ASSERT(s1.UseCount() == 1); |
| 24 | { |
| 25 | TSharedInt s2(s1); |
| 26 | |
| 27 | UNIT_ASSERT(s1.UseCount() == 2); |
| 28 | UNIT_ASSERT(s2.UseCount() == 2); |
| 29 | |
| 30 | TWeakInt w1(s1); |
| 31 | TWeakInt w2 = s2; |
| 32 | |
| 33 | UNIT_ASSERT(s1.UseCount() == 2); |
| 34 | UNIT_ASSERT(w1.UseCount() == 2); |
| 35 | |
| 36 | TSharedInt s3(w1); |
| 37 | |
| 38 | UNIT_ASSERT(s3.UseCount() == 3); |
| 39 | UNIT_ASSERT(s3.Get() != (int*)nullptr && *s3 == 42); |
| 40 | |
| 41 | { |
| 42 | TSharedInt s4 = w1; |
| 43 | UNIT_ASSERT(s4.UseCount() == 4); |
| 44 | |
| 45 | w01 = s4; |
| 46 | w02 = w1; |
| 47 | } |
| 48 | |
| 49 | UNIT_ASSERT(w01.UseCount() == 3); |
| 50 | UNIT_ASSERT(*s3 == 42); |
| 51 | |
| 52 | s3 = TSharedInt(); |
| 53 | |
| 54 | UNIT_ASSERT(s3.UseCount() == 0); |
| 55 | UNIT_ASSERT(w1.UseCount() == 2); |
| 56 | |
| 57 | s3.Swap(s2); |
| 58 | |
| 59 | UNIT_ASSERT(s3.UseCount() == 2); |
| 60 | UNIT_ASSERT(s2.UseCount() == 0); |
| 61 | |
| 62 | s3.Reset(); |
| 63 | |
| 64 | UNIT_ASSERT(s3.UseCount() == 0); |
| 65 | UNIT_ASSERT(s1.UseCount() == 1); |
| 66 | UNIT_ASSERT(w01.UseCount() == 1); |
| 67 | } |