Skip to content

Commit dcf3152

Browse files
committed
Add unit tests with gtest
1 parent 135d367 commit dcf3152

File tree

261 files changed

+102328
-1038
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

261 files changed

+102328
-1038
lines changed

CMakeLists.txt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,24 @@ set(build_version 0.0.3)
1313

1414
project(mim VERSION ${build_version})
1515

16-
1716
set(is_root_project OFF)
1817

19-
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
18+
# Check if we are the root project
19+
if(PROJECT_NAME STREQUAL CMAKE_PROJECT_NAME)
2020
set(is_root_project ON)
2121
endif()
2222

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

2628
include(GNUInstallDirs)
2729

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

32+
# include the global configuration file
33+
include(cmake/GlobalConfig.cmake)
3034

3135

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

4650
target_sources(${PROJECT_NAME} PRIVATE
47-
src/_dummy.cpp
51+
src/random.cpp
4852
)
4953

5054

@@ -63,6 +67,12 @@ endif()
6367

6468
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)
6569

70+
if (MIM_BUILD_TEST)
71+
enable_testing()
72+
add_subdirectory(ext)
73+
add_subdirectory(test)
74+
endif()
75+
6676
if(MIM_INSTALL)
6777
include(GNUInstallDirs)
6878
include(CMakePackageConfigHelpers)

cmake/GlobalConfig.cmake

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
include(cmake/Macros.cmake)
2+
3+
# TODO: I need more data on CYGWIN and MSYS to know if they are supported or not.
4+
# Try to look into this further at a later date.
5+
6+
# check for cygwin
7+
if(CYGWIN)
8+
if(NOT MIM_NO_CMAKE_WARNINGS)
9+
message(WARNING "MIM:
10+
Cygwin may not be supported. If you notice any issues please report it!
11+
REPORT ALL ISSUES HERE: https://github.com/Rinzii/mim/issues/
12+
13+
To disable this warning set MIM_NO_CMAKE_WARNINGS to ON
14+
" )
15+
endif ()
16+
endif()
17+
18+
# check for msys
19+
if(MSYS)
20+
if(NOT MIM_NO_CMAKE_WARNINGS)
21+
message(WARNING "MIM:
22+
MSYS may not be supported. If you notice any issues please report it!
23+
REPORT ALL ISSUES HERE: https://github.com/Rinzii/mim/issues/
24+
25+
To disable this warning set MIM_NO_CMAKE_WARNINGS to ON
26+
" )
27+
endif ()
28+
endif()
29+
30+
31+
32+
# detect our OS
33+
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
34+
set(MIM_OS_WINDOWS 1)
35+
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
36+
set(MIM_OS_LINUX 1)
37+
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
38+
set(MIM_OS_MACOS 1)
39+
elseif(CMAKE_SYSTEM_NAME MATCHES "^k?FreeBSD$")
40+
set(MIM_OS_FREEBSD 1)
41+
elseif(CMAKE_SYSTEM_NAME MATCHES "^OpenBSD$")
42+
set(MIM_OS_OPENBSD 1)
43+
elseif(CMAKE_SYSTEM_NAME MATCHES "^NetBSD$")
44+
set(MIM_OS_NETBSD 1)
45+
else()
46+
message(FATAL_ERROR "Unsupported OS: ${CMAKE_SYSTEM_NAME}")
47+
endif()
48+
49+
50+
# detect the compiler
51+
# Note: The detection order is important because:
52+
# - Visual Studio can both use MSVC and Clang
53+
# - GNUCXX can still be set on macOS when using Clang
54+
if(MSVC)
55+
set(MIM_COMPILER_MSVC 1)
56+
57+
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
58+
set(MIM_COMPILER_CLANG_CL 1)
59+
endif()
60+
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
61+
set(MIM_COMPILER_CLANG 1)
62+
elseif(CMAKE_COMPILER_IS_GNUCXX)
63+
set(MIM_COMPILER_GCC 1)
64+
65+
execute_process(COMMAND "${CMAKE_CXX_COMPILER}" "--version" OUTPUT_VARIABLE GCC_COMPILER_VERSION)
66+
string(REGEX MATCHALL ".*(tdm[64]*-[1-9]).*" MIM_COMPILER_GCC_TDM "${GCC_COMPILER_VERSION}")
67+
else()
68+
message(FATAL_ERROR "Unsupported compiler")
69+
return()
70+
endif()
71+
72+
73+
# detect the architecture (note: this test won't work for cross-compilation)
74+
include(CheckTypeSize)
75+
check_type_size(void* SIZEOF_VOID_PTR)
76+
if(${SIZEOF_VOID_PTR} STREQUAL "4")
77+
set(MIM_ARCH_32BITS 1)
78+
elseif(${SIZEOF_VOID_PTR} STREQUAL "8")
79+
set(MIM_ARCH_64BITS 1)
80+
else()
81+
message(FATAL_ERROR "Unsupported architecture")
82+
return()
83+
endif()
84+
85+
86+
87+
88+
# TODO: Maybe define macros internally here so that we don't have to define them with preprocessor macros later.
89+
90+

cmake/Macros.cmake

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
macro ( mim_message_color NAME )
2+
mim_message ( COLOR ${NAME} " ${NAME}" )
3+
endmacro ()
4+
5+
function ( text )
6+
cmake_parse_arguments ( PARSE_ARGV 0 "_TEXT" "BOLD" "COLOR" "" )
7+
8+
set ( _TEXT_OPTIONS -E cmake_echo_color --no-newline )
9+
10+
if ( _TEXT_COLOR )
11+
string ( TOLOWER "${_TEXT_COLOR}" _TEXT_COLOR_LOWER )
12+
if( _TEXT_COLOR_LOWER STREQUAL "warning" )
13+
set ( _TEXT_COLOR_LOWER "yellow" )
14+
endif ()
15+
if ( NOT ${_TEXT_COLOR_LOWER} MATCHES "^default|black|red|green|yellow|warning|blue|magenta|cyan|white" )
16+
mim_message ( "Only these colours are supported:" )
17+
mim_message_color ( DEFAULT )
18+
mim_message_color ( BLACK )
19+
mim_message_color ( RED )
20+
mim_message_color ( GREEN )
21+
mim_message_color ( YELLOW )
22+
mim_message_color ( WARNING )
23+
mim_message_color ( BLUE )
24+
mim_message_color ( MAGENTA )
25+
mim_message_color ( CYAN )
26+
mim_message_color ( WHITE )
27+
TEXT ( WARING "Color ${_TEXT_COLOR} is not support." )
28+
else ()
29+
list ( APPEND _TEXT_OPTIONS --${_TEXT_COLOR_LOWER} )
30+
endif ()
31+
endif ()
32+
33+
if ( _TEXT_BOLD )
34+
list ( APPEND _TEXT_OPTIONS --bold )
35+
endif ()
36+
37+
execute_process ( COMMAND ${CMAKE_COMMAND} -E env CLICOLOR_FORCE=1 ${CMAKE_COMMAND} ${_TEXT_OPTIONS} "-- " ${_TEXT_UNPARSED_ARGUMENTS}
38+
OUTPUT_VARIABLE _TEXT_RESULT
39+
ECHO_ERROR_VARIABLE
40+
)
41+
42+
set ( TEXT_RESULT ${_TEXT_RESULT} PARENT_SCOPE )
43+
endfunction ()
44+
unset ( mim_message_color )
45+
46+
function ( mim_message )
47+
text ( ${ARGN} )
48+
message ( ${TEXT_RESULT} )
49+
endfunction ()

config.cmake.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
@PACKAGE_INIT@
22

33
include("${CMAKE_CURRENT_LIST_DIR}/mim-targets.cmake")
4+
5+

ext/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
project(mim-ext)
2+
3+
4+
if (MIM_BUILD_TEST)
5+
# PThreads are not available on Windows
6+
# So tell gtest to not use them.
7+
if(MIM_WINDOWS)
8+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
9+
set(gtest_disable_pthreads ON CACHE BOOL "" FORCE)
10+
endif()
11+
12+
set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
13+
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
14+
set(gtest_build_samples OFF CACHE BOOL "" FORCE)
15+
set(gtest_build_tests OFF CACHE BOOL "" FORCE)
16+
add_subdirectory(googletest)
17+
add_library(gtest::gtest ALIAS gtest)
18+
endif()
19+
20+
find_package(Threads REQUIRED)
21+
22+
add_library(mim-ext INTERFACE)
23+
add_library(mim::ext ALIAS mim-ext)
24+
25+
if(MIM_BUILD_TEST)
26+
target_link_libraries(mim-ext INTERFACE
27+
gtest::gtest
28+
)
29+
endif ()
30+
31+
target_link_libraries(mim-ext INTERFACE
32+
Threads::Threads
33+
)

ext/googletest/.gitignore

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Ignore CI build directory
2+
build/
3+
xcuserdata
4+
cmake-build-debug/
5+
.idea/
6+
bazel-bin
7+
bazel-genfiles
8+
bazel-googletest
9+
bazel-out
10+
bazel-testlogs
11+
# python
12+
*.pyc
13+
14+
# Visual Studio files
15+
.vs
16+
*.sdf
17+
*.opensdf
18+
*.VC.opendb
19+
*.suo
20+
*.user
21+
_ReSharper.Caches/
22+
Win32-Debug/
23+
Win32-Release/
24+
x64-Debug/
25+
x64-Release/
26+
27+
# Ignore autoconf / automake files
28+
Makefile.in
29+
aclocal.m4
30+
configure
31+
build-aux/
32+
autom4te.cache/
33+
googletest/m4/libtool.m4
34+
googletest/m4/ltoptions.m4
35+
googletest/m4/ltsugar.m4
36+
googletest/m4/ltversion.m4
37+
googletest/m4/lt~obsolete.m4
38+
googlemock/m4
39+
40+
# Ignore generated directories.
41+
googlemock/fused-src/
42+
googletest/fused-src/
43+
44+
# macOS files
45+
.DS_Store
46+
googletest/.DS_Store
47+
googletest/xcode/.DS_Store
48+
49+
# Ignore cmake generated directories and files.
50+
CMakeFiles
51+
CTestTestfile.cmake
52+
Makefile
53+
cmake_install.cmake
54+
googlemock/CMakeFiles
55+
googlemock/CTestTestfile.cmake
56+
googlemock/Makefile
57+
googlemock/cmake_install.cmake
58+
googlemock/gtest
59+
/bin
60+
/googlemock/gmock.dir
61+
/googlemock/gmock_main.dir
62+
/googlemock/RUN_TESTS.vcxproj.filters
63+
/googlemock/RUN_TESTS.vcxproj
64+
/googlemock/INSTALL.vcxproj.filters
65+
/googlemock/INSTALL.vcxproj
66+
/googlemock/gmock_main.vcxproj.filters
67+
/googlemock/gmock_main.vcxproj
68+
/googlemock/gmock.vcxproj.filters
69+
/googlemock/gmock.vcxproj
70+
/googlemock/gmock.sln
71+
/googlemock/ALL_BUILD.vcxproj.filters
72+
/googlemock/ALL_BUILD.vcxproj
73+
/lib
74+
/Win32
75+
/ZERO_CHECK.vcxproj.filters
76+
/ZERO_CHECK.vcxproj
77+
/RUN_TESTS.vcxproj.filters
78+
/RUN_TESTS.vcxproj
79+
/INSTALL.vcxproj.filters
80+
/INSTALL.vcxproj
81+
/googletest-distribution.sln
82+
/CMakeCache.txt
83+
/ALL_BUILD.vcxproj.filters
84+
/ALL_BUILD.vcxproj

ext/googletest/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Note: CMake support is community-based. The maintainers do not use CMake
2+
# internally.
3+
4+
cmake_minimum_required(VERSION 3.5)
5+
6+
if (POLICY CMP0048)
7+
cmake_policy(SET CMP0048 NEW)
8+
endif (POLICY CMP0048)
9+
10+
if (POLICY CMP0069)
11+
cmake_policy(SET CMP0069 NEW)
12+
endif (POLICY CMP0069)
13+
14+
if (POLICY CMP0077)
15+
cmake_policy(SET CMP0077 NEW)
16+
endif (POLICY CMP0077)
17+
18+
project(googletest-distribution)
19+
set(GOOGLETEST_VERSION 1.13.0)
20+
21+
if(NOT CYGWIN AND NOT MSYS AND NOT ${CMAKE_SYSTEM_NAME} STREQUAL QNX)
22+
set(CMAKE_CXX_EXTENSIONS OFF)
23+
endif()
24+
25+
enable_testing()
26+
27+
include(CMakeDependentOption)
28+
include(GNUInstallDirs)
29+
30+
#Note that googlemock target already builds googletest
31+
option(BUILD_GMOCK "Builds the googlemock subproject" ON)
32+
option(INSTALL_GTEST "Enable installation of googletest. (Projects embedding googletest may want to turn this OFF.)" ON)
33+
option(GTEST_HAS_ABSL "Use Abseil and RE2. Requires Abseil and RE2 to be separately added to the build." OFF)
34+
35+
if(BUILD_GMOCK)
36+
add_subdirectory( googlemock )
37+
else()
38+
add_subdirectory( googletest )
39+
endif()

ext/googletest/LICENSE

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Copyright 2008, Google Inc.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are
6+
met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
* Redistributions in binary form must reproduce the above
11+
copyright notice, this list of conditions and the following disclaimer
12+
in the documentation and/or other materials provided with the
13+
distribution.
14+
* Neither the name of Google Inc. nor the names of its
15+
contributors may be used to endorse or promote products derived from
16+
this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

0 commit comments

Comments
 (0)