aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/LazyPtrTest.cpp
blob: 1b845bc502777c361782694ba4ed539acad44b83 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "Test.h"
#include "SkLazyPtr.h"
#include "SkRunnable.h"
#include "SkTaskGroup.h"

namespace {

struct CreateIntFromFloat {
    CreateIntFromFloat(float val) : fVal(val) {}
    int* operator()() const { return SkNEW_ARGS(int, ((int)fVal)); }
    float fVal;
};

// As a template argument this must have external linkage.
void custom_destroy(int* ptr) { *ptr = 99; }

} // namespace

DEF_TEST(LazyPtr, r) {
    // Basic usage: calls SkNEW(int).
    SkLazyPtr<int> lazy;
    int* ptr = lazy.get();
    REPORTER_ASSERT(r, ptr);
    REPORTER_ASSERT(r, lazy.get() == ptr);

    // Advanced usage: calls a functor.
    SkLazyPtr<int> lazyFunctor;
    int* six = lazyFunctor.get(CreateIntFromFloat(6.4f));
    REPORTER_ASSERT(r, six);
    REPORTER_ASSERT(r, 6 == *six);

    // Just makes sure this is safe.
    SkLazyPtr<double> neverRead;

    // SkLazyPtr supports custom destroy methods.
    {
        SkLazyPtr<int, custom_destroy> customDestroy;
        ptr = customDestroy.get();
        // custom_destroy called here.
    }
    REPORTER_ASSERT(r, ptr);
    REPORTER_ASSERT(r, 99 == *ptr);
    // Since custom_destroy didn't actually delete ptr, we do now.
    SkDELETE(ptr);
}

namespace {

struct Racer : public SkRunnable {
    Racer() : fLazy(NULL), fSeen(NULL) {}

    void run() override { fSeen = fLazy->get(); }

    SkLazyPtr<int>* fLazy;
    int* fSeen;
};

} // namespace

DEF_TEST(LazyPtr_Threaded, r) {
    static const int kRacers = 321;

    SkLazyPtr<int> lazy;

    Racer racers[kRacers];
    for (int i = 0; i < kRacers; i++) {
        racers[i].fLazy = &lazy;
    }

    SkTaskGroup tg;
    for (int i = 0; i < kRacers; i++) {
        tg.add(racers + i);
    }
    tg.wait();

    for (int i = 1; i < kRacers; i++) {
        REPORTER_ASSERT(r, racers[i].fSeen);
        REPORTER_ASSERT(r, racers[i].fSeen == racers[0].fSeen);
    }
}