aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/VptrTest.cpp
blob: b8a9e1ae0a12e9e35d3ceaba7dfd23578ef931f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkMakeUnique.h"
#include "SkVptr.h"
#include "Test.h"

namespace {

    struct Base {
        virtual ~Base() = default;
        virtual size_t val() const = 0;
    };

    struct SubclassA : public Base {
        SubclassA(size_t val) : fVal(val) {}

        size_t val() const override { return fVal; }

        size_t fVal;
    };

    struct SubclassB : public Base {
        SubclassB() {}

        size_t val() const override { return 42; }
    };

}

DEF_TEST(Vptr, r) {
    std::unique_ptr<Base> a = skstd::make_unique<SubclassA>(21),
                          b = skstd::make_unique<SubclassB>(),
                          c = skstd::make_unique<SubclassA>(22),
                          d = skstd::make_unique<SubclassB>();

    // These 4 objects all have unique identities.
    REPORTER_ASSERT(r, a != b);
    REPORTER_ASSERT(r, a != c);
    REPORTER_ASSERT(r, a != d);
    REPORTER_ASSERT(r, b != c);
    REPORTER_ASSERT(r, b != d);
    REPORTER_ASSERT(r, c != d);

    // Only b and d have the same val().
    REPORTER_ASSERT(r, a->val() != b->val());
    REPORTER_ASSERT(r, a->val() != c->val());
    REPORTER_ASSERT(r, a->val() != d->val());
    REPORTER_ASSERT(r, b->val() != c->val());
    REPORTER_ASSERT(r, b->val() == d->val());
    REPORTER_ASSERT(r, c->val() != d->val());

    // SkVptr() returns the same value for objects of the same concrete type.
    REPORTER_ASSERT(r, SkVptr(*a) == SkVptr(*c));
    REPORTER_ASSERT(r, SkVptr(*b) == SkVptr(*d));
    REPORTER_ASSERT(r, SkVptr(*a) != SkVptr(*b));
}