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