diff --git a/README.md b/README.md index 78a137954..a1b66269d 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,7 @@ - [Using the superbuild pattern](chapter-08/recipe-01/README.md) - [Managing dependencies with a superbuild I. The Boost libraries](chapter-08/recipe-02/README.md) - [Managing dependencies with a superbuild II. The FFTW library](chapter-08/recipe-03/README.md) -- [Managing dependencies with a superbuild III. The Eigen library](chapter-08/recipe-04/README.md) +- [Managing dependencies with a superbuild III. The Google test framework](chapter-08/recipe-04/README.md) - [Managing your project as a superbuild](chapter-08/recipe-05/README.md) diff --git a/chapter-08/README.md b/chapter-08/README.md index 274132c12..3b9d1d487 100644 --- a/chapter-08/README.md +++ b/chapter-08/README.md @@ -3,5 +3,5 @@ - [Using the superbuild pattern](recipe-01/README.md) - [Managing dependencies with a superbuild I. The Boost libraries](recipe-02/README.md) - [Managing dependencies with a superbuild II. The FFTW library](recipe-03/README.md) -- [Managing dependencies with a superbuild III. The Eigen library](recipe-04/README.md) +- [Managing dependencies with a superbuild III. The Google test framework](recipe-04/README.md) - [Managing your project as a superbuild](recipe-05/README.md) diff --git a/chapter-08/recipe-04/README.md b/chapter-08/recipe-04/README.md index b1abd9e8f..fdd99985f 100644 --- a/chapter-08/recipe-04/README.md +++ b/chapter-08/recipe-04/README.md @@ -1,4 +1,4 @@ -# Managing dependencies with a superbuild III. The Eigen library +# Managing dependencies with a superbuild III. The Google test framework Abstract to be written ... diff --git a/chapter-08/recipe-04/cxx-example-3.5/CMakeLists.txt b/chapter-08/recipe-04/cxx-example-3.5/CMakeLists.txt index bf5478371..129937c55 100644 --- a/chapter-08/recipe-04/cxx-example-3.5/CMakeLists.txt +++ b/chapter-08/recipe-04/cxx-example-3.5/CMakeLists.txt @@ -1,34 +1,53 @@ +# set minimum cmake version cmake_minimum_required(VERSION 3.5 FATAL_ERROR) -project(recipe-04 LANGUAGES CXX) +# project name and language +project(recipe-03 LANGUAGES CXX) +# require C++11 set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_STANDARD_REQUIRED ON) -set_property(DIRECTORY PROPERTY EP_BASE ${CMAKE_BINARY_DIR}/subprojects) - -option(Eigen3_FORCE_SUPERBUILD "Always build Eigen3 on our own" OFF) - -add_subdirectory(external/upstream) - -include(ExternalProject) -ExternalProject_Add(${PROJECT_NAME}_core - DEPENDS - eigen3_external - SOURCE_DIR - ${CMAKE_CURRENT_SOURCE_DIR}/src - CMAKE_ARGS - -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} - -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} - -DCMAKE_CXX_EXTENSIONS=${CMAKE_CXX_EXTENSIONS} - -DCMAKE_CXX_STANDARD_REQUIRED=${CMAKE_CXX_STANDARD_REQUIRED} - -DEigen3_DIR=${Eigen3_DIR} - CMAKE_CACHE_ARGS - -DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS} - -DCMAKE_PREFIX_PATH:PATH=${CMAKE_PREFIX_PATH} - BUILD_ALWAYS - 1 - INSTALL_COMMAND - "" - ) +set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) + +# example library +add_library(sum_integers sum_integers.cpp) + +# main code +add_executable(sum_up main.cpp) +target_link_libraries(sum_up sum_integers) + +# we will use the network to fetch Google Test sources +# make it possible to disable unit tests when not on network +option(ENABLE_UNIT_TESTS "Enable unit tests" ON) +message(STATUS "Enable testing: ${ENABLE_UNIT_TESTS}") + +include(googletest.cmake) + +if(ENABLE_UNIT_TESTS) + fetch_googletest( + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_BINARY_DIR}/googletest + ) + + add_executable(cpp_test "") + + target_sources(cpp_test + PRIVATE + test.cpp + ) + + target_link_libraries(cpp_test + PRIVATE + sum_integers + gtest_main + ) + + enable_testing() + + add_test( + NAME google_test + COMMAND $<TARGET_FILE:cpp_test> + ) +endif() diff --git a/chapter-08/recipe-04/cxx-example-3.5/external/upstream/CMakeLists.txt b/chapter-08/recipe-04/cxx-example-3.5/external/upstream/CMakeLists.txt deleted file mode 100644 index f566a8ff3..000000000 --- a/chapter-08/recipe-04/cxx-example-3.5/external/upstream/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -set(DEPS_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/Deps) -message(STATUS "Installing dependencies to: ${DEPS_INSTALL_PREFIX}") - -add_subdirectory(eigen3) diff --git a/chapter-08/recipe-04/cxx-example-3.5/external/upstream/eigen3/CMakeLists.txt b/chapter-08/recipe-04/cxx-example-3.5/external/upstream/eigen3/CMakeLists.txt deleted file mode 100644 index 309f21406..000000000 --- a/chapter-08/recipe-04/cxx-example-3.5/external/upstream/eigen3/CMakeLists.txt +++ /dev/null @@ -1,27 +0,0 @@ -find_package(Eigen3 3.3.4 QUIET CONFIG) -if(TARGET Eigen3::Eigen AND (NOT Eigen3_FORCE_SUPERBUILD)) - message(STATUS "Found Eigen3: ${EIGEN3_INCLUDE_DIR} (found version ${EIGEN3_VERSION_STRING})") - add_library(eigen3_external INTERFACE) # dummy -else() - if(Eigen3_FORCE_SUPERBUILD) - message(STATUS "Forcing superbuild of Eigen3.") - else() - message(STATUS "Suitable Eigen3 could not be located. Downloading and building!") - endif() - include(ExternalProject) - ExternalProject_Add(eigen3_external - HG_REPOSITORY - https://bitbucket.org/eigen/eigen - HG_TAG - 3.3.4 - UPDATE_COMMAND - "" - CMAKE_ARGS - -DCMAKE_INSTALL_PREFIX=${DEPS_INSTALL_PREFIX} - ) - set(Eigen3_DIR ${DEPS_INSTALL_PREFIX}/share/eigen3/cmake - CACHE PATH - "Path to internally built Eigen3Config.cmake" - FORCE - ) -endif() diff --git a/chapter-08/recipe-04/cxx-example-3.5/googletest-download.cmake b/chapter-08/recipe-04/cxx-example-3.5/googletest-download.cmake new file mode 100644 index 000000000..b7e12cf4d --- /dev/null +++ b/chapter-08/recipe-04/cxx-example-3.5/googletest-download.cmake @@ -0,0 +1,20 @@ +# code copied from https://crascit.com/2015/07/25/cmake-gtest/ +cmake_minimum_required(VERSION 3.5 FATAL_ERROR) + +project(googletest-download LANGUAGES NONE) + +include(ExternalProject) + +ExternalProject_Add( + googletest + SOURCE_DIR "@GOOGLETEST_DOWNLOAD_ROOT@/googletest-src" + BINARY_DIR "@GOOGLETEST_DOWNLOAD_ROOT@/googletest-build" + GIT_REPOSITORY + https://github.com/google/googletest.git + GIT_TAG + release-1.8.0 + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" + ) diff --git a/chapter-08/recipe-04/cxx-example-3.5/googletest.cmake b/chapter-08/recipe-04/cxx-example-3.5/googletest.cmake new file mode 100644 index 000000000..ccc4eaf9c --- /dev/null +++ b/chapter-08/recipe-04/cxx-example-3.5/googletest.cmake @@ -0,0 +1,50 @@ +# download and unpack googletest at configure time + +# the following code to fetch googletest +# is inspired by and adapted after https://crascit.com/2015/07/25/cmake-gtest/ + +function(fetch_googletest _download_module_path _download_root) + # Variable used in googletest-download.cmake + set(GOOGLETEST_DOWNLOAD_ROOT ${_download_root}) + configure_file( + ${_download_module_path}/googletest-download.cmake + ${_download_root}/CMakeLists.txt + @ONLY + ) + unset(GOOGLETEST_DOWNLOAD_ROOT) + + execute_process( + COMMAND + "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" . + WORKING_DIRECTORY + ${_download_root} + ) + execute_process( + COMMAND + "${CMAKE_COMMAND}" --build . + WORKING_DIRECTORY + ${_download_root} + ) + + # Prevent GoogleTest from overriding our compiler/linker options + # when building with Visual Studio + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + # Prevent GoogleTest from using PThreads + set(gtest_disable_pthreads ON CACHE BOOL "" FORCE) + + # adds the targets: gtest, gtest_main, gmock, gmock_main + add_subdirectory( + ${_download_root}/googletest-src + ${_download_root}/googletest-build + ) + + # Silence std::tr1 warning on MSVC + if(MSVC) + foreach(_tgt gtest gtest_main gmock gmock_main) + target_compile_definitions(${_tgt} + PRIVATE + "_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING" + ) + endforeach() + endif() +endfunction() diff --git a/chapter-08/recipe-04/cxx-example-3.5/main.cpp b/chapter-08/recipe-04/cxx-example-3.5/main.cpp new file mode 120000 index 000000000..487fdf54d --- /dev/null +++ b/chapter-08/recipe-04/cxx-example-3.5/main.cpp @@ -0,0 +1 @@ +../../../chapter-04/recipe-01/cxx-example/main.cpp \ No newline at end of file diff --git a/chapter-08/recipe-04/cxx-example-3.5/menu.yml b/chapter-08/recipe-04/cxx-example-3.5/menu.yml index fa0bd20a8..6c5ad3628 100644 --- a/chapter-08/recipe-04/cxx-example-3.5/menu.yml +++ b/chapter-08/recipe-04/cxx-example-3.5/menu.yml @@ -1,25 +1,2 @@ -appveyor-vs: - definitions: - - Eigen3_FORCE_SUPERBUILD: 'ON' - -appveyor-msys: - definitions: - - Eigen3_FORCE_SUPERBUILD: 'ON' - -travis-linux: - definitions: - - Eigen3_FORCE_SUPERBUILD: 'ON' - -# OpenMP does not work with clang -travis-osx: - definitions: - - Eigen3_FORCE_SUPERBUILD: 'ON' - failing_generators: - - 'Unix Makefiles' - - 'Ninja' - -local: - definitions: - - Eigen3_FORCE_SUPERBUILD: 'ON' - env: - - VERBOSE_OUTPUT: 'ON' +targets: + - test diff --git a/chapter-08/recipe-04/cxx-example-3.5/src/CMakeLists.txt b/chapter-08/recipe-04/cxx-example-3.5/src/CMakeLists.txt deleted file mode 100644 index ef4cd4039..000000000 --- a/chapter-08/recipe-04/cxx-example-3.5/src/CMakeLists.txt +++ /dev/null @@ -1,22 +0,0 @@ -cmake_minimum_required(VERSION 3.5 FATAL_ERROR) - -project(recipe-04_core LANGUAGES CXX) - -find_package(OpenMP REQUIRED) - -find_package(Eigen3 3.3.4 REQUIRED CONFIG) -message(STATUS "Found Eigen3: ${EIGEN3_INCLUDE_DIR} (found version ${EIGEN3_VERSION_STRING})") - -add_executable(linear-algebra linear-algebra.cpp) -target_link_libraries(linear-algebra - PUBLIC - Eigen3::Eigen - ) -set_target_properties( - linear-algebra - PROPERTIES - COMPILE_FLAGS - ${OpenMP_CXX_FLAGS} - LINK_FLAGS - ${OpenMP_CXX_FLAGS} - ) diff --git a/chapter-08/recipe-04/cxx-example-3.5/src/linear-algebra.cpp b/chapter-08/recipe-04/cxx-example-3.5/src/linear-algebra.cpp deleted file mode 100644 index c623cdfe2..000000000 --- a/chapter-08/recipe-04/cxx-example-3.5/src/linear-algebra.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include <chrono> -#include <cmath> -#include <cstdlib> -#include <iomanip> -#include <iostream> -#include <vector> - -#include <Eigen/Dense> - -int main(int argc, char **argv) { - if (argc != 2) { - std::cout << "Usage: ./linear-algebra dim" << std::endl; - return EXIT_FAILURE; - } - - std::chrono::time_point<std::chrono::system_clock> start, end; - std::chrono::duration<double> elapsed_seconds; - std::time_t end_time; - - std::cout << "Number of threads used by Eigen: " << Eigen::nbThreads() - << std::endl; - - // Allocate matrices and right-hand side vector - start = std::chrono::system_clock::now(); - int dim = std::atoi(argv[1]); - Eigen::MatrixXd A = Eigen::MatrixXd::Random(dim, dim); - Eigen::VectorXd b = Eigen::VectorXd::Random(dim); - end = std::chrono::system_clock::now(); - - // Report times - elapsed_seconds = end - start; - end_time = std::chrono::system_clock::to_time_t(end); - std::cout << "matrices allocated and initialized " - << std::put_time(std::localtime(&end_time), "%a %b %d %Y %r\n") - << "elapsed time: " << elapsed_seconds.count() << "s\n"; - - start = std::chrono::system_clock::now(); - // Save matrix and RHS - Eigen::MatrixXd A1 = A; - Eigen::VectorXd b1 = b; - end = std::chrono::system_clock::now(); - end_time = std::chrono::system_clock::to_time_t(end); - std::cout << "Scaling done, A and b saved " - << std::put_time(std::localtime(&end_time), "%a %b %d %Y %r\n") - << "elapsed time: " << elapsed_seconds.count() << "s\n"; - - start = std::chrono::system_clock::now(); - Eigen::VectorXd x = A.lu().solve(b); - end = std::chrono::system_clock::now(); - // Report times - elapsed_seconds = end - start; - end_time = std::chrono::system_clock::to_time_t(end); - - double relative_error = (A * x - b).norm() / b.norm(); - - std::cout << "Linear system solver done " - << std::put_time(std::localtime(&end_time), "%a %b %d %Y %r\n") - << "elapsed time: " << elapsed_seconds.count() << "s\n"; - std::cout << "relative error is " << relative_error << std::endl; - - return 0; -} diff --git a/chapter-08/recipe-04/cxx-example-3.5/sum_integers.cpp b/chapter-08/recipe-04/cxx-example-3.5/sum_integers.cpp new file mode 120000 index 000000000..c0b7a15ea --- /dev/null +++ b/chapter-08/recipe-04/cxx-example-3.5/sum_integers.cpp @@ -0,0 +1 @@ +../../../chapter-04/recipe-01/cxx-example/sum_integers.cpp \ No newline at end of file diff --git a/chapter-08/recipe-04/cxx-example-3.5/sum_integers.hpp b/chapter-08/recipe-04/cxx-example-3.5/sum_integers.hpp new file mode 120000 index 000000000..75c3410cf --- /dev/null +++ b/chapter-08/recipe-04/cxx-example-3.5/sum_integers.hpp @@ -0,0 +1 @@ +../../../chapter-04/recipe-01/cxx-example/sum_integers.hpp \ No newline at end of file diff --git a/chapter-08/recipe-04/cxx-example-3.5/test.cpp b/chapter-08/recipe-04/cxx-example-3.5/test.cpp new file mode 120000 index 000000000..2610d2964 --- /dev/null +++ b/chapter-08/recipe-04/cxx-example-3.5/test.cpp @@ -0,0 +1 @@ +../cxx-example/test.cpp \ No newline at end of file diff --git a/chapter-08/recipe-04/cxx-example/CMakeLists.txt b/chapter-08/recipe-04/cxx-example/CMakeLists.txt index d1ab7a0dd..fb23676d1 100644 --- a/chapter-08/recipe-04/cxx-example/CMakeLists.txt +++ b/chapter-08/recipe-04/cxx-example/CMakeLists.txt @@ -1,34 +1,85 @@ -cmake_minimum_required(VERSION 3.9 FATAL_ERROR) +# set minimum cmake version +cmake_minimum_required(VERSION 3.11 FATAL_ERROR) -project(recipe-04 LANGUAGES CXX) +# project name and language +project(recipe-03 LANGUAGES CXX) +# require C++11 set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_STANDARD_REQUIRED ON) -set_property(DIRECTORY PROPERTY EP_BASE ${CMAKE_BINARY_DIR}/subprojects) - -option(Eigen3_FORCE_SUPERBUILD "Always build Eigen3 on our own" OFF) - -add_subdirectory(external/upstream) - -include(ExternalProject) -ExternalProject_Add(${PROJECT_NAME}_core - DEPENDS - eigen3_external - SOURCE_DIR - ${CMAKE_CURRENT_SOURCE_DIR}/src - CMAKE_ARGS - -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} - -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} - -DCMAKE_CXX_EXTENSIONS=${CMAKE_CXX_EXTENSIONS} - -DCMAKE_CXX_STANDARD_REQUIRED=${CMAKE_CXX_STANDARD_REQUIRED} - -DEigen3_DIR=${Eigen3_DIR} - CMAKE_CACHE_ARGS - -DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS} - -DCMAKE_PREFIX_PATH:PATH=${CMAKE_PREFIX_PATH} - BUILD_ALWAYS - 1 - INSTALL_COMMAND - "" +set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) + +# example library +add_library(sum_integers sum_integers.cpp) + +# main code +add_executable(sum_up main.cpp) +target_link_libraries(sum_up sum_integers) + +# we will use the network to fetch Google Test sources +# make it possible to disable unit tests when not on network +option(ENABLE_UNIT_TESTS "Enable unit tests" ON) +message(STATUS "Enable testing: ${ENABLE_UNIT_TESTS}") + +if(ENABLE_UNIT_TESTS) + # the following code to fetch googletest + # is inspired by and adapted after: + # - https://cmake.org/cmake/help/git-stage/module/FetchContent.html + include(FetchContent) + + FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG release-1.8.0 ) + + FetchContent_GetProperties(googletest) + + if(NOT googletest_POPULATED) + FetchContent_Populate(googletest) + + # Prevent GoogleTest from overriding our compiler/linker options + # when building with Visual Studio + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + # Prevent GoogleTest from using PThreads + set(gtest_disable_pthreads ON CACHE BOOL "" FORCE) + + # adds the targers: gtest, gtest_main, gmock, gmock_main + add_subdirectory( + ${googletest_SOURCE_DIR} + ${googletest_BINARY_DIR} + ) + + # Silence std::tr1 warning on MSVC + if(MSVC) + foreach(_tgt gtest gtest_main gmock gmock_main) + target_compile_definitions(${_tgt} + PRIVATE + "_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING" + ) + endforeach() + endif() + endif() + + add_executable(cpp_test "") + + target_sources(cpp_test + PRIVATE + test.cpp + ) + + target_link_libraries(cpp_test + PRIVATE + sum_integers + gtest_main + ) + + enable_testing() + + add_test( + NAME google_test + COMMAND $<TARGET_FILE:cpp_test> + ) +endif() diff --git a/chapter-08/recipe-04/cxx-example/external/upstream/CMakeLists.txt b/chapter-08/recipe-04/cxx-example/external/upstream/CMakeLists.txt deleted file mode 100644 index f566a8ff3..000000000 --- a/chapter-08/recipe-04/cxx-example/external/upstream/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -set(DEPS_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/Deps) -message(STATUS "Installing dependencies to: ${DEPS_INSTALL_PREFIX}") - -add_subdirectory(eigen3) diff --git a/chapter-08/recipe-04/cxx-example/external/upstream/eigen3/CMakeLists.txt b/chapter-08/recipe-04/cxx-example/external/upstream/eigen3/CMakeLists.txt deleted file mode 100644 index 654a76504..000000000 --- a/chapter-08/recipe-04/cxx-example/external/upstream/eigen3/CMakeLists.txt +++ /dev/null @@ -1,28 +0,0 @@ -find_package(Eigen3 3.3.5 QUIET CONFIG) -if(TARGET Eigen3::Eigen AND (NOT Eigen3_FORCE_SUPERBUILD)) - message(STATUS "Found Eigen3: ${EIGEN3_INCLUDE_DIR} (found version ${EIGEN3_VERSION_STRING})") - add_library(eigen3_external INTERFACE) # dummy -else() - if(Eigen3_FORCE_SUPERBUILD) - message(STATUS "Forcing superbuild of Eigen3.") - else() - message(STATUS "Suitable Eigen3 could not be located. Downloading and building!") - endif() - include(ExternalProject) - ExternalProject_Add(eigen3_external - GIT_REPOSITORY - https://github.com/eigenteam/eigen-git-mirror.git - GIT_TAG - 3.3.5 - UPDATE_COMMAND - "" - CMAKE_ARGS - -DCMAKE_INSTALL_PREFIX=${DEPS_INSTALL_PREFIX} - -DBUILD_TESTING=OFF - ) - set(Eigen3_DIR ${DEPS_INSTALL_PREFIX}/share/eigen3/cmake - CACHE PATH - "Path to internally built Eigen3Config.cmake" - FORCE - ) -endif() diff --git a/chapter-08/recipe-04/cxx-example/main.cpp b/chapter-08/recipe-04/cxx-example/main.cpp new file mode 120000 index 000000000..487fdf54d --- /dev/null +++ b/chapter-08/recipe-04/cxx-example/main.cpp @@ -0,0 +1 @@ +../../../chapter-04/recipe-01/cxx-example/main.cpp \ No newline at end of file diff --git a/chapter-08/recipe-04/cxx-example/menu.yml b/chapter-08/recipe-04/cxx-example/menu.yml index fa0bd20a8..6c5ad3628 100644 --- a/chapter-08/recipe-04/cxx-example/menu.yml +++ b/chapter-08/recipe-04/cxx-example/menu.yml @@ -1,25 +1,2 @@ -appveyor-vs: - definitions: - - Eigen3_FORCE_SUPERBUILD: 'ON' - -appveyor-msys: - definitions: - - Eigen3_FORCE_SUPERBUILD: 'ON' - -travis-linux: - definitions: - - Eigen3_FORCE_SUPERBUILD: 'ON' - -# OpenMP does not work with clang -travis-osx: - definitions: - - Eigen3_FORCE_SUPERBUILD: 'ON' - failing_generators: - - 'Unix Makefiles' - - 'Ninja' - -local: - definitions: - - Eigen3_FORCE_SUPERBUILD: 'ON' - env: - - VERBOSE_OUTPUT: 'ON' +targets: + - test diff --git a/chapter-08/recipe-04/cxx-example/src/CMakeLists.txt b/chapter-08/recipe-04/cxx-example/src/CMakeLists.txt deleted file mode 100644 index a9d4acca9..000000000 --- a/chapter-08/recipe-04/cxx-example/src/CMakeLists.txt +++ /dev/null @@ -1,22 +0,0 @@ -cmake_minimum_required(VERSION 3.9 FATAL_ERROR) - -project(recipe-04_core LANGUAGES CXX) - -find_package(OpenMP REQUIRED) - -find_package(Eigen3 3.3.5 REQUIRED CONFIG) -message(STATUS "Found Eigen3: ${EIGEN3_INCLUDE_DIR} (found version ${EIGEN3_VERSION_STRING})") - -add_executable(linear-algebra linear-algebra.cpp) -target_link_libraries(linear-algebra - PUBLIC - Eigen3::Eigen - ) -set_target_properties( - linear-algebra - PROPERTIES - COMPILE_FLAGS - ${OpenMP_CXX_FLAGS} - LINK_FLAGS - ${OpenMP_CXX_FLAGS} - ) diff --git a/chapter-08/recipe-04/cxx-example/src/linear-algebra.cpp b/chapter-08/recipe-04/cxx-example/src/linear-algebra.cpp deleted file mode 100644 index c623cdfe2..000000000 --- a/chapter-08/recipe-04/cxx-example/src/linear-algebra.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include <chrono> -#include <cmath> -#include <cstdlib> -#include <iomanip> -#include <iostream> -#include <vector> - -#include <Eigen/Dense> - -int main(int argc, char **argv) { - if (argc != 2) { - std::cout << "Usage: ./linear-algebra dim" << std::endl; - return EXIT_FAILURE; - } - - std::chrono::time_point<std::chrono::system_clock> start, end; - std::chrono::duration<double> elapsed_seconds; - std::time_t end_time; - - std::cout << "Number of threads used by Eigen: " << Eigen::nbThreads() - << std::endl; - - // Allocate matrices and right-hand side vector - start = std::chrono::system_clock::now(); - int dim = std::atoi(argv[1]); - Eigen::MatrixXd A = Eigen::MatrixXd::Random(dim, dim); - Eigen::VectorXd b = Eigen::VectorXd::Random(dim); - end = std::chrono::system_clock::now(); - - // Report times - elapsed_seconds = end - start; - end_time = std::chrono::system_clock::to_time_t(end); - std::cout << "matrices allocated and initialized " - << std::put_time(std::localtime(&end_time), "%a %b %d %Y %r\n") - << "elapsed time: " << elapsed_seconds.count() << "s\n"; - - start = std::chrono::system_clock::now(); - // Save matrix and RHS - Eigen::MatrixXd A1 = A; - Eigen::VectorXd b1 = b; - end = std::chrono::system_clock::now(); - end_time = std::chrono::system_clock::to_time_t(end); - std::cout << "Scaling done, A and b saved " - << std::put_time(std::localtime(&end_time), "%a %b %d %Y %r\n") - << "elapsed time: " << elapsed_seconds.count() << "s\n"; - - start = std::chrono::system_clock::now(); - Eigen::VectorXd x = A.lu().solve(b); - end = std::chrono::system_clock::now(); - // Report times - elapsed_seconds = end - start; - end_time = std::chrono::system_clock::to_time_t(end); - - double relative_error = (A * x - b).norm() / b.norm(); - - std::cout << "Linear system solver done " - << std::put_time(std::localtime(&end_time), "%a %b %d %Y %r\n") - << "elapsed time: " << elapsed_seconds.count() << "s\n"; - std::cout << "relative error is " << relative_error << std::endl; - - return 0; -} diff --git a/chapter-08/recipe-04/cxx-example/sum_integers.cpp b/chapter-08/recipe-04/cxx-example/sum_integers.cpp new file mode 120000 index 000000000..c0b7a15ea --- /dev/null +++ b/chapter-08/recipe-04/cxx-example/sum_integers.cpp @@ -0,0 +1 @@ +../../../chapter-04/recipe-01/cxx-example/sum_integers.cpp \ No newline at end of file diff --git a/chapter-08/recipe-04/cxx-example/sum_integers.hpp b/chapter-08/recipe-04/cxx-example/sum_integers.hpp new file mode 120000 index 000000000..75c3410cf --- /dev/null +++ b/chapter-08/recipe-04/cxx-example/sum_integers.hpp @@ -0,0 +1 @@ +../../../chapter-04/recipe-01/cxx-example/sum_integers.hpp \ No newline at end of file diff --git a/chapter-08/recipe-04/cxx-example/test.cpp b/chapter-08/recipe-04/cxx-example/test.cpp new file mode 100644 index 000000000..a712f30e1 --- /dev/null +++ b/chapter-08/recipe-04/cxx-example/test.cpp @@ -0,0 +1,21 @@ +#include "sum_integers.hpp" +#include "gtest/gtest.h" + +#include <vector> + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} + +TEST(example, sum_zero) { + auto integers = {1, -1, 2, -2, 3, -3}; + auto result = sum_integers(integers); + ASSERT_EQ(result, 0); +} + +TEST(example, sum_five) { + auto integers = {1, 2, 3, 4, 5}; + auto result = sum_integers(integers); + ASSERT_EQ(result, 15); +} diff --git a/chapter-08/recipe-04/title.txt b/chapter-08/recipe-04/title.txt index f95176a18..b0ba8b6b7 100644 --- a/chapter-08/recipe-04/title.txt +++ b/chapter-08/recipe-04/title.txt @@ -1 +1 @@ -Managing dependencies with a superbuild III. The Eigen library +Managing dependencies with a superbuild III. The Google test framework