blob: f52541b0bbbeb2e0da39a15e5d59361bbcc1c2d5 [file] [log] [blame]
#include "Test.h"
#include "SkRefDict.h"
class TestRC : public SkRefCnt {
};
static void TestRefDict(skiatest::Reporter* reporter) {
TestRC data0, data1;
SkRefDict dict;
REPORTER_ASSERT(reporter, NULL == dict.find(NULL));
REPORTER_ASSERT(reporter, NULL == dict.find("foo"));
REPORTER_ASSERT(reporter, NULL == dict.find("bar"));
dict.set("foo", &data0);
REPORTER_ASSERT(reporter, &data0 == dict.find("foo"));
REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());
dict.set("foo", &data0);
REPORTER_ASSERT(reporter, &data0 == dict.find("foo"));
REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());
dict.set("foo", &data1);
REPORTER_ASSERT(reporter, &data1 == dict.find("foo"));
REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
REPORTER_ASSERT(reporter, 2 == data1.getRefCnt());
dict.set("foo", NULL);
REPORTER_ASSERT(reporter, NULL == dict.find("foo"));
REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
REPORTER_ASSERT(reporter, 1 == data1.getRefCnt());
dict.set("foo", &data0);
dict.set("bar", &data1);
REPORTER_ASSERT(reporter, &data0 == dict.find("foo"));
REPORTER_ASSERT(reporter, &data1 == dict.find("bar"));
REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());
REPORTER_ASSERT(reporter, 2 == data1.getRefCnt());
dict.set("foo", &data1);
REPORTER_ASSERT(reporter, &data1 == dict.find("foo"));
REPORTER_ASSERT(reporter, &data1 == dict.find("bar"));
REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
REPORTER_ASSERT(reporter, 3 == data1.getRefCnt());
dict.removeAll();
REPORTER_ASSERT(reporter, NULL == dict.find("foo"));
REPORTER_ASSERT(reporter, NULL == dict.find("bar"));
REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
REPORTER_ASSERT(reporter, 1 == data1.getRefCnt());
{
SkRefDict d;
REPORTER_ASSERT(reporter, NULL == d.find("foo"));
REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
d.set("foo", &data0);
REPORTER_ASSERT(reporter, &data0 == d.find("foo"));
REPORTER_ASSERT(reporter, 2 == data0.getRefCnt());
// let d go out of scope still with a ref on data0
}
// be sure d's destructor lowered data0's owner count back to 1
REPORTER_ASSERT(reporter, 1 == data0.getRefCnt());
}
#include "TestClassDef.h"
DEFINE_TESTCLASS("RefDict", RefDictTestClass, TestRefDict)