Restored unit tests

This commit is contained in:
emeric
2020-02-13 20:04:20 +01:00
parent 15e53caa2d
commit f44addc2a2
16 changed files with 54 additions and 11 deletions
+1
View File
@@ -5,6 +5,7 @@ add_subdirectory(cover)
add_subdirectory(database)
add_subdirectory(recommendation)
add_subdirectory(scanner)
add_subdirectory(som)
add_subdirectory(subsonic)
add_subdirectory(utils)
+1
View File
@@ -35,3 +35,4 @@ target_link_libraries(lmsdatabase PUBLIC
install(TARGETS lmsdatabase DESTINATION lib)
add_subdirectory(test)
+11
View File
@@ -0,0 +1,11 @@
add_executable(test-database
DatabaseTest.cpp
)
target_link_libraries(test-database PRIVATE
lmsdatabase
)
add_test(NAME database COMMAND test-database)
File diff suppressed because it is too large Load Diff
+1 -5
View File
@@ -2,8 +2,6 @@
add_library(lmsrecommendation SHARED
impl/Engine.cpp
impl/ProviderCreator.cpp
impl/features/som/DataNormalizer.cpp
impl/features/som/Network.cpp
)
target_include_directories(lmsrecommendation INTERFACE
@@ -16,9 +14,7 @@ target_include_directories(lmsrecommendation PRIVATE
target_link_libraries(lmsrecommendation PRIVATE
lmsdatabase
)
target_link_libraries(lmsrecommendation PUBLIC
lmssom
)
install(TARGETS lmsrecommendation DESTINATION lib)
+19
View File
@@ -0,0 +1,19 @@
add_library(lmssom STATIC
impl/DataNormalizer.cpp
impl/Network.cpp
)
target_include_directories(lmssom INTERFACE
include
)
target_include_directories(lmssom PRIVATE
include
)
target_link_libraries(lmssom PUBLIC
lmsutils
)
add_subdirectory(test)
@@ -17,7 +17,7 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DataNormalizer.hpp"
#include "som/DataNormalizer.hpp"
#include <algorithm>
#include <numeric>
@@ -17,7 +17,7 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Network.hpp"
#include "som/Network.hpp"
#include <algorithm>
#include <chrono>
@@ -23,6 +23,8 @@
#include <vector>
#include <cmath>
#include "utils/Exception.hpp"
namespace SOM
{
@@ -25,7 +25,6 @@
#include <ostream>
#include <functional>
#include "utils/Exception.hpp"
#include "InputVector.hpp"
#include "Matrix.hpp"
+11
View File
@@ -0,0 +1,11 @@
add_executable(test-som
SomTest.cpp
)
target_link_libraries(test-som PRIVATE
lmssom
)
add_test(NAME som COMMAND test-som)
+116
View File
@@ -0,0 +1,116 @@
/*
* Copyright (C) 2019 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sstream>
#include <cassert>
#include <iostream>
#include "som/DataNormalizer.hpp"
#include "som/Network.hpp"
using namespace SOM;
int main()
{
static const InputVector::value_type EPSILON = 0.01;
{
Matrix<int> testMatrix {2, 2, 123};
assert((testMatrix[{0,0}] == 123));
assert((testMatrix[{0,1}] == 123));
assert((testMatrix[{1,0}] == 123));
assert((testMatrix[{1,1}] == 123));
}
{
InputVector test1 {2};
test1[0] = 0;
test1[1] = 1;
InputVector test2 {2};
test2[0] = 1;
test2[1] = 0;
InputVector test3 {test1};
test3 += test2;
assert(std::abs(test3[0] - 1) < EPSILON);
assert(std::abs(test3[1] - 1) < EPSILON);
}
{
Network network {2, 2, 1};
const InputVector weights {1, 1};
std::vector<InputVector> trainData {
{ 1, 50 },
{ 1, 100 },
{ 1, 150 },
{ 1, 200 },
};
DataNormalizer normalizer {1};
normalizer.computeNormalizationFactors(trainData);
for (auto& data: trainData)
normalizer.normalizeData(data);
network.dump(std::cout);
network.train(trainData, 20);
network.dump(std::cout);
std::cout << "MEAN dist = " << network.computeRefVectorsDistanceMean() << std::endl;
std::cout << "MEDIAN dist = " << network.computeRefVectorsDistanceMedian() << std::endl;
auto distFunc {network.getDistanceFunc()};
assert((std::abs(distFunc({1, 0}, {1, 1}, weights) - 1) < EPSILON));
assert((std::abs(distFunc({1, 0}, {1, 2}, weights) - 4) < EPSILON));
assert((std::abs(distFunc({1, 0}, {1, 0.33}, weights) - distFunc({1, 0.66}, {1, 1.}, weights)) < EPSILON));
{
std::set<Position> positions;
for (const InputVector& data : trainData)
positions.insert(network.getClosestRefVectorPosition(data));
assert(positions.size() == 4);
}
{
Position pos {network.getClosestRefVectorPosition(InputVector{1, 0.66})};
for (std::size_t i {}; i < 40; ++i)
{
InputVector input {1, 130 + static_cast<InputVector::value_type>(i) };
normalizer.normalizeData(input);
assert( network.getClosestRefVectorPosition(input) == pos);
}
}
{
Position pos {network.getClosestRefVectorPosition(InputVector{1, 1})};
for (std::size_t i {}; i < 40; ++i)
{
InputVector input {1, 180 + static_cast<InputVector::value_type>(i) };
normalizer.normalizeData(input);
assert( network.getClosestRefVectorPosition(input) == pos);
}
}
}
return 0;
}
+2
View File
@@ -24,7 +24,9 @@ target_link_libraries(lmsutils PRIVATE
)
target_link_libraries(lmsutils PUBLIC
boost_system
stdc++fs
wt
)
install(TARGETS lmsutils DESTINATION lib)