epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@google.com | 0e190d0 | 2011-01-25 23:36:05 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | #include "SkRefDict.h" |
| 10 | |
| 11 | class TestRC : public SkRefCnt { |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 12 | public: |
| 13 | SK_DECLARE_INST_COUNT(TestRC) |
| 14 | private: |
| 15 | typedef SkRefCnt INHERITED; |
reed@google.com | 0e190d0 | 2011-01-25 23:36:05 +0000 | [diff] [blame] | 16 | }; |
| 17 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 18 | SK_DEFINE_INST_COUNT(TestRC) |
| 19 | |
reed@google.com | 0e190d0 | 2011-01-25 23:36:05 +0000 | [diff] [blame] | 20 | static 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.com | 3636ed5 | 2011-01-25 23:50:57 +0000 | [diff] [blame] | 62 | 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.com | 0e190d0 | 2011-01-25 23:36:05 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | #include "TestClassDef.h" |
| 79 | DEFINE_TESTCLASS("RefDict", RefDictTestClass, TestRefDict) |