UtilsTest.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "include/utils/SkRandom.h"
  9. #include "src/core/SkTSearch.h"
  10. #include "src/core/SkTSort.h"
  11. #include "tests/Test.h"
  12. class RefClass : public SkRefCnt {
  13. public:
  14. RefClass(int n) : fN(n) {}
  15. int get() const { return fN; }
  16. private:
  17. int fN;
  18. typedef SkRefCnt INHERITED;
  19. };
  20. static void test_autounref(skiatest::Reporter* reporter) {
  21. RefClass obj(0);
  22. REPORTER_ASSERT(reporter, obj.unique());
  23. sk_sp<RefClass> tmp(&obj);
  24. REPORTER_ASSERT(reporter, &obj == tmp.get());
  25. REPORTER_ASSERT(reporter, obj.unique());
  26. REPORTER_ASSERT(reporter, &obj == tmp.release());
  27. REPORTER_ASSERT(reporter, obj.unique());
  28. REPORTER_ASSERT(reporter, nullptr == tmp.release());
  29. REPORTER_ASSERT(reporter, nullptr == tmp.get());
  30. obj.ref();
  31. REPORTER_ASSERT(reporter, !obj.unique());
  32. {
  33. sk_sp<RefClass> tmp2(&obj);
  34. }
  35. REPORTER_ASSERT(reporter, obj.unique());
  36. }
  37. static void test_autostarray(skiatest::Reporter* reporter) {
  38. RefClass obj0(0);
  39. RefClass obj1(1);
  40. REPORTER_ASSERT(reporter, obj0.unique());
  41. REPORTER_ASSERT(reporter, obj1.unique());
  42. {
  43. SkAutoSTArray<2, sk_sp<RefClass> > tmp;
  44. REPORTER_ASSERT(reporter, 0 == tmp.count());
  45. tmp.reset(0); // test out reset(0) when already at 0
  46. tmp.reset(4); // this should force a new allocation
  47. REPORTER_ASSERT(reporter, 4 == tmp.count());
  48. tmp[0].reset(SkRef(&obj0));
  49. tmp[1].reset(SkRef(&obj1));
  50. REPORTER_ASSERT(reporter, !obj0.unique());
  51. REPORTER_ASSERT(reporter, !obj1.unique());
  52. // test out reset with data in the array (and a new allocation)
  53. tmp.reset(0);
  54. REPORTER_ASSERT(reporter, 0 == tmp.count());
  55. REPORTER_ASSERT(reporter, obj0.unique());
  56. REPORTER_ASSERT(reporter, obj1.unique());
  57. tmp.reset(2); // this should use the preexisting allocation
  58. REPORTER_ASSERT(reporter, 2 == tmp.count());
  59. tmp[0].reset(SkRef(&obj0));
  60. tmp[1].reset(SkRef(&obj1));
  61. }
  62. // test out destructor with data in the array (and using existing allocation)
  63. REPORTER_ASSERT(reporter, obj0.unique());
  64. REPORTER_ASSERT(reporter, obj1.unique());
  65. {
  66. // test out allocating ctor (this should allocate new memory)
  67. SkAutoSTArray<2, sk_sp<RefClass> > tmp(4);
  68. REPORTER_ASSERT(reporter, 4 == tmp.count());
  69. tmp[0].reset(SkRef(&obj0));
  70. tmp[1].reset(SkRef(&obj1));
  71. REPORTER_ASSERT(reporter, !obj0.unique());
  72. REPORTER_ASSERT(reporter, !obj1.unique());
  73. // Test out resut with data in the array and malloced storage
  74. tmp.reset(0);
  75. REPORTER_ASSERT(reporter, obj0.unique());
  76. REPORTER_ASSERT(reporter, obj1.unique());
  77. tmp.reset(2); // this should use the preexisting storage
  78. tmp[0].reset(SkRef(&obj0));
  79. tmp[1].reset(SkRef(&obj1));
  80. REPORTER_ASSERT(reporter, !obj0.unique());
  81. REPORTER_ASSERT(reporter, !obj1.unique());
  82. tmp.reset(4); // this should force a new malloc
  83. REPORTER_ASSERT(reporter, obj0.unique());
  84. REPORTER_ASSERT(reporter, obj1.unique());
  85. tmp[0].reset(SkRef(&obj0));
  86. tmp[1].reset(SkRef(&obj1));
  87. REPORTER_ASSERT(reporter, !obj0.unique());
  88. REPORTER_ASSERT(reporter, !obj1.unique());
  89. }
  90. REPORTER_ASSERT(reporter, obj0.unique());
  91. REPORTER_ASSERT(reporter, obj1.unique());
  92. }
  93. /////////////////////////////////////////////////////////////////////////////
  94. #define kSEARCH_COUNT 91
  95. static void test_search(skiatest::Reporter* reporter) {
  96. int i, array[kSEARCH_COUNT];
  97. SkRandom rand;
  98. for (i = 0; i < kSEARCH_COUNT; i++) {
  99. array[i] = rand.nextS();
  100. }
  101. SkTHeapSort<int>(array, kSEARCH_COUNT);
  102. // make sure we got sorted properly
  103. for (i = 1; i < kSEARCH_COUNT; i++) {
  104. REPORTER_ASSERT(reporter, array[i-1] <= array[i]);
  105. }
  106. // make sure we can find all of our values
  107. for (i = 0; i < kSEARCH_COUNT; i++) {
  108. int index = SkTSearch<int>(array, kSEARCH_COUNT, array[i], sizeof(int));
  109. REPORTER_ASSERT(reporter, index == i);
  110. }
  111. // make sure that random values are either found, or the correct
  112. // insertion index is returned
  113. for (i = 0; i < 10000; i++) {
  114. int value = rand.nextS();
  115. int index = SkTSearch<int>(array, kSEARCH_COUNT, value, sizeof(int));
  116. if (index >= 0) {
  117. REPORTER_ASSERT(reporter,
  118. index < kSEARCH_COUNT && array[index] == value);
  119. } else {
  120. index = ~index;
  121. REPORTER_ASSERT(reporter, index <= kSEARCH_COUNT);
  122. if (index < kSEARCH_COUNT) {
  123. REPORTER_ASSERT(reporter, value < array[index]);
  124. if (index > 0) {
  125. REPORTER_ASSERT(reporter, value > array[index - 1]);
  126. }
  127. } else {
  128. // we should append the new value
  129. REPORTER_ASSERT(reporter, value > array[kSEARCH_COUNT - 1]);
  130. }
  131. }
  132. }
  133. }
  134. DEF_TEST(Utils, reporter) {
  135. test_search(reporter);
  136. test_autounref(reporter);
  137. test_autostarray(reporter);
  138. }