From fc09386164e36c2ba765e8741da89ebeb500d26a Mon Sep 17 00:00:00 2001 From: Miki Rozloznik Date: Tue, 21 Jan 2025 15:20:03 +0100 Subject: [PATCH] Add others tests --- test/others/gif/CMakeLists.txt | 32 +++++++++ test/others/gif/ClangTidySuppressions.txt | 28 ++++++++ test/others/gif/cpp/GifTest.cpp | 83 +++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 test/others/gif/CMakeLists.txt create mode 100644 test/others/gif/ClangTidySuppressions.txt create mode 100644 test/others/gif/cpp/GifTest.cpp diff --git a/test/others/gif/CMakeLists.txt b/test/others/gif/CMakeLists.txt new file mode 100644 index 0000000..24419fd --- /dev/null +++ b/test/others/gif/CMakeLists.txt @@ -0,0 +1,32 @@ +add_library(gif_zs STATIC ${TEST_ZS_ROOT}/gif.zs) +zserio_generate_cpp( + TARGET gif_zs + SRC_DIR ${TEST_ZS_ROOT} + GEN_DIR ${CMAKE_CURRENT_BINARY_DIR}/gen + EXTRA_ARGS ${ZSERIO_EXTRA_ARGS} + GENERATED_SOURCES_VAR GENERATED_SOURCES + OUTPUT_VAR ZSERIO_LOG + ERROR_VAR ZSERIO_LOG +) +target_link_libraries(gif_zs PUBLIC ZserioCpp17Runtime) +if (ZSERIO_LOG) + file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/zserio_log.txt "${ZSERIO_LOG}") + check_zserio_warnings("${ZSERIO_LOG}" 0) +endif () + +add_custom_command(OUTPUT gif_data_copy + COMMAND ${CMAKE_COMMAND} -E copy_directory ${TEST_ZS_ROOT}/../data + ${CMAKE_CURRENT_BINARY_DIR}/data + DEPENDS ${CMAKE_SOURCE_DIR}/data/others/gif + COMMENT "Copying data directory for gif test") +add_custom_target(gif_data_copy_target DEPENDS gif_data_copy) +add_dependencies(gif_zs gif_data_copy_target) + +add_custom_test(gif + DEPENDS + gif_zs + SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/cpp/GifTest.cpp + GENERATED_SOURCES + ${GENERATED_SOURCES} +) diff --git a/test/others/gif/ClangTidySuppressions.txt b/test/others/gif/ClangTidySuppressions.txt new file mode 100644 index 0000000..848cdb3 --- /dev/null +++ b/test/others/gif/ClangTidySuppressions.txt @@ -0,0 +1,28 @@ +bugprone-exception-escape:gen/gif/gif_data/ApplicationExtension.cpp +bugprone-exception-escape:gen/gif/gif_data/BlockData.cpp +bugprone-exception-escape:gen/gif/gif_data/BlockData.h +bugprone-exception-escape:gen/gif/gif_data/CommentExtension.cpp +bugprone-exception-escape:gen/gif/gif_data/CommentExtension.h +bugprone-exception-escape:gen/gif/gif_data/ExtensionBlock.cpp +bugprone-exception-escape:gen/gif/gif_data/ExtensionTypes.h +bugprone-exception-escape:gen/gif/gif_data/GifData.cpp +bugprone-exception-escape:gen/gif/gif_data/GifData.h +bugprone-exception-escape:gen/gif/gif_data/ImageBlock.cpp +bugprone-exception-escape:gen/gif/gif_data/PlainTextExtension.cpp +bugprone-exception-escape:gen/gif/gif_data/RasterData.cpp +bugprone-exception-escape:gen/gif/gif_data/RasterData.h +bugprone-exception-escape:gen/gif/gif_data/SubBlock.cpp +bugprone-exception-escape:gen/gif/gif_data/ZippedBlockData.cpp +bugprone-exception-escape:gen/gif/gif_data/ZippedBlockData.h +bugprone-exception-escape:gen/gif/gif_data/ZippedSubBlock.cpp +bugprone-exception-escape:gen/gif/GifFile.cpp + +bugprone-misplaced-widening-cast:gen/gif/gif_data/ImageDescriptor.cpp +bugprone-misplaced-widening-cast:gen/gif/screen_descriptor/ScreenDescriptor.cpp + +cppcoreguidelines-pro-type-reinterpret-cast:cpp/GifTest.cpp + +hicpp-signed-bitwise:gen/gif/gif_data/ImageDescriptor.cpp +hicpp-signed-bitwise:gen/gif/screen_descriptor/ScreenDescriptor.cpp + +misc-no-recursion # reports also tested_release and some system headers diff --git a/test/others/gif/cpp/GifTest.cpp b/test/others/gif/cpp/GifTest.cpp new file mode 100644 index 0000000..05c3696 --- /dev/null +++ b/test/others/gif/cpp/GifTest.cpp @@ -0,0 +1,83 @@ +#include + +#include "gif/GifFile.h" +#include "gtest/gtest.h" +#include "zserio/RebindAlloc.h" +#include "zserio/SerializeUtil.h" + +namespace gif +{ + +using AllocatorType = GifFile::AllocatorType; +using StringType = zserio::BasicString>; +template +using VectorType = zserio::Vector>; +using BitBufferType = zserio::BasicBitBuffer; + +class GifTest : public ::testing::Test +{ +protected: + bool readFileToBuffer(const StringType& fileName, VectorType& buffer) + { + std::ifstream inputStream(fileName.c_str(), std::ios::binary); + if (!inputStream) + { + return false; + } + + inputStream.seekg(0, inputStream.end); + const size_t fileSize = static_cast(inputStream.tellg()); + inputStream.seekg(0, inputStream.beg); + buffer.resize(fileSize); + inputStream.read(reinterpret_cast(buffer.data()), static_cast(buffer.size())); + const bool result = static_cast((inputStream)); + inputStream.close(); + + return result; + } + + void convertUInt8ArrayToString(const VectorType& array, StringType& outputString) + { + for (zserio::UInt8 element : array) + { + outputString.append(1, static_cast(element)); + } + } +}; + +TEST_F(GifTest, OnePixGif) +{ + const StringType onePixGifFileName("others/gif/data/1pix.gif"); + VectorType buffer; + ASSERT_TRUE(readFileToBuffer(onePixGifFileName, buffer)); + GifFile data; + zserio::deserialize(BitBufferType(buffer), data); + + StringType fileFormat; + convertUInt8ArrayToString(data.signature.format, fileFormat); + const StringType expectedGifFileFormat("GIF"); + ASSERT_EQ(expectedGifFileFormat, fileFormat); + + StringType fileVersion; + convertUInt8ArrayToString(data.signature.version, fileVersion); + const StringType expectedGifFileVersion("89a"); + ASSERT_EQ(expectedGifFileVersion, fileVersion); + + const screen_descriptor::ScreenDescriptor& screenDescriptor = data.screen; + const uint16_t expectedGifScreenWidth = 256; + ASSERT_EQ(expectedGifScreenWidth, screenDescriptor.width); + + const uint16_t expectedGifScreenHeight = 256; + ASSERT_EQ(expectedGifScreenHeight, screenDescriptor.height); + + const uint8_t expectedGifScreenBgColor = 255; + ASSERT_EQ(expectedGifScreenBgColor, screenDescriptor.bgColor); + + const uint8_t expectedScreenBitsOfColorResolution = 7; + ASSERT_EQ(expectedScreenBitsOfColorResolution, screenDescriptor.bitsOfColorResolution); + + const uint8_t expectedScreenBitsPerPixel = 7; + ASSERT_EQ(expectedScreenBitsPerPixel, screenDescriptor.bitsPerPixel); +} + +} // namespace gif