Skip to content

Commit

Permalink
Add unit tests with gtest
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinzii committed Jun 6, 2023
1 parent 135d367 commit dcf3152
Show file tree
Hide file tree
Showing 261 changed files with 102,328 additions and 1,038 deletions.
16 changes: 13 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ set(build_version 0.0.3)

project(mim VERSION ${build_version})


set(is_root_project OFF)

if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
# Check if we are the root project
if(PROJECT_NAME STREQUAL CMAKE_PROJECT_NAME)
set(is_root_project ON)
endif()

# TO THE IMPLEMENTOR: If MIM_BUILD_TEST is set to OFF then googletest can be deleted from the ext folder.
option(MIM_BUILD_TEST "Build mim tests" ${is_root_project})
option(MIM_INSTALL "Setup install and package steps" ${is_root_project})
option(MIM_NO_CMAKE_WARNINGS "Suppress mim warnings outputted through cmake" OFF)

include(GNUInstallDirs)

configure_file(src/version.hpp.in "${CMAKE_CURRENT_BINARY_DIR}/include/${PROJECT_NAME}/version.hpp" @ONLY)

# include the global configuration file
include(cmake/GlobalConfig.cmake)


add_library(${PROJECT_NAME})
Expand All @@ -44,7 +48,7 @@ include(header_list.cmake)
target_sources(${PROJECT_NAME} PRIVATE ${mim_headers})

target_sources(${PROJECT_NAME} PRIVATE
src/_dummy.cpp
src/random.cpp
)


Expand All @@ -63,6 +67,12 @@ endif()

target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)

if (MIM_BUILD_TEST)
enable_testing()
add_subdirectory(ext)
add_subdirectory(test)
endif()

if(MIM_INSTALL)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
Expand Down
90 changes: 90 additions & 0 deletions cmake/GlobalConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
include(cmake/Macros.cmake)

# TODO: I need more data on CYGWIN and MSYS to know if they are supported or not.
# Try to look into this further at a later date.

# check for cygwin
if(CYGWIN)
if(NOT MIM_NO_CMAKE_WARNINGS)
message(WARNING "MIM:
Cygwin may not be supported. If you notice any issues please report it!
REPORT ALL ISSUES HERE: https://github.com/Rinzii/mim/issues/
To disable this warning set MIM_NO_CMAKE_WARNINGS to ON
" )
endif ()
endif()

# check for msys
if(MSYS)
if(NOT MIM_NO_CMAKE_WARNINGS)
message(WARNING "MIM:
MSYS may not be supported. If you notice any issues please report it!
REPORT ALL ISSUES HERE: https://github.com/Rinzii/mim/issues/
To disable this warning set MIM_NO_CMAKE_WARNINGS to ON
" )
endif ()
endif()



# detect our OS
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
set(MIM_OS_WINDOWS 1)
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
set(MIM_OS_LINUX 1)
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
set(MIM_OS_MACOS 1)
elseif(CMAKE_SYSTEM_NAME MATCHES "^k?FreeBSD$")
set(MIM_OS_FREEBSD 1)
elseif(CMAKE_SYSTEM_NAME MATCHES "^OpenBSD$")
set(MIM_OS_OPENBSD 1)
elseif(CMAKE_SYSTEM_NAME MATCHES "^NetBSD$")
set(MIM_OS_NETBSD 1)
else()
message(FATAL_ERROR "Unsupported OS: ${CMAKE_SYSTEM_NAME}")
endif()


# detect the compiler
# Note: The detection order is important because:
# - Visual Studio can both use MSVC and Clang
# - GNUCXX can still be set on macOS when using Clang
if(MSVC)
set(MIM_COMPILER_MSVC 1)

if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(MIM_COMPILER_CLANG_CL 1)
endif()
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(MIM_COMPILER_CLANG 1)
elseif(CMAKE_COMPILER_IS_GNUCXX)
set(MIM_COMPILER_GCC 1)

execute_process(COMMAND "${CMAKE_CXX_COMPILER}" "--version" OUTPUT_VARIABLE GCC_COMPILER_VERSION)
string(REGEX MATCHALL ".*(tdm[64]*-[1-9]).*" MIM_COMPILER_GCC_TDM "${GCC_COMPILER_VERSION}")
else()
message(FATAL_ERROR "Unsupported compiler")
return()
endif()


# detect the architecture (note: this test won't work for cross-compilation)
include(CheckTypeSize)
check_type_size(void* SIZEOF_VOID_PTR)
if(${SIZEOF_VOID_PTR} STREQUAL "4")
set(MIM_ARCH_32BITS 1)
elseif(${SIZEOF_VOID_PTR} STREQUAL "8")
set(MIM_ARCH_64BITS 1)
else()
message(FATAL_ERROR "Unsupported architecture")
return()
endif()




# TODO: Maybe define macros internally here so that we don't have to define them with preprocessor macros later.


49 changes: 49 additions & 0 deletions cmake/Macros.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
macro ( mim_message_color NAME )
mim_message ( COLOR ${NAME} " ${NAME}" )
endmacro ()

function ( text )
cmake_parse_arguments ( PARSE_ARGV 0 "_TEXT" "BOLD" "COLOR" "" )

set ( _TEXT_OPTIONS -E cmake_echo_color --no-newline )

if ( _TEXT_COLOR )
string ( TOLOWER "${_TEXT_COLOR}" _TEXT_COLOR_LOWER )
if( _TEXT_COLOR_LOWER STREQUAL "warning" )
set ( _TEXT_COLOR_LOWER "yellow" )
endif ()
if ( NOT ${_TEXT_COLOR_LOWER} MATCHES "^default|black|red|green|yellow|warning|blue|magenta|cyan|white" )
mim_message ( "Only these colours are supported:" )
mim_message_color ( DEFAULT )
mim_message_color ( BLACK )
mim_message_color ( RED )
mim_message_color ( GREEN )
mim_message_color ( YELLOW )
mim_message_color ( WARNING )
mim_message_color ( BLUE )
mim_message_color ( MAGENTA )
mim_message_color ( CYAN )
mim_message_color ( WHITE )
TEXT ( WARING "Color ${_TEXT_COLOR} is not support." )
else ()
list ( APPEND _TEXT_OPTIONS --${_TEXT_COLOR_LOWER} )
endif ()
endif ()

if ( _TEXT_BOLD )
list ( APPEND _TEXT_OPTIONS --bold )
endif ()

execute_process ( COMMAND ${CMAKE_COMMAND} -E env CLICOLOR_FORCE=1 ${CMAKE_COMMAND} ${_TEXT_OPTIONS} "-- " ${_TEXT_UNPARSED_ARGUMENTS}
OUTPUT_VARIABLE _TEXT_RESULT
ECHO_ERROR_VARIABLE
)

set ( TEXT_RESULT ${_TEXT_RESULT} PARENT_SCOPE )
endfunction ()
unset ( mim_message_color )

function ( mim_message )
text ( ${ARGN} )
message ( ${TEXT_RESULT} )
endfunction ()
2 changes: 2 additions & 0 deletions config.cmake.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/mim-targets.cmake")


33 changes: 33 additions & 0 deletions ext/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
project(mim-ext)


if (MIM_BUILD_TEST)
# PThreads are not available on Windows
# So tell gtest to not use them.
if(MIM_WINDOWS)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
set(gtest_disable_pthreads ON CACHE BOOL "" FORCE)
endif()

set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
set(gtest_build_samples OFF CACHE BOOL "" FORCE)
set(gtest_build_tests OFF CACHE BOOL "" FORCE)
add_subdirectory(googletest)
add_library(gtest::gtest ALIAS gtest)
endif()

find_package(Threads REQUIRED)

add_library(mim-ext INTERFACE)
add_library(mim::ext ALIAS mim-ext)

if(MIM_BUILD_TEST)
target_link_libraries(mim-ext INTERFACE
gtest::gtest
)
endif ()

target_link_libraries(mim-ext INTERFACE
Threads::Threads
)
84 changes: 84 additions & 0 deletions ext/googletest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Ignore CI build directory
build/
xcuserdata
cmake-build-debug/
.idea/
bazel-bin
bazel-genfiles
bazel-googletest
bazel-out
bazel-testlogs
# python
*.pyc

# Visual Studio files
.vs
*.sdf
*.opensdf
*.VC.opendb
*.suo
*.user
_ReSharper.Caches/
Win32-Debug/
Win32-Release/
x64-Debug/
x64-Release/

# Ignore autoconf / automake files
Makefile.in
aclocal.m4
configure
build-aux/
autom4te.cache/
googletest/m4/libtool.m4
googletest/m4/ltoptions.m4
googletest/m4/ltsugar.m4
googletest/m4/ltversion.m4
googletest/m4/lt~obsolete.m4
googlemock/m4

# Ignore generated directories.
googlemock/fused-src/
googletest/fused-src/

# macOS files
.DS_Store
googletest/.DS_Store
googletest/xcode/.DS_Store

# Ignore cmake generated directories and files.
CMakeFiles
CTestTestfile.cmake
Makefile
cmake_install.cmake
googlemock/CMakeFiles
googlemock/CTestTestfile.cmake
googlemock/Makefile
googlemock/cmake_install.cmake
googlemock/gtest
/bin
/googlemock/gmock.dir
/googlemock/gmock_main.dir
/googlemock/RUN_TESTS.vcxproj.filters
/googlemock/RUN_TESTS.vcxproj
/googlemock/INSTALL.vcxproj.filters
/googlemock/INSTALL.vcxproj
/googlemock/gmock_main.vcxproj.filters
/googlemock/gmock_main.vcxproj
/googlemock/gmock.vcxproj.filters
/googlemock/gmock.vcxproj
/googlemock/gmock.sln
/googlemock/ALL_BUILD.vcxproj.filters
/googlemock/ALL_BUILD.vcxproj
/lib
/Win32
/ZERO_CHECK.vcxproj.filters
/ZERO_CHECK.vcxproj
/RUN_TESTS.vcxproj.filters
/RUN_TESTS.vcxproj
/INSTALL.vcxproj.filters
/INSTALL.vcxproj
/googletest-distribution.sln
/CMakeCache.txt
/ALL_BUILD.vcxproj.filters
/ALL_BUILD.vcxproj
39 changes: 39 additions & 0 deletions ext/googletest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Note: CMake support is community-based. The maintainers do not use CMake
# internally.

cmake_minimum_required(VERSION 3.5)

if (POLICY CMP0048)
cmake_policy(SET CMP0048 NEW)
endif (POLICY CMP0048)

if (POLICY CMP0069)
cmake_policy(SET CMP0069 NEW)
endif (POLICY CMP0069)

if (POLICY CMP0077)
cmake_policy(SET CMP0077 NEW)
endif (POLICY CMP0077)

project(googletest-distribution)
set(GOOGLETEST_VERSION 1.13.0)

if(NOT CYGWIN AND NOT MSYS AND NOT ${CMAKE_SYSTEM_NAME} STREQUAL QNX)
set(CMAKE_CXX_EXTENSIONS OFF)
endif()

enable_testing()

include(CMakeDependentOption)
include(GNUInstallDirs)

#Note that googlemock target already builds googletest
option(BUILD_GMOCK "Builds the googlemock subproject" ON)
option(INSTALL_GTEST "Enable installation of googletest. (Projects embedding googletest may want to turn this OFF.)" ON)
option(GTEST_HAS_ABSL "Use Abseil and RE2. Requires Abseil and RE2 to be separately added to the build." OFF)

if(BUILD_GMOCK)
add_subdirectory( googlemock )
else()
add_subdirectory( googletest )
endif()
28 changes: 28 additions & 0 deletions ext/googletest/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Copyright 2008, Google Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Loading

0 comments on commit dcf3152

Please sign in to comment.