/** Test Exprs. * See http://www.radford.edu/itec380/2015fall/Homeworks/Project/ * * Meant to cover most syntax situations for Q0-Q3. * HOWEVER, the testing approach has some systemic flaws -- * we never test against the expected internal representation. * (For example, testParseToString just checks that parse and * 'toString' are inverses of each other; if they are both * the identity function, it would pass all tests.) * * Compiling this function will generate a warning * ('unchecked generic array creation', due to using Arrays.asList * and varargs). * * For compiling with junit (if your IDE doesn't automatically recognize it), * see the note at end of this file. * * @author Ian Barland * @version 2013.Nov.08 */ public class ExprTest extends junit.framework.TestCase { static final double TOLERANCE = 0.000001; java.util.List> allTests; /* allTests should be a list of pairs: * a Q0 Expr, and what that expression evaluates to. * Use a Double, for Num values. */ /** * Sets up the test fixture. * * Called before every test case method. */ public void setUp() { /* allTests should be a list of pairs: * a Q0 Expr, and what that expression evaluates to. * Use a Double, for Num values. */ allTests = java.util.Arrays.asList( /* Q0 tests: */ new utils.Pair( "7", 7 ) ,new utils.Pair( "7", 7.000000001 ) //,new utils.Pair( "7.0", 7 ) // parseString("7.0").toString() return "7", not "7.0". ,new utils.Pair( "[[3]]", 3 ) ,new utils.Pair( "(3 add 4 )", 7 ) ,new utils.Pair( "( 3 mul 4)", 12 ) ,new utils.Pair( "((3 add 4) add ( 3 mul 4 ))", 19) ,new utils.Pair( "([[3]] mul [[(2 add [[3]])]])", 15) ,new utils.Pair( "parity 0 even: 1 odd: 2;", 1 ) ,new utils.Pair( "parity 1 even: 1 odd: 2;", 2 ) ,new utils.Pair( "parity (3 add -3) even: 1 odd: 2;", 1 ) ,new utils.Pair( "parity (parity parity -1 even: 1 odd: 2; even: 3 odd: 4 ; add -3) even: 1 odd: 2;", 1 ) /* *** Q1 tests: *** */ // Uncomment these tests, once `mod` is implemented: ,new utils.Pair( "(3 mod 4)", 3) ,new utils.Pair( "((5 add 6) mod 3)", 2) ,new utils.Pair( "(8.1 mod 3)", 2.1) ,new utils.Pair( "(8 mod 3.1)", 1.8) ,new utils.Pair( "(-8.1 mod 3)", 0.9) ,new utils.Pair( "(-8 mod 3.1)", 1.3) ,new utils.Pair( "(8.1 mod -3)", -0.9) ,new utils.Pair( "(8 mod -3.1)", -1.3) ,new utils.Pair( "(-8.1 mod -3)", -2.1) ,new utils.Pair( "(-8 mod -3.1)", -1.8) ,new utils.Pair( "(8 mod 2)", 0) ,new utils.Pair( "(-8 mod 2)", 0) ,new utils.Pair( "(8 mod -2)", 0) ,new utils.Pair( "(-8 mod -2)", 0) ,new utils.Pair( "(8 mod 3)", 2) ,new utils.Pair( "(-8 mod 3)", 1) ,new utils.Pair( "(8 mod -3)", -1) ,new utils.Pair( "(-8 mod -3)", -2) ,new utils.Pair( "say x be 5 in 17 mates", 17) ,new utils.Pair( "say x be 5 in x mates", 5) ,new utils.Pair( "say x be 5 in (x add x) mates", 10 ) ,new utils.Pair( "say x be 5 in say z be 7 in (x add z) mates mates", 12 ) ,new utils.Pair( "say x be say z be 5 in (z add 3) mates in (x add x) mates", 16 ) ,new utils.Pair( "say x be [[say z be 5 in (z add 3) mates]] in (x add x) mates", 16 ) ); } /** For every element in allTests, * parse the string, and.even call toString on the result, * checking that we get back exactly the input string * (up to whitespace). */ public void testParseToString() { int currTestNum=0; for ( utils.Pair t : allTests ) { ++currTestNum; String expected = t.getFirst(); String actual = Expr.parse( t.getFirst() ).toString(); if (! utils.UtilIan.equalsIgnoreWhitespace(expected, actual, Expr.PUNCTUATION)) { System.err.printf("Parsing-test #%d: ",currTestNum); // This assert will fail; just present it to the user: assertEquals( expected, actual ); } } } /** For every element in allTests, * parse the string and eval the result, * checking that we get back the second item in the pair. * If the second item is a number, we check within {#value TOLERANCE}. * If it's not a number (as in Q2, once function-values are introduced), * this code doesn't actually test anything, and asserts an error saying so. */ public void testEval() { for ( utils.Pair t : allTests ) { if (Number.class.isInstance( t.getSecond() )) { assertEquals( ((Number)t.getSecond()).doubleValue(), ((Num)(Expr.parse( t.getFirst() ).eval())).doubleValue(), TOLERANCE ); } else { assertEquals( t.getFirst() + " didn't evaluate to a double", "How to express the expected Fun value?" ); } } } public void testMyStuff() { assertEquals( Expr.parse("2").eval(), new Num(2) ); // Add more specific tests here, // if you want things more specific that provided via adding to `allTests` above. } /** * Tears down the test fixture. * * Called after every test case method. */ public void tearDown() { } } /* In the Q0-java code, a couple of classes extend junit.framework.TestCase. In order to compile these, you'll need to have that class (and others) in your CLASSPATH. I know that BlueJ comes with the junit-classes automatically, and I imagine that Eclipse does, as well. If you're getting an error "package junit.framework does not exist", you'll want to download 'junit.jar' (I got it from junit.org), and extract it somewhere in your classpath (e.g., in the same dir as your project). To run the tests [again, BlueJ and Eclipse have buttons to do this] from the command line, you'd type: java org.junit.runner.JUnitCore TestExpr */