Skip to content

Commit

Permalink
Added helper folder with analyzer and debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Aug 11, 2024
1 parent 349e19c commit f9a1da9
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 34 deletions.
36 changes: 36 additions & 0 deletions examples/helpers/helper_funcs.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>
#include "../../src/helpers/timer.h"
#include "../../src/helpers/debug.h"
#include "../../src/classes/graph/graph.h"
#include "../../src/algorithms/number_theory/eratosthenes_sieve.h"

int main() {
weighted_graph<int> g("directed");
g.add_edge(0, 1, 5);
g.add_edge(1, 2, 4);
g.add_edge(2, 3, 5);
g.add_edge(3, 4, 2);
g.add_edge(4, 5, 1);

// in order to pass member function, you need to make them lambda first,
// if they're static function, this step could be ignored
auto shortest_path = [&](int a, int b) -> double {
return g.shortest_path(a, b);
};

auto dfs = [&](int a) -> std::vector<int> {
return g.dfs(a);
};

// you can even do this: auto bound_dfs = std::bind(&graph<int>::dfs, &g, std::placeholders::_1);
TIMER::exec_time(shortest_path, 0, 4);

// printing all the arguments(0, 2, 3, 4, 'a')
DEBUG::get_args(shortest_path, 0, 2, 3, 4, 'a');

// returns which function is faster
std::cout << TIMER::check_faster(shortest_path, std::make_tuple(0, 1), dfs, std::make_tuple(0)) << '\n';

// analyzes the time complxity, also returns a useful graph
TIMER::time_complexity(soe, std::make_tuple(4), std::make_tuple(5), std::make_tuple(10), std::make_tuple(15), std::make_tuple(50), std::make_tuple(100), std::make_tuple(500), std::make_tuple(10000), std::make_tuple(99999));
}
24 changes: 24 additions & 0 deletions examples/helpers/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"execution_time": [
0.002,
0.002,
0.002,
0.003,
0.004,
0.006,
0.026,
0.5950000000000001,
5.0809999999999995
],
"input_size": [
4,
5,
10,
15,
50,
100,
500,
10000,
99999
]
}
33 changes: 0 additions & 33 deletions src/helpers/test.cc

This file was deleted.

7 changes: 6 additions & 1 deletion src/helpers/timer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef TIMER_H
#define TIMER_H

#include <filesystem>
#include <utility>
#ifdef __cplusplus
#include <iostream>
Expand Down Expand Up @@ -95,7 +96,11 @@ namespace TIMER {
std::ofstream file("info.json");
file << j.dump(4);
file.close();
int rr = system("python3 analyzer.py");

std::filesystem::path header_path = __FILE__;
std::filesystem::path script_path = header_path.parent_path() / "analyzer.py";
std::string command = "python3 " + script_path.string();
int rr = system(command.c_str());
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/helpers/debug.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "../../src/helpers/debug.h"
#include "../../third_party/catch.hpp"
#include "../../src/classes/graph/graph.h"

TEST_CASE("Testing get args for debug namespace") {
weighted_graph<int> g("directed");
g.add_edge(0, 1, 5);
g.add_edge(1, 2, 4);
g.add_edge(2, 3, 5);
g.add_edge(3, 4, 2);
g.add_edge(4, 5, 1);

// in order to pass member function, you need to make them lambda first,
// if they're static function, this step could be ignored
auto shortest_path = [&](int a, int b) -> double {
return g.shortest_path(a, b);
};

CHECK_NOTHROW(DEBUG::get_args(shortest_path, 0, 2));
}
67 changes: 67 additions & 0 deletions tests/helpers/timer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "../../src/helpers/timer.h"
#include "../../third_party/catch.hpp"
#include "../../src/classes/graph/graph.h"


TEST_CASE("Testing execution time for timer namespcae") {
weighted_graph<int> g("directed");
g.add_edge(0, 1, 5);
g.add_edge(1, 2, 4);
g.add_edge(2, 3, 5);
g.add_edge(3, 4, 2);
g.add_edge(4, 5, 1);

// in order to pass member function, you need to make them lambda first,
// if they're static function, this step could be ignored
auto shortest_path = [&](int a, int b) -> double {
return g.shortest_path(a, b);
};

auto dfs = [&](int a) -> std::vector<int> {
return g.dfs(a);
};

CHECK_NOTHROW(TIMER::exec_time(shortest_path, 0, 4));
}

TEST_CASE("Testing time complxity for timer namespace") {
weighted_graph<int> g("directed");
g.add_edge(0, 1, 5);
g.add_edge(1, 2, 4);
g.add_edge(2, 3, 5);
g.add_edge(3, 4, 2);
g.add_edge(4, 5, 1);

// in order to pass member function, you need to make them lambda first,
// if they're static function, this step could be ignored
auto shortest_path = [&](int a, int b) -> double {
return g.shortest_path(a, b);
};

auto dfs = [&](int a) -> std::vector<int> {
return g.dfs(a);
};

CHECK_NOTHROW(TIMER::time_complexity(dfs, std::make_tuple(0), std::make_tuple(2), std::make_tuple(4)));
}

TEST_CASE("Testing check faster for timer namespace") {
weighted_graph<int> g("directed");
g.add_edge(0, 1, 5);
g.add_edge(1, 2, 4);
g.add_edge(2, 3, 5);
g.add_edge(3, 4, 2);
g.add_edge(4, 5, 1);

// in order to pass member function, you need to make them lambda first,
// if they're static function, this step could be ignored
auto shortest_path = [&](int a, int b) -> double {
return g.shortest_path(a, b);
};

auto dfs = [&](int a) -> std::vector<int> {
return g.dfs(a);
};

CHECK_NOTHROW(TIMER::check_faster(shortest_path, std::make_tuple(0, 1), dfs, std::make_tuple(0)));
}

0 comments on commit f9a1da9

Please sign in to comment.