blob: 61f64e9a2c05ef021d750940501d0dba0cb08d8f [file] [log] [blame]
reed@google.com0e190d02011-01-25 23:36:05 +00001#include "Test.h"
2#include "SkRefDict.h"
3
4class TestRC : public SkRefCnt {
5};
6
7static void TestRefDict(skiatest::Reporter* reporter) {
8 TestRC data0, data1;
9 SkRefDict dict;
10
11 REPORTER_ASSERT(reporter, NULL == dict.find(NULL));
12 REPORTER_ASSERT(reporter, NULL == dict.find("foo"));
13 REPORTER_ASSERT(reporter, NULL == dict.find("bar"));
14
15 dict.set("foo", &data0);
16 REPORTER_ASSERT(reporter, &data0 == dict.find("foo"));
17 REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());
18
19 dict.set("foo", &data0);
20 REPORTER_ASSERT(reporter, &data0 == dict.find("foo"));
21 REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());
22
23 dict.set("foo", &data1);
24 REPORTER_ASSERT(reporter, &data1 == dict.find("foo"));
25 REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
26 REPORTER_ASSERT(reporter, 2 == data1.getRefCnt());
27
28 dict.set("foo", NULL);
29 REPORTER_ASSERT(reporter, NULL == dict.find("foo"));
30 REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
31 REPORTER_ASSERT(reporter, 1 == data1.getRefCnt());
32
33 dict.set("foo", &data0);
34 dict.set("bar", &data1);
35 REPORTER_ASSERT(reporter, &data0 == dict.find("foo"));
36 REPORTER_ASSERT(reporter, &data1 == dict.find("bar"));
37 REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());
38 REPORTER_ASSERT(reporter, 2 == data1.getRefCnt());
39
40 dict.set("foo", &data1);
41 REPORTER_ASSERT(reporter, &data1 == dict.find("foo"));
42 REPORTER_ASSERT(reporter, &data1 == dict.find("bar"));
43 REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
44 REPORTER_ASSERT(reporter, 3 == data1.getRefCnt());
45
46 dict.removeAll();
47 REPORTER_ASSERT(reporter, NULL == dict.find("foo"));
48 REPORTER_ASSERT(reporter, NULL == dict.find("bar"));
49}
50
51#include "TestClassDef.h"
52DEFINE_TESTCLASS("RefDict", RefDictTestClass, TestRefDict)