OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkRefDict.h" | 8 #include "SkRefDict.h" |
9 #include "RefCntIs.h" | |
10 #include "Test.h" | 9 #include "Test.h" |
11 | 10 |
12 class TestRC : public SkRefCnt { | 11 class TestRC : public SkRefCnt { |
13 public: | 12 public: |
14 SK_DECLARE_INST_COUNT(TestRC) | 13 SK_DECLARE_INST_COUNT(TestRC) |
15 private: | 14 private: |
16 typedef SkRefCnt INHERITED; | 15 typedef SkRefCnt INHERITED; |
17 }; | 16 }; |
18 | 17 |
19 DEF_TEST(RefDict, reporter) { | 18 DEF_TEST(RefDict, reporter) { |
20 TestRC data0, data1; | 19 TestRC data0, data1; |
21 SkRefDict dict; | 20 SkRefDict dict; |
22 | 21 |
23 REPORTER_ASSERT(reporter, NULL == dict.find(NULL)); | 22 REPORTER_ASSERT(reporter, NULL == dict.find(NULL)); |
24 REPORTER_ASSERT(reporter, NULL == dict.find("foo")); | 23 REPORTER_ASSERT(reporter, NULL == dict.find("foo")); |
25 REPORTER_ASSERT(reporter, NULL == dict.find("bar")); | 24 REPORTER_ASSERT(reporter, NULL == dict.find("bar")); |
26 | 25 |
27 dict.set("foo", &data0); | 26 dict.set("foo", &data0); |
28 REPORTER_ASSERT(reporter, &data0 == dict.find("foo")); | 27 REPORTER_ASSERT(reporter, &data0 == dict.find("foo")); |
29 REPORTER_ASSERT(reporter, RefCntIs(data0, 2)); | 28 REPORTER_ASSERT(reporter, 2 == data0.getRefCnt()); |
30 | 29 |
31 dict.set("foo", &data0); | 30 dict.set("foo", &data0); |
32 REPORTER_ASSERT(reporter, &data0 == dict.find("foo")); | 31 REPORTER_ASSERT(reporter, &data0 == dict.find("foo")); |
33 REPORTER_ASSERT(reporter, RefCntIs(data0, 2)); | 32 REPORTER_ASSERT(reporter, 2 == data0.getRefCnt()); |
34 | 33 |
35 dict.set("foo", &data1); | 34 dict.set("foo", &data1); |
36 REPORTER_ASSERT(reporter, &data1 == dict.find("foo")); | 35 REPORTER_ASSERT(reporter, &data1 == dict.find("foo")); |
37 REPORTER_ASSERT(reporter, RefCntIs(data0, 1)); | 36 REPORTER_ASSERT(reporter, 1 == data0.getRefCnt()); |
38 REPORTER_ASSERT(reporter, RefCntIs(data1, 2)); | 37 REPORTER_ASSERT(reporter, 2 == data1.getRefCnt()); |
39 | 38 |
40 dict.set("foo", NULL); | 39 dict.set("foo", NULL); |
41 REPORTER_ASSERT(reporter, NULL == dict.find("foo")); | 40 REPORTER_ASSERT(reporter, NULL == dict.find("foo")); |
42 REPORTER_ASSERT(reporter, RefCntIs(data0, 1)); | 41 REPORTER_ASSERT(reporter, 1 == data0.getRefCnt()); |
43 REPORTER_ASSERT(reporter, RefCntIs(data1, 1)); | 42 REPORTER_ASSERT(reporter, 1 == data1.getRefCnt()); |
44 | 43 |
45 dict.set("foo", &data0); | 44 dict.set("foo", &data0); |
46 dict.set("bar", &data1); | 45 dict.set("bar", &data1); |
47 REPORTER_ASSERT(reporter, &data0 == dict.find("foo")); | 46 REPORTER_ASSERT(reporter, &data0 == dict.find("foo")); |
48 REPORTER_ASSERT(reporter, &data1 == dict.find("bar")); | 47 REPORTER_ASSERT(reporter, &data1 == dict.find("bar")); |
49 REPORTER_ASSERT(reporter, RefCntIs(data0, 2)); | 48 REPORTER_ASSERT(reporter, 2 == data0.getRefCnt()); |
50 REPORTER_ASSERT(reporter, RefCntIs(data1, 2)); | 49 REPORTER_ASSERT(reporter, 2 == data1.getRefCnt()); |
51 | 50 |
52 dict.set("foo", &data1); | 51 dict.set("foo", &data1); |
53 REPORTER_ASSERT(reporter, &data1 == dict.find("foo")); | 52 REPORTER_ASSERT(reporter, &data1 == dict.find("foo")); |
54 REPORTER_ASSERT(reporter, &data1 == dict.find("bar")); | 53 REPORTER_ASSERT(reporter, &data1 == dict.find("bar")); |
55 REPORTER_ASSERT(reporter, RefCntIs(data0, 1)); | 54 REPORTER_ASSERT(reporter, 1 == data0.getRefCnt()); |
56 REPORTER_ASSERT(reporter, RefCntIs(data1, 3)); | 55 REPORTER_ASSERT(reporter, 3 == data1.getRefCnt()); |
57 | 56 |
58 dict.removeAll(); | 57 dict.removeAll(); |
59 REPORTER_ASSERT(reporter, NULL == dict.find("foo")); | 58 REPORTER_ASSERT(reporter, NULL == dict.find("foo")); |
60 REPORTER_ASSERT(reporter, NULL == dict.find("bar")); | 59 REPORTER_ASSERT(reporter, NULL == dict.find("bar")); |
61 REPORTER_ASSERT(reporter, RefCntIs(data0, 1)); | 60 REPORTER_ASSERT(reporter, 1 == data0.getRefCnt()); |
62 REPORTER_ASSERT(reporter, RefCntIs(data1, 1)); | 61 REPORTER_ASSERT(reporter, 1 == data1.getRefCnt()); |
63 | 62 |
64 { | 63 { |
65 SkRefDict d; | 64 SkRefDict d; |
66 REPORTER_ASSERT(reporter, NULL == d.find("foo")); | 65 REPORTER_ASSERT(reporter, NULL == d.find("foo")); |
67 REPORTER_ASSERT(reporter, RefCntIs(data0, 1)); | 66 REPORTER_ASSERT(reporter, 1 == data0.getRefCnt()); |
68 d.set("foo", &data0); | 67 d.set("foo", &data0); |
69 REPORTER_ASSERT(reporter, &data0 == d.find("foo")); | 68 REPORTER_ASSERT(reporter, &data0 == d.find("foo")); |
70 REPORTER_ASSERT(reporter, RefCntIs(data0, 2)); | 69 REPORTER_ASSERT(reporter, 2 == data0.getRefCnt()); |
71 // let d go out of scope still with a ref on data0 | 70 // let d go out of scope still with a ref on data0 |
72 } | 71 } |
73 // be sure d's destructor lowered data0's owner count back to 1 | 72 // be sure d's destructor lowered data0's owner count back to 1 |
74 REPORTER_ASSERT(reporter, RefCntIs(data0, 1)); | 73 REPORTER_ASSERT(reporter, 1 == data0.getRefCnt()); |
75 } | 74 } |
OLD | NEW |