/** Test Exprs. * See http://www.radford.edu/itec380/2017fall/Homeworks/Project/ * * This file includes both some specific tests for S0 and S1, * as well as some helper-functions which are helpful for S0-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 2017.Nov.26 */ 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 S0 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 S0 Expr, and what that expression evaluates to. * Use a Double, for Num values. */ allTests = java.util.Arrays.asList( /* S0 tests: */ new Pair( "7", 7 ) ,new Pair( "7", 7.000000001 ) //,new Pair( "7.0", 7 ) // parseString("7.0").toString() return "7", not "7.0". ,new Pair( "|3|", 3 ) ,new Pair( "?ad 3 4", 7 ) ,new Pair( "?mu 3 4 ", 12 ) ,new Pair( "?ad ?mu 3 4 ?ad 3 4", 19) ,new Pair( "?mu |3| |?ad 2 3|", 15) ,new Pair( "(5) !0 1 : 2", 1 ) ,new Pair( "(0) !0 1 : 2", 2 ) ,new Pair( "?ad 3 (-3) !0 1 : 2", 4 ) //,new Pair( "(? ad ((-1) !0 1 : 2) !0 3 : 4 -3) !0 1 : 2", 2 ) /* *** S1 tests: *** */ /* Uncomment these tests, once `mod` is implemented: ,new Pair("?mo 3 4", 3) ,new Pair("?mo ?ad 5 6 3", 2) ,new Pair("?mo 8.1 3", 2.1) ,new Pair("?mo 8 3.1", 1.8) ,new Pair("?mo -8.1 3", 0.9) ,new Pair("?mo -8 3.1", 1.3) ,new Pair("?mo 8.1 -3", -0.9) ,new Pair("?mo 8 -3.1", -1.3) ,new Pair("?mo -8.1 -3", -2.1) ,new Pair("?mo -8 -3.1", -1.8) ,new Pair("?mo 8 2", 0) ,new Pair("?mo -8 2", 0) ,new Pair("?mo 8 -2", 0) ,new Pair("?mo -8 -2", 0) ,new Pair("?mo 8 3", 2) ,new Pair("?mo -8 3", 1) ,new Pair("?mo 8 -3", -1) ,new Pair("?mo -8 -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 S0 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(); } } 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. } 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 S0-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/2017fall-ibarland/Lectures/how-to-run-junit.html */