/** Test Exprs. * See http://www.radford.edu/~cs380/2025spring/Homeworks/Project/ * * This file includes both some specific tests for H0 and H1, * as well as some helper-functions which are helpful for H0-H3. * 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.Nov.11 */ 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 = "43"; String e1 = "shake 43 bake"; String e2 = "add 4 to 3"; String e3 = "shake shake add 4 to shake 3 bake bake bake"; String e4 = "add shake 43 bake to scale 42 to serve 3"; java.util.List> allTests; /* allTests should be a list of pairs: * a H0 Expr, and what that expression evaluates to. * Use a Double, for Num values. */ // @Test // Some expressions to test in a non-automated way: public void testMyStuff() { dbg("testMyStuff: parsing\n"); assertEquals( Expr.parse("99"), new Num(99)); assertEquals( Expr.parse("-99"), new Num(-99)); assertEquals( Expr.parse("shake 34 bake"), new Paren( new Num(34) )); assertEquals( Expr.parse(" shake 34 bake "), new Paren( new Num(34) )); assertEquals( Expr.parse("add 3 to 4"), new BinOp("add", new Num(3), new Num(4)) ); assertEquals( Expr.parse("add shake 99 bake to scale 3 to serve 4"), new BinOp( "add", new Paren(new Num(99)), new BinOp( "scale", new Num(3), new Num(4))) ); assertEquals( Expr.parse("sample 7; add 9 to taste or use 3 instead"), new IfZero( new Num(7), new Num(9), new Num(3))); assertEquals( Expr.parse("sample scale 3 to serve 4; add 7 to taste or use shake 99 bake instead"), new IfZero(new BinOp("scale", new Num(3), new Num(4)), new Num(7), new Paren(new Num(99)))); assertEquals( Expr.parse(""" sample sample 3; add 7 to taste or use 9 instead; add shake 99 bake to taste or use add shake 34 bake to scale 3 to serve 4 instead"""), new IfZero(new IfZero(new Num(3), new Num(7), new Num(9)), new Paren(new Num(99)), new BinOp( "add", new Paren(new Num(34)), new BinOp( "scale", new Num(3), new Num(4)))) ); // toString dbg("testMyStuff: testing toString\n"); assertEquals( Expr.parse("34").toString(), "34"); assertEquals( Expr.parse("shake 34 bake").toString(), "shake 34 bake"); assertEquals( Expr.parse(" shake 34 bake ").toString(), "shake 34 bake"); assertEquals( Expr.parse("add 3 to 4 ").toString(), "add 3 to 4"); assertEquals( Expr.parse("add shake 34 bake to scale 3 to serve 4").toString(), "add shake 34 bake to scale 3 to serve 4"); assertEquals( Expr.parse("sample 7; add 9 to taste or use 3 instead").toString(), "sample 7; add 9 to taste or use 3 instead"); assertEquals( Expr.parse("sample scale 3 to serve 4; add 7 to taste or use shake 34 bake instead").toString(), "sample scale 3 to serve 4; add 7 to taste or use shake 34 bake instead"); assertEquals( Expr.parse(""" sample add shake 34 bake to scale 3 to serve 4; add sample 7; add 9 to taste or use sample 4; add 5 to taste or use 6 instead instead to taste or use shake 34 bake instead""").toString(), "sample add shake 34 bake to scale 3 to serve 4; add sample 7; add 9 to taste or use sample 4; add 5 to taste or use 6 instead instead to taste or use shake 34 bake instead"); //eval dbg("testMyStuff: testing eval\n"); assertEquals( new Num(99).eval(), new Num(99) ); assertEquals( Expr.parse("99").eval(), new Num(99) ); assertEquals( Expr.parse("shake 99 bake").eval(), new Num(99) ); assertEquals( Expr.parse("add 3 to 4").eval(), new Num(7) ); assertEquals( Expr.parse("add shake 99 bake to scale 3 to serve 4").eval(), new Num(111) ); assertEquals( Expr.parse("sample 7; add 9 to taste or use 3 instead").eval(), new Num(3) ); assertEquals( Expr.parse("sample scale 3 to serve 4; add 7 to taste or use shake 99 bake instead").eval(), new Num(99) ); assertEquals( Expr.parse(""" sample sample 3; add 7 to taste or use 9 instead; add shake 99 bake to taste or use add shake 34 bake to scale 3 to serve 4 instead""").eval(), new Num(46) ); // "if sample 3; add 7 to taste or use 9 // equinox shake 99 bake // : add shake 34 bake // to scale 3 to serve 4" // Add more specific tests here, // if you want things more specific that provided via adding to `allTests` below. } // @Test 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 34,34,7,7,49 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("add 4 to 3"), new BinOp("add", new Num(4), new Num(3)) ); assertEquals( Expr.parse("add 4 to 3").eval(), new Num(7) ); assertEquals( Expr.parse("add 4 to 3").toString(), "add 4 to 3"); } /** * Sets up the test fixture. * Called before every test case method. */ // @Test public void setUp() { /* allTests should be a list of pairs: * a H0 Expr, and what that expression evaluates to. * Use a Double, for Num values. */ allTests = java.util.Arrays.asList( //;>>>H0-tests" new Pair("7", 7) ,new Pair("shake 3 bake", 3) ,new Pair("add 3 to 4", 7) ,new Pair("scale 3 to serve 4", 12) ,new Pair("add add 3 to 4 to scale 3 to serve 4", 19) ,new Pair("scale shake 3 bake to serve shake add 2 to 3 bake", 15) ,new Pair("sample 0; add 1 to taste or use 2 instead", 1) ,new Pair("sample 1; add 1 to taste or use 2 instead", 2) ,new Pair("sample add 3 to -3; add 1 to taste or use 2 instead", 1) ,new Pair("sample add sample sample 0; add 1 to taste or use 2 instead; add 3 to taste or use 4 instead to -3; add 1 to taste or use 2 instead", 2) /********* //;>>>H1-tests //; Uncomment these tests, once `mod` is implemented: ,new Pair("chop 3 into 4", 3) ,new Pair("chop add 5 to 6 into 3", 2) ,new Pair("chop 8.1 into 3", 2.1) ,new Pair("chop 8 into 3.1", 1.8) ,new Pair("chop -8.1 into 3", 0.9) ,new Pair("chop -8 into 3.1", 1.3) ,new Pair("chop 8.1 into -3", -0.9) ,new Pair("chop 8 into -3.1", -1.3) ,new Pair("chop -8.1 into -3", -2.1) ,new Pair("chop -8 into -3.1", -1.8) ,new Pair("chop 8 into 2", 0) ,new Pair("chop -8 into 2", 0) ,new Pair("chop 8 into -2", 0) ,new Pair("chop -8 into -2", 0) ,new Pair("chop 8 into 3", 2) ,new Pair("chop -8 into 3", 1) ,new Pair("chop 8 into -3", -1) ,new Pair("chop -8 into -3", -2) // STUDENT-TODO: YOU-MUST-CREATE-SOME-TESTS-FOR-IfLT+'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). */ // @Test 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 H0 Num, * and leave the double-comparison to Num.equals(Object). */ // @Test public void testEval() { dbg("testEval\n"); for ( Pair t : allTests ) { assertEquals( Number.class.isInstance(t.second()) ? new 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() { } // If you want to use real JUnit, then comment-out this homebrewed `assertEquals`. // (Also, yuou can UNcomment our `@Test` annotations for the functions below.) // /** assert that `actual.equals(expected)`. * Comment out if actually using JUnit. */ static void assertEquals( Object actual, Object expected ) { if (! actual.equals(expected)) throw new AssertionError(String.format("assertEquals Failed: %s ≠ %s.", actual, expected)); } /** 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 H0-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/~cs380/2025spring-ibarland/Homework/Project/H0-java/how-to-run-junit.html */