RU beehive logo ITEC dept promo banner
ITEC 380
2014fall
ibarland

homelecturesrecipeexamshwsD2Lbreeze (snow day)

P2-test-requirements
P2: initial tests

Complete the following tests, adding to the test cases already in the provided P0. They should be complete, running tests (even though they may not pass yet, and eval etc. might throw exceptions. But do have any new needed data definitions/classes with their fields, even if no code for them). On D2L, submit just your test-files containing this code (new data definitions/classes not needed).
Since you need to do P1 in both racket and Java, you need P1 tests for both languages (#1,#2). However, for P2, you only need test cases for the language you are using for P2 (#3 or #4).
Attach two files: the racket tests in one file, and the Java tests in another. (You can add these to the existing test files distributed with P0.)

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

  1. some P1 tests, racket

    You might insert your tests below into to the provided file expr-test-P0.rkt (and perhaps update the file-name to indicate that it now includes P1 and P2 tests).
    (require rackunit)
    
    (define prog0 "(: 8.1 %%% <3> :)")
    (check-equal? (string->expr prog0) ___________________________________________________)
    (check-equal? (expr->string (string->expr prog0)) _________________________ )
    (check-equal? (eval (string->expr prog0))  ________________ )
    
    
    ; Now, make more a test for a `whenP` expression.
    ; You only need one example for this submission, but
    ; in your real program you'll probably want at least two or three.
    
  2. some P1 tests, Java

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

      @Test
      void testAFew() {
        String prog0 = "(: 8.1 %%% 3 :)";
        assertEquals( Expr.parse(prog0), new BinExpr( ___________________________________________________ ) );
        assertEquals( Expr.parse(prog0).toString(), __________________________________________ );
        assertEquals( Expr.parse(prog0).eval(), __________________________________________ );
    
        
        // Now, make more a test for a `whenP` 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 JUnit1, 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() );
            }
          }    
      

  3. P2 tests, racket

    (define prog1 "fornow x be 5 in (:4 *** x:) stopit") ;2
    (check-equal? (string->expr prog1) ___________________________________________________)
    (check-equal? (expr->string (string->expr prog1)) _________________________ )
    (check-equal? (eval (string->expr prog1))  ________________ )
    
    ; Make an additional example, where the `let` is *not* the top-level expression:
    ; Then, have the three tests for it, as above.
    
    
    
    
    
    
    
    
    ; The last paragraph of #2 on hw07 mentions that you'll have to do substitution in a tree.
    ; Although `substitute` returns a *tree* (an Expr), 
    ; we can use `parse` (a.k.a. string->expr) (already tested!) to help us generate our expected-results.
    ;
    (check-equal? (substitute "x" 9 (string->expr "3"))   (string->expr ________________________________) )
    (check-equal? (substitute "x" 9 (string->expr "x"))   (string->expr ________________________________) )
    (check-equal? (substitute "z" 7 (string->expr "x"))   (string->expr ________________________________) )
    (check-equal? (substitute "z" 7 (string->expr "(: 4 +++ z :)"))   (string->expr ________________________________) )
    (check-equal? (substitute "z" 7 (string->expr "fornow x be z in (: x *** z :) stopit"))   (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.)
    
    
  4. some P2 tests, Java

        String prog1 = "fornow x be 5 in (:4 *** x:) stopit";
        assertEquals( Expr.parse(prog1),  ___________________________________________________)
        assertEquals( Expr.parse(prog1).toString(),  _________________________ )
        assertEquals( Expr.parse(prog1).eval(),  ________________ )
        /*
         ; Make an example, where the `let` is *not* the top-level expression:
         ; Then, have the three tests for it, as above.
        */
    
       
       
        
        
        /*
         ; The last paragraph of #2 on hw07 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("(4 +++ z)").substitute(new Id("z"),new Num(7)), Expr.parse( _________________________ ) );
        assertEquals( Expr.parse("fornow x be z in (:x *** z:) stopit").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 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 will eventually need to write an equals and hashCode method, as done in the provided P0 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.


1 Many IDEs have JUnit built-in (e.g. eclipse and BlueJ). If yours doesn't, you need to download a .zip from junit.org, place it in the same directory, and perhaps adjust the CLASSPATH for your environment so knows where to look for other classes. Post on the discussion board, to leverage your classmates' knowledge!      

2This exact string is subject to exact syntax we decide on in class. However, the three tests involving prog1 can all be completed w/o committing to that exact syntax!      

homelecturesrecipeexamshwsD2Lbreeze (snow day)


©2014, Ian Barland, Radford University
Last modified 2014.Nov.09 (Sun)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.