blob: 70829d49d7b3251299524eff153f3adb4b98e207 [file] [log] [blame]
reed@google.come7330712011-03-30 18:23:21 +00001#include "Test.h"
2#include "SkMetaData.h"
3
4static void test_ptrs(skiatest::Reporter* reporter) {
5 SkRefCnt ref;
6 REPORTER_ASSERT(reporter, 1 == ref.getRefCnt());
7
8 {
9 SkMetaData md0, md1;
10 const char name[] = "refcnt";
11
12 md0.setRefCnt(name, &ref);
13 REPORTER_ASSERT(reporter, md0.findRefCnt(name));
14 REPORTER_ASSERT(reporter, md0.hasRefCnt(name, &ref));
15 REPORTER_ASSERT(reporter, 2 == ref.getRefCnt());
16
17 md1 = md0;
18 REPORTER_ASSERT(reporter, md1.findRefCnt(name));
19 REPORTER_ASSERT(reporter, md1.hasRefCnt(name, &ref));
20 REPORTER_ASSERT(reporter, 3 == ref.getRefCnt());
21
22 REPORTER_ASSERT(reporter, md0.removeRefCnt(name));
23 REPORTER_ASSERT(reporter, !md0.findRefCnt(name));
24 REPORTER_ASSERT(reporter, !md0.hasRefCnt(name, &ref));
25 REPORTER_ASSERT(reporter, 2 == ref.getRefCnt());
26 }
27 REPORTER_ASSERT(reporter, 1 == ref.getRefCnt());
28}
29
30static void TestMetaData(skiatest::Reporter* reporter) {
31 SkMetaData m1;
32
33 REPORTER_ASSERT(reporter, !m1.findS32("int"));
34 REPORTER_ASSERT(reporter, !m1.findScalar("scalar"));
35 REPORTER_ASSERT(reporter, !m1.findString("hello"));
36 REPORTER_ASSERT(reporter, !m1.removeS32("int"));
37 REPORTER_ASSERT(reporter, !m1.removeScalar("scalar"));
38 REPORTER_ASSERT(reporter, !m1.removeString("hello"));
39 REPORTER_ASSERT(reporter, !m1.removeString("true"));
40 REPORTER_ASSERT(reporter, !m1.removeString("false"));
41
42 m1.setS32("int", 12345);
43 m1.setScalar("scalar", SK_Scalar1 * 42);
44 m1.setString("hello", "world");
45 m1.setPtr("ptr", &m1);
46 m1.setBool("true", true);
47 m1.setBool("false", false);
48
49 int32_t n;
50 SkScalar s;
51
52 m1.setScalar("scalar", SK_Scalar1/2);
53
54 REPORTER_ASSERT(reporter, m1.findS32("int", &n) && n == 12345);
55 REPORTER_ASSERT(reporter, m1.findScalar("scalar", &s) && s == SK_Scalar1/2);
56 REPORTER_ASSERT(reporter, !strcmp(m1.findString("hello"), "world"));
57 REPORTER_ASSERT(reporter, m1.hasBool("true", true));
58 REPORTER_ASSERT(reporter, m1.hasBool("false", false));
59
60 SkMetaData::Iter iter(m1);
61 const char* name;
62
63 static const struct {
64 const char* fName;
65 SkMetaData::Type fType;
66 int fCount;
67 } gElems[] = {
68 { "int", SkMetaData::kS32_Type, 1 },
69 { "scalar", SkMetaData::kScalar_Type, 1 },
70 { "ptr", SkMetaData::kPtr_Type, 1 },
71 { "hello", SkMetaData::kString_Type, sizeof("world") },
72 { "true", SkMetaData::kBool_Type, 1 },
73 { "false", SkMetaData::kBool_Type, 1 }
74 };
75
76 int loop = 0;
77 int count;
78 SkMetaData::Type t;
79 while ((name = iter.next(&t, &count)) != NULL)
80 {
81 int match = 0;
82 for (unsigned i = 0; i < SK_ARRAY_COUNT(gElems); i++)
83 {
84 if (!strcmp(name, gElems[i].fName))
85 {
86 match += 1;
87 REPORTER_ASSERT(reporter, gElems[i].fType == t);
88 REPORTER_ASSERT(reporter, gElems[i].fCount == count);
89 }
90 }
91 REPORTER_ASSERT(reporter, match == 1);
92 loop += 1;
93 }
94 REPORTER_ASSERT(reporter, loop == SK_ARRAY_COUNT(gElems));
95
96 REPORTER_ASSERT(reporter, m1.removeS32("int"));
97 REPORTER_ASSERT(reporter, m1.removeScalar("scalar"));
98 REPORTER_ASSERT(reporter, m1.removeString("hello"));
99 REPORTER_ASSERT(reporter, m1.removeBool("true"));
100 REPORTER_ASSERT(reporter, m1.removeBool("false"));
101
102 REPORTER_ASSERT(reporter, !m1.findS32("int"));
103 REPORTER_ASSERT(reporter, !m1.findScalar("scalar"));
104 REPORTER_ASSERT(reporter, !m1.findString("hello"));
105 REPORTER_ASSERT(reporter, !m1.findBool("true"));
106 REPORTER_ASSERT(reporter, !m1.findBool("false"));
107
108 test_ptrs(reporter);
109}
110
111#include "TestClassDef.h"
112DEFINE_TESTCLASS("MetaData", TestMetaDataClass, TestMetaData)