diff --git a/examples/helpers/helper_funcs.cc b/examples/helpers/helper_funcs.cc new file mode 100644 index 00000000..90e6c3e8 --- /dev/null +++ b/examples/helpers/helper_funcs.cc @@ -0,0 +1,36 @@ +#include +#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 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 { + return g.dfs(a); + }; + + // you can even do this: auto bound_dfs = std::bind(&graph::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)); +} diff --git a/examples/helpers/info.json b/examples/helpers/info.json new file mode 100644 index 00000000..c017c46b --- /dev/null +++ b/examples/helpers/info.json @@ -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 + ] +} \ No newline at end of file diff --git a/src/helpers/test.cc b/src/helpers/test.cc deleted file mode 100644 index 8b29f458..00000000 --- a/src/helpers/test.cc +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include "timer.h" -#include "debug.h" -#include "../classes/graph/graph.h" -#include "../algorithms/number_theory/eratosthenes_sieve.h" - -int main() { - weighted_graph 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); - auto y_combinator = [&](int a, int b) -> double { - return g.shortest_path(a, b); - }; - - auto y_combinator2 = [&](int a) -> std::vector { - return g.dfs(a); - }; - - auto insert = [&](int a, int b, int c) -> void { - g.add_edge(a, b, c); - }; - - // auto bound_dfs = std::bind(&graph::dfs, &g, std::placeholders::_1); - TIMER::exec_time(y_combinator, 0, 4); - DEBUG::get_args(y_combinator, 0, 2, 3, 4, 'a'); - - std::cout << TIMER::check_faster(y_combinator,std::make_tuple(0, 1), y_combinator2, std::make_tuple(0)) << '\n'; - // TIMER::time_complexity(y_combinator, std::make_tuple(0, 1), std::make_tuple(1, 2), std::make_tuple(2, 4), std::make_tuple(4, 5), std::make_tuple(0, 5)); - 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)); -} diff --git a/src/helpers/timer.h b/src/helpers/timer.h index 1ee934d3..7cc3ac64 100644 --- a/src/helpers/timer.h +++ b/src/helpers/timer.h @@ -1,6 +1,7 @@ #ifndef TIMER_H #define TIMER_H +#include #include #ifdef __cplusplus #include @@ -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()); } } diff --git a/tests/helpers/debug.cc b/tests/helpers/debug.cc new file mode 100644 index 00000000..f78b64a5 --- /dev/null +++ b/tests/helpers/debug.cc @@ -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 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)); +} diff --git a/tests/helpers/timer.cc b/tests/helpers/timer.cc new file mode 100644 index 00000000..81bb4e95 --- /dev/null +++ b/tests/helpers/timer.cc @@ -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 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 { + return g.dfs(a); + }; + + CHECK_NOTHROW(TIMER::exec_time(shortest_path, 0, 4)); +} + +TEST_CASE("Testing time complxity for timer namespace") { + weighted_graph 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 { + 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 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 { + return g.dfs(a); + }; + + CHECK_NOTHROW(TIMER::check_faster(shortest_path, std::make_tuple(0, 1), dfs, std::make_tuple(0))); +}