Newer
Older
/**
* DESCRIPTION OF THE CLASS
*
* @author David Vojtek
* @date 2018
*/
kra568
committed
#define BOOST_TEST_MODULE ErrorFunctions_test
#include "boost_unit_tests_preamble.h"
kra568
committed
#include "../ErrorFunction/ErrorFunctions.h"
Martin Beseda
committed
using namespace lib4neuro;
MOCK_BASE_CLASS(mock_network, lib4neuro::NeuralNetwork)
{
MOCK_METHOD(get_subnet, 2)
MOCK_METHOD(add_neuron, 3)
MOCK_METHOD(add_connection_simple, 4)
MOCK_METHOD(add_existing_connection, 4)
MOCK_METHOD(copy_parameter_space, 1)
MOCK_METHOD(set_parameter_space_pointers, 1)
MOCK_METHOD(eval_single, 3)
MOCK_METHOD(add_to_gradient_single, 4)
MOCK_METHOD(randomize_weights, 0)
MOCK_METHOD(randomize_biases, 0)
MOCK_METHOD(randomize_parameters, 0)
MOCK_METHOD(get_n_inputs, 0)
MOCK_METHOD(get_n_outputs, 0)
MOCK_METHOD(get_n_weights, 0)
MOCK_METHOD(get_n_biases, 0)
MOCK_METHOD(get_neuron_bias_index, 1)
MOCK_METHOD(get_n_neurons, 0)
MOCK_METHOD(specify_input_neurons, 1)
MOCK_METHOD(specify_output_neurons, 1)
MOCK_METHOD(get_parameter_ptr_biases, 0)
MOCK_METHOD(get_parameter_ptr_weights, 0)
MOCK_METHOD(save_text, 1)
Martin Beseda
committed
MOCK_METHOD(write_weights, 0, void(), id1)
MOCK_METHOD(write_weights, 1, void(std::string), id2)
MOCK_METHOD(write_weights, 1, void(std::ofstream*), id3)
MOCK_METHOD(write_biases, 0, void(), id4)
MOCK_METHOD(write_biases, 1, void(std::string), id5)
MOCK_METHOD(write_biases, 1, void(std::ofstream*), id6)
MOCK_METHOD(write_stats, 0, void(), id7)
MOCK_METHOD(write_stats, 1, void(std::string), id8)
MOCK_METHOD(write_stats, 1, void(std::ofstream*), id9)
Martin Beseda
committed
MOCK_BASE_CLASS(mock_error_fun, ErrorFunction)
{
Martin Beseda
committed
MOCK_METHOD(get_dimension, 0)
MOCK_METHOD(get_parameters, 0)
MOCK_METHOD(get_dataset, 0)
Martin Beseda
committed
MOCK_METHOD(get_network_instance, 0)
Martin Beseda
committed
MOCK_METHOD(divide_data_train_test, 1)
Martin Beseda
committed
MOCK_METHOD(return_full_data_set_for_training, 0)
Martin Beseda
committed
MOCK_METHOD(eval_on_test_data, 1, double(std::vector<double>*), id1)
MOCK_METHOD(eval_on_test_data, 2, double(std::string, std::vector<double>*), id2)
MOCK_METHOD(eval_on_test_data, 2, double(std::ofstream*, std::vector<double>*), id3)
MOCK_METHOD(eval_on_data_set, 2, double(DataSet*, std::vector<double>*), id4)
MOCK_METHOD(eval_on_data_set, 3, double(DataSet*, std::string, std::vector<double>*), id5)
MOCK_METHOD(eval_on_data_set, 3, double(DataSet*, std::ofstream*, std::vector<double>*), id6)
};
MOCK_BASE_CLASS(mock_dataSet, lib4neuro::DataSet)
{
mock_dataSet(std::vector<std::pair<std::vector<double>, std::vector<double>>> *i)
: lib4neuro::DataSet(i)
{
}
MOCK_METHOD(add_data_pair, 2)
MOCK_METHOD(get_n_elements, 0)
MOCK_METHOD(get_input_dim, 0)
MOCK_METHOD(get_output_dim, 0)
MOCK_METHOD(print_data, 0)
MOCK_METHOD(store_text, 1)
Martin Beseda
committed
MOCK_METHOD(store_data_text, 1, void(std::string), id1)
MOCK_METHOD(store_data_text, 1, void(std::ofstream*), id2)
/**
* Boost testing suite for testing ErrorFunction.h
* doesn't test inherited methods
*/
Martin Beseda
committed
BOOST_AUTO_TEST_SUITE(ErrorFunctions_test);
BOOST_AUTO_TEST_CASE(ErrorFunction_MSE_Construction_Test) {
mock_network network;
MOCK_EXPECT(network.get_n_biases).returns(1);
MOCK_EXPECT(network.get_n_weights).returns(1);
std::vector<double> inp, out;
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec_dy;
inp = { 0.0 };
out = { 8.0 };
data_vec_dy.emplace_back(std::make_pair(inp, out));
mock_dataSet dataSet(&data_vec_dy);
BOOST_CHECK_NO_THROW(MSE mse(&network, &dataSet));
}
BOOST_AUTO_TEST_CASE(ErrorFunction_MSE_Eval_Test) {
mock_network network;
MOCK_EXPECT(network.get_n_biases).returns(1);
MOCK_EXPECT(network.get_n_weights).returns(1);
MOCK_EXPECT(network.eval_single);
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
std::vector<double> inp, out;
for (int i = 0; i < 1; i++) {
inp.push_back(i);
out.push_back(i + 4);
}
data_vec.emplace_back(std::make_pair(inp, out));
std::vector<double> weights;
weights.push_back(1);
MSE mse(&network, &dataSet);
BOOST_CHECK_EQUAL(16, mse.eval(&weights));
BOOST_AUTO_TEST_CASE(ErrorFunction_MSE_Get_dimension_test) {
mock_network network;
MOCK_EXPECT(network.get_n_biases).returns(1);
MOCK_EXPECT(network.get_n_weights).returns(1);
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
std::vector<double> inp, out;
for (int i = 0; i < 1; i++) {
inp.push_back(i);
out.push_back(i + 4);
}
data_vec.emplace_back(std::make_pair(inp, out));
MSE mse(&network, &dataSet);
BOOST_CHECK_EQUAL(2, mse.get_dimension());
}
BOOST_AUTO_TEST_CASE(ErrorFunction_MSE_SUM_Construction_Test) {
BOOST_CHECK_NO_THROW(ErrorSum mse_sum);
}
BOOST_AUTO_TEST_CASE(ErrorFunction_MSE_SUM_Add_Error_Function_Test) {
mock_error_fun f;
MOCK_EXPECT(f.get_dimension).returns(1);
BOOST_CHECK_NO_THROW(mse_sum.add_error_function(&f, 1));
BOOST_AUTO_TEST_CASE(ErrorFunction_MSE_SUM_Eval_Test) {
mock_error_fun f;
MOCK_EXPECT(f.get_dimension).returns(1);
MOCK_EXPECT(f.eval).returns(1.75);
ErrorSum mse_sum;
std::vector<double> weights;
weights.push_back(1);
mse_sum.add_error_function(&f);
BOOST_CHECK_EQUAL(1.75, mse_sum.eval(&weights));
BOOST_AUTO_TEST_CASE(ErrorFunction_MSE_SUM_Get_Dimension_test) {
ErrorSum mse_sum;
BOOST_CHECK_EQUAL(0, mse_sum.get_dimension());
mock_error_fun f;
MOCK_EXPECT(f.get_dimension).returns(2);
MOCK_EXPECT(f.eval).returns(1.75);
std::vector<double> weights;
weights.push_back(1);
BOOST_CHECK_EQUAL(2, mse_sum.get_dimension());
}