Skip to content

Instantly share code, notes, and snippets.

@kvalle
Last active December 14, 2015 19:29
Show Gist options
  • Select an option

  • Save kvalle/5137378 to your computer and use it in GitHub Desktop.

Select an option

Save kvalle/5137378 to your computer and use it in GitHub Desktop.

Revisions

  1. kvalle revised this gist Mar 12, 2013. 1 changed file with 1 addition and 7 deletions.
    8 changes: 1 addition & 7 deletions hw7testsprovided.sml
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ fun geom_equal (v1, v2) =

    fun assert_equals_geom args = mk_assert_formatted geom_equal geom_formatter args;

    (* Preprocess tests *)
    (* Provided tests *)

    test("preprocess converts a LineSegment to a Point successfully",
    assert_equals_geom(
    @@ -48,22 +48,16 @@ test("preprocess flips LineSegment if x1 > x2",
    LineSegment(1.0,0.1,2.0,0.2),
    preprocess_prog(LineSegment(2.0,0.2,1.0,0.1))));

    (* eval_prog tests with Shift*)

    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))), [])));

    (* Using a Var *)

    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))])));

    (* With Variable Shadowing *)

    test("eval_prog with shadowing 'a' in environment is working properly",
    assert_equals_geom(
    Point(7.0,8.0) ,
  2. kvalle revised this gist Mar 12, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions hw7testsprovided.sml
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,7 @@ fun geom_equal (v1, v2) =
    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;
  3. kvalle revised this gist Mar 12, 2013. 1 changed file with 46 additions and 47 deletions.
    93 changes: 46 additions & 47 deletions hw7testsprovided.sml
    Original file line number Diff line number Diff line change
    @@ -1,72 +1,71 @@
    (* University of Washington, Programming Languages, Homework 7
    hw7testsprovided.sml *)
    (* Will not compile until you implement preprocess and eval_prog *)

    (* Re-written to work with https://github.com/kvalle/sml-testing *)

    (* These tests do NOT cover all the various cases, especially for intersection *)
    Based on hw7testsprovided.sml *)

    use "hw7.sml";
    use "testing.sml";
    use "testing.sml"; (* Requires https://github.com/kvalle/sml-testing to run! *)

    open SmlTests;

    (* Must implement preprocess_prog and Shift before running these tests *)

    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)
    | _ => false; (* equality check only work on values *)

    fun assert_equals_geom args = mk_assert_formatted geom_equal geom_formatter args;

    (* Preprocess tests *)

    test("preprocess converts a LineSegment to a Point successfully",
    assert_true(
    let
    val Point(a,b) = preprocess_prog(LineSegment(3.2,4.1,3.2,4.1))
    val Point(c,d) = Point(3.2,4.1)
    in
    real_equal(a,c) andalso real_equal(b,d)
    end));

    test("preprocess flips an improper LineSegment successfully",
    assert_true(
    let
    val LineSegment(a,b,c,d) = preprocess_prog (LineSegment(3.2,4.1,~3.2,~4.1))
    val LineSegment(e,f,g,h) = LineSegment(~3.2,~4.1,3.2,4.1)
    in
    real_equal(a,e) andalso real_equal(b,f) andalso real_equal(c,g) andalso real_equal(d,h)
    end));
    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))));

    (* eval_prog tests with Shift*)

    test("eval_prog with empty environment worked",
    assert_true(
    let
    val Point(a,b) = (eval_prog (preprocess_prog (Shift(3.0, 4.0, Point(4.0,4.0))), []))
    val Point(c,d) = Point(7.0,8.0)
    in
    real_equal(a,c) andalso real_equal(b,d)
    end));
    assert_equals_geom(
    Point(7.0,8.0),
    eval_prog (preprocess_prog (Shift(3.0, 4.0, Point(4.0,4.0))), [])));

    (* Using a Var *)

    test("eval_prog with 'a' in environment is working properly",
    assert_true(
    let
    val Point(a,b) = (eval_prog (Shift(3.0,4.0,Var "a"), [("a",Point(4.0,4.0))]))
    val Point(c,d) = Point(7.0,8.0)
    in
    real_equal(a,c) andalso real_equal(b,d)
    end));
    assert_equals_geom(
    Point(7.0,8.0),
    eval_prog (Shift(3.0,4.0,Var "a"), [("a",Point(4.0,4.0))])));

    (* With Variable Shadowing *)

    test("eval_prog with shadowing 'a' in environment is working properly",
    assert_true(
    let
    val Point(a,b) = (eval_prog (Shift(3.0,4.0,Var "a"),
    [("a",Point(4.0,4.0)),("a",Point(1.0,1.0))]))
    val Point(c,d) = Point(7.0,8.0)
    in
    real_equal(a,c) andalso real_equal(b,d)
    end));
    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();
  4. kvalle created this gist Mar 11, 2013.
    72 changes: 72 additions & 0 deletions hw7testsprovided.sml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    (* University of Washington, Programming Languages, Homework 7
    hw7testsprovided.sml *)
    (* Will not compile until you implement preprocess and eval_prog *)

    (* Re-written to work with https://github.com/kvalle/sml-testing *)

    (* These tests do NOT cover all the various cases, especially for intersection *)

    use "hw7.sml";
    use "testing.sml";

    open SmlTests;

    (* Must implement preprocess_prog and Shift before running these tests *)

    fun real_equal(x,y) = Real.compare(x,y) = General.EQUAL;

    (* Preprocess tests *)

    test("preprocess converts a LineSegment to a Point successfully",
    assert_true(
    let
    val Point(a,b) = preprocess_prog(LineSegment(3.2,4.1,3.2,4.1))
    val Point(c,d) = Point(3.2,4.1)
    in
    real_equal(a,c) andalso real_equal(b,d)
    end));

    test("preprocess flips an improper LineSegment successfully",
    assert_true(
    let
    val LineSegment(a,b,c,d) = preprocess_prog (LineSegment(3.2,4.1,~3.2,~4.1))
    val LineSegment(e,f,g,h) = LineSegment(~3.2,~4.1,3.2,4.1)
    in
    real_equal(a,e) andalso real_equal(b,f) andalso real_equal(c,g) andalso real_equal(d,h)
    end));

    (* eval_prog tests with Shift*)

    test("eval_prog with empty environment worked",
    assert_true(
    let
    val Point(a,b) = (eval_prog (preprocess_prog (Shift(3.0, 4.0, Point(4.0,4.0))), []))
    val Point(c,d) = Point(7.0,8.0)
    in
    real_equal(a,c) andalso real_equal(b,d)
    end));

    (* Using a Var *)

    test("eval_prog with 'a' in environment is working properly",
    assert_true(
    let
    val Point(a,b) = (eval_prog (Shift(3.0,4.0,Var "a"), [("a",Point(4.0,4.0))]))
    val Point(c,d) = Point(7.0,8.0)
    in
    real_equal(a,c) andalso real_equal(b,d)
    end));

    (* With Variable Shadowing *)

    test("eval_prog with shadowing 'a' in environment is working properly",
    assert_true(
    let
    val Point(a,b) = (eval_prog (Shift(3.0,4.0,Var "a"),
    [("a",Point(4.0,4.0)),("a",Point(1.0,1.0))]))
    val Point(c,d) = Point(7.0,8.0)
    in
    real_equal(a,c) andalso real_equal(b,d)
    end));

    run();