Skip to content

Commit

Permalink
Format tests and fix one formatting issue in src
Browse files Browse the repository at this point in the history
  • Loading branch information
bukka committed Apr 16, 2024
1 parent 518eb6b commit 1783cbf
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 335 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CMakeCache.txt
CPackConfig.cmake
CPackSourceConfig.cmake
cmake_install.cmake
.cmake/
fann-config.cmake
fann.pc
install_manifest.txt
Expand Down
1 change: 1 addition & 0 deletions format.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/sh

find src -regex '.*\.\(cpp\|hpp\|c\|h\)' -exec clang-format -i {} \;
find tests -regex '.*\.\(cpp\|hpp\|c\|h\)' -exec clang-format -i {} \;
9 changes: 5 additions & 4 deletions src/fann.c
Original file line number Diff line number Diff line change
Expand Up @@ -1413,10 +1413,11 @@ void fann_update_stepwise(struct fann *ann) {
fann_min(ann->multiplier - (fann_type)(ann->multiplier / 100.0 + 1.0), ann->multiplier - 1);

for (i = 0; i < 6; i++) {
ann->sigmoid_values[i] = (fann_type)(
((log(ann->multiplier / (float)ann->sigmoid_results[i] - 1) * (float)ann->multiplier) /
-2.0) *
(float)ann->multiplier);
ann->sigmoid_values[i] =
(fann_type)(((log(ann->multiplier / (float)ann->sigmoid_results[i] - 1) *
(float)ann->multiplier) /
-2.0) *
(float)ann->multiplier);
ann->sigmoid_symmetric_values[i] =
(fann_type)(((log((ann->multiplier - (float)ann->sigmoid_symmetric_results[i]) /
((float)ann->sigmoid_symmetric_results[i] + ann->multiplier)) *
Expand Down
193 changes: 96 additions & 97 deletions tests/fann_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,172 +4,171 @@
using namespace std;

void FannTest::SetUp() {
//ensure random generator is seeded at a known value to ensure reproducible results
srand(0);
fann_disable_seed_rand();
// ensure random generator is seeded at a known value to ensure reproducible results
srand(0);
fann_disable_seed_rand();
}

void FannTest::TearDown() {
net.destroy();
data.destroy_train();
net.destroy();
data.destroy_train();
}

void FannTest::AssertCreate(neural_net &net, unsigned int numLayers, const unsigned int *layers,
unsigned int neurons, unsigned int connections) {
EXPECT_EQ(numLayers, net.get_num_layers());
EXPECT_EQ(layers[0], net.get_num_input());
EXPECT_EQ(layers[numLayers - 1], net.get_num_output());
unsigned int *layers_res = new unsigned int[numLayers];
net.get_layer_array(layers_res);
for (unsigned int i = 0; i < numLayers; i++) {
EXPECT_EQ(layers[i], layers_res[i]);
}
delete[] layers_res;
EXPECT_EQ(numLayers, net.get_num_layers());
EXPECT_EQ(layers[0], net.get_num_input());
EXPECT_EQ(layers[numLayers - 1], net.get_num_output());
unsigned int *layers_res = new unsigned int[numLayers];
net.get_layer_array(layers_res);
for (unsigned int i = 0; i < numLayers; i++) {
EXPECT_EQ(layers[i], layers_res[i]);
}
delete[] layers_res;

EXPECT_EQ(neurons, net.get_total_neurons());
EXPECT_EQ(connections, net.get_total_connections());
EXPECT_EQ(neurons, net.get_total_neurons());
EXPECT_EQ(connections, net.get_total_connections());

AssertWeights(net, -0.09, 0.09, 0.0);
AssertWeights(net, -0.09, 0.09, 0.0);
}

void FannTest::AssertCreateAndCopy(neural_net &net, unsigned int numLayers, const unsigned int *layers, unsigned int neurons,
void FannTest::AssertCreateAndCopy(neural_net &net, unsigned int numLayers,
const unsigned int *layers, unsigned int neurons,
unsigned int connections) {
AssertCreate(net, numLayers, layers, neurons, connections);
neural_net net_copy(net);
AssertCreate(net_copy, numLayers, layers, neurons, connections);
AssertCreate(net, numLayers, layers, neurons, connections);
neural_net net_copy(net);
AssertCreate(net_copy, numLayers, layers, neurons, connections);
}

void FannTest::AssertWeights(neural_net &net, fann_type min, fann_type max, fann_type avg) {
connection *connections = new connection[net.get_total_connections()];
net.get_connection_array(connections);
connection *connections = new connection[net.get_total_connections()];
net.get_connection_array(connections);

fann_type minWeight = connections[0].weight;
fann_type maxWeight = connections[0].weight;
fann_type totalWeight = 0.0;
for (int i = 1; i < net.get_total_connections(); ++i) {
if (connections[i].weight < minWeight)
minWeight = connections[i].weight;
if (connections[i].weight > maxWeight)
maxWeight = connections[i].weight;
totalWeight += connections[i].weight;
}
fann_type minWeight = connections[0].weight;
fann_type maxWeight = connections[0].weight;
fann_type totalWeight = 0.0;
for (int i = 1; i < net.get_total_connections(); ++i) {
if (connections[i].weight < minWeight) minWeight = connections[i].weight;
if (connections[i].weight > maxWeight) maxWeight = connections[i].weight;
totalWeight += connections[i].weight;
}

EXPECT_NEAR(min, minWeight, 0.05);
EXPECT_NEAR(max, maxWeight, 0.05);
EXPECT_NEAR(avg, totalWeight / (fann_type) net.get_total_connections(), 0.5);
EXPECT_NEAR(min, minWeight, 0.05);
EXPECT_NEAR(max, maxWeight, 0.05);
EXPECT_NEAR(avg, totalWeight / (fann_type)net.get_total_connections(), 0.5);
}

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

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

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

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

TEST_F(FannTest, CreateStandardFourLayersVector) {
vector<unsigned int> layers{2, 3, 4, 5};
neural_net net(LAYER, layers.begin(), layers.end());
AssertCreateAndCopy(net, 4, layers.data(), 17, 50);
vector<unsigned int> layers{2, 3, 4, 5};
neural_net net(LAYER, layers.begin(), layers.end());
AssertCreateAndCopy(net, 4, layers.data(), 17, 50);
}

TEST_F(FannTest, CreateSparseFourLayers) {
neural_net net(0.5, 4, 2, 3, 4, 5);
unsigned int layers[] = {2, 3, 4, 5};
AssertCreateAndCopy(net, 4, layers, 17, 31);
neural_net net(0.5, 4, 2, 3, 4, 5);
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));
unsigned int layers[] = {2, 3, 4, 5};
AssertCreateAndCopy(net, 4, layers, 17, 31);
ASSERT_TRUE(net.create_sparse(0.5f, 4, 2, 3, 4, 5));
unsigned int layers[] = {2, 3, 4, 5};
AssertCreateAndCopy(net, 4, layers, 17, 31);
}

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

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

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

TEST_F(FannTest, CreateShortcutFourLayers) {
neural_net net(SHORTCUT, 4, 2, 3, 4, 5);
unsigned int layers[] = {2, 3, 4, 5};
AssertCreateAndCopy(net, 4, layers, 15, 83);
EXPECT_EQ(SHORTCUT, net.get_network_type());
neural_net net(SHORTCUT, 4, 2, 3, 4, 5);
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));
unsigned int layers[] = {2, 3, 4, 5};
AssertCreateAndCopy(net, 4, layers, 15, 83);
EXPECT_EQ(SHORTCUT, net.get_network_type());
ASSERT_TRUE(net.create_shortcut(4, 2, 3, 4, 5));
unsigned int layers[] = {2, 3, 4, 5};
AssertCreateAndCopy(net, 4, layers, 15, 83);
EXPECT_EQ(SHORTCUT, net.get_network_type());
}

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

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

TEST_F(FannTest, CreateFromFile) {
ASSERT_TRUE(net.create_standard(3, 2, 3, 4));
neural_net netToBeSaved(LAYER, 3, 2, 3, 4);
ASSERT_TRUE(netToBeSaved.save("tmpfile"));
ASSERT_TRUE(net.create_standard(3, 2, 3, 4));
neural_net netToBeSaved(LAYER, 3, 2, 3, 4);
ASSERT_TRUE(netToBeSaved.save("tmpfile"));

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

TEST_F(FannTest, CreateFromFileUsingCreateMethod) {
ASSERT_TRUE(net.create_standard(3, 2, 3, 4));
neural_net inputNet(LAYER, 3, 2, 3, 4);
ASSERT_TRUE(inputNet.save("tmpfile"));
ASSERT_TRUE(net.create_standard(3, 2, 3, 4));
neural_net inputNet(LAYER, 3, 2, 3, 4);
ASSERT_TRUE(inputNet.save("tmpfile"));

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

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

TEST_F(FannTest, RandomizeWeights) {
neural_net net(LAYER, 2, 20, 10);
net.randomize_weights(-1.0, 1.0);
AssertWeights(net, -1.0, 1.0, 0);
neural_net net(LAYER, 2, 20, 10);
net.randomize_weights(-1.0, 1.0);
AssertWeights(net, -1.0, 1.0, 0);
}
20 changes: 10 additions & 10 deletions tests/fann_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
using namespace FANN;

class FannTest : public testing::Test {
protected:
neural_net net;
training_data data;
protected:
neural_net net;
training_data data;

void AssertCreateAndCopy(neural_net &net, unsigned int numLayers, const unsigned int *layers, unsigned int neurons,
unsigned int connections);
void AssertCreateAndCopy(neural_net &net, unsigned int numLayers, const unsigned int *layers,
unsigned int neurons, unsigned int connections);

void AssertCreate(neural_net &net, unsigned int numLayers, const unsigned int *layers,
unsigned int neurons, unsigned int connections);
void AssertCreate(neural_net &net, unsigned int numLayers, const unsigned int *layers,
unsigned int neurons, unsigned int connections);

void AssertWeights(neural_net &net, fann_type min, fann_type max, fann_type avg);
void AssertWeights(neural_net &net, fann_type min, fann_type max, fann_type avg);

virtual void SetUp();
virtual void SetUp();

virtual void TearDown();
virtual void TearDown();
};

#endif
Loading

0 comments on commit 1783cbf

Please sign in to comment.