Skip to content

Commit

Permalink
Some codecov fixes for ml classes
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Oct 22, 2024
1 parent 34e3f44 commit bbdbeb4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 19 deletions.
15 changes: 7 additions & 8 deletions src/machine_learning/search_algorithms/best_first/best_first.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef BEST_FIRST_H
#define BEST_FIRST_H
#ifndef BEST_FIRST_H
#define BEST_FIRST_H

#ifdef __cplusplus
#include <iostream>
Expand All @@ -13,9 +13,9 @@
*/
template <typename T> class best_first{
private:
std::unordered_map<T, std::vector<std::pair<T, double> > > adj;
std::unordered_map<T, std::vector<std::pair<T, double> > > adj;
std::unordered_map<T, double> nodes;

public:

/**
Expand All @@ -39,7 +39,7 @@ template <typename T> class best_first{
std::cerr << e.what() << '\n';
}
}

/**
* @brief insert_node function
* @param u: the node ID
Expand Down Expand Up @@ -67,7 +67,7 @@ template <typename T> class best_first{
}
return false;
}

/**
* @brief add_edge function
* @param u: the first node
Expand All @@ -86,7 +86,7 @@ template <typename T> class best_first{
std::cerr << e.what() << '\n';
}
}

/**
* @brief search function
* @param start: starting node
Expand Down Expand Up @@ -124,4 +124,3 @@ template <typename T> class best_first{


#endif

Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,26 @@
*/
template <typename T> class hill_climbing{
private:
std::unordered_map<T, std::vector<std::pair<T, double> > > adj;
std::unordered_map<T, std::vector<std::pair<T, double> > > adj;
std::unordered_map<T, double> nodes;

public:

/**
* @brief hill_climbing constructor
* @param v: unordered_map<T, pair<T, int64_t> > initializer vector. Default = {}
*
* @param v: unordered_map<T, pair<T, double> > initializer vector. Default = {}
* @param nodes: unordered_map<T, double> initializer values for the nodes. Default = {}
*/
explicit hill_climbing(std::unordered_map<T, std::vector<std::pair<T, double> > > v = {}){
if(!v.empty()){
explicit hill_climbing(std::unordered_map<T, std::vector<std::pair<T, double> > > v = {},
std::unordered_map<T, double> nodes = {}){
if(!v.empty()) {
this->adj = v;
}
if(!nodes.empty()) {
this->nodes = nodes;
}
}

/**
* @brief insert_node function
* @param u: the node ID
Expand Down Expand Up @@ -57,7 +61,7 @@ template <typename T> class hill_climbing{
}
return false;
}

/**
* @brief add_edge function
* @param u: the first node
Expand All @@ -76,7 +80,7 @@ template <typename T> class hill_climbing{
std::cerr << e.what() << '\n';
}
}

/**
* @brief search function
* @param start: starting node
Expand Down
32 changes: 32 additions & 0 deletions tests/machine_learning/search_algorithms/best_first.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ TEST_CASE("testing edges with insertion for best first class"){
REQUIRE(h.has_edge(0, 3) == false);
}

TEST_CASE("Testing default constructor of best first with passed adjacency list") {
std::unordered_map<int, std::vector<std::pair<int, double> > > adj;
std::unordered_map<int, double> nodes;

nodes[0] = INT_MAX;
nodes[1] = 10;
nodes[2] = 20;
nodes[3] = 10;

adj[0].push_back({1, nodes[1]});

best_first<int> h(adj, nodes);
REQUIRE(h.has_edge(0, 1) == true);
REQUIRE(h.has_edge(0, 3) == false);
REQUIRE(h.has_edge(1, 0) == false);
}

TEST_CASE("testing search function for best_first class"){
best_first<char> h;
Expand Down Expand Up @@ -52,3 +68,19 @@ TEST_CASE("testing search function for best_first class"){
}


TEST_CASE("Testing throws on add_edge for best first function") {
std::unordered_map<int, std::vector<std::pair<int, double> > > adj;
std::unordered_map<int, double> nodes;

nodes[0] = INT_MAX;
nodes[1] = 10;
nodes[2] = 20;
nodes[3] = 10;

adj[0].push_back({1, nodes[1]});

best_first<int> h(adj, nodes);

CHECK_NOTHROW(h.add_edge(10, 20));
CHECK_NOTHROW(h.add_edge(0, 1));
}
19 changes: 17 additions & 2 deletions tests/machine_learning/search_algorithms/hill_climbing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ TEST_CASE("testing edges with insertion for hill climbing class"){
REQUIRE(h.has_edge(0, 3) == false);
}

TEST_CASE("Testing default constructor of hill climbing") {
std::unordered_map<int, double> node;
std::unordered_map<int, std::vector<std::pair<int, double> > > adj;

node[0] = INT_MAX;
node[1] = 10;
node[2] = 20;
node[3] = 10;

adj[0].push_back({1, node[1]});

hill_climbing<int> h(adj, node);

REQUIRE(h.has_edge(0, 1) == true);
REQUIRE(h.has_edge(0, 3) == false);
}

TEST_CASE("testing search function for hill climbing class"){
hill_climbing<char> h;
h.insert_node('s', INT_MAX);
Expand Down Expand Up @@ -49,5 +66,3 @@ TEST_CASE("testing search function for hill climbing class"){

REQUIRE(h2.search('s', 'G') == false);
}


0 comments on commit bbdbeb4

Please sign in to comment.