Fix data() method and add unit test.

I wrote code that called SkAutoTArray::data() and discovered that it
was broken, but not generating compile errors because it's part of a
template and never instantiated anywhere else. I fixed the
implementation and added it to our container unit test to prevent later
regression. This revealed another issue, that "containers in
SkTemplates.h [should] all have a consistent api", according to
test_container_apis. However, data() was never added to the non-array
container APIs. So I added data() to the other containers as well.

Change-Id: I52532c91fdab3fc8c4539053ba8420815b7b0ee5
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/323276
Auto-Submit: John Stiles <johnstiles@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
This commit is contained in:
John Stiles 2020-10-06 20:28:18 -04:00 committed by Skia Commit-Bot
parent df47abcbb9
commit 8354e9b8f6
2 changed files with 20 additions and 4 deletions

View File

@ -114,9 +114,9 @@ public:
return fArray[index];
}
// aliases matching other types like std::vector
const T* data() const { return fArray; }
T* data() { return fArray; }
/** Aliases matching other types, like std::vector. */
const T* data() const { return fArray.get(); }
T* data() { return fArray.get(); }
private:
std::unique_ptr<T[]> fArray;
@ -207,7 +207,7 @@ public:
return fArray[index];
}
// aliases matching other types like std::vector
/** Aliases matching other types, like std::vector. */
const T* data() const { return fArray; }
T* data() { return fArray; }
size_t size() const { return fCount; }
@ -266,6 +266,10 @@ public:
const T& operator[](int index) const { return fPtr.get()[index]; }
/** Aliases matching other types, like std::vector. */
const T* data() const { return fPtr.get(); }
T* data() { return fPtr.get(); }
/**
* Transfer ownership of the ptr to the caller, setting the internal
* pointer to NULL. Note that this differs from get(), which also returns
@ -335,6 +339,10 @@ public:
return fPtr[index];
}
/** Aliases matching other types, like std::vector. */
const T* data() const { return fPtr; }
T* data() { return fPtr; }
// Reallocs the array, can be used to shrink the allocation. Makes no attempt to be intelligent
void realloc(size_t count) {
if (count > kCount) {

View File

@ -77,24 +77,32 @@ constexpr int static kStackPreallocCount = 10;
template<typename TContainer, typename TCount>
static void test_container_apis(skiatest::Reporter* reporter) {
REPORTER_ASSERT(reporter, !TContainer((TCount)0).get());
REPORTER_ASSERT(reporter, !TContainer((TCount)0).data());
REPORTER_ASSERT(reporter, TContainer((TCount)1).get());
REPORTER_ASSERT(reporter, TContainer((TCount)1).data());
REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount).get());
REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount).data());
REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount + 1).get());
REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount + 1).data());
TContainer container;
// The default constructor may or may not init to empty, depending on the type of container.
container.reset((TCount)1);
REPORTER_ASSERT(reporter, container.get());
REPORTER_ASSERT(reporter, container.get() == container.data());
container.reset((TCount)kStackPreallocCount);
REPORTER_ASSERT(reporter, container.get());
REPORTER_ASSERT(reporter, container.get() == container.data());
container.reset((TCount)kStackPreallocCount + 1);
REPORTER_ASSERT(reporter, container.get());
REPORTER_ASSERT(reporter, container.get() == container.data());
container.reset((TCount)0);
REPORTER_ASSERT(reporter, !container.get());
REPORTER_ASSERT(reporter, !container.data());
}
DEF_TEST(TemplateContainerAPIs, reporter) {