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