RU beehive logo promo banner for Computing & Info Sciences
ITEC 380
2024spring
ibarland

F2 initial tests

Optional, but highly recommended: Complete the following tests for F2.html, adding to the test cases already in the provided F0. They should be complete, running tests (even though they may not pass yet,and eval etc. might throw exceptions). But do write or update any data definitions/classes as needed.

When filling in the blanks below, the result of string->expr will be fairly verbose (spanning more than one line), but will also be the most informative in understanding how the code works.

F2-soln (link will be available after class on due-date.).

  1. some F1 tests, racket

    You might insert your tests below into to the provided file F0-tests.rkt (and perhaps update the file-name to indicate that it now includes F1 and F2 tests).
    (require rackunit)
    
    (define prog0 "(tilt 8.1 fall (3))")
    (check-expect (string->expr prog0)                                                                                                                                  )
    (check-expect (expr->string (string->expr prog0)) "                                                        " )
    (check-expect (eval (string->expr prog0))           )
    
    
    ; Now, make a test for an ifLT expression.
    ; You only need one example for this submission, but
    ; in your real program you'll probably want at least three.
    
  2. some F1 tests, Java

    You might add the following to the file ExprTest.java.

    @Test
    void testAFew() {
      String prog0 = "(tilt 8.1 fall (3))";  
      assertEquals( Expr.parse(prog0), new BinOp( fall,                                                                                                          ) );
      assertEquals( Expr.parse(prog0).toString(), "                                                        " );
      assertEquals( Expr.parse(prog0).eval(),                                      );
    
    
      // Now, make a test for a `IfLT` expression.
      // You only need one example for this submission, but
      // in your real program you'll probably want at least two or three.
      
      }

    If you are using JUnit (see how-to-run-junit.html), these tests should work directly. Otherwise, you might want to write your own method

    void assertEquals( Object actual,  Object expected ) {
      if (!actual.equals(expected)) {
        System.err.printf("assertion failed:\nExpect: %s\nActual: %s\n", expected.toString(), actual.toString() );
        }
      }
    It is not a requirement to use JUnit for this assignment.

  3. For F2, you'll use either racket or Java (that is, only complete (a) or (b)):
    TODO: I must re-arrange the order of the `let` expressions (swap the Id and the last Expr).
    1. F2 tests, racket

      (define prog2 "moon x on 5 eclipses tilt 4 fall x")
      (check-expect (string->expr prog2)                                                                                                                                           )
      (check-expect (expr->string (string->expr prog2))                )
      (check-expect (eval (string->expr prog2))         )
      
      ; Make an additional example, where the `let` is *not* the top-level expression:
      ; Then, have the three tests for it, as above.
      
      
      
      
      
      
      
      ; For F2, the item B.iv mentions that you'll have to do substitution in a tree.
      ; Although `substitute` returns a *tree* (an Expr),
      ; we can use `string->expr` (already tested!) to help us generate our expected-results.
      ;
      (check-expect (subst "x" 9 (string->expr "3"))   (string->expr         ) )
      (check-expect (subst "x" 9 (string->expr "x"))   (string->expr         ) )
      (check-expect (subst "z" 7 (string->expr "x"))   (string->expr         ) )
      (check-expect (subst "z" 7 (string->expr "tilt 4 spr z"))   (string->expr                                           ) )
      (check-expect (subst "z" 7 (string->expr "moon x on z eclipses tilt x sum z"))
                    (string->expr                                                                                                         ) )
      
      ; Give at least one more interesting tree, to test `substitute` on,
      ; with parse-tree of height of 2 or more.
      ; You do *not* need to do `substitute` on a parse tree containing a `let` inside of it ... yet.
      ; (But you are encouraged to start thinking about what you want to happen, in that situation.)
      
    2. some F2 tests, Java

      String prog2 = "let x := 5 @ [spr 4 x]";
      assertEquals( Expr.parse(prog2),                                                                                                                                                                                                                                                          );
      assertEquals( Expr.parse(prog2).toString(),                );
      assertEquals( Expr.parse(prog2).eval(),                                  );
      /*
       ; Make an example, where the `let` is *not* the top-level expression:
       ; Then, have the three tests for it, as above.
      */
      
      
      
      
      
      
      /*
       ; For F2, item B.iv mentions that you'll have to do substitution in a tree.
       ; Although `substitute` returns a *tree* (an Expr),
       ; we can use `parse` (already tested!) to help us generate our expected-results.
      */
      assertEquals( Expr.parse("3").substitute(new Id("x"),new Num(9)), Expr.parse(         ) );
      assertEquals( Expr.parse("x").substitute(new Id("x"),new Num(9)), Expr.parse(         ) );
      assertEquals( Expr.parse("x").substitute(new Id("z"),new Num(7)), Expr.parse(         ) );
      assertEquals( Expr.parse("[wntr 4 z]").substitute(new Id("z"),new Num(7)), Expr.parse(                                    ) );
      assertEquals( Expr.parse("let x := z in [x mlt z]").substitute(new Id("z"),new Num(7)),
                    Expr.parse(                                                                         ) );
      /*
       ; Give at least one more interesting tree, to test `substitute` on,
       ; with parse-tree of height 2 or more.
       ; You do *not* need to do `substitute` on a parse tree containing a `let` inside of it ... yet.
       ; (But you are encouraged to start thinking about what you want to happen, in that situation.)
      */
      

This code should compile and run, though of course they won't yet pass.
Note that before these tests compile/run, you'll need to define the structs/classes for Id and LetExprs. You don't need to submit those /structs classes with the test cases but presumably you'll at least have the class-with-field declarations, and (for Java) the routine constructor. Note that you don't need to write any getters for your Java fields, but you'll eventually need to write an equals and hashCode method, as done in the provided F0 classes.

These tests are not meant to be comprehensive of everything you'll eventually want to test for. They are just intended to get you started on the homework, and understanding what the functions need to do, before the weekend.


logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.