/** Test Exprs. * See http://www.radford.edu/itec380/2024spring/Homeworks/Project/ * * This file includes both some specific tests for F0 and F1, * as well as some helper-functions which are helpful for F0-F3. * HOWEVER, they aren't a cure-all -- * no tests are made against the expected internal representation (Expr-tree). * 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 may 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 2024.Apr.03 */ import java.util.*; public class ExprTest extends junit.framework.TestCase { /** Manually call our test-functions * (in case calling JUnit from your IDE isn't easy). */ public static void main(String... args) { ExprTest t = new ExprTest(); t.setUp(); t.testMyStuff(); t.tearDown(); t.setUp(); t.testTowardsAutomating(); t.tearDown(); t.setUp(); t.testParseToString(); t.tearDown(); t.setUp(); t.testEval(); t.tearDown(); // Alternately, this might work, but might require declaring a package // rather than the un-named package: calling `main` from org.junit: // org.junit.runner.JUnitCore.main("ExprTest"); t.dbg("Testing complete.\n"); } static final double TOLERANCE = 0.000001; String e0 = "34"; String e1 = "(34)"; String e2 = "tilt 3 sum 4"; String e3 = "tilt (34) sum tilt 3 spr 4"; String e4 = "if 7 equinox 3 : 9"; java.util.List> allTests; /* allTests should be a list of pairs: * a F0 Expr, and what that expression evaluates to. * Use a Double, for Num values. */ // Some expressions to test in a non-automated way: public void testMyStuff() { dbg("testMyStuff: parsing\n"); assertEquals( Expr.parse("99"), new Expr.Num(99)); assertEquals( Expr.parse("-99"), new Expr.Num(-99)); assertEquals( Expr.parse("(99)"), new Expr.Paren( new Expr.Num(99) )); assertEquals( Expr.parse("( 99 )"), new Expr.Paren( new Expr.Num(99) )); assertEquals( Expr.parse("tilt 3 sum 4"), new Expr.BinOp("sum", new Expr.Num(3), new Expr.Num(4)) ); assertEquals( Expr.parse("tilt (99) sum tilt 3 spr 4"), new Expr.BinOp( "sum", new Expr.Paren(new Expr.Num(99)), new Expr.BinOp( "spr", new Expr.Num(3), new Expr.Num(4))) ); assertEquals( Expr.parse("if 7 equinox 9 : 3"), new Expr.IfZero( new Expr.Num(7), new Expr.Num(9), new Expr.Num(3))); assertEquals( Expr.parse("if 3 equinox 4 : 5"), new Expr.IfZero(new Expr.Num(3), new Expr.Num(4), new Expr.Num(5) ) ); assertEquals( Expr.parse("if tilt 3 spr 4 equinox 7 : (99)"), new Expr.IfZero(new Expr.BinOp("spr", new Expr.Num(3), new Expr.Num(4)), new Expr.Num(7), new Expr.Paren(new Expr.Num(99)))); assertEquals( Expr.parse("if if 3 equinox 7:9 equinox (99) : tilt (34) sum tilt 3 spr 4"), new Expr.IfZero(new Expr.IfZero(new Expr.Num(3), new Expr.Num(7), new Expr.Num(9)), new Expr.Paren(new Expr.Num(99)), new Expr.BinOp( "sum", new Expr.Paren(new Expr.Num(34)), new Expr.BinOp( "spr", new Expr.Num(3), new Expr.Num(4)))) ); // toString dbg("testMyStuff: testing toString\n"); assertEquals( Expr.parse("34").toString(), "34"); assertEquals( Expr.parse("(34)").toString(), "(34)"); assertEquals( Expr.parse(" ( 34 ) ").toString(), "(34)"); assertEquals( Expr.parse("tilt 3 sum 4 ").toString(), "tilt 3 sum 4"); assertEquals( Expr.parse("tilt (34) sum tilt 3 spr 4").toString(), "tilt (34) sum tilt 3 spr 4"); assertEquals( Expr.parse("if 7 equinox 9 : 3").toString(), "if 7 equinox 9 : 3"); assertEquals( Expr.parse("if tilt 3 spr 4 equinox 7 : ( 34 )").toString(), "if tilt 3 spr 4 equinox 7 : (34)"); assertEquals( Expr.parse("if tilt (34) sum tilt 3 spr 4 equinox if 7 equinox 9 : if 4 equinox 5 : 6 : (34)").toString(), // "if tilt (34) sum ]3 spr 4 [ // equinox if 7 // equinox 9 // : if 4 equinox 5 : 6 // : (34)" "if tilt (34) sum tilt 3 spr 4 equinox if 7 equinox 9 : if 4 equinox 5 : 6 : (34)"); //eval dbg("testMyStuff: testing eval\n"); assertEquals( new Expr.Num(99).eval(), new Expr.Num(99) ); assertEquals( Expr.parse("99").eval(), new Expr.Num(99) ); assertEquals( Expr.parse("(99)").eval(), new Expr.Num(99) ); assertEquals( Expr.parse("tilt 3 sum 4").eval(), new Expr.Num(7) ); assertEquals( Expr.parse("tilt (99) sum tilt 3 spr 4").eval(), new Expr.Num(111) ); assertEquals( Expr.parse("if 7 equinox 9 : 3").eval(), new Expr.Num(3) ); assertEquals( Expr.parse("if tilt 3 spr 4 equinox 7 : (99)").eval(), new Expr.Num(99) ); assertEquals( Expr.parse("if if 3 equinox 7:9 equinox (99): tilt (34) sum tilt 3 spr 4").eval(), new Expr.Num(46) ); // "if if 3 equinox 7:9 // equinox (99) // : tilt (34) // sum // tilt 3 spr 4" // Add more specific tests here, // if you want things more specific that provided via adding to `allTests` below. } public void testTowardsAutomating() { dbg("testTowardsAutomating\n"); // we can automate checking that parse! is the (right)inverses of expr->string: for( String e : Arrays.asList(e0,e1,e2,e3,e4) ) { assertEquals( Expr.parse(e).toString(), e ); } // Though we also want to check that e0..e4 eval to 43,43,7,7,169 respectively. Iterator exps = Arrays.asList(e0,e1,e2,e3,e4).iterator(); Iterator dubs = Arrays.asList(34.0, 34.0, 7.0, 46.0, 9.0).iterator(); while (exps.hasNext()) { Expr.Num actual = (Expr.Num)(Expr.parse(exps.next()).eval()); Expr.Num expected = new Expr.Num(dubs.next()); assertEquals( actual, expected ); } // The above is a promising start, to automating tests. // Okay, we'll generalize the above to a more complete test-harness. // One thing, is that we don't want to have two parallel-lists; // instead keep a list of pairs. // Three sorts of tests we want to make, for many different exprs: assertEquals( Expr.parse("tilt 4 sum 3"), new Expr.BinOp("sum", new Expr.Num(4), new Expr.Num(3)) ); assertEquals( Expr.parse("tilt 4 sum 3").eval(), new Expr.Num(7) ); assertEquals( Expr.parse("tilt 4 sum 3").toString(), "tilt 4 sum 3"); } /** * Sets up the test fixture. * * Called before test case method. */ public void setUp() { /* allTests should be a list of pairs: * a F0 Expr, and what that expression evaluates to. * Use a Double, for Num values. */ allTests = java.util.Arrays.asList( //;>>>F0-tests" new Pair("7", 7) ,new Pair("(3)", 3) ,new Pair("tilt 3 sum 4", 7) ,new Pair("tilt 3 spr 4", 12) ,new Pair("tilt tilt 3 sum 4 sum tilt 3 spr 4", 19) ,new Pair("tilt (3) spr (tilt 2 sum 3)", 15) ,new Pair("if 0 equinox 1 : 2", 1) ,new Pair("if 1 equinox 1 : 2", 2) ,new Pair("if tilt 3 sum -3 equinox 1 : 2", 1) ,new Pair("if tilt if if 0 equinox 1 : 2 equinox 3 : 4 sum -3 equinox 1 : 2", 2) /********* //;>>>F1 tests //; Uncomment these tests, once `mod` is implemented: ,new Pair("tilt 3 fall 4", 3) ,new Pair("tilt tilt 5 sum 6 fall 3", 2) ,new Pair("tilt 8.1 fall 3", 2.1) ,new Pair("tilt 8 fall 3.1", 1.8) ,new Pair("tilt -8.1 fall 3", 0.9) ,new Pair("tilt -8 fall 3.1", 1.3) ,new Pair("tilt 8.1 fall -3", -0.9) ,new Pair("tilt 8 fall -3.1", -1.3) ,new Pair("tilt -8.1 fall -3", -2.1) ,new Pair("tilt -8 fall -3.1", -1.8) ,new Pair("tilt 8 fall 2", 0) ,new Pair("tilt -8 fall 2", 0) ,new Pair("tilt 8 fall -2", 0) ,new Pair("tilt -8 fall -2", 0) ,new Pair("tilt 8 fall 3", 2) ,new Pair("tilt -8 fall 3", 1) ,new Pair("tilt 8 fall -3", -1) ,new Pair("tilt -8 fall -3", -2) // STUDENT-TODO: YOU-MUST-CREATE-SOME-TESTS-FOR-IfLE's *********/ ); } ////////////////////////////////////////////////////////////////////////////////////////////// ///////////// DO NOT CHANGE the below ///////////// (unless you really want to) ///////////// ////////////////////////////////////// helper functions //;////////////////////////////////// /** For every element in allTests, * parse the string, and then call toString on the result, * checking that we get back exactly the input string * (up to whitespace). */ public void testParseToString() { dbg("testParseToString\n"); for ( Pair t : allTests ) { String expected = t.first(); String actual = Expr.parse( t.first() ).toString(); if (! UtilIan.equalsIgnoreWhitespace(expected, actual, Expr.PUNCTUATION)) { // This assert will fail; just present it to the user: assertEquals( expected, actual ); } else { logPassedTest(); } } dbg("\n"); } /** 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 Java Number, convert it to an F0 Num, * and leave the double-comparison to Num.equals(Object). */ public void testEval() { dbg("testEval\n"); for ( Pair t : allTests ) { assertEquals( Number.class.isInstance(t.second()) ? new Expr.Num( ((Number)t.second()).doubleValue() ) : t.second(), Expr.parse( t.first() ).eval() ); logPassedTest(); } dbg("\n"); } private int testCaseNum = 0; public void logPassedTest() { ++testCaseNum; System.err.printf(".%s", (testCaseNum % 5 == 0) ? " " : "" ); } /** * Tears down the test fixture. * Called after every test case method. */ public void tearDown() { } /** Just like `System.err.printf` but for debugging-output; * comment out its body to disable. * (This same method might be repeated in multiple classes, so that * you turn messages on/off on a per-file basis.) * @see java.io.PrintStream#printf * @param fmt the format string * @param infos Any data needed by the `fmt` (one item per '%' in `fmt`) */ public void dbg( String fmt, Object... infos ) { System.err.printf( fmt, infos ); testCaseNum = 0; // reset the grouping of "."s for passing test-cases. } } /* In the F0-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 Some slight further info at http://www.radford.edu/itec380/2024spring-ibarland/Homework/Project/F0-java/how-to-run-junit.html */