Skip to content

Commit

Permalink
test refactoring and more tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
steffennissen committed Nov 1, 2015
1 parent 6b1e9c9 commit cbf0a48
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 183 deletions.
6 changes: 4 additions & 2 deletions .idea/fann.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/fann_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#endif
#endif

FANN_EXTERNAL extern FILE * FANN_API fann_default_error_log = (FILE *)-1;
FANN_EXTERNAL FILE * FANN_API fann_default_error_log = (FILE *)-1;

/* resets the last error number
*/
Expand Down
13 changes: 12 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
project (fann_tests)
include(CheckCXXCompilerFlag)

INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/include)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/lib/googletest/include)

ADD_EXECUTABLE(fann_tests create_net.cpp main.cpp fann_test.h train_data.cpp fann_test.cpp)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

ADD_EXECUTABLE(fann_tests main.cpp fann_test.cpp fann_test_data.cpp fann_test_train.cpp)
target_link_libraries(fann_tests gtest doublefann)
91 changes: 0 additions & 91 deletions tests/create_net.cpp

This file was deleted.

96 changes: 58 additions & 38 deletions tests/fann_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,9 @@ void FannTest::SetUp() {
//ensure random generator is seeded at a known value to ensure reproducible results
srand(0);
fann_disable_seed_rand();

numData = 2;
numInput = 3;
numOutput = 1;
inputValue = 1.1;
outputValue = 2.2;

inputData = new fann_type *[numData];
outputData = new fann_type *[numData];

InitializeTrainDataStructure(numData, numInput, numOutput, inputValue, outputValue, inputData, outputData);
}

void FannTest::TearDown() {
delete(inputData);
delete(outputData);
net.destroy();
data.destroy_train();
}
Expand Down Expand Up @@ -70,35 +57,68 @@ void FannTest::AssertWeights(fann_type min, fann_type max,
EXPECT_NEAR(avg, totalWeight / (fann_type) net.get_total_connections(), 0.1);
}

TEST_F(FannTest, CreateStandardThreeLayers) {
ASSERT_TRUE(net.create_standard(3, 2, 3, 4));
unsigned int layers[] = {2, 3, 4};
AssertCreateAndCopy(3, layers, 11, 25);
}

void FannTest::InitializeTrainDataStructure(unsigned int numData,
unsigned int numInput,
unsigned int numOutput,
fann_type inputValue, fann_type outputValue,
fann_type **inputData,
fann_type **outputData) {
for (unsigned int i = 0; i < numData; i++) {
inputData[i] = new fann_type[numInput];
outputData[i] = new fann_type[numOutput];
for (unsigned int j = 0; j < numInput; j++)
inputData[i][j] = inputValue;
for (unsigned int j = 0; j < numOutput; j++)
outputData[i][j] = outputValue;
}
TEST_F(FannTest, CreateStandardFourLayers) {
ASSERT_TRUE(net.create_standard(4, 2, 3, 4, 5));
unsigned int layers[] = {2, 3, 4, 5};
AssertCreateAndCopy(4, layers, 17, 50);
}

TEST_F(FannTest, CreateStandardFourLayersArray) {
unsigned int layers[] = {2, 3, 4, 5};
ASSERT_TRUE(net.create_standard_array(4, layers));
AssertCreateAndCopy(4, layers, 17, 50);
}

void FannTest::AssertTrainData(FANN::training_data &trainingData, unsigned int numData, unsigned int numInput,
unsigned int numOutput, fann_type inputValue, fann_type outputValue) {
EXPECT_EQ(numData, trainingData.length_train_data());
EXPECT_EQ(numInput, trainingData.num_input_train_data());
EXPECT_EQ(numOutput, trainingData.num_output_train_data());
TEST_F(FannTest, CreateSparseFourLayers) {
ASSERT_TRUE(net.create_sparse(0.5f, 4, 2, 3, 4, 5));
unsigned int layers[] = {2, 3, 4, 5};
AssertCreateAndCopy(4, layers, 17, 31);
}

for (int i = 0; i < numData; i++) {
for (int j = 0; j < numInput; j++)
EXPECT_DOUBLE_EQ(inputValue, trainingData.get_input()[i][j]);
for (int j = 0; j < numOutput; j++)
EXPECT_DOUBLE_EQ(outputValue, trainingData.get_output()[i][j]);
}
TEST_F(FannTest, CreateSparseArrayFourLayers) {
unsigned int layers[] = {2, 3, 4, 5};
ASSERT_TRUE(net.create_sparse_array(0.5f, 4, layers));
AssertCreateAndCopy(4, layers, 17, 31);
}

TEST_F(FannTest, CreateSparseArrayWithMinimalConnectivity) {
unsigned int layers[] = {2, 2, 2};
ASSERT_TRUE(net.create_sparse_array(0.01f, 3, layers));
AssertCreateAndCopy(3, layers, 8, 8);
}

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

TEST_F(FannTest, CreateShortcutArrayFourLayers) {
unsigned int layers[] = {2, 3, 4, 5};
ASSERT_TRUE(net.create_shortcut_array(4, layers));
AssertCreateAndCopy(4, layers, 15, 83);
EXPECT_EQ(FANN::SHORTCUT, net.get_network_type());
}

TEST_F(FannTest, CreateFromFile) {
ASSERT_TRUE(net.create_standard(3, 2, 3, 4));
ASSERT_TRUE(net.save("tmpfile"));
net.destroy();
ASSERT_TRUE(net.create_from_file("tmpfile"));

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

TEST_F(FannTest, RandomizeWeights) {
net.create_standard(2, 20, 10);
net.randomize_weights(-1.0, 1.0);
AssertWeights(-1.0, 1.0, 0);
}
22 changes: 3 additions & 19 deletions tests/fann_test.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef FANN_FANN_TESTFIXTURE_H
#define FANN_FANN_TESTFIXTURE_H
#ifndef FANN_FANN_TEST_H
#define FANN_FANN_TEST_H

#include "gtest/gtest.h"

Expand All @@ -12,15 +12,6 @@ class FannTest : public testing::Test {
FANN::neural_net net;
FANN::training_data data;

unsigned int numData;
unsigned int numInput;
unsigned int numOutput;
fann_type inputValue;
fann_type outputValue;

fann_type **inputData;
fann_type **outputData;

void AssertCreateAndCopy(unsigned int numLayers, unsigned int *layers, unsigned int neurons,
unsigned int connections);

Expand All @@ -29,16 +20,9 @@ class FannTest : public testing::Test {

void AssertWeights(fann_type min, fann_type max, fann_type avg);

void AssertTrainData(FANN::training_data &trainingData, unsigned int numData, unsigned int numInput,
unsigned int numOutput, fann_type inputValue, fann_type outputValue);

virtual void SetUp();

virtual void TearDown();

void InitializeTrainDataStructure(unsigned int numData, unsigned int numInput, unsigned int numOutput,
fann_type inputValue, fann_type outputValue, fann_type **inputData,
fann_type **outputData);
};

#endif //FANN_FANN_TESTFIXTURE_H
#endif
Loading

0 comments on commit cbf0a48

Please sign in to comment.