aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/RefDictTest.cpp
blob: 1e18a6815d0b1ac6047205c61f4b9af75ca97075 (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
/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkRefDict.h"
#include "Test.h"

class TestRC : public SkRefCnt {
public:
    SK_DECLARE_INST_COUNT(TestRC)
private:
    typedef SkRefCnt INHERITED;
};

DEF_TEST(RefDict, reporter) {
    TestRC    data0, data1;
    SkRefDict dict;

    REPORTER_ASSERT(reporter, NULL == dict.find(NULL));
    REPORTER_ASSERT(reporter, NULL == dict.find("foo"));
    REPORTER_ASSERT(reporter, NULL == dict.find("bar"));

    dict.set("foo", &data0);
    REPORTER_ASSERT(reporter, &data0 == dict.find("foo"));
    REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());

    dict.set("foo", &data0);
    REPORTER_ASSERT(reporter, &data0 == dict.find("foo"));
    REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());

    dict.set("foo", &data1);
    REPORTER_ASSERT(reporter, &data1 == dict.find("foo"));
    REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
    REPORTER_ASSERT(reporter, 2 == data1.getRefCnt());

    dict.set("foo", NULL);
    REPORTER_ASSERT(reporter, NULL == dict.find("foo"));
    REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
    REPORTER_ASSERT(reporter, 1 == data1.getRefCnt());

    dict.set("foo", &data0);
    dict.set("bar", &data1);
    REPORTER_ASSERT(reporter, &data0 == dict.find("foo"));
    REPORTER_ASSERT(reporter, &data1 == dict.find("bar"));
    REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());
    REPORTER_ASSERT(reporter, 2 == data1.getRefCnt());

    dict.set("foo", &data1);
    REPORTER_ASSERT(reporter, &data1 == dict.find("foo"));
    REPORTER_ASSERT(reporter, &data1 == dict.find("bar"));
    REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
    REPORTER_ASSERT(reporter, 3 == data1.getRefCnt());

    dict.removeAll();
    REPORTER_ASSERT(reporter, NULL == dict.find("foo"));
    REPORTER_ASSERT(reporter, NULL == dict.find("bar"));
    REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
    REPORTER_ASSERT(reporter, 1 == data1.getRefCnt());

    {
        SkRefDict d;
        REPORTER_ASSERT(reporter, NULL == d.find("foo"));
        REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
        d.set("foo", &data0);
        REPORTER_ASSERT(reporter, &data0 == d.find("foo"));
        REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());
        // let d go out of scope still with a ref on data0
    }
    // be sure d's destructor lowered data0's owner count back to 1
    REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
}