blob: 4613d86551cfcffd94317eb42513235c5012bbb6 [file] [log] [blame]
Mike Klein0554d492017-12-06 12:27:44 -05001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/core/SkVptr.h"
9#include "tests/Test.h"
Mike Klein0554d492017-12-06 12:27:44 -050010
11namespace {
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
32}
33
34DEF_TEST(Vptr, r) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050035 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 Klein0554d492017-12-06 12:27:44 -050039
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}