MetaDataTest.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include "include/core/SkRefCnt.h"
  8. #include "tools/SkMetaData.h"
  9. #include "tests/Test.h"
  10. static void test_ptrs(skiatest::Reporter* reporter) {
  11. SkRefCnt ref;
  12. REPORTER_ASSERT(reporter, ref.unique());
  13. {
  14. SkMetaData md0, md1;
  15. const char name[] = "refcnt";
  16. md0.setRefCnt(name, &ref);
  17. REPORTER_ASSERT(reporter, md0.findRefCnt(name));
  18. REPORTER_ASSERT(reporter, md0.hasRefCnt(name, &ref));
  19. REPORTER_ASSERT(reporter, !ref.unique());
  20. md1 = md0;
  21. REPORTER_ASSERT(reporter, md1.findRefCnt(name));
  22. REPORTER_ASSERT(reporter, md1.hasRefCnt(name, &ref));
  23. REPORTER_ASSERT(reporter, !ref.unique());
  24. REPORTER_ASSERT(reporter, md0.removeRefCnt(name));
  25. REPORTER_ASSERT(reporter, !md0.findRefCnt(name));
  26. REPORTER_ASSERT(reporter, !md0.hasRefCnt(name, &ref));
  27. REPORTER_ASSERT(reporter, !ref.unique());
  28. }
  29. REPORTER_ASSERT(reporter, ref.unique());
  30. }
  31. DEF_TEST(MetaData, reporter) {
  32. SkMetaData m1;
  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. m1.setS32("int", 12345);
  42. m1.setScalar("scalar", SK_Scalar1 * 42);
  43. m1.setString("hello", "world");
  44. m1.setPtr("ptr", &m1);
  45. m1.setBool("true", true);
  46. m1.setBool("false", false);
  47. int32_t n;
  48. SkScalar s;
  49. m1.setScalar("scalar", SK_Scalar1/2);
  50. REPORTER_ASSERT(reporter, m1.findS32("int", &n) && n == 12345);
  51. REPORTER_ASSERT(reporter, m1.findScalar("scalar", &s) && s == SK_Scalar1/2);
  52. REPORTER_ASSERT(reporter, !strcmp(m1.findString("hello"), "world"));
  53. REPORTER_ASSERT(reporter, m1.hasBool("true", true));
  54. REPORTER_ASSERT(reporter, m1.hasBool("false", false));
  55. SkMetaData::Iter iter(m1);
  56. const char* name;
  57. static const struct {
  58. const char* fName;
  59. SkMetaData::Type fType;
  60. int fCount;
  61. } gElems[] = {
  62. { "int", SkMetaData::kS32_Type, 1 },
  63. { "scalar", SkMetaData::kScalar_Type, 1 },
  64. { "ptr", SkMetaData::kPtr_Type, 1 },
  65. { "hello", SkMetaData::kString_Type, sizeof("world") },
  66. { "true", SkMetaData::kBool_Type, 1 },
  67. { "false", SkMetaData::kBool_Type, 1 }
  68. };
  69. int loop = 0;
  70. int count;
  71. SkMetaData::Type t;
  72. while ((name = iter.next(&t, &count)) != nullptr)
  73. {
  74. int match = 0;
  75. for (unsigned i = 0; i < SK_ARRAY_COUNT(gElems); i++)
  76. {
  77. if (!strcmp(name, gElems[i].fName))
  78. {
  79. match += 1;
  80. REPORTER_ASSERT(reporter, gElems[i].fType == t);
  81. REPORTER_ASSERT(reporter, gElems[i].fCount == count);
  82. }
  83. }
  84. REPORTER_ASSERT(reporter, match == 1);
  85. loop += 1;
  86. }
  87. REPORTER_ASSERT(reporter, loop == SK_ARRAY_COUNT(gElems));
  88. REPORTER_ASSERT(reporter, m1.removeS32("int"));
  89. REPORTER_ASSERT(reporter, m1.removeScalar("scalar"));
  90. REPORTER_ASSERT(reporter, m1.removeString("hello"));
  91. REPORTER_ASSERT(reporter, m1.removeBool("true"));
  92. REPORTER_ASSERT(reporter, m1.removeBool("false"));
  93. REPORTER_ASSERT(reporter, !m1.findS32("int"));
  94. REPORTER_ASSERT(reporter, !m1.findScalar("scalar"));
  95. REPORTER_ASSERT(reporter, !m1.findString("hello"));
  96. REPORTER_ASSERT(reporter, !m1.findBool("true"));
  97. REPORTER_ASSERT(reporter, !m1.findBool("false"));
  98. test_ptrs(reporter);
  99. }