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