Skip to content

Commit

Permalink
Add others tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Jan 21, 2025
1 parent 1b65bd6 commit fc09386
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/others/gif/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}
)
28 changes: 28 additions & 0 deletions test/others/gif/ClangTidySuppressions.txt
Original file line number Diff line number Diff line change
@@ -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
83 changes: 83 additions & 0 deletions test/others/gif/cpp/GifTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <fstream>

#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<zserio::RebindAlloc<AllocatorType, char>>;
template <typename T>
using VectorType = zserio::Vector<T, zserio::RebindAlloc<AllocatorType, T>>;
using BitBufferType = zserio::BasicBitBuffer<AllocatorType>;

class GifTest : public ::testing::Test
{
protected:
bool readFileToBuffer(const StringType& fileName, VectorType<uint8_t>& 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<size_t>(inputStream.tellg());
inputStream.seekg(0, inputStream.beg);
buffer.resize(fileSize);
inputStream.read(reinterpret_cast<char*>(buffer.data()), static_cast<std::streamsize>(buffer.size()));
const bool result = static_cast<bool>((inputStream));
inputStream.close();

return result;
}

void convertUInt8ArrayToString(const VectorType<zserio::UInt8>& array, StringType& outputString)
{
for (zserio::UInt8 element : array)
{
outputString.append(1, static_cast<char>(element));
}
}
};

TEST_F(GifTest, OnePixGif)
{
const StringType onePixGifFileName("others/gif/data/1pix.gif");
VectorType<uint8_t> 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

0 comments on commit fc09386

Please sign in to comment.