blob: aa8f9c95de885369077d56b7a4d52966f916f58f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.orge4fafb12013-12-12 21:11:12 +00007
reed@android.comed673312009-02-27 16:24:51 +00008#include "SkRandom.h"
reed@android.comb00cd722010-04-16 20:35:47 +00009#include "SkRefCnt.h"
reed@android.comed673312009-02-27 16:24:51 +000010#include "SkTSearch.h"
11#include "SkTSort.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000012#include "Test.h"
reed@android.comed673312009-02-27 16:24:51 +000013
reed@android.comb00cd722010-04-16 20:35:47 +000014class RefClass : public SkRefCnt {
15public:
halcanary9d524f22016-03-29 09:03:52 -070016
robertphillips@google.coma22e2112012-08-16 14:58:06 +000017
reed@android.comb00cd722010-04-16 20:35:47 +000018 RefClass(int n) : fN(n) {}
19 int get() const { return fN; }
20
21private:
22 int fN;
robertphillips@google.coma22e2112012-08-16 14:58:06 +000023
24 typedef SkRefCnt INHERITED;
reed@android.comb00cd722010-04-16 20:35:47 +000025};
26
reed@google.coma67573e2011-02-25 18:10:29 +000027static void test_autounref(skiatest::Reporter* reporter) {
28 RefClass obj(0);
mtkleinbbb61d72014-11-24 13:09:39 -080029 REPORTER_ASSERT(reporter, obj.unique());
reed@google.coma67573e2011-02-25 18:10:29 +000030
Hal Canary342b7ac2016-11-04 11:49:42 -040031 sk_sp<RefClass> tmp(&obj);
reed@google.coma67573e2011-02-25 18:10:29 +000032 REPORTER_ASSERT(reporter, &obj == tmp.get());
mtkleinbbb61d72014-11-24 13:09:39 -080033 REPORTER_ASSERT(reporter, obj.unique());
reed@google.coma67573e2011-02-25 18:10:29 +000034
mtklein18300a32016-03-16 13:53:35 -070035 REPORTER_ASSERT(reporter, &obj == tmp.release());
mtkleinbbb61d72014-11-24 13:09:39 -080036 REPORTER_ASSERT(reporter, obj.unique());
mtklein18300a32016-03-16 13:53:35 -070037 REPORTER_ASSERT(reporter, nullptr == tmp.release());
halcanary96fcdcc2015-08-27 07:41:13 -070038 REPORTER_ASSERT(reporter, nullptr == tmp.get());
reed@google.coma67573e2011-02-25 18:10:29 +000039
40 obj.ref();
mtkleinbbb61d72014-11-24 13:09:39 -080041 REPORTER_ASSERT(reporter, !obj.unique());
reed@google.coma67573e2011-02-25 18:10:29 +000042 {
Hal Canary342b7ac2016-11-04 11:49:42 -040043 sk_sp<RefClass> tmp2(&obj);
reed@google.coma67573e2011-02-25 18:10:29 +000044 }
mtkleinbbb61d72014-11-24 13:09:39 -080045 REPORTER_ASSERT(reporter, obj.unique());
reed@google.coma67573e2011-02-25 18:10:29 +000046}
47
robertphillips@google.com4d376732013-07-12 18:44:23 +000048static void test_autostarray(skiatest::Reporter* reporter) {
49 RefClass obj0(0);
50 RefClass obj1(1);
mtkleinbbb61d72014-11-24 13:09:39 -080051 REPORTER_ASSERT(reporter, obj0.unique());
52 REPORTER_ASSERT(reporter, obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +000053
54 {
Hal Canary342b7ac2016-11-04 11:49:42 -040055 SkAutoSTArray<2, sk_sp<RefClass> > tmp;
robertphillips@google.com4d376732013-07-12 18:44:23 +000056 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());
bungeman733418f2014-07-17 12:17:55 -070061 tmp[0].reset(SkRef(&obj0));
62 tmp[1].reset(SkRef(&obj1));
mtkleinbbb61d72014-11-24 13:09:39 -080063 REPORTER_ASSERT(reporter, !obj0.unique());
64 REPORTER_ASSERT(reporter, !obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +000065
66 // test out reset with data in the array (and a new allocation)
67 tmp.reset(0);
68 REPORTER_ASSERT(reporter, 0 == tmp.count());
mtkleinbbb61d72014-11-24 13:09:39 -080069 REPORTER_ASSERT(reporter, obj0.unique());
70 REPORTER_ASSERT(reporter, obj1.unique());
humper@google.com9c96d4b2013-07-14 01:44:59 +000071
robertphillips@google.com4d376732013-07-12 18:44:23 +000072 tmp.reset(2); // this should use the preexisting allocation
73 REPORTER_ASSERT(reporter, 2 == tmp.count());
bungeman733418f2014-07-17 12:17:55 -070074 tmp[0].reset(SkRef(&obj0));
75 tmp[1].reset(SkRef(&obj1));
robertphillips@google.com4d376732013-07-12 18:44:23 +000076 }
77
78 // test out destructor with data in the array (and using existing allocation)
mtkleinbbb61d72014-11-24 13:09:39 -080079 REPORTER_ASSERT(reporter, obj0.unique());
80 REPORTER_ASSERT(reporter, obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +000081
82 {
83 // test out allocating ctor (this should allocate new memory)
Hal Canary342b7ac2016-11-04 11:49:42 -040084 SkAutoSTArray<2, sk_sp<RefClass> > tmp(4);
robertphillips@google.com4d376732013-07-12 18:44:23 +000085 REPORTER_ASSERT(reporter, 4 == tmp.count());
86
bungeman733418f2014-07-17 12:17:55 -070087 tmp[0].reset(SkRef(&obj0));
88 tmp[1].reset(SkRef(&obj1));
mtkleinbbb61d72014-11-24 13:09:39 -080089 REPORTER_ASSERT(reporter, !obj0.unique());
90 REPORTER_ASSERT(reporter, !obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +000091
92 // Test out resut with data in the array and malloced storage
93 tmp.reset(0);
mtkleinbbb61d72014-11-24 13:09:39 -080094 REPORTER_ASSERT(reporter, obj0.unique());
95 REPORTER_ASSERT(reporter, obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +000096
97 tmp.reset(2); // this should use the preexisting storage
bungeman733418f2014-07-17 12:17:55 -070098 tmp[0].reset(SkRef(&obj0));
99 tmp[1].reset(SkRef(&obj1));
mtkleinbbb61d72014-11-24 13:09:39 -0800100 REPORTER_ASSERT(reporter, !obj0.unique());
101 REPORTER_ASSERT(reporter, !obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +0000102
103 tmp.reset(4); // this should force a new malloc
mtkleinbbb61d72014-11-24 13:09:39 -0800104 REPORTER_ASSERT(reporter, obj0.unique());
105 REPORTER_ASSERT(reporter, obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +0000106
bungeman733418f2014-07-17 12:17:55 -0700107 tmp[0].reset(SkRef(&obj0));
108 tmp[1].reset(SkRef(&obj1));
mtkleinbbb61d72014-11-24 13:09:39 -0800109 REPORTER_ASSERT(reporter, !obj0.unique());
110 REPORTER_ASSERT(reporter, !obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +0000111 }
112
mtkleinbbb61d72014-11-24 13:09:39 -0800113 REPORTER_ASSERT(reporter, obj0.unique());
114 REPORTER_ASSERT(reporter, obj1.unique());
robertphillips@google.com4d376732013-07-12 18:44:23 +0000115}
116
epoger@google.combcc56832011-05-20 17:35:46 +0000117/////////////////////////////////////////////////////////////////////////////
reed@android.comb00cd722010-04-16 20:35:47 +0000118
reed@android.comed673312009-02-27 16:24:51 +0000119#define kSEARCH_COUNT 91
120
121static void test_search(skiatest::Reporter* reporter) {
122 int i, array[kSEARCH_COUNT];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000123 SkRandom rand;
reed@android.comed673312009-02-27 16:24:51 +0000124
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.orge4fafb12013-12-12 21:11:12 +0000166DEF_TEST(Utils, reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000167 test_search(reporter);
reed@google.coma67573e2011-02-25 18:10:29 +0000168 test_autounref(reporter);
robertphillips@google.com4d376732013-07-12 18:44:23 +0000169 test_autostarray(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000170}