Mike Klein | 0554d49 | 2017-12-06 12:27:44 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/core/SkVptr.h" |
| 9 | #include "tests/Test.h" |
Mike Klein | 0554d49 | 2017-12-06 12:27:44 -0500 | [diff] [blame] | 10 | |
| 11 | namespace { |
| 12 | |
| 13 | struct Base { |
| 14 | virtual ~Base() = default; |
| 15 | virtual size_t val() const = 0; |
| 16 | }; |
| 17 | |
| 18 | struct SubclassA : public Base { |
| 19 | SubclassA(size_t val) : fVal(val) {} |
| 20 | |
| 21 | size_t val() const override { return fVal; } |
| 22 | |
| 23 | size_t fVal; |
| 24 | }; |
| 25 | |
| 26 | struct SubclassB : public Base { |
| 27 | SubclassB() {} |
| 28 | |
| 29 | size_t val() const override { return 42; } |
| 30 | }; |
| 31 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 32 | } // namespace |
Mike Klein | 0554d49 | 2017-12-06 12:27:44 -0500 | [diff] [blame] | 33 | |
| 34 | DEF_TEST(Vptr, r) { |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 35 | std::unique_ptr<Base> a = std::make_unique<SubclassA>(21), |
| 36 | b = std::make_unique<SubclassB>(), |
| 37 | c = std::make_unique<SubclassA>(22), |
| 38 | d = std::make_unique<SubclassB>(); |
Mike Klein | 0554d49 | 2017-12-06 12:27:44 -0500 | [diff] [blame] | 39 | |
| 40 | // These 4 objects all have unique identities. |
| 41 | REPORTER_ASSERT(r, a != b); |
| 42 | REPORTER_ASSERT(r, a != c); |
| 43 | REPORTER_ASSERT(r, a != d); |
| 44 | REPORTER_ASSERT(r, b != c); |
| 45 | REPORTER_ASSERT(r, b != d); |
| 46 | REPORTER_ASSERT(r, c != d); |
| 47 | |
| 48 | // Only b and d have the same val(). |
| 49 | REPORTER_ASSERT(r, a->val() != b->val()); |
| 50 | REPORTER_ASSERT(r, a->val() != c->val()); |
| 51 | REPORTER_ASSERT(r, a->val() != d->val()); |
| 52 | REPORTER_ASSERT(r, b->val() != c->val()); |
| 53 | REPORTER_ASSERT(r, b->val() == d->val()); |
| 54 | REPORTER_ASSERT(r, c->val() != d->val()); |
| 55 | |
| 56 | // SkVptr() returns the same value for objects of the same concrete type. |
| 57 | REPORTER_ASSERT(r, SkVptr(*a) == SkVptr(*c)); |
| 58 | REPORTER_ASSERT(r, SkVptr(*b) == SkVptr(*d)); |
| 59 | REPORTER_ASSERT(r, SkVptr(*a) != SkVptr(*b)); |
| 60 | } |