xref: /aosp_15_r20/external/skia/tests/MetaDataTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
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  */
7 
8 #include "include/core/SkScalar.h"
9 #include "tests/Test.h"
10 #include "tools/SkMetaData.h"
11 
12 #include <array>
13 #include <cstddef>
14 #include <cstdint>
15 #include <cstring>
16 
DEF_TEST(MetaData,reporter)17 DEF_TEST(MetaData, reporter) {
18     SkMetaData m1;
19 
20     REPORTER_ASSERT(reporter, !m1.findS32("int"));
21     REPORTER_ASSERT(reporter, !m1.findScalar("scalar"));
22     REPORTER_ASSERT(reporter, !m1.removeS32("int"));
23     REPORTER_ASSERT(reporter, !m1.removeScalar("scalar"));
24 
25     m1.setS32("int", 12345);
26     m1.setScalar("scalar", SK_Scalar1 * 42);
27     m1.setPtr("ptr", &m1);
28     m1.setBool("true", true);
29     m1.setBool("false", false);
30 
31     int32_t n;
32     SkScalar s;
33 
34     m1.setScalar("scalar", SK_Scalar1/2);
35 
36     REPORTER_ASSERT(reporter, m1.findS32("int", &n) && n == 12345);
37     REPORTER_ASSERT(reporter, m1.findScalar("scalar", &s) && s == SK_Scalar1/2);
38     REPORTER_ASSERT(reporter, m1.hasBool("true", true));
39     REPORTER_ASSERT(reporter, m1.hasBool("false", false));
40 
41     SkMetaData::Iter iter(m1);
42     const char* name;
43 
44     static const struct {
45         const char*         fName;
46         SkMetaData::Type    fType;
47         int                 fCount;
48     } gElems[] = {
49         { "int",    SkMetaData::kS32_Type,      1 },
50         { "scalar", SkMetaData::kScalar_Type,   1 },
51         { "ptr",    SkMetaData::kPtr_Type,      1 },
52         { "true",   SkMetaData::kBool_Type,     1 },
53         { "false",  SkMetaData::kBool_Type,     1 }
54     };
55 
56     size_t loop = 0;
57     int count;
58     SkMetaData::Type t;
59     while ((name = iter.next(&t, &count)) != nullptr)
60     {
61         int match = 0;
62         for (unsigned i = 0; i < std::size(gElems); i++)
63         {
64             if (!strcmp(name, gElems[i].fName))
65             {
66                 match += 1;
67                 REPORTER_ASSERT(reporter, gElems[i].fType == t);
68                 REPORTER_ASSERT(reporter, gElems[i].fCount == count);
69             }
70         }
71         REPORTER_ASSERT(reporter, match == 1);
72         loop += 1;
73     }
74     REPORTER_ASSERT(reporter, loop == std::size(gElems));
75 
76     REPORTER_ASSERT(reporter, m1.removeS32("int"));
77     REPORTER_ASSERT(reporter, m1.removeScalar("scalar"));
78     REPORTER_ASSERT(reporter, m1.removeBool("true"));
79     REPORTER_ASSERT(reporter, m1.removeBool("false"));
80 
81     REPORTER_ASSERT(reporter, !m1.findS32("int"));
82     REPORTER_ASSERT(reporter, !m1.findScalar("scalar"));
83     REPORTER_ASSERT(reporter, !m1.findBool("true"));
84     REPORTER_ASSERT(reporter, !m1.findBool("false"));
85 }
86