/** Test Exprs. * See http://www.radford.edu/itec380/2018fall/Homeworks/Project/ * * This file includes both some specific tests for T0 and T1, * as well as some helper-functions which are helpful for T0-S3. * 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 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 2018.Nov.21 */ 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. // org.junit.runner.JUnitCore.main("ExprTest"); } static final double TOLERANCE = 0.000001; String e0 = "43"; String e1 = "<43>"; String e2 = "#4 boii 3#"; String e3 = "<<#4 boii <3>#>>"; String e4 = "#<43> boii #42 boiii 3##"; java.util.List> allTests; /* allTests should be a list of pairs: * a T0 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() { // parsing assertEquals( Expr.parse("34"), new Num(34) ); assertEquals( Expr.parse("-34"), new Num(-34) ); assertEquals( Expr.parse("<34>"), new Paren( new Num(34) ) ); assertEquals( Expr.parse("#3 boii 4#"), new BinOp( new Num(3), "boii", new Num(4) ) ); assertEquals( Expr.parse("#3 boii 4#"), new BinOp( new Num(3), "boii", new Num(4) ) ); assertEquals( Expr.parse("#<34> boii #3 boiii 4##"), new BinOp( new Paren(new Num(34)), "boii", new BinOp(new Num(3), "boiii", new Num(4)) ) ); assertEquals( Expr.parse("even? 3 dope 7 nope 9 dawg"), new Parity( new Num(3), new Num(7), new Num(9) ) ); assertEquals( Expr.parse("even? <34> dope #<34> boii #3 boiii 4## nope even? 3 dope 7 nope 9 dawg dawg"), new Parity( new Paren(new Num(34)), new BinOp( new Paren(new Num(34)), "boii", new BinOp(new Num(3), "boiii", new Num(4)) ), new Parity( new Num(3), new Num(7), new Num(9) ))); //eval assertEquals( new Num(34).eval(), new Num(34) ); assertEquals( Expr.parse("<34>").eval(), new Num(34) ); assertEquals( Expr.parse("#3 boii 4#").eval(), new Num(7) ); assertEquals( Expr.parse("#3 boi 4#").eval(), new Num(-1) ); assertEquals( Expr.parse("#3 boiii 4#").eval(), new Num(12) ); assertEquals( Expr.parse("even? 3 dope 4 nope 5 dawg").eval(), new Num(5) ); assertEquals( Expr.parse("even? 0 dope 4 nope 5 dawg").eval(), new Num(4) ); assertEquals( Expr.parse("even? #2 boi 4# dope #1 boii 2# nope #3 boii 4# dawg").eval(), new Num(3) ); assertEquals( Expr.parse("even? 7 dope #3 boii 4# nope 5 dawg").eval(), new Num(5) ); // toString assertEquals( Expr.parse("34").toString(), "34"); assertEquals( Expr.parse("<34>").toString(), "<34>"); assertEquals( Expr.parse("#3 boii 4#").toString(), "#3 boii 4#"); assertEquals( Expr.parse("#3 boi 4#").toString(), "#3 boi 4#"); assertEquals( Expr.parse(" # 3 boiii 4 # ").toString(), "#3 boiii 4#"); assertEquals( Expr.parse("even? 3 dope 4 nope 5 dawg").toString(), "even? 3 dope 4 nope 5 dawg"); assertEquals( Expr.parse("even? 0 dope #3 boii 4# nope 5 dawg").toString(), "even? 0 dope #3 boii 4# nope 5 dawg"); // Add more specific tests here, // if you want things more specific that provided via adding to `allTests` below. } public void testTowardsAutomating() { // 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(43.0, 43.0, 7.0, 7.0, 169.0).iterator(); while (exps.hasNext()) { Num actual = (Num)(Expr.parse(exps.next()).eval()); Num expected = new 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("#4 boii 3#"), new BinOp( new Num(4), "boii", new Num(3)) ); assertEquals( Expr.parse("#4 boii 3#").eval(), new Num(7) ); assertEquals( Expr.parse("#4 boii 3#").toString(), "#4 boii 3#"); } /** * Sets up the test fixture. * * Called before every test case method. */ public void setUp() { /* allTests should be a list of pairs: * a T0 Expr, and what that expression evaluates to. * Use a Double, for Num values. */ allTests = java.util.Arrays.asList( /* T0 tests: */ new Pair( "7", 7 ) ,new Pair( "7", 7.000000001 ) // confirm we compare `double`s merely within a tolerance ,new Pair( "<3>", 3 ) ,new Pair("#3 boii 4#", 7 ) ,new Pair("#3 boiii 4#", 12 ) ,new Pair("##3 boii 4# boii #3 boiii 4##", 19 ) ,new Pair("#<3> boiii <#2 boii 3#>#", 15 ) ,new Pair("even? 0 dope 1 nope 2 dawg", 1 ) ,new Pair("even? 1 dope 1 nope 2 dawg", 2 ) ,new Pair("even? #3 boii -3# dope 1 nope 2 dawg", 1 ) ,new Pair("even? #even? even? 0 dope 1 nope 2 dawg dope 3 nope 4 dawg boii -3# dope 1 nope 2 dawg", 2 ) /* *** T1 tests: *** */ /* Uncomment these tests, once `mod` is implemented: ,new Pair("#3 bae 4#", 3) ,new Pair("##5 boii 6# bae 3#", 2) ,new Pair("#8.1 bae 3#", 2.1) ,new Pair("#8 bae 3.1#", 1.8) ,new Pair("#-8.1 bae 3#", 0.9) ,new Pair("#-8 bae 3.1#", 1.3) ,new Pair("#8.1 bae -3#", -0.9) ,new Pair("#8 bae -3.1#", -1.3) ,new Pair("#-8.1 bae -3#", -2.1) ,new Pair("#-8 bae -3.1#", -1.8) ,new Pair("#8 bae 2#", 0) ,new Pair("#-8 bae 2#", 0) ,new Pair("#8 bae -2#", 0) ,new Pair("#-8 bae -2#", 0) ,new Pair("#8 bae 3#", 2) ,new Pair("#-8 bae 3#", 1) ,new Pair("#8 bae -3#", -1) ,new Pair("#-8 bae -3#", -2) YOU MUST CREATE SOME TESTS FOR IfGT's *** */ ); } /** 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() { for ( Pair t : allTests ) { String expected = t.getFirst(); String actual = Expr.parse( t.getFirst() ).toString(); if (! UtilIan.equalsIgnoreWhitespace(expected, actual, Expr.PUNCTUATION)) { // This assert will fail; just present it to the user: assertEquals( expected, actual ); } else { logPassedTest(); } } } /** 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 T0 Num, * and leave the double-comparison to Num.equals(Object). */ public void testEval() { for ( Pair t : allTests ) { assertEquals( Expr.parse( t.getFirst() ).eval(), Number.class.isInstance(t.getSecond()) ? new Num( ((Number)t.getSecond()).doubleValue() ) : t.getSecond() ); logPassedTest(); } } 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() { } } /* In the T0-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/2018fall-ibarland/Lectures/how-to-run-junit.html */