Skip to content

Instantly share code, notes, and snippets.

@lshort
lshort / RuHaOfVeOfPr
Created October 2, 2014 20:21
RustHashmapOfVectorsOfPairs
pub struct Edge {
to: char,
from: char,
weight: int
}
pub struct digraph {
_vertices: Vec<char>,
_adj_list: HashMap<char, Vec<(char,int)> >
}
@lshort
lshort / recursiveExpectException
Created June 30, 2014 04:16
Recursive expect_exception
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 );
@lshort
lshort / testExample
Created June 30, 2014 04:05
test_example
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);
@lshort
lshort / finalExpect
Created June 30, 2014 04:03
final expect_exception
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();
@lshort
lshort / GraphTests
Last active August 29, 2015 14:03
Graph tests with expect_exception
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; };
@lshort
lshort / ImprovedExpectException
Created June 30, 2014 02:56
Improved expect_exception
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;
@lshort
lshort / ExpectExceptionBasic
Last active August 29, 2015 14:03
ExpectExceptionBasic
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 (...) {
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;
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;
template<typename return_type, typename ...params>
auto _L( return_type (*fp) (params... args))
{ return [fp] (params... args) { return fp(args...); }; };