From 77135739cf72cfc02603332c681baae4dcea28f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana?= Date: Tue, 21 Mar 2023 09:55:40 +0100 Subject: [PATCH] Reformat some test files --- tests/ArffFiles.cpp | 35 ++++++++++++----------------------- tests/Metrics_unittest.cpp | 27 +++++++++++---------------- 2 files changed, 23 insertions(+), 39 deletions(-) diff --git a/tests/ArffFiles.cpp b/tests/ArffFiles.cpp index b576699..0d88818 100644 --- a/tests/ArffFiles.cpp +++ b/tests/ArffFiles.cpp @@ -7,43 +7,35 @@ using namespace std; ArffFiles::ArffFiles() = default; -vector ArffFiles::getLines() const -{ +vector ArffFiles::getLines() const { return lines; } -unsigned long int ArffFiles::getSize() const -{ +unsigned long int ArffFiles::getSize() const { return lines.size(); } -vector> ArffFiles::getAttributes() const -{ +vector> ArffFiles::getAttributes() const { return attributes; } -string ArffFiles::getClassName() const -{ +string ArffFiles::getClassName() const { return className; } -string ArffFiles::getClassType() const -{ +string ArffFiles::getClassType() const { return classType; } -vector>& ArffFiles::getX() -{ +vector> &ArffFiles::getX() { return X; } -vector& ArffFiles::getY() -{ +vector &ArffFiles::getY() { return y; } -void ArffFiles::load(const string& fileName, bool classLast) -{ +void ArffFiles::load(const string &fileName, bool classLast) { ifstream file(fileName); if (!file.is_open()) { throw invalid_argument("Unable to open file"); @@ -87,8 +79,7 @@ void ArffFiles::load(const string& fileName, bool classLast) } -void ArffFiles::generateDataset(bool classLast) -{ +void ArffFiles::generateDataset(bool classLast) { X = vector>(attributes.size(), vector(lines.size())); auto yy = vector(lines.size(), ""); int labelIndex = classLast ? static_cast(attributes.size()) : 0; @@ -108,21 +99,19 @@ void ArffFiles::generateDataset(bool classLast) y = factorize(yy); } -string ArffFiles::trim(const string& source) -{ +string ArffFiles::trim(const string &source) { string s(source); s.erase(0, s.find_first_not_of(" \n\r\t")); s.erase(s.find_last_not_of(" \n\r\t") + 1); return s; } -vector ArffFiles::factorize(const vector& labels_t) -{ +vector ArffFiles::factorize(const vector &labels_t) { vector yy; yy.reserve(labels_t.size()); map labelMap; int i = 0; - for (const string& label : labels_t) { + for (const string &label: labels_t) { if (labelMap.find(label) == labelMap.end()) { labelMap[label] = i++; } diff --git a/tests/Metrics_unittest.cpp b/tests/Metrics_unittest.cpp index 5cd73d6..e059fac 100644 --- a/tests/Metrics_unittest.cpp +++ b/tests/Metrics_unittest.cpp @@ -1,44 +1,39 @@ #include "gtest/gtest.h" #include "../Metrics.h" - namespace mdlp { - class TestMetrics: public Metrics, public testing::Test { + class TestMetrics : public Metrics, public testing::Test { public: - labels_t y_ = { 1, 1, 1, 1, 1, 2, 2, 2, 2, 2 }; - indices_t indices_ = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + labels_t y_ = {1, 1, 1, 1, 1, 2, 2, 2, 2, 2}; + indices_t indices_ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; precision_t precision = 0.000001f; - TestMetrics(): Metrics(y_, indices_) {}; + TestMetrics() : Metrics(y_, indices_) {}; - void SetUp() override - { + void SetUp() override { setData(y_, indices_); } }; - TEST_F(TestMetrics, NumClasses) - { - y = { 1, 1, 1, 1, 1, 1, 1, 1, 2, 1 }; + TEST_F(TestMetrics, NumClasses) { + y = {1, 1, 1, 1, 1, 1, 1, 1, 2, 1}; EXPECT_EQ(1, computeNumClasses(4, 8)); EXPECT_EQ(2, computeNumClasses(0, 10)); EXPECT_EQ(2, computeNumClasses(8, 10)); } - TEST_F(TestMetrics, Entropy) - { + TEST_F(TestMetrics, Entropy) { EXPECT_EQ(1, entropy(0, 10)); EXPECT_EQ(0, entropy(0, 5)); - y = { 1, 1, 1, 1, 1, 1, 1, 1, 2, 1 }; + y = {1, 1, 1, 1, 1, 1, 1, 1, 2, 1}; setData(y, indices); ASSERT_NEAR(0.468996f, entropy(0, 10), precision); } - TEST_F(TestMetrics, InformationGain) - { + TEST_F(TestMetrics, InformationGain) { ASSERT_NEAR(1, informationGain(0, 5, 10), precision); ASSERT_NEAR(1, informationGain(0, 5, 10), precision); // For cache - y = { 1, 1, 1, 1, 1, 1, 1, 1, 2, 1 }; + y = {1, 1, 1, 1, 1, 1, 1, 1, 2, 1}; setData(y, indices); ASSERT_NEAR(0.108032f, informationGain(0, 5, 10), precision); }