Commit f9daeba5 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Added tests for zlib compression

parent c3073f50
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -28,3 +28,16 @@ if( BUILD_CUDA )
      add_test( ${target} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${target}${CMAKE_EXECUTABLE_SUFFIX} )
   endforeach()
endif()

# special test needing external library
find_package( ZLIB )
if( ZLIB_FOUND )
   foreach( target IN ITEMS zlibCompressionTest )
      add_executable( ${target} ${target}.cpp )
      target_compile_definitions( ${target} PUBLIC "-DHAVE_ZLIB" )
      target_compile_options( ${target} PRIVATE ${CXX_TESTS_FLAGS} )
      target_include_directories(${target} PUBLIC ${ZLIB_INCLUDE_DIRS})
      target_link_libraries( ${target} ${GTEST_BOTH_LIBRARIES} ${ZLIB_LIBRARIES} )
      add_test( ${target} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${target}${CMAKE_EXECUTABLE_SUFFIX} )
   endforeach()
endif()
+67 −0
Original line number Diff line number Diff line
/***************************************************************************
                          base64.cpp  -  description
                             -------------------
    begin                : Apr 11, 2021
    copyright            : (C) 2021 by Tomas Oberhuber et al.
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/* See Copyright Notice in tnl/Copyright */

#include <sstream>
#ifdef HAVE_GTEST
#include <gtest/gtest.h>
#endif

#include <sstream>

#include <TNL/zlib_compression.h>

#ifdef HAVE_GTEST
template< typename HeaderType >
void test_compress( std::string input, std::string expected_output )
{
   std::stringstream str;
   TNL::write_compressed_block< HeaderType >( input.c_str(), input.length(), str );
   const std::string output = str.str();
   EXPECT_EQ( output, expected_output );
}

template< typename HeaderType >
void test_decompress( std::string input, std::string expected_output )
{
   // decompress C string
   {
      const auto result = TNL::decompress_block< HeaderType, char >( input.c_str() );
      EXPECT_EQ( result.first, expected_output.length() );
      const std::string output( result.second.get(), result.first );
      EXPECT_EQ( output, expected_output );
   }
   // decompress stream
   {
      std::stringstream str( input );
      const auto result = TNL::decompress_block< HeaderType, char >( str );
      EXPECT_EQ( result.first, expected_output.length() );
      const std::string output( result.second.get(), result.first );
      EXPECT_EQ( output, expected_output );
   }
}

TEST( base64Test, compress_string )
{
   test_compress< std::int32_t > ( "hello, world", "AQAAAAwAAAAMAAAAFAAAAA==eJzLSM3JyddRKM8vykkBAB1UBIk=" );
   test_compress< std::uint32_t >( "hello, world", "AQAAAAwAAAAMAAAAFAAAAA==eJzLSM3JyddRKM8vykkBAB1UBIk=" );
   test_compress< std::int64_t > ( "hello, world", "AQAAAAAAAAAMAAAAAAAAAAwAAAAAAAAAFAAAAAAAAAA=eJzLSM3JyddRKM8vykkBAB1UBIk=" );
   test_compress< std::uint64_t >( "hello, world", "AQAAAAAAAAAMAAAAAAAAAAwAAAAAAAAAFAAAAAAAAAA=eJzLSM3JyddRKM8vykkBAB1UBIk=" );
}

TEST( base64Test, decode )
{
   test_decompress< std::int32_t  >( "AQAAAAwAAAAMAAAAFAAAAA==eJzLSM3JyddRKM8vykkBAB1UBIk=", "hello, world" );
   test_decompress< std::uint32_t >( "AQAAAAwAAAAMAAAAFAAAAA==eJzLSM3JyddRKM8vykkBAB1UBIk=", "hello, world" );
   test_decompress< std::int64_t  >( "AQAAAAAAAAAMAAAAAAAAAAwAAAAAAAAAFAAAAAAAAAA=eJzLSM3JyddRKM8vykkBAB1UBIk=", "hello, world" );
   test_decompress< std::uint64_t >( "AQAAAAAAAAAMAAAAAAAAAAwAAAAAAAAAFAAAAAAAAAA=eJzLSM3JyddRKM8vykkBAB1UBIk=", "hello, world" );
}
#endif

#include "main.h"