RefDictTest.cpp revision 3636ed558fb2af5a48a9634efec55fd8a87c88d7
1#include "Test.h"
2#include "SkRefDict.h"
3
4class TestRC : public SkRefCnt {
5};
6
7static void TestRefDict(skiatest::Reporter* reporter) {
8    TestRC    data0, data1;
9    SkRefDict dict;
10
11    REPORTER_ASSERT(reporter, NULL == dict.find(NULL));
12    REPORTER_ASSERT(reporter, NULL == dict.find("foo"));
13    REPORTER_ASSERT(reporter, NULL == dict.find("bar"));
14
15    dict.set("foo", &data0);
16    REPORTER_ASSERT(reporter, &data0 == dict.find("foo"));
17    REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());
18
19    dict.set("foo", &data0);
20    REPORTER_ASSERT(reporter, &data0 == dict.find("foo"));
21    REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());
22
23    dict.set("foo", &data1);
24    REPORTER_ASSERT(reporter, &data1 == dict.find("foo"));
25    REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
26    REPORTER_ASSERT(reporter, 2 == data1.getRefCnt());
27
28    dict.set("foo", NULL);
29    REPORTER_ASSERT(reporter, NULL == dict.find("foo"));
30    REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
31    REPORTER_ASSERT(reporter, 1 == data1.getRefCnt());
32
33    dict.set("foo", &data0);
34    dict.set("bar", &data1);
35    REPORTER_ASSERT(reporter, &data0 == dict.find("foo"));
36    REPORTER_ASSERT(reporter, &data1 == dict.find("bar"));
37    REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());
38    REPORTER_ASSERT(reporter, 2 == data1.getRefCnt());
39
40    dict.set("foo", &data1);
41    REPORTER_ASSERT(reporter, &data1 == dict.find("foo"));
42    REPORTER_ASSERT(reporter, &data1 == dict.find("bar"));
43    REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
44    REPORTER_ASSERT(reporter, 3 == data1.getRefCnt());
45
46    dict.removeAll();
47    REPORTER_ASSERT(reporter, NULL == dict.find("foo"));
48    REPORTER_ASSERT(reporter, NULL == dict.find("bar"));
49    REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
50    REPORTER_ASSERT(reporter, 1 == data1.getRefCnt());
51
52    {
53        SkRefDict d;
54        REPORTER_ASSERT(reporter, NULL == d.find("foo"));
55        REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
56        d.set("foo", &data0);
57        REPORTER_ASSERT(reporter, &data0 == d.find("foo"));
58        REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());
59        // let d go out of scope still with a ref on data0
60    }
61    // be sure d's destructor lowered data0's owner count back to 1
62    REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
63}
64
65#include "TestClassDef.h"
66DEFINE_TESTCLASS("RefDict", RefDictTestClass, TestRefDict)
67