Skip to content

Commit

Permalink
Add initial test project
Browse files Browse the repository at this point in the history
  • Loading branch information
rikyoz committed Nov 5, 2022
1 parent 23edc97 commit fded8c1
Show file tree
Hide file tree
Showing 11 changed files with 341 additions and 5 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@
.qmake.stash
*.a
third_party/
bin/
build/
out/
test/files/
pack/
test/
roadmap.txt
gh-pages/
doc/wiki/
doc/xml/
doc/html/
doc/rst/
doc/md/
test*/
.vs/
x64/
\.idea/
Expand Down
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,9 @@ endif()
include( cmake/BuildOptions.cmake )

# compiler-specific options
include( cmake/CompilerOptions.cmake )
include( cmake/CompilerOptions.cmake )

if( BIT7Z_BUILD_TESTS )
# tests
add_subdirectory( test )
endif()
5 changes: 4 additions & 1 deletion cmake/BuildOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ option( BIT7Z_USE_NATIVE_STRING "Enable or disable using the OS native string ty
message( STATUS "Use native string: ${BIT7Z_USE_NATIVE_STRING}" )
if( BIT7Z_USE_NATIVE_STRING )
target_compile_definitions( ${LIB_TARGET} PUBLIC BIT7Z_USE_NATIVE_STRING )
endif()
endif()

option( BIT7Z_BUILD_TESTS "Enable or disable building the testing executable" )
message( STATUS "Build tests: ${BIT7Z_BUILD_TESTS}" )
27 changes: 27 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

# sources
set( SOURCE_FILES
src/main.cpp
src/test_bit7zlibrary.cpp )

set( TEST_TARGET bit7z${ARCH_POSTFIX}-tests )
add_executable( ${TEST_TARGET} ${SOURCE_FILES} )

option( BIT7Z_USE_SYSTEM_7ZIP "Enable or disable using system's 7-zip shared library when executing tests" )
message( STATUS "Use system 7-zip: ${BIT7Z_USE_SYSTEM_7ZIP}" )
if( BIT7Z_USE_SYSTEM_7ZIP )
target_compile_definitions( ${TEST_TARGET} PRIVATE BIT7Z_USE_SYSTEM_7ZIP )
endif()

target_link_libraries( ${TEST_TARGET} PRIVATE ${LIB_TARGET} )

# Catch2
include( cmake/Catch2.cmake )
target_link_libraries( ${TEST_TARGET} PRIVATE Catch2::Catch2 )

include( CTest )
include( Catch )
catch_discover_tests( ${TEST_TARGET} )
19 changes: 19 additions & 0 deletions test/cmake/Catch2.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

# download catch2 unit testing framework

include( FetchContent )

FetchContent_declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.13.10
)

FetchContent_MakeAvailable( Catch2 )

set_property( GLOBAL PROPERTY CTEST_TARGETS_ADDED 1 )

list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/contrib/)
56 changes: 56 additions & 0 deletions test/src/compiler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* bit7z - A C++ static library to interface with the 7-zip shared libraries.
* Copyright (c) 2014-2022 Riccardo Ostani - All Rights Reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

#ifndef COMPILER_HPP
#define COMPILER_HPP


#define STRING2( x ) #x
#define STRING( x ) STRING2(x)
#define VER_STRING( major, minor, patch ) STRING(major) "." STRING(minor) "." STRING(patch)

namespace bit7z {
namespace test {
namespace compiler {

// Compiler name and version
#if defined(__clang__)
constexpr auto name = "clang++";
constexpr auto version = VER_STRING(__clang_major__, __clang_minor__, __clang_patchlevel__);
#elif defined(__GNUC__)
# if !(defined(__MINGW32__) || defined(__MINGW64__))
constexpr auto name = "g++";
# elif defined(__MINGW64__)
constexpr auto name = "MinGW-w64";
# else
constexpr auto name = "MinGW";
# endif
constexpr auto version = VER_STRING(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
#elif defined(_MSC_VER)
constexpr auto name = "MSVC";
constexpr auto version = STRING( _MSC_VER );
#else
constexpr auto name = "Unknown";
constexpr auto name = "N/A";
#endif

// Compiler target architecture
#if defined(_WIN64) || defined(__x86_64__)
constexpr auto target_arch = "x64";
#elif defined(_WIN32) || defined(__i386__)
constexpr auto target_arch = "x86";
#else
constexpr auto target_arch = "Unsupported Architecture";
#endif

}
}
}

#endif //COMPILER_HPP
54 changes: 54 additions & 0 deletions test/src/filesystem.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* bit7z - A C++ static library to interface with the 7-zip shared libraries.
* Copyright (c) 2014-2022 Riccardo Ostani - All Rights Reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

#ifndef FILESYSTEM_HPP
#define FILESYSTEM_HPP

#include <bitfs.hpp>

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#ifdef _WIN32
#include <Windows.h>
#else
#include <climits> // for PATH_MAX
#include <unistd.h> // for readlink (Linux) or getpid (macOS)
#ifdef __APPLE__
#include <libproc.h> // for proc_pidpath and PROC_PIDPATHINFO_MAXSIZE
#endif
#endif

namespace bit7z {
namespace test {
namespace filesystem {

inline auto exe_path() -> fs::path {
#ifdef _WIN32
wchar_t path[MAX_PATH] = { 0 };
GetModuleFileNameW( nullptr, path, MAX_PATH );
return path;
#else
#ifdef __APPLE__
char result[PROC_PIDPATHINFO_MAXSIZE];
ssize_t result_size = proc_pidpath(getpid(), result, sizeof(result));
#else
char result[PATH_MAX];
ssize_t result_size = readlink("/proc/self/exe", result, PATH_MAX);
#endif
return result_size > 0 ? std::string(result, result_size) : "";
#endif
}

}
}
}

#endif //FILESYSTEM_HPP
39 changes: 39 additions & 0 deletions test/src/flags.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* bit7z - A C++ static library to interface with the 7-zip shared libraries.
* Copyright (c) 2014-2022 Riccardo Ostani - All Rights Reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

#ifndef FLAGS_HPP
#define FLAGS_HPP

namespace bit7z {
namespace test {
namespace flags {

#ifdef BIT7Z_AUTO_FORMAT
constexpr auto auto_format = "ON";
#else
constexpr auto auto_format = "OFF";
#endif

#ifdef BIT7Z_REGEX_MATCHING
constexpr auto regex_matching = "ON";
#else
constexpr auto regex_matching = "OFF";
#endif

#ifdef BIT7Z_USE_NATIVE_STRING
constexpr auto native_string = "ON";
#else
constexpr auto native_string = "OFF";
#endif

}
}
}

#endif //FLAGS_HPP
40 changes: 40 additions & 0 deletions test/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com

/*
* bit7z - A C++ static library to interface with the 7-zip shared libraries.
* Copyright (c) 2014-2022 Riccardo Ostani - All Rights Reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

#define CATCH_CONFIG_RUNNER
#include <catch2/catch.hpp> //IMPORTANT: it must be included before any other include!

#include <iostream>

#include "compiler.hpp"
#include "flags.hpp"

int main( int argc, char* argv[] ) {
using namespace bit7z::test;

std::cout << "[Compiler]" << std::endl;
std::cout << "Name: " << compiler::name << std::endl;
std::cout << "Version: " << compiler::version << std::endl;
std::cout << "Target Architecture: " << compiler::target_arch << std::endl << std::endl;

#ifdef _WIN32
std::cout << "[Runtime]" << std::endl;
std::cout << "Code page: " << GetACP() << std::endl << std::endl;
#endif

std::cout << "[Flags]" << std::endl;
std::cout << "BIT7Z_AUTO_FORMAT: " << flags::auto_format << std::endl;
std::cout << "BIT7Z_REGEX_MATCHING: " << flags::regex_matching << std::endl;
std::cout << "BIT7Z_USE_NATIVE_STRING: " << flags::native_string << std::endl << std::endl;

return Catch::Session().run( argc, argv );
}
42 changes: 42 additions & 0 deletions test/src/shared_lib.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* bit7z - A C++ static library to interface with the 7-zip shared libraries.
* Copyright (c) 2014-2022 Riccardo Ostani - All Rights Reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

#ifndef SHARED_LIB_HPP
#define SHARED_LIB_HPP

#include <bittypes.hpp>

#ifndef BIT7Z_USE_SYSTEM_7ZIP
#include "filesystem.hpp"
#endif

namespace bit7z {
namespace test {

inline auto sevenzip_lib_path() -> tstring {
#ifdef BIT7Z_USE_SYSTEM_7ZIP
#ifdef _WIN32
static const tstring lib_path = BIT7Z_STRING( "C:\\Program Files\\7-Zip\\7z.dll" );
#elif defined( __linux__ )
static const tstring lib_path = "/usr/lib/p7zip/7z.so"; //default installation path of p7zip shared library
#else
static const tstring lib_path = "./7z.so";
#endif
#elif defined(_WIN32)
static const auto lib_path = ( filesystem::exe_path().parent_path() / "7z.dll" ).string< tchar >();
#else
static const auto lib_path = ( filesystem::exe_path().parent_path() / "7z.so" ).string();
#endif
return lib_path;
}

}
}

#endif //SHARED_LIB_HPP
53 changes: 53 additions & 0 deletions test/src/test_bit7zlibrary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com

/*
* bit7z - A C++ static library to interface with the 7-zip shared libraries.
* Copyright (c) 2014-2022 Riccardo Ostani - All Rights Reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <catch2/catch.hpp>

#include <bit7zlibrary.hpp>
#include <bitexception.hpp>

#include "shared_lib.hpp"

namespace bit7z {
namespace test {

TEST_CASE( "Bit7zLibrary: Constructing from non-existing shared library", "[bit7zlibrary]" ) {
REQUIRE_THROWS_WITH( Bit7zLibrary( BIT7Z_STRING( "NonExisting7z.dll" ) ),
Catch::Matchers::StartsWith( "Failed to load 7-zip library" ) );

#ifdef _WIN32
REQUIRE_THROWS_MATCHES( Bit7zLibrary( BIT7Z_STRING( "NonExisting7z.dll" ) ),
BitException,
Catch::Matchers::Predicate< BitException >( [ & ]( const BitException& ex ) -> bool {
return std::strcmp( ex.code().category().name(), "HRESULT" );
}, "Error code should be E_FAIL" ) );
#else
REQUIRE_THROWS_MATCHES( Bit7zLibrary( BIT7Z_STRING( "NonExisting7z.dll" ) ),
BitException,
Catch::Matchers::Predicate< BitException >( [ & ]( const BitException& ex ) -> bool {
return ex.code() == std::errc::bad_file_descriptor;
}, "Error code should be bad file descriptor" ) );
#endif
}

TEST_CASE( "Bit7zLibrary: Normal construction", "[bit7zlibrary]" ) {
const auto lib_path = sevenzip_lib_path();
INFO( "Library path: " << lib_path )

Bit7zLibrary lib{ lib_path };

SECTION( "setLargePageMode" ) {
REQUIRE_NOTHROW( lib.setLargePageMode() );
}
}

}
}

0 comments on commit fded8c1

Please sign in to comment.