Skip to content

Commit

Permalink
fix test for MSVC
Browse files Browse the repository at this point in the history
  • Loading branch information
slyshykO committed Mar 17, 2019
1 parent 92cc6c6 commit bb61d36
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 29 deletions.
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@ endif ()

project (FANN)

INCLUDE(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG(-std=c++11 COMPILER_SUPPORTS_CXX11)
if(MSVC)
if(MSVC_VERSION GREATER_EQUAL "1800")
set(COMPILER_SUPPORTS_CXX11 ON)
else()
set(COMPILER_SUPPORTS_CXX11 OFF)
endif()
else()
INCLUDE(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG(-std=c++11 COMPILER_SUPPORTS_CXX11)
endif()

IF(NOT COMPILER_SUPPORTS_CXX11)
message(WARNING "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Tests will not be compiled. To enable tests use a compiler that supports C++11.")
Expand Down
34 changes: 18 additions & 16 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ include(CheckCXXCompilerFlag)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/include)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/lib/googletest/include)

CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX14)
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has C++14 support.")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
elseif(COMPILER_SUPPORTS_CXX11)
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has C++11 support.")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has C++0x support.")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++0x, C++11 or C++14 support. FANN will still work with no problem, but the tests will not be able to compile.")
return()
endif()
if(NOT MSVC)
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX14)
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has C++14 support.")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
elseif(COMPILER_SUPPORTS_CXX11)
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has C++11 support.")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has C++0x support.")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++0x, C++11 or C++14 support. FANN will still work with no problem, but the tests will not be able to compile.")
return()
endif()
endif(NOT MSVC)

ADD_EXECUTABLE(fann_tests main.cpp fann_test.cpp fann_test_data.cpp fann_test_train.cpp)
target_link_libraries(fann_tests gtest doublefann)
21 changes: 14 additions & 7 deletions tests/fann_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ void FannTest::AssertWeights(neural_net &net, fann_type min, fann_type max, fann

TEST_F(FannTest, CreateStandardThreeLayers) {
neural_net net(LAYER, 3, 2, 3, 4);
AssertCreateAndCopy(net, 3, (const unsigned int[]) {2, 3, 4}, 11, 25);
unsigned int layers[] = {2, 3, 4};
AssertCreateAndCopy(net, 3, layers, 11, 25);
}

TEST_F(FannTest, CreateStandardThreeLayersUsingCreateMethod) {
Expand Down Expand Up @@ -90,12 +91,14 @@ TEST_F(FannTest, CreateStandardFourLayersVector) {

TEST_F(FannTest, CreateSparseFourLayers) {
neural_net net(0.5, 4, 2, 3, 4, 5);
AssertCreateAndCopy(net, 4, (const unsigned int[]){2, 3, 4, 5}, 17, 31);
unsigned int layers[] = {2, 3, 4, 5};
AssertCreateAndCopy(net, 4, layers, 17, 31);
}

TEST_F(FannTest, CreateSparseFourLayersUsingCreateMethod) {
ASSERT_TRUE(net.create_sparse(0.5f, 4, 2, 3, 4, 5));
AssertCreateAndCopy(net, 4, (const unsigned int[]){2, 3, 4, 5}, 17, 31);
unsigned int layers[] = {2, 3, 4, 5};
AssertCreateAndCopy(net, 4, layers, 17, 31);
}

TEST_F(FannTest, CreateSparseArrayFourLayers) {
Expand All @@ -118,13 +121,15 @@ TEST_F(FannTest, CreateSparseArrayWithMinimalConnectivity) {

TEST_F(FannTest, CreateShortcutFourLayers) {
neural_net net(SHORTCUT, 4, 2, 3, 4, 5);
AssertCreateAndCopy(net, 4, (const unsigned int[]){2, 3, 4, 5}, 15, 83);
unsigned int layers[] = {2, 3, 4, 5};
AssertCreateAndCopy(net, 4, layers, 15, 83);
EXPECT_EQ(SHORTCUT, net.get_network_type());
}

TEST_F(FannTest, CreateShortcutFourLayersUsingCreateMethod) {
ASSERT_TRUE(net.create_shortcut(4, 2, 3, 4, 5));
AssertCreateAndCopy(net, 4, (const unsigned int[]){2, 3, 4, 5}, 15, 83);
unsigned int layers[] = {2, 3, 4, 5};
AssertCreateAndCopy(net, 4, layers, 15, 83);
EXPECT_EQ(SHORTCUT, net.get_network_type());
}

Expand All @@ -148,7 +153,8 @@ TEST_F(FannTest, CreateFromFile) {
ASSERT_TRUE(netToBeSaved.save("tmpfile"));

neural_net netToBeLoaded("tmpfile");
AssertCreateAndCopy(netToBeLoaded, 3, (const unsigned int[]){2, 3, 4}, 11, 25);
unsigned int layers[] = {2, 3, 4};
AssertCreateAndCopy(netToBeLoaded, 3, layers, 11, 25);
}

TEST_F(FannTest, CreateFromFileUsingCreateMethod) {
Expand All @@ -158,7 +164,8 @@ TEST_F(FannTest, CreateFromFileUsingCreateMethod) {

ASSERT_TRUE(net.create_from_file("tmpfile"));

AssertCreateAndCopy(net, 3, (const unsigned int[]){2, 3, 4}, 11, 25);
unsigned int layers[] = {2, 3, 4};
AssertCreateAndCopy(net, 3, layers, 11, 25);
}

TEST_F(FannTest, RandomizeWeights) {
Expand Down
16 changes: 12 additions & 4 deletions tests/fann_test_train.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ TEST_F(FannTestTrain, TrainOnDateSimpleXor) {
TEST_F(FannTestTrain, TrainSimpleIncrementalXor) {
neural_net net(LAYER, 3, 2, 3, 1);

fann_type in_0 [] = {0.0, 0.0};
fann_type out_0 [] = {0.0};
fann_type in_1 [] = {1.0, 0.0};
fann_type out_1 [] = {1.0};
fann_type in_2 [] = {0.0, 1.0};
fann_type out_2 [] = {1.0};
fann_type in_3 [] = {1.0, 1.0};
fann_type out_3 [] = {0.0};
for(int i = 0; i < 100000; i++) {
net.train((fann_type*) (const fann_type[]) {0.0, 0.0}, (fann_type*) (const fann_type[]) {0.0});
net.train((fann_type*) (const fann_type[]) {1.0, 0.0}, (fann_type*) (const fann_type[]) {1.0});
net.train((fann_type*) (const fann_type[]) {0.0, 1.0}, (fann_type*) (const fann_type[]) {1.0});
net.train((fann_type*) (const fann_type[]) {1.0, 1.0}, (fann_type*) (const fann_type[]) {0.0});
net.train((fann_type*) in_0, (fann_type*) out_0);
net.train((fann_type*) in_1, (fann_type*) out_1);
net.train((fann_type*) in_2, (fann_type*) out_2);
net.train((fann_type*) in_3, (fann_type*) out_3);
}

EXPECT_LT(net.get_MSE(), 0.01);
Expand Down

0 comments on commit bb61d36

Please sign in to comment.