blob: 38a990a4397f72316f90d0dac56c71c1789528c7 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@google.com0e190d02011-01-25 23:36:05 +00008#include "Test.h"
9#include "SkRefDict.h"
10
11class TestRC : public SkRefCnt {
robertphillips@google.coma22e2112012-08-16 14:58:06 +000012public:
13 SK_DECLARE_INST_COUNT(TestRC)
14private:
15 typedef SkRefCnt INHERITED;
reed@google.com0e190d02011-01-25 23:36:05 +000016};
17
robertphillips@google.coma22e2112012-08-16 14:58:06 +000018SK_DEFINE_INST_COUNT(TestRC)
19
reed@google.com0e190d02011-01-25 23:36:05 +000020static void TestRefDict(skiatest::Reporter* reporter) {
21 TestRC data0, data1;
22 SkRefDict dict;
23
24 REPORTER_ASSERT(reporter, NULL == dict.find(NULL));
25 REPORTER_ASSERT(reporter, NULL == dict.find("foo"));
26 REPORTER_ASSERT(reporter, NULL == dict.find("bar"));
27
28 dict.set("foo", &data0);
29 REPORTER_ASSERT(reporter, &data0 == dict.find("foo"));
30 REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());
31
32 dict.set("foo", &data0);
33 REPORTER_ASSERT(reporter, &data0 == dict.find("foo"));
34 REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());
35
36 dict.set("foo", &data1);
37 REPORTER_ASSERT(reporter, &data1 == dict.find("foo"));
38 REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
39 REPORTER_ASSERT(reporter, 2 == data1.getRefCnt());
40
41 dict.set("foo", NULL);
42 REPORTER_ASSERT(reporter, NULL == dict.find("foo"));
43 REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
44 REPORTER_ASSERT(reporter, 1 == data1.getRefCnt());
45
46 dict.set("foo", &data0);
47 dict.set("bar", &data1);
48 REPORTER_ASSERT(reporter, &data0 == dict.find("foo"));
49 REPORTER_ASSERT(reporter, &data1 == dict.find("bar"));
50 REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());
51 REPORTER_ASSERT(reporter, 2 == data1.getRefCnt());
52
53 dict.set("foo", &data1);
54 REPORTER_ASSERT(reporter, &data1 == dict.find("foo"));
55 REPORTER_ASSERT(reporter, &data1 == dict.find("bar"));
56 REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
57 REPORTER_ASSERT(reporter, 3 == data1.getRefCnt());
58
59 dict.removeAll();
60 REPORTER_ASSERT(reporter, NULL == dict.find("foo"));
61 REPORTER_ASSERT(reporter, NULL == dict.find("bar"));
reed@google.com3636ed52011-01-25 23:50:57 +000062 REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
63 REPORTER_ASSERT(reporter, 1 == data1.getRefCnt());
64
65 {
66 SkRefDict d;
67 REPORTER_ASSERT(reporter, NULL == d.find("foo"));
68 REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
69 d.set("foo", &data0);
70 REPORTER_ASSERT(reporter, &data0 == d.find("foo"));
71 REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());
72 // let d go out of scope still with a ref on data0
73 }
74 // be sure d's destructor lowered data0's owner count back to 1
75 REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
reed@google.com0e190d02011-01-25 23:36:05 +000076}
77
78#include "TestClassDef.h"
79DEFINE_TESTCLASS("RefDict", RefDictTestClass, TestRefDict)