Refactor Algorithm

This commit is contained in:
2022-12-21 11:33:55 +01:00
parent dd1e67ec78
commit f449f438ef
11 changed files with 101 additions and 543 deletions

View File

@@ -4,31 +4,26 @@
#include <iostream>
namespace mdlp {
class TestFImdlp : public CPPFImdlp, public testing::Test {
class TestFImdlp: public CPPFImdlp, public testing::Test {
public:
precision_t precision = 0.000001;
TestFImdlp() : CPPFImdlp(false) {}
void SetUp() {
TestFImdlp(): CPPFImdlp() {}
void SetUp()
{
// 5.0, 5.1, 5.1, 5.1, 5.2, 5.3, 5.6, 5.7, 5.9, 6.0]
//(5.0, 1) (5.1, 1) (5.1, 2) (5.1, 2) (5.2, 1) (5.3, 1) (5.6, 2) (5.7, 1) (5.9, 2) (6.0, 2)
X = {5.7, 5.3, 5.2, 5.1, 5.0, 5.6, 5.1, 6.0, 5.1, 5.9};
y = {1, 1, 1, 1, 1, 2, 2, 2, 2, 2};
X = { 5.7, 5.3, 5.2, 5.1, 5.0, 5.6, 5.1, 6.0, 5.1, 5.9 };
y = { 1, 1, 1, 1, 1, 2, 2, 2, 2, 2 };
algorithm = false;
fit(X, y);
}
void setalgorithm(bool value) {
void setalgorithm(bool value)
{
algorithm = value;
}
// void initIndices()
// {
// indices = indices_t();
// }
void checkSortedVector() {
indices_t testSortedIndices = sortIndices(X);
void checkSortedVector()
{
indices_t testSortedIndices = sortIndices(X, y);
precision_t prev = X[testSortedIndices[0]];
for (auto i = 0; i < X.size(); ++i) {
EXPECT_EQ(testSortedIndices[i], indices[i]);
@@ -36,54 +31,55 @@ namespace mdlp {
prev = X[testSortedIndices[i]];
}
}
void checkCutPoints(cutPoints_t &expected) {
void checkCutPoints(cutPoints_t& expected)
{
int expectedSize = expected.size();
EXPECT_EQ(cutPoints.size(), expectedSize);
for (auto i = 0; i < cutPoints.size(); i++) {
EXPECT_NEAR(cutPoints[i], expected[i], precision);
}
}
template<typename T, typename A>
void checkVectors(std::vector<T, A> const &expected, std::vector<T, A> const &computed) {
void checkVectors(std::vector<T, A> const& expected, std::vector<T, A> const& computed)
{
EXPECT_EQ(expected.size(), computed.size());
ASSERT_EQ(expected.size(), computed.size());
for (auto i = 0; i < expected.size(); i++) {
EXPECT_NEAR(expected[i], computed[i],precision);
EXPECT_NEAR(expected[i], computed[i], precision);
}
}
};
TEST_F(TestFImdlp, FitErrorEmptyDataset) {
TEST_F(TestFImdlp, FitErrorEmptyDataset)
{
X = samples_t();
y = labels_t();
EXPECT_THROW(fit(X, y), std::invalid_argument);
}
TEST_F(TestFImdlp, FitErrorDifferentSize) {
X = {1, 2, 3};
y = {1, 2};
TEST_F(TestFImdlp, FitErrorDifferentSize)
{
X = { 1, 2, 3 };
y = { 1, 2 };
EXPECT_THROW(fit(X, y), std::invalid_argument);
}
TEST_F(TestFImdlp, SortIndices) {
X = {5.7, 5.3, 5.2, 5.1, 5.0, 5.6, 5.1, 6.0, 5.1, 5.9};
indices = {4, 3, 6, 8, 2, 1, 5, 0, 9, 7};
TEST_F(TestFImdlp, SortIndices)
{
X = { 5.7, 5.3, 5.2, 5.1, 5.0, 5.6, 5.1, 6.0, 5.1, 5.9 };
indices = { 4, 3, 6, 8, 2, 1, 5, 0, 9, 7 };
checkSortedVector();
X = {5.77, 5.88, 5.99};
indices = {0, 1, 2};
X = { 5.77, 5.88, 5.99 };
indices = { 0, 1, 2 };
checkSortedVector();
X = {5.33, 5.22, 5.11};
indices = {2, 1, 0};
X = { 5.33, 5.22, 5.11 };
indices = { 2, 1, 0 };
checkSortedVector();
}
TEST_F(TestFImdlp, TestDataset) {
algorithm = false;
TEST_F(TestFImdlp, TestDataset)
{
algorithm = 0;
fit(X, y);
computeCutPointsOriginal(0, 10);
cutPoints_t expected = {5.6499996185302734};
computeCutPoints(0, 10);
cutPoints_t expected = { 5.6499996185302734 };
vector<precision_t> computed = getCutPoints();
computed = getCutPoints();
int expectedSize = expected.size();
@@ -92,49 +88,49 @@ namespace mdlp {
EXPECT_NEAR(computed[i], expected[i], precision);
}
}
TEST_F(TestFImdlp, ComputeCutPointsOriginal) {
cutPoints_t expected = {5.65};
TEST_F(TestFImdlp, ComputeCutPoints)
{
cutPoints_t expected = { 5.65 };
algorithm = false;
computeCutPointsOriginal(0, 10);
computeCutPoints(0, 10);
checkCutPoints(expected);
}
TEST_F(TestFImdlp, ComputeCutPointsOriginalGCase) {
TEST_F(TestFImdlp, ComputeCutPointsGCase)
{
cutPoints_t expected;
algorithm = false;
expected = {2};
samples_t X_ = {0, 1, 2, 2};
labels_t y_ = {1, 1, 1, 2};
expected = { 2 };
samples_t X_ = { 0, 1, 2, 2 };
labels_t y_ = { 1, 1, 1, 2 };
fit(X_, y_);
checkCutPoints(expected);
}
TEST_F(TestFImdlp, ComputeCutPointsalgorithm) {
TEST_F(TestFImdlp, ComputeCutPointsalAlternative)
{
algorithm = true;
cutPoints_t expected;
expected = {};
fit(X, y);
computeCutPointsalgorithm();
computeCutPointsAlternative(0, 10);
checkCutPoints(expected);
}
TEST_F(TestFImdlp, ComputeCutPointsalgorithmGCase) {
TEST_F(TestFImdlp, ComputeCutPointsAlternativeGCase)
{
cutPoints_t expected;
expected = {1.5};
expected = { 1.5 };
algorithm = true;
samples_t X_ = {0, 1, 2, 2};
labels_t y_ = {1, 1, 1, 2};
samples_t X_ = { 0, 1, 2, 2 };
labels_t y_ = { 1, 1, 1, 2 };
fit(X_, y_);
checkCutPoints(expected);
}
TEST_F(TestFImdlp, GetCutPoints) {
samples_t computed, expected = {5.65};
TEST_F(TestFImdlp, GetCutPoints)
{
samples_t computed, expected = { 5.65 };
algorithm = false;
computeCutPointsOriginal(0, 10);
computeCutPoints(0, 10);
computed = getCutPoints();
for (auto item: cutPoints)
for (auto item : cutPoints)
cout << setprecision(6) << item << endl;
checkVectors(expected, computed);
}