From 8679402f240f9a9095e633a56008f0c10a38d4f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 25 Dec 2016 15:05:45 +0100 Subject: [PATCH] Equality comparison operators for PixelStorage. --- src/Magnum/PixelStorage.cpp | 21 ++++++++++ src/Magnum/PixelStorage.h | 16 ++++++++ src/Magnum/Test/PixelStorageTest.cpp | 57 ++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) diff --git a/src/Magnum/PixelStorage.cpp b/src/Magnum/PixelStorage.cpp index 0b0e99971..e938cbf39 100644 --- a/src/Magnum/PixelStorage.cpp +++ b/src/Magnum/PixelStorage.cpp @@ -156,6 +156,21 @@ std::size_t PixelStorage::pixelSize(PixelFormat format, PixelType type) { CORRADE_ASSERT_UNREACHABLE(); /* LCOV_EXCL_LINE */ } +bool PixelStorage::operator==(const PixelStorage& other) const { + return + #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) + _rowLength == other._rowLength && + #endif + #ifndef MAGNUM_TARGET_GLES2 + _imageHeight == other._imageHeight && + #endif + _skip == other._skip && + #ifndef MAGNUM_TARGET_GLES + _swapBytes == other._swapBytes && + #endif + _alignment == other._alignment; +} + std::tuple, Math::Vector3, std::size_t> PixelStorage::dataProperties(const PixelFormat format, const PixelType type, const Vector3i& size) const { const std::size_t pixelSize = PixelStorage::pixelSize(format, type); const Math::Vector3 dataSize{ @@ -269,6 +284,12 @@ void PixelStorage::applyUnpack() { } #ifndef MAGNUM_TARGET_GLES +bool CompressedPixelStorage::operator==(const CompressedPixelStorage& other) const { + return PixelStorage::operator==(other) && + _blockSize == other._blockSize && + _blockDataSize == other._blockDataSize; +} + void CompressedPixelStorage::applyInternal(const bool isUnpack) { PixelStorage::applyInternal(isUnpack); diff --git a/src/Magnum/PixelStorage.h b/src/Magnum/PixelStorage.h index 51b42da92..2b60ed62e 100644 --- a/src/Magnum/PixelStorage.h +++ b/src/Magnum/PixelStorage.h @@ -81,6 +81,14 @@ class MAGNUM_EXPORT PixelStorage { */ constexpr /*implicit*/ PixelStorage() noexcept; + /** @brief Equality comparison */ + bool operator==(const PixelStorage& other) const; + + /** @brief Non-equality comparison */ + bool operator!=(const PixelStorage& other) const { + return !operator==(other); + } + #ifndef MAGNUM_TARGET_GLES /** * @brief Whether to reverse byte order @@ -275,6 +283,14 @@ class MAGNUM_EXPORT CompressedPixelStorage: public PixelStorage { #endif /*implicit*/ CompressedPixelStorage() noexcept: _blockSize{0}, _blockDataSize{0} {} + /** @brief Equality comparison */ + bool operator==(const CompressedPixelStorage& other) const; + + /** @brief Non-equality comparison */ + bool operator!=(const CompressedPixelStorage& other) const { + return !operator==(other); + } + /** @brief Compressed block size */ constexpr Vector3i compressedBlockSize() const { return _blockSize; } diff --git a/src/Magnum/Test/PixelStorageTest.cpp b/src/Magnum/Test/PixelStorageTest.cpp index 15de32f0b..20d0e3cd4 100644 --- a/src/Magnum/Test/PixelStorageTest.cpp +++ b/src/Magnum/Test/PixelStorageTest.cpp @@ -36,6 +36,11 @@ struct PixelStorageTest: TestSuite::Tester { void pixelSize(); + void compare(); + #ifndef MAGNUM_TARGET_GLES + void compareCompressed(); + #endif + void dataProperties(); void dataPropertiesAlignment(); #if !(defined(MAGNUM_TARGET_WEBGL) && defined(MAGNUM_TARGET_GLES2)) @@ -63,6 +68,11 @@ typedef Math::Vector3 Vector3st; PixelStorageTest::PixelStorageTest() { addTests({&PixelStorageTest::pixelSize, + &PixelStorageTest::compare, + #ifndef MAGNUM_TARGET_GLES + &PixelStorageTest::compareCompressed, + #endif + &PixelStorageTest::dataProperties, &PixelStorageTest::dataPropertiesAlignment, #if !(defined(MAGNUM_TARGET_WEBGL) && defined(MAGNUM_TARGET_GLES2)) @@ -95,6 +105,53 @@ void PixelStorageTest::pixelSize() { CORRADE_COMPARE(PixelStorage::pixelSize(PixelFormat::DepthStencil, PixelType::UnsignedInt248), 4); } +void PixelStorageTest::compare() { + PixelStorage a; + a + #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) + .setRowLength(1) + #endif + #ifndef MAGNUM_TARGET_GLES2 + .setImageHeight(15) + #endif + .setSkip({1, 3, 4}) + #ifndef MAGNUM_TARGET_GLES + .setSwapBytes(true) + #endif + .setAlignment(3); + + CORRADE_VERIFY(a == a); + CORRADE_VERIFY(a != PixelStorage{}); + CORRADE_VERIFY(PixelStorage{} == PixelStorage{}); + #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) + CORRADE_VERIFY(PixelStorage{}.setRowLength(15) != PixelStorage{}.setRowLength(17)); + #endif + #ifndef MAGNUM_TARGET_GLES2 + CORRADE_VERIFY(PixelStorage{}.setImageHeight(32) != PixelStorage{}.setImageHeight(31)); + #endif + CORRADE_VERIFY(PixelStorage{}.setSkip({1, 5, 7}) != PixelStorage{}.setSkip({7, 1, 5})); + #ifndef MAGNUM_TARGET_GLES + CORRADE_VERIFY(PixelStorage{}.setSwapBytes(false) != PixelStorage{}.setSwapBytes(true)); + #endif + CORRADE_VERIFY(PixelStorage{}.setAlignment(3) != PixelStorage{}.setAlignment(5)); +} + +#ifndef MAGNUM_TARGET_GLES +void PixelStorageTest::compareCompressed() { + CompressedPixelStorage a; + a.setSkip({16, 2, 1}) + .setCompressedBlockSize({4, 8, 2}) + .setCompressedBlockDataSize(16); + + CORRADE_VERIFY(a == a); + CORRADE_VERIFY(a != CompressedPixelStorage{}); + CORRADE_VERIFY(CompressedPixelStorage{} == CompressedPixelStorage{}); + CORRADE_VERIFY(CompressedPixelStorage{}.setSkip({16, 4, 17}) != CompressedPixelStorage{}.setSkip({4, 35, 12})); + CORRADE_VERIFY(CompressedPixelStorage{}.setCompressedBlockSize({2, 7, 19}) != CompressedPixelStorage{}.setCompressedBlockSize({2, 7, 16})); + CORRADE_VERIFY(CompressedPixelStorage{}.setCompressedBlockDataSize(32) != CompressedPixelStorage{}.setCompressedBlockDataSize(30)); +} +#endif + void PixelStorageTest::dataProperties() { PixelStorage storage; storage.setAlignment(1);