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@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 8 | #include "SkRandom.h" |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 9 | #include "SkRefCnt.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 10 | #include "SkTSearch.h" |
| 11 | #include "SkTSort.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 12 | #include "Test.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 13 | |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 14 | class RefClass : public SkRefCnt { |
| 15 | public: |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 16 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 17 | |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 18 | RefClass(int n) : fN(n) {} |
| 19 | int get() const { return fN; } |
| 20 | |
| 21 | private: |
| 22 | int fN; |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 23 | |
| 24 | typedef SkRefCnt INHERITED; |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 27 | static void test_autounref(skiatest::Reporter* reporter) { |
| 28 | RefClass obj(0); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 29 | REPORTER_ASSERT(reporter, obj.unique()); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 30 | |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 31 | sk_sp<RefClass> tmp(&obj); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 32 | REPORTER_ASSERT(reporter, &obj == tmp.get()); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 33 | REPORTER_ASSERT(reporter, obj.unique()); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 34 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 35 | REPORTER_ASSERT(reporter, &obj == tmp.release()); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 36 | REPORTER_ASSERT(reporter, obj.unique()); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 37 | REPORTER_ASSERT(reporter, nullptr == tmp.release()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 38 | REPORTER_ASSERT(reporter, nullptr == tmp.get()); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 39 | |
| 40 | obj.ref(); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 41 | REPORTER_ASSERT(reporter, !obj.unique()); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 42 | { |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 43 | sk_sp<RefClass> tmp2(&obj); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 44 | } |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 45 | REPORTER_ASSERT(reporter, obj.unique()); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 46 | } |
| 47 | |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 48 | static void test_autostarray(skiatest::Reporter* reporter) { |
| 49 | RefClass obj0(0); |
| 50 | RefClass obj1(1); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 51 | REPORTER_ASSERT(reporter, obj0.unique()); |
| 52 | REPORTER_ASSERT(reporter, obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 53 | |
| 54 | { |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 55 | SkAutoSTArray<2, sk_sp<RefClass> > tmp; |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 56 | REPORTER_ASSERT(reporter, 0 == tmp.count()); |
| 57 | |
| 58 | tmp.reset(0); // test out reset(0) when already at 0 |
| 59 | tmp.reset(4); // this should force a new allocation |
| 60 | REPORTER_ASSERT(reporter, 4 == tmp.count()); |
bungeman | 733418f | 2014-07-17 12:17:55 -0700 | [diff] [blame] | 61 | tmp[0].reset(SkRef(&obj0)); |
| 62 | tmp[1].reset(SkRef(&obj1)); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 63 | REPORTER_ASSERT(reporter, !obj0.unique()); |
| 64 | REPORTER_ASSERT(reporter, !obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 65 | |
| 66 | // test out reset with data in the array (and a new allocation) |
| 67 | tmp.reset(0); |
| 68 | REPORTER_ASSERT(reporter, 0 == tmp.count()); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 69 | REPORTER_ASSERT(reporter, obj0.unique()); |
| 70 | REPORTER_ASSERT(reporter, obj1.unique()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 71 | |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 72 | tmp.reset(2); // this should use the preexisting allocation |
| 73 | REPORTER_ASSERT(reporter, 2 == tmp.count()); |
bungeman | 733418f | 2014-07-17 12:17:55 -0700 | [diff] [blame] | 74 | tmp[0].reset(SkRef(&obj0)); |
| 75 | tmp[1].reset(SkRef(&obj1)); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // test out destructor with data in the array (and using existing allocation) |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 79 | REPORTER_ASSERT(reporter, obj0.unique()); |
| 80 | REPORTER_ASSERT(reporter, obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 81 | |
| 82 | { |
| 83 | // test out allocating ctor (this should allocate new memory) |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 84 | SkAutoSTArray<2, sk_sp<RefClass> > tmp(4); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 85 | REPORTER_ASSERT(reporter, 4 == tmp.count()); |
| 86 | |
bungeman | 733418f | 2014-07-17 12:17:55 -0700 | [diff] [blame] | 87 | tmp[0].reset(SkRef(&obj0)); |
| 88 | tmp[1].reset(SkRef(&obj1)); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 89 | REPORTER_ASSERT(reporter, !obj0.unique()); |
| 90 | REPORTER_ASSERT(reporter, !obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 91 | |
| 92 | // Test out resut with data in the array and malloced storage |
| 93 | tmp.reset(0); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 94 | REPORTER_ASSERT(reporter, obj0.unique()); |
| 95 | REPORTER_ASSERT(reporter, obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 96 | |
| 97 | tmp.reset(2); // this should use the preexisting storage |
bungeman | 733418f | 2014-07-17 12:17:55 -0700 | [diff] [blame] | 98 | tmp[0].reset(SkRef(&obj0)); |
| 99 | tmp[1].reset(SkRef(&obj1)); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 100 | REPORTER_ASSERT(reporter, !obj0.unique()); |
| 101 | REPORTER_ASSERT(reporter, !obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 102 | |
| 103 | tmp.reset(4); // this should force a new malloc |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 104 | REPORTER_ASSERT(reporter, obj0.unique()); |
| 105 | REPORTER_ASSERT(reporter, obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 106 | |
bungeman | 733418f | 2014-07-17 12:17:55 -0700 | [diff] [blame] | 107 | tmp[0].reset(SkRef(&obj0)); |
| 108 | tmp[1].reset(SkRef(&obj1)); |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 109 | REPORTER_ASSERT(reporter, !obj0.unique()); |
| 110 | REPORTER_ASSERT(reporter, !obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 111 | } |
| 112 | |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 113 | REPORTER_ASSERT(reporter, obj0.unique()); |
| 114 | REPORTER_ASSERT(reporter, obj1.unique()); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 115 | } |
| 116 | |
epoger@google.com | bcc5683 | 2011-05-20 17:35:46 +0000 | [diff] [blame] | 117 | ///////////////////////////////////////////////////////////////////////////// |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 118 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 119 | #define kSEARCH_COUNT 91 |
| 120 | |
| 121 | static void test_search(skiatest::Reporter* reporter) { |
| 122 | int i, array[kSEARCH_COUNT]; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 123 | SkRandom rand; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 124 | |
| 125 | for (i = 0; i < kSEARCH_COUNT; i++) { |
| 126 | array[i] = rand.nextS(); |
| 127 | } |
| 128 | |
| 129 | SkTHeapSort<int>(array, kSEARCH_COUNT); |
| 130 | // make sure we got sorted properly |
| 131 | for (i = 1; i < kSEARCH_COUNT; i++) { |
| 132 | REPORTER_ASSERT(reporter, array[i-1] <= array[i]); |
| 133 | } |
| 134 | |
| 135 | // make sure we can find all of our values |
| 136 | for (i = 0; i < kSEARCH_COUNT; i++) { |
| 137 | int index = SkTSearch<int>(array, kSEARCH_COUNT, array[i], sizeof(int)); |
| 138 | REPORTER_ASSERT(reporter, index == i); |
| 139 | } |
| 140 | |
| 141 | // make sure that random values are either found, or the correct |
| 142 | // insertion index is returned |
| 143 | for (i = 0; i < 10000; i++) { |
| 144 | int value = rand.nextS(); |
| 145 | int index = SkTSearch<int>(array, kSEARCH_COUNT, value, sizeof(int)); |
| 146 | |
| 147 | if (index >= 0) { |
| 148 | REPORTER_ASSERT(reporter, |
| 149 | index < kSEARCH_COUNT && array[index] == value); |
| 150 | } else { |
| 151 | index = ~index; |
| 152 | REPORTER_ASSERT(reporter, index <= kSEARCH_COUNT); |
| 153 | if (index < kSEARCH_COUNT) { |
| 154 | REPORTER_ASSERT(reporter, value < array[index]); |
| 155 | if (index > 0) { |
| 156 | REPORTER_ASSERT(reporter, value > array[index - 1]); |
| 157 | } |
| 158 | } else { |
| 159 | // we should append the new value |
| 160 | REPORTER_ASSERT(reporter, value > array[kSEARCH_COUNT - 1]); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 166 | DEF_TEST(Utils, reporter) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 167 | test_search(reporter); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 168 | test_autounref(reporter); |
robertphillips@google.com | 4d37673 | 2013-07-12 18:44:23 +0000 | [diff] [blame] | 169 | test_autostarray(reporter); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 170 | } |