skia2/tests/UtilsTest.cpp
Mike Klein c0bd9f9fe5 rewrite includes to not need so much -Ifoo
Current strategy: everything from the top

Things to look at first are the manual changes:

   - added tools/rewrite_includes.py
   - removed -Idirectives from BUILD.gn
   - various compile.sh simplifications
   - tweak tools/embed_resources.py
   - update gn/find_headers.py to write paths from the top
   - update gn/gn_to_bp.py SkUserConfig.h layout
     so that #include "include/config/SkUserConfig.h" always
     gets the header we want.

No-Presubmit: true
Change-Id: I73a4b181654e0e38d229bc456c0d0854bae3363e
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/209706
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Hal Canary <halcanary@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
2019-04-24 16:27:11 +00:00

171 lines
5.2 KiB
C++

/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkRefCnt.h"
#include "include/private/SkTSearch.h"
#include "include/utils/SkRandom.h"
#include "src/core/SkTSort.h"
#include "tests/Test.h"
class RefClass : public SkRefCnt {
public:
RefClass(int n) : fN(n) {}
int get() const { return fN; }
private:
int fN;
typedef SkRefCnt INHERITED;
};
static void test_autounref(skiatest::Reporter* reporter) {
RefClass obj(0);
REPORTER_ASSERT(reporter, obj.unique());
sk_sp<RefClass> tmp(&obj);
REPORTER_ASSERT(reporter, &obj == tmp.get());
REPORTER_ASSERT(reporter, obj.unique());
REPORTER_ASSERT(reporter, &obj == tmp.release());
REPORTER_ASSERT(reporter, obj.unique());
REPORTER_ASSERT(reporter, nullptr == tmp.release());
REPORTER_ASSERT(reporter, nullptr == tmp.get());
obj.ref();
REPORTER_ASSERT(reporter, !obj.unique());
{
sk_sp<RefClass> tmp2(&obj);
}
REPORTER_ASSERT(reporter, obj.unique());
}
static void test_autostarray(skiatest::Reporter* reporter) {
RefClass obj0(0);
RefClass obj1(1);
REPORTER_ASSERT(reporter, obj0.unique());
REPORTER_ASSERT(reporter, obj1.unique());
{
SkAutoSTArray<2, sk_sp<RefClass> > tmp;
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());
tmp[0].reset(SkRef(&obj0));
tmp[1].reset(SkRef(&obj1));
REPORTER_ASSERT(reporter, !obj0.unique());
REPORTER_ASSERT(reporter, !obj1.unique());
// test out reset with data in the array (and a new allocation)
tmp.reset(0);
REPORTER_ASSERT(reporter, 0 == tmp.count());
REPORTER_ASSERT(reporter, obj0.unique());
REPORTER_ASSERT(reporter, obj1.unique());
tmp.reset(2); // this should use the preexisting allocation
REPORTER_ASSERT(reporter, 2 == tmp.count());
tmp[0].reset(SkRef(&obj0));
tmp[1].reset(SkRef(&obj1));
}
// test out destructor with data in the array (and using existing allocation)
REPORTER_ASSERT(reporter, obj0.unique());
REPORTER_ASSERT(reporter, obj1.unique());
{
// test out allocating ctor (this should allocate new memory)
SkAutoSTArray<2, sk_sp<RefClass> > tmp(4);
REPORTER_ASSERT(reporter, 4 == tmp.count());
tmp[0].reset(SkRef(&obj0));
tmp[1].reset(SkRef(&obj1));
REPORTER_ASSERT(reporter, !obj0.unique());
REPORTER_ASSERT(reporter, !obj1.unique());
// Test out resut with data in the array and malloced storage
tmp.reset(0);
REPORTER_ASSERT(reporter, obj0.unique());
REPORTER_ASSERT(reporter, obj1.unique());
tmp.reset(2); // this should use the preexisting storage
tmp[0].reset(SkRef(&obj0));
tmp[1].reset(SkRef(&obj1));
REPORTER_ASSERT(reporter, !obj0.unique());
REPORTER_ASSERT(reporter, !obj1.unique());
tmp.reset(4); // this should force a new malloc
REPORTER_ASSERT(reporter, obj0.unique());
REPORTER_ASSERT(reporter, obj1.unique());
tmp[0].reset(SkRef(&obj0));
tmp[1].reset(SkRef(&obj1));
REPORTER_ASSERT(reporter, !obj0.unique());
REPORTER_ASSERT(reporter, !obj1.unique());
}
REPORTER_ASSERT(reporter, obj0.unique());
REPORTER_ASSERT(reporter, obj1.unique());
}
/////////////////////////////////////////////////////////////////////////////
#define kSEARCH_COUNT 91
static void test_search(skiatest::Reporter* reporter) {
int i, array[kSEARCH_COUNT];
SkRandom rand;
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]);
}
}
}
}
DEF_TEST(Utils, reporter) {
test_search(reporter);
test_autounref(reporter);
test_autostarray(reporter);
}