This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub struct Edge { | |
to: char, | |
from: char, | |
weight: int | |
} | |
pub struct digraph { | |
_vertices: Vec<char>, | |
_adj_list: HashMap<char, Vec<(char,int)> > | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
auto show_fail = [] () { cout << "Caught Expected Exception..." << endl; }; | |
auto show_err = [] (int ignore) { cout << "Failed to Catch Exception!!!"; }; | |
auto fail1 = [z] () { graph_tests(z,'A','D', set<string>{"Topsort"}); | |
return 1; }; | |
expect_exception(fail1, true, show_fail, show_err ); | |
auto fail2 = [x, none] () { graph_tests(x,'A','D', none); return 1; }; | |
expect_exception(fail2, true, show_fail, show_err ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unordered_map<string,string> expected_results_y { | |
{"BFS","BFS visiting {A,B,E,C,D,}"}, | |
{"DFS","DFS visiting {A,E,B,C,D,}"}, | |
{"Topsort", "Topsort visiting {A,E,B,C,D,}"}, | |
{"Dijkstra", "Dijkstra visiting {A,E,D,}"}, | |
{"Bellman", "Bellman visiting {(E:A),(D:E),(C:B),(B:A),(A:-),}"} | |
}; | |
graph_tests(y,'A','D', none, expected_results_y); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename ExecLambda, typename OnThrowLambda, typename NoThrowLambda> | |
auto expect_exception( ExecLambda exec_lambda, bool expect_to_throw, | |
OnThrowLambda throw_lambda, NoThrowLambda no_throw_lambda, | |
decltype(throw_lambda()) expected_return_value) | |
{ | |
bool threw; | |
decltype( exec_lambda() ) x; | |
try { | |
x = exec_lambda(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void graph_tests(const digraph &g, digraph::nodename source, | |
digraph::nodename destination, const set<string> & except_on ) | |
{ | |
auto throw_return = [] () | |
{ cout << "Caught Expected Exception" << endl; }; | |
auto tst = [exc_return, except_on] (string fcn, auto a) | |
{ | |
bool expect_throw = except_on.end()!=except_on.find(fcn); | |
auto normal_return = [fcn] (auto retval) | |
{ cout << fcn << " visiting " << retval << endl; }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename ExecLambda, typename OnThrowLambda, typename NoThrowLambda> | |
auto expect_exception( ExecLambda exec_lambda, bool expect_to_throw, | |
OnThrowLambda exc_lambda, NoThrowLambda no_exc_lambda) | |
{ | |
bool threw; | |
decltype( exec_lambda() ) x; | |
try { | |
x = exec_lambda(); | |
threw = false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename ExecLambda> | |
void expect_exception( ExecLambda exec_lambda, bool expect_to_throw) | |
{ | |
bool threw; | |
decltype( exec_lambda() ) x; | |
try { | |
x = exec_lambda(); | |
threw = false; | |
} | |
catch (...) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename return_type, typename arg1_type, typename ...params> | |
auto _L1( return_type (*fp) (arg1_type x, params...args), arg1_type x) | |
{ return [fp, x] (params...args) { return fp(x,args...); }; }; | |
double mult(double x, double y) { return x*y; } | |
auto to_radians = _L1(mult,M_PI/180.0); | |
auto cos_in_degrees = _L(cos) * to_radians; | |
cout << cos_in_degrees(45.0) << endl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
auto incr = [](auto a) {return a+1;}; | |
int add1(int x) { return x+1; } | |
int sub1(int x) { return x-1; } | |
auto ident = _L(add1) * _L(sub1); | |
auto ident2 = incr * _L(sub1); | |
cout << ident(1) << "," << ident2(1) << endl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename return_type, typename ...params> | |
auto _L( return_type (*fp) (params... args)) | |
{ return [fp] (params... args) { return fp(args...); }; }; |
NewerOlder