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

added test suite #32

Merged
merged 1 commit into from
Mar 15, 2022
Merged
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
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ project(PSOPT VERSION 5.0.0 LANGUAGES CXX)

option(BUILD_EXAMPLES "Build examples from the example subdirectory." OFF)
option(WITH_SNOPT_INTERFACE "Build interface for the SNOPT optimizer." OFF)
option(BUILD_TESTS "Build tests for PSOPT." OFF)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
find_package(Eigen3 REQUIRED NO_MODULE)
Expand Down Expand Up @@ -37,7 +38,9 @@ if(${BUILD_EXAMPLES})
add_subdirectory(examples/)
endif()

# set(CMAKE_CXX_FLAGS "-g")
if(${BUILD_TESTS})
add_subdirectory(tests/)
endif()


# INSTALLING and PAKAGING
Expand Down
48 changes: 48 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
cmake_minimum_required(VERSION 3.10)
project(PSOPT_Tests)

find_package(GTest)
find_package(Threads REQUIRED)

if(NOT ${GTest_FOUND})
message(STATUS "Google Test not found. It will be downloaded.")
# 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()

add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
endif()

file(GLOB SRC "*.cpp")
add_executable(${PROJECT_NAME} ${SRC})
add_dependencies(${PROJECT_NAME} PSOPT gtest)

target_link_libraries(${PROJECT_NAME} PRIVATE
gtest_main
Threads::Threads
Eigen3::Eigen
adolc
PkgConfig::ipopt
PSOPT
)

if(${WITH_SNOPT_INTERFACE})
enable_language(Fortran)
target_link_libraries(${PROJECT_NAME} PRIVATE gfortran ${snopt7_LIBRARIES} PSOPT_SNOPT_interface)
endif()

enable_testing()
add_test(test ${PROJECT_NAME})
15 changes: 15 additions & 0 deletions tests/CMakeLists.txt.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.10)

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 ""
)
17 changes: 17 additions & 0 deletions tests/test_something.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "gtest/gtest.h"
#include <Eigen/Dense>
#include <psopt.h>

typedef Eigen::Matrix<adouble, 3, 1> Vector3ad;

TEST(Utils, crossProduct) {
Vector3ad a, b, c;
a << 1, 2, 3;
b << 1, 5, 7;

cross(a.data(), b.data(), c.data());

ASSERT_EQ(c[0], -1);
ASSERT_EQ(c[1], -4);
ASSERT_EQ(c[2], 3);
}
6 changes: 6 additions & 0 deletions tests/tests_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "gtest/gtest.h"

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