2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2013-12-12 21:11:12 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/private/SkTSearch.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "src/core/SkTSort.h"
|
|
|
|
#include "tests/Test.h"
|
2009-02-27 16:24:51 +00:00
|
|
|
|
2010-04-16 20:35:47 +00:00
|
|
|
class RefClass : public SkRefCnt {
|
|
|
|
public:
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2012-08-16 14:58:06 +00:00
|
|
|
|
2010-04-16 20:35:47 +00:00
|
|
|
RefClass(int n) : fN(n) {}
|
|
|
|
int get() const { return fN; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int fN;
|
2012-08-16 14:58:06 +00:00
|
|
|
|
|
|
|
typedef SkRefCnt INHERITED;
|
2010-04-16 20:35:47 +00:00
|
|
|
};
|
|
|
|
|
2011-02-25 18:10:29 +00:00
|
|
|
static void test_autounref(skiatest::Reporter* reporter) {
|
|
|
|
RefClass obj(0);
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj.unique());
|
2011-02-25 18:10:29 +00:00
|
|
|
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<RefClass> tmp(&obj);
|
2011-02-25 18:10:29 +00:00
|
|
|
REPORTER_ASSERT(reporter, &obj == tmp.get());
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj.unique());
|
2011-02-25 18:10:29 +00:00
|
|
|
|
2016-03-16 20:53:35 +00:00
|
|
|
REPORTER_ASSERT(reporter, &obj == tmp.release());
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj.unique());
|
2016-03-16 20:53:35 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == tmp.release());
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == tmp.get());
|
2011-02-25 18:10:29 +00:00
|
|
|
|
|
|
|
obj.ref();
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, !obj.unique());
|
2011-02-25 18:10:29 +00:00
|
|
|
{
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<RefClass> tmp2(&obj);
|
2011-02-25 18:10:29 +00:00
|
|
|
}
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj.unique());
|
2011-02-25 18:10:29 +00:00
|
|
|
}
|
|
|
|
|
2013-07-12 18:44:23 +00:00
|
|
|
static void test_autostarray(skiatest::Reporter* reporter) {
|
|
|
|
RefClass obj0(0);
|
|
|
|
RefClass obj1(1);
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
|
|
|
|
{
|
2016-11-04 15:49:42 +00:00
|
|
|
SkAutoSTArray<2, sk_sp<RefClass> > tmp;
|
2013-07-12 18:44:23 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == tmp.count());
|
|
|
|
|
|
|
|
tmp.reset(0); // test out reset(0) when already at 0
|
|
|
|
tmp.reset(4); // this should force a new allocation
|
|
|
|
REPORTER_ASSERT(reporter, 4 == tmp.count());
|
2014-07-17 19:17:55 +00:00
|
|
|
tmp[0].reset(SkRef(&obj0));
|
|
|
|
tmp[1].reset(SkRef(&obj1));
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, !obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, !obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
|
|
|
|
// test out reset with data in the array (and a new allocation)
|
|
|
|
tmp.reset(0);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == tmp.count());
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, obj1.unique());
|
2013-07-14 01:44:59 +00:00
|
|
|
|
2013-07-12 18:44:23 +00:00
|
|
|
tmp.reset(2); // this should use the preexisting allocation
|
|
|
|
REPORTER_ASSERT(reporter, 2 == tmp.count());
|
2014-07-17 19:17:55 +00:00
|
|
|
tmp[0].reset(SkRef(&obj0));
|
|
|
|
tmp[1].reset(SkRef(&obj1));
|
2013-07-12 18:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test out destructor with data in the array (and using existing allocation)
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// test out allocating ctor (this should allocate new memory)
|
2016-11-04 15:49:42 +00:00
|
|
|
SkAutoSTArray<2, sk_sp<RefClass> > tmp(4);
|
2013-07-12 18:44:23 +00:00
|
|
|
REPORTER_ASSERT(reporter, 4 == tmp.count());
|
|
|
|
|
2014-07-17 19:17:55 +00:00
|
|
|
tmp[0].reset(SkRef(&obj0));
|
|
|
|
tmp[1].reset(SkRef(&obj1));
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, !obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, !obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
|
|
|
|
// Test out resut with data in the array and malloced storage
|
|
|
|
tmp.reset(0);
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
|
|
|
|
tmp.reset(2); // this should use the preexisting storage
|
2014-07-17 19:17:55 +00:00
|
|
|
tmp[0].reset(SkRef(&obj0));
|
|
|
|
tmp[1].reset(SkRef(&obj1));
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, !obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, !obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
|
|
|
|
tmp.reset(4); // this should force a new malloc
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
|
2014-07-17 19:17:55 +00:00
|
|
|
tmp[0].reset(SkRef(&obj0));
|
|
|
|
tmp[1].reset(SkRef(&obj1));
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, !obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, !obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 21:09:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, obj0.unique());
|
|
|
|
REPORTER_ASSERT(reporter, obj1.unique());
|
2013-07-12 18:44:23 +00:00
|
|
|
}
|
|
|
|
|
2011-05-20 17:35:46 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2010-04-16 20:35:47 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
#define kSEARCH_COUNT 91
|
|
|
|
|
|
|
|
static void test_search(skiatest::Reporter* reporter) {
|
|
|
|
int i, array[kSEARCH_COUNT];
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2009-02-27 16:24:51 +00:00
|
|
|
|
|
|
|
for (i = 0; i < kSEARCH_COUNT; i++) {
|
|
|
|
array[i] = rand.nextS();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkTHeapSort<int>(array, kSEARCH_COUNT);
|
|
|
|
// make sure we got sorted properly
|
|
|
|
for (i = 1; i < kSEARCH_COUNT; i++) {
|
|
|
|
REPORTER_ASSERT(reporter, array[i-1] <= array[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we can find all of our values
|
|
|
|
for (i = 0; i < kSEARCH_COUNT; i++) {
|
|
|
|
int index = SkTSearch<int>(array, kSEARCH_COUNT, array[i], sizeof(int));
|
|
|
|
REPORTER_ASSERT(reporter, index == i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that random values are either found, or the correct
|
|
|
|
// insertion index is returned
|
|
|
|
for (i = 0; i < 10000; i++) {
|
|
|
|
int value = rand.nextS();
|
|
|
|
int index = SkTSearch<int>(array, kSEARCH_COUNT, value, sizeof(int));
|
|
|
|
|
|
|
|
if (index >= 0) {
|
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
index < kSEARCH_COUNT && array[index] == value);
|
|
|
|
} else {
|
|
|
|
index = ~index;
|
|
|
|
REPORTER_ASSERT(reporter, index <= kSEARCH_COUNT);
|
|
|
|
if (index < kSEARCH_COUNT) {
|
|
|
|
REPORTER_ASSERT(reporter, value < array[index]);
|
|
|
|
if (index > 0) {
|
|
|
|
REPORTER_ASSERT(reporter, value > array[index - 1]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we should append the new value
|
|
|
|
REPORTER_ASSERT(reporter, value > array[kSEARCH_COUNT - 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(Utils, reporter) {
|
2009-02-27 16:24:51 +00:00
|
|
|
test_search(reporter);
|
2011-02-25 18:10:29 +00:00
|
|
|
test_autounref(reporter);
|
2013-07-12 18:44:23 +00:00
|
|
|
test_autostarray(reporter);
|
2009-02-27 16:24:51 +00:00
|
|
|
}
|