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