blob: 5b9987e29aaf23cfb08f15d9d9049138be44d476 [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 "SkRefDict.h"
mtklein4ae94ff2014-07-08 06:48:17 -07009#include "RefCntIs.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000010#include "Test.h"
reed@google.com0e190d02011-01-25 23:36:05 +000011
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"));
mtklein4ae94ff2014-07-08 06:48:17 -070029 REPORTER_ASSERT(reporter, RefCntIs(data0, 2));
reed@google.com0e190d02011-01-25 23:36:05 +000030
31 dict.set("foo", &data0);
32 REPORTER_ASSERT(reporter, &data0 == dict.find("foo"));
mtklein4ae94ff2014-07-08 06:48:17 -070033 REPORTER_ASSERT(reporter, RefCntIs(data0, 2));
reed@google.com0e190d02011-01-25 23:36:05 +000034
35 dict.set("foo", &data1);
36 REPORTER_ASSERT(reporter, &data1 == dict.find("foo"));
mtklein4ae94ff2014-07-08 06:48:17 -070037 REPORTER_ASSERT(reporter, RefCntIs(data0, 1));
38 REPORTER_ASSERT(reporter, RefCntIs(data1, 2));
reed@google.com0e190d02011-01-25 23:36:05 +000039
40 dict.set("foo", NULL);
41 REPORTER_ASSERT(reporter, NULL == dict.find("foo"));
mtklein4ae94ff2014-07-08 06:48:17 -070042 REPORTER_ASSERT(reporter, RefCntIs(data0, 1));
43 REPORTER_ASSERT(reporter, RefCntIs(data1, 1));
reed@google.com0e190d02011-01-25 23:36:05 +000044
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"));
mtklein4ae94ff2014-07-08 06:48:17 -070049 REPORTER_ASSERT(reporter, RefCntIs(data0, 2));
50 REPORTER_ASSERT(reporter, RefCntIs(data1, 2));
reed@google.com0e190d02011-01-25 23:36:05 +000051
52 dict.set("foo", &data1);
53 REPORTER_ASSERT(reporter, &data1 == dict.find("foo"));
54 REPORTER_ASSERT(reporter, &data1 == dict.find("bar"));
mtklein4ae94ff2014-07-08 06:48:17 -070055 REPORTER_ASSERT(reporter, RefCntIs(data0, 1));
56 REPORTER_ASSERT(reporter, RefCntIs(data1, 3));
reed@google.com0e190d02011-01-25 23:36:05 +000057
58 dict.removeAll();
59 REPORTER_ASSERT(reporter, NULL == dict.find("foo"));
60 REPORTER_ASSERT(reporter, NULL == dict.find("bar"));
mtklein4ae94ff2014-07-08 06:48:17 -070061 REPORTER_ASSERT(reporter, RefCntIs(data0, 1));
62 REPORTER_ASSERT(reporter, RefCntIs(data1, 1));
reed@google.com3636ed52011-01-25 23:50:57 +000063
64 {
65 SkRefDict d;
66 REPORTER_ASSERT(reporter, NULL == d.find("foo"));
mtklein4ae94ff2014-07-08 06:48:17 -070067 REPORTER_ASSERT(reporter, RefCntIs(data0, 1));
reed@google.com3636ed52011-01-25 23:50:57 +000068 d.set("foo", &data0);
69 REPORTER_ASSERT(reporter, &data0 == d.find("foo"));
mtklein4ae94ff2014-07-08 06:48:17 -070070 REPORTER_ASSERT(reporter, RefCntIs(data0, 2));
reed@google.com3636ed52011-01-25 23:50:57 +000071 // 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
mtklein4ae94ff2014-07-08 06:48:17 -070074 REPORTER_ASSERT(reporter, RefCntIs(data0, 1));
reed@google.com0e190d02011-01-25 23:36:05 +000075}