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 "src/core/SkVptr.h"
9 #include "tests/Test.h"
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 {
SubclassA__anon1307b0980111::SubclassA19 SubclassA(size_t val) : fVal(val) {}
20
val__anon1307b0980111::SubclassA21 size_t val() const override { return fVal; }
22
23 size_t fVal;
24 };
25
26 struct SubclassB : public Base {
SubclassB__anon1307b0980111::SubclassB27 SubclassB() {}
28
val__anon1307b0980111::SubclassB29 size_t val() const override { return 42; }
30 };
31
32 } // namespace
33
DEF_TEST(Vptr,r)34 DEF_TEST(Vptr, r) {
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>();
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 }
61