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 8 #include "SkMakeUnique.h" 9 #include "SkVptr.h" 10 #include "Test.h" 11 12 namespace { 13 14 struct Base { 15 virtual ~Base() = default; 16 virtual size_t val() const = 0; 17 }; 18 19 struct SubclassA : public Base { 20 SubclassA(size_t val) : fVal(val) {} 21 22 size_t val() const override { return fVal; } 23 24 size_t fVal; 25 }; 26 27 struct SubclassB : public Base { 28 SubclassB() {} 29 30 size_t val() const override { return 42; } 31 }; 32 33 } 34 35 DEF_TEST(Vptr, r) { 36 std::unique_ptr<Base> a = skstd::make_unique<SubclassA>(21), 37 b = skstd::make_unique<SubclassB>(), 38 c = skstd::make_unique<SubclassA>(22), 39 d = skstd::make_unique<SubclassB>(); 40 41 // These 4 objects all have unique identities. 42 REPORTER_ASSERT(r, a != b); 43 REPORTER_ASSERT(r, a != c); 44 REPORTER_ASSERT(r, a != d); 45 REPORTER_ASSERT(r, b != c); 46 REPORTER_ASSERT(r, b != d); 47 REPORTER_ASSERT(r, c != d); 48 49 // Only b and d have the same val(). 50 REPORTER_ASSERT(r, a->val() != b->val()); 51 REPORTER_ASSERT(r, a->val() != c->val()); 52 REPORTER_ASSERT(r, a->val() != d->val()); 53 REPORTER_ASSERT(r, b->val() != c->val()); 54 REPORTER_ASSERT(r, b->val() == d->val()); 55 REPORTER_ASSERT(r, c->val() != d->val()); 56 57 // SkVptr() returns the same value for objects of the same concrete type. 58 REPORTER_ASSERT(r, SkVptr(*a) == SkVptr(*c)); 59 REPORTER_ASSERT(r, SkVptr(*b) == SkVptr(*d)); 60 REPORTER_ASSERT(r, SkVptr(*a) != SkVptr(*b)); 61 } 62