From c5c5791294c5ee6a10a0120b61339524fd2af1da Mon Sep 17 00:00:00 2001 From: Ioana Ghiban Date: Wed, 28 Feb 2024 19:48:15 +0200 Subject: [PATCH] Initialise Array2D only once `expected` and `actual` are initialised with different values. --- test/api/test_resize_to_quarter.cpp | 1 + test/api/test_rgb_and_gray.cpp | 2 +- test/api/test_scale.cpp | 1 - test/api/test_yuv_to_rgb.cpp | 4 +++- test/framework/array.h | 20 ++++++++++++-------- test/framework/kernel.h | 2 +- test/framework/operation.h | 3 +++ test/framework/test_array2d.cpp | 8 ++++++++ test/framework/test_kernel.cpp | 20 ++++++++++++++------ 9 files changed, 43 insertions(+), 18 deletions(-) diff --git a/test/api/test_resize_to_quarter.cpp b/test/api/test_resize_to_quarter.cpp index 17c62b7d3..15a319c0c 100644 --- a/test/api/test_resize_to_quarter.cpp +++ b/test/api/test_resize_to_quarter.cpp @@ -23,6 +23,7 @@ class ResizeToQuarterTest final { channels_}; test::Array2D actual{out_logical_width, dst_height, padding_, channels_}; + actual.fill(42); test::Array2D expected{out_logical_width, dst_height, padding_, channels_}; diff --git a/test/api/test_rgb_and_gray.cpp b/test/api/test_rgb_and_gray.cpp index 491f4b793..add41d4a2 100644 --- a/test/api/test_rgb_and_gray.cpp +++ b/test/api/test_rgb_and_gray.cpp @@ -26,7 +26,7 @@ class GrayTest final { source.set(0, 0, {1}); source.set(1, 0, {0xFF}); source.set(2, 0, {10, 15, 1}); - + actual.fill(42); calculate_expected(source, expected); auto err = impl(source.data(), source.stride(), actual.data(), diff --git a/test/api/test_scale.cpp b/test/api/test_scale.cpp index 4ecf57e7a..a197d3240 100644 --- a/test/api/test_scale.cpp +++ b/test/api/test_scale.cpp @@ -149,7 +149,6 @@ class ScaleTestLinear3 final : public ScaleTestLinearBase { template class ScaleTestAdd final : public ScaleTestBase { - using ArrayType = test::Array2D; using Elements = typename UnaryOperationTest::Elements; float scale() override { return 6; } diff --git a/test/api/test_yuv_to_rgb.cpp b/test/api/test_yuv_to_rgb.cpp index 05922720f..ded75ec26 100644 --- a/test/api/test_yuv_to_rgb.cpp +++ b/test/api/test_yuv_to_rgb.cpp @@ -38,6 +38,7 @@ class YuvTest final { void execute_test(F impl, size_t logical_width, bool is_nv21, size_t padding) { test::Array2D input_y{logical_width, 5, padding}; + input_y.fill(0); input_y.set(0, 0, {10, 20, 255, 199}); input_y.set(1, 0, {1, 120, 0, 17}); input_y.set(2, 0, {2, 3, 240, 228}); @@ -46,6 +47,7 @@ class YuvTest final { // the width of the UV input must be even test::Array2D input_uv{ INTRINSICCV_TARGET_NAMESPACE::align_up(logical_width, 2), 3, padding}; + input_uv.fill(0); input_uv.set(0, 0, {100, 130, 255, 255}); input_uv.set(1, 0, {0, 1, 3, 4}); input_uv.set(2, 0, {7, 8, 9, 10}); @@ -56,7 +58,7 @@ class YuvTest final { test::Array2D actual{logical_width * channel_number_, input_y.height(), padding}; - + actual.fill(42); auto err = impl(input_y.data(), input_y.stride(), input_uv.data(), input_uv.stride(), actual.data(), actual.stride(), diff --git a/test/framework/array.h b/test/framework/array.h index 83be903e2..f854c9819 100644 --- a/test/framework/array.h +++ b/test/framework/array.h @@ -218,6 +218,10 @@ class Array2D : public TwoDimensional { } private: + // Returns the number of elements between the end of one row and the start of + // the next row. + size_t padding() { return stride() / sizeof(ElementType) - width(); } + // Returns the offset to the first padding byte within a row. size_t padding_offset() const { return width() * sizeof(ElementType); } @@ -287,21 +291,21 @@ class Array2D : public TwoDimensional { // Tries to allocate backing memory. void try_allocate() { - size_t data_size = height_ * stride_; + size_t element_count = height_ * (width_ + padding()); // Allocate extra to allow weakening alignment. - size_t allocation_size = data_size + alignof(ElementType); + size_t allocation_count = element_count + 1; try { - buffer_ = std::make_unique(allocation_size); + buffer_ = + std::unique_ptr(new ElementType[allocation_count]); // Weaken alignment to flush out potential alignment issues. // buffer_.get() will contain a pointer that is at least 16-byte aligned. // By adding a small offset to that we get a pointer that is only aligned - // to alignof(ElementType). - data_ = - reinterpret_cast(buffer_.get() + alignof(ElementType)); + // to sizeof(ElementType). + data_ = buffer_.get() + 1; } catch (...) { reset(); - GTEST_FAIL() << "Failed to allocate memory of " << allocation_size + GTEST_FAIL() << "Failed to allocate memory of " << allocation_count << " bytes"; } } @@ -310,7 +314,7 @@ class Array2D : public TwoDimensional { static constexpr uint8_t kPaddingValue = std::numeric_limits::max(); // Smart pointer to the managed memory. - std::unique_ptr buffer_; + std::unique_ptr buffer_; // Pointer to the start of the data. This is offset from the start of buffer_ // to flush out potential alignment issues. ElementType *data_{nullptr}; diff --git a/test/framework/kernel.h b/test/framework/kernel.h index 0a2a3b9ed..54917a141 100644 --- a/test/framework/kernel.h +++ b/test/framework/kernel.h @@ -276,7 +276,7 @@ class KernelTest { // Input operand for the operation. Array2D input_; - // Input operand with borders, used to calculate expeected values. + // Input operand with borders, used to calculate expected values. Array2D input_with_borders_; // Expected result of the operation. Array2D expected_; diff --git a/test/framework/operation.h b/test/framework/operation.h index 08d3ec05b..29743297f 100644 --- a/test/framework/operation.h +++ b/test/framework/operation.h @@ -36,16 +36,19 @@ class OperationTest { void test() { for (auto& input : inputs_) { input = ArrayType{width(), height(), padding()}; + input.fill(0); ASSERT_TRUE(input.valid()); } for (auto& expected : expected_) { expected = ArrayType{width(), height()}; + expected.fill(0); ASSERT_TRUE(expected.valid()); } for (auto& actual : actual_) { actual = ArrayType{width(), height(), padding()}; + actual.fill(42); // fill with any value different than `expected` ASSERT_TRUE(actual.valid()); } diff --git a/test/framework/test_array2d.cpp b/test/framework/test_array2d.cpp index 2a780baec..cc5a9d50e 100644 --- a/test/framework/test_array2d.cpp +++ b/test/framework/test_array2d.cpp @@ -228,6 +228,8 @@ TEST(Array2D, ExpectEq_Equal) { size_t width = 5, height = 2; test::Array2D array_1{width, height}; test::Array2D array_2{width, height}; + array_1.fill(0); + array_2.fill(0); EXPECT_EQ_ARRAY2D(array_1, array_2); } @@ -237,6 +239,8 @@ TEST(Array2D, ExpectEq_Equal_StrideInvariant) { size_t width = 5, height = 2; test::Array2D array_1{width, height}; test::Array2D array_2{width, height, 1}; + array_1.fill(0); + array_2.fill(0); EXPECT_EQ_ARRAY2D(array_1, array_2); } @@ -293,6 +297,8 @@ TEST(Array2D, ExpectEq_NotEqual_Data) { size_t width = 5, height = 2; test::Array2D array_1{width, height}; test::Array2D array_2{width, height}; + array_1.fill(0); + array_2.fill(0); array_2.at(0, 0)[0] = 42; EXPECT_EQ_ARRAY2D(array_1, array_2); } @@ -308,6 +314,8 @@ TEST(Array2D, ExpectNe_Equal) { size_t width = 5, height = 2; test::Array2D array_1{width, height}; test::Array2D array_2{width, height}; + array_1.fill(0); + array_2.fill(0); EXPECT_NE_ARRAY2D(array_1, array_2); } }; diff --git a/test/framework/test_kernel.cpp b/test/framework/test_kernel.cpp index 81b96a8ef..c8f6a8127 100644 --- a/test/framework/test_kernel.cpp +++ b/test/framework/test_kernel.cpp @@ -16,6 +16,7 @@ TEST(Kernel, ConstructOdd) { using ElementType = uint8_t; test::Array2D mask{3, 3}; + mask.fill(0); mask.set(0, 0, {1, 2, 3}); mask.set(2, 0, {4, 5, 6}); @@ -163,15 +164,22 @@ class ExampleKernelTest : public test::KernelTest { // Disable check for possible exceptions thrown outside main. // The check isn't important in a test program. // NOLINTBEGIN(cert-err58-cpp) +template +test::Array2D fill_kernel( + size_t width, size_t height, + typename KernelTestParams::IntermediateType value) { + test::Array2D array{width, + height}; + array.fill(value); + return array; +} + template const std::array, 3> ExampleKernelTest::kKernels = { - test::Kernel{ - test::Array2D{3, 3}}, - test::Kernel{ - test::Array2D{4, 4}}, - test::Kernel{ - test::Array2D{8, 4}}, + test::Kernel{fill_kernel(3, 3, 0)}, + test::Kernel{fill_kernel(4, 4, 0)}, + test::Kernel{fill_kernel(8, 4, 0)}, }; // NOLINTEND(cert-err58-cpp) -- GitLab