Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding google-test infra-structure. #151

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Tests/UnitTest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# File based on: https://github.com/google/googletest/blob/master/googletest/README.md
project(UnitTests)
cmake_minimum_required(VERSION 3.15)
set (CMAKE_CXX_STANDARD 11)

# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)

execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)

if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()

execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()

# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)

# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()

# Now simply link against gtest or gtest_main as needed. Eg
add_executable(unit_test unit_test.cpp)
target_link_libraries(unit_test gtest_main)
add_test(NAME test COMMAND unit_test)
16 changes: 16 additions & 0 deletions Tests/UnitTest/CMakeLists.txt.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# File based on: https://github.com/google/googletest/blob/master/googletest/README.md
cmake_minimum_required(VERSION 2.8.2)

project(googletest-download NONE)

include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
10 changes: 10 additions & 0 deletions Tests/UnitTest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Build Instructions
## CMake
Download CMake for free from [here](http://www.cmake.org)

## Generate Makefiles
> cmake CMakeLists.txt
> make

# Running the tests
> ./run_unit_tests
54 changes: 54 additions & 0 deletions Tests/UnitTest/unit_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <gtest/gtest.h>
#include <Eigen/Dense>
#include "../../Optimizer/optimizer"

#include <string>

namespace util
{
double plus_ten_square(double x)
{
return std::pow(x + 10, 2);
}

template <typename T>
auto transform_precision(T in, int precision) -> T
{
std::stringstream ss;
ss << std::setprecision(precision) << in;
return std::stod(ss.str());
}
} // namespace util

TEST(SingleVariable, BoundingPhase)
{
double initial_point = 5.4;
auto result = Optimizer::BoundingPhase(util::plus_ten_square, initial_point);
EXPECT_EQ(util::transform_precision(result(0), 5), util::transform_precision(-26.1, 5));
EXPECT_EQ(util::transform_precision(result(1), 5), util::transform_precision( -2.1, 5));
EXPECT_EQ(result.size(), 2);
}

TEST(SingleVariable, ExhaustiveSearch)
{
double initial_point = 5.4;
auto result = Optimizer::Exhaustive(util::plus_ten_square, initial_point);
EXPECT_EQ(util::transform_precision(result(0), 5), util::transform_precision(-10.6, 5));
EXPECT_EQ(util::transform_precision(result(1), 5), util::transform_precision( -9.6, 5));
EXPECT_EQ(result.size(), 2);
}

TEST(SingleVariable, Derivatives)
{
double initial_point = 5.4;
auto result = Optimizer::Derivative(util::plus_ten_square, initial_point);
EXPECT_EQ(util::transform_precision(result(0), 5), util::transform_precision(30.8, 5));
EXPECT_EQ(util::transform_precision(result(1), 6), util::transform_precision( 1.99975, 6));
EXPECT_EQ(result.size(), 2);
}

int main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}