Skip to content

Instantly share code, notes, and snippets.

@kvalle
Last active December 14, 2015 19:29
Show Gist options
  • Save kvalle/5137378 to your computer and use it in GitHub Desktop.
Save kvalle/5137378 to your computer and use it in GitHub Desktop.
Provided tests for SML part of HW7 in Coursera course on programming languages, re-written to work with https://github.com/kvalle/sml-testing
(* University of Washington, Programming Languages, Homework 7
Based on hw7testsprovided.sml *)
use "hw7.sml";
use "testing.sml"; (* Requires https://github.com/kvalle/sml-testing to run! *)
open SmlTests;
fun real_equal(x,y) = Real.compare(x,y) = General.EQUAL;
(* Custom formatters / asserts *)
fun geom_formatter exp =
case exp of
NoPoints => "NoPoints"
| Point(x,y) => "Point(" ^ Real.toString(x) ^ ", " ^ Real.toString(y) ^ ")"
| Line(m,b) => "Line(" ^ Real.toString(m) ^ ", " ^ Real.toString(b) ^ ")"
| VerticalLine(x) => "VerticalLine(" ^ Real.toString(x) ^ ")"
| LineSegment(x1,y1,x2,y2) => "LineSegment("
^ Real.toString(x1) ^ ", " ^ Real.toString(y1) ^ ","
^ Real.toString(x2) ^ ", " ^ Real.toString(y2) ^ ")"
| Var s => "Var("^ s ^ ")"
| Let(s,e1,e2) => "Let-exp"
| Intersect(e1,e2) => "Intersect-exp"
| Shift(dx,dy,exp) => "Shift-exp";
fun geom_equal (v1, v2) =
case (v1, v2) of
(Point(a,b), Point(c,d)) => real_equal(a,c) andalso real_equal(b,d)
| (LineSegment(a,b,c,d), LineSegment(e,f,g,h)) => real_equal(a,e) andalso
real_equal(b,f) andalso
real_equal(c,g) andalso
real_equal(d,h)
(* insert more clauses here as needed *)
| _ => false; (* equality check only work on values *)
fun assert_equals_geom args = mk_assert_formatted geom_equal geom_formatter args;
(* Provided tests *)
test("preprocess converts a LineSegment to a Point successfully",
assert_equals_geom(
Point(3.2,4.1),
preprocess_prog(LineSegment(3.2,4.1,3.2,4.1))));
test("preprocess flips LineSegment if x1 > x2",
assert_equals_geom(
LineSegment(1.0,0.1,2.0,0.2),
preprocess_prog(LineSegment(2.0,0.2,1.0,0.1))));
test("eval_prog with empty environment worked",
assert_equals_geom(
Point(7.0,8.0),
eval_prog (preprocess_prog (Shift(3.0, 4.0, Point(4.0,4.0))), [])));
test("eval_prog with 'a' in environment is working properly",
assert_equals_geom(
Point(7.0,8.0),
eval_prog (Shift(3.0,4.0,Var "a"), [("a",Point(4.0,4.0))])));
test("eval_prog with shadowing 'a' in environment is working properly",
assert_equals_geom(
Point(7.0,8.0) ,
eval_prog (Shift(3.0,4.0,Var "a"), [("a",Point(4.0,4.0)),("a",Point(1.0,1.0))])));
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment