import java.util.*; /* If using JUnit: * uncomment the imports below, * uncomment the `@Test` annotations, * comment out our homebrew `assertEquals` */ //import static org.junit.Assert.*; //import org.junit.Test; /** The test class UtilIanTest. */ public class UtilIanTest { public static void main(String... __) { var ack = new UtilIanTest(); ack.scannerTest(); ack.nextMatchTest(); ack.nextSplittingByTest(); ack.equalsApproxTest(); ack.nextDoubleSplittingByTest(); ack.mathTests(); ack.equalsIgnoreWhitespaceTest(); } //@Test public void mathTests() { System.out.print("starting mathTests..."); assertEquals(3.000, UtilIan.intToDouble(3), 0.00001); assertEquals(-2.0, UtilIan.intToDouble(-2), 0.00001); assertEquals(4, UtilIan.doubleToInt(4.001)); assertEquals(4, UtilIan.doubleToInt(4.9999)); assertEquals(-4, UtilIan.doubleToInt(-4.0001)); assertEquals(-3, UtilIan.doubleToInt(-3.9999)); assertEquals(4, UtilIan.roundToInt(4.0001)); assertEquals(4, UtilIan.roundToInt(3.9999)); assertEquals(-4, UtilIan.roundToInt(-3.99999)); assertEquals(-4, UtilIan.roundToInt(-4.0001)); assertEquals(5, UtilIan.roundToInt(4.5)); assertEquals(6, UtilIan.roundToInt(5.5)); assertEquals(-3, UtilIan.roundToInt(-3.5)); assertEquals(-4, UtilIan.roundToInt(-4.5)); assertEquals(4.0, UtilIan.roundTo(4.001,0),0.000001); assertEquals(4.0, UtilIan.roundTo(3.999,0),0.000001); assertEquals(-4.0, UtilIan.roundTo(-4.001,0),0.000001); assertEquals(-4.0, UtilIan.roundTo(-3.999,0),0.000001); assertEquals(-4.01, UtilIan.roundTo(-4.009,2),0.000001); assertEquals(-3.99, UtilIan.roundTo(-3.991,2),0.000001); assertEquals(-4.00, UtilIan.roundTo(-4.001,2),0.000001); assertEquals(-4.00, UtilIan.roundTo(-3.996,2),0.000001); assertEquals(-3.50, UtilIan.roundTo(-3.505,2),0.000001); assertEquals(200.0, UtilIan.roundTo(170.0,-2),0.000001); assertEquals(200.0, UtilIan.roundTo(249.9,-2),0.000001); assertEquals(1e1, UtilIan.roundTo(1.23e1,-1),0.000001); assertEquals(1e18, UtilIan.roundTo(1.23e18,-18),0.000000001e18); System.out.println("done"); } //@Test public void maxMinTests() { System.out.print("starting maxMinTests..."); assertEquals( Integer.valueOf(50), UtilIan.max( new Integer[] {20,50,30}) ); assertEquals( Integer.valueOf(50), UtilIan.max( Arrays.asList(20,50,30 )) ); assertEquals( Integer.valueOf(20), UtilIan.min( new Integer[] {20,50,30}) ); assertEquals( Integer.valueOf(20), UtilIan.min( Arrays.asList(20,50,30 )) ); assertEquals( Integer.valueOf(20), UtilIan.min( Arrays.asList(20)) ); // Causes an exception: //assertEquals( new Integer(20), UtilIan.min( new Integer[] {} ) ); System.out.println("done"); } //@Test public void scannerTest() { System.out.print("starting scannerTest..."); Scanner s = new Scanner(" hi there all" ); UtilIan.skipWhitespace(s); assertEquals( true, s.hasNext("hi") ); UtilIan.skipWhitespace(s); assertEquals( true, s.hasNext("hi") ); assertEquals( true, UtilIan.hasNextChar(s,'h') ); assertEquals( true, UtilIan.hasNextChar(s,'h') ); assertEquals( true, UtilIan.hasNextChar(s) ); assertEquals( true, UtilIan.hasNextChar(s,'h') ); assertEquals( Character.valueOf('h'), UtilIan.nextChar(s,'h') ); assertEquals( true, UtilIan.hasNextChar(s,'i') ); assertEquals( Character.valueOf('i'), UtilIan.nextChar(s,'i') ); assertEquals( true, UtilIan.hasNextChar(s, 't') ); assertEquals( Character.valueOf('t'), UtilIan.nextChar(s) ); assertEquals( true, UtilIan.hasNextChar(s,'h') ); s.next(); s.next(); assertEquals( false, UtilIan.hasNextChar(s,'h') ); assertEquals( false, UtilIan.hasNextChar(s) ); UtilIan.skipWhitespace(s); UtilIan.skipWhitespace(s); assertEquals( false, UtilIan.hasNextChar(s,'h') ); assertEquals( false, UtilIan.hasNextChar(s) ); System.out.println("done"); } //@Test public void nextMatchTest() { System.out.print("starting nextMatchTest..."); Scanner s = new Scanner(" hi the9re99 9all9" ); assertEquals("hi t", UtilIan.nextMatch(s,"hi t") ); assertEquals("he", UtilIan.nextMatch(s,"[a-z]*") ); assertEquals("", UtilIan.nextMatch(s,"[a-z]*") ); assertEquals(null, UtilIan.nextMatch(s,"[a-z]+") ); assertEquals(null, UtilIan.nextMatch(s,"[a-z]+") ); assertEquals("9re99 ",UtilIan.nextMatch(s,"\\d*[a-z]*\\d* ") ); assertEquals("9all9", UtilIan.nextMatch(s,".*") ); assertEquals("", UtilIan.nextMatch(s,".*") ); assertEquals(null, UtilIan.nextMatch(s,".+") ); assertEquals(false, s.hasNext() ); System.out.println("done"); } //@Test public void nextSplittingByTest() { System.out.print("starting nextSplittingByTest..."); Scanner s; s = new Scanner(" hi the9re99 0 9all9" ); assertEquals("hi", UtilIan.nextSplittingBy(s,"l9") ); assertEquals("the", UtilIan.nextSplittingBy(s,"l9") ); assertEquals("9", UtilIan.nextSplittingBy(s,"l9") ); assertEquals("re", UtilIan.nextSplittingBy(s,"l9") ); assertEquals("9", UtilIan.nextSplittingBy(s,"l9") ); assertEquals("9", UtilIan.nextSplittingBy(s,"l9") ); assertEquals("0", UtilIan.nextSplittingBy(s,"l9") ); assertEquals("9", UtilIan.nextSplittingBy(s,"l9") ); assertEquals("a", UtilIan.nextSplittingBy(s,"l9") ); assertEquals("l", UtilIan.nextSplittingBy(s,"l9") ); assertEquals("l", UtilIan.nextSplittingBy(s,"l9") ); assertEquals("9", UtilIan.nextSplittingBy(s,"l9") ); assertEquals(false, s.hasNext() ); s = new Scanner("?0 hi"); assertEquals( "?", UtilIan.nextSplittingBy(s,"?!") ); assertEquals( "0", UtilIan.nextSplittingBy(s,"?!") ); assertEquals( "hi", UtilIan.nextSplittingBy(s,"?!") ); assertEquals( true, UtilIan.hasNextSplittingBy( new Scanner("hello"), "hello", "{}" ) ); assertEquals( true, UtilIan.hasNextSplittingBy( new Scanner("hello{bye}"), "hello", "{}" ) ); assertEquals( false, UtilIan.hasNextSplittingBy( new Scanner("hello{bye}"), "hell", "{}" ) ); System.out.println("done"); } //@Test public void nextDoubleSplittingByTest() { System.out.print("starting nextDoubleSplittingByTest..."); Scanner s; String punct; punct = "!"; s = new Scanner("23.0 23 +4. 99 --3"); assertEquals(23.0, UtilIan.nextDoubleSplittingBy(s,punct), 0.001 ); assertEquals(23.0, UtilIan.nextDoubleSplittingBy(s,punct), 0.001 ); assertEquals( 4.0, UtilIan.nextDoubleSplittingBy(s,punct), 0.001 ); assertEquals(99.0, UtilIan.nextDoubleSplittingBy(s,punct), 0.001 ); s = new Scanner("23.0 23 +4. 99 --3"); assertEquals(true, UtilIan.hasNextDoubleSplittingBy(s,punct) ); assertEquals(23.0, UtilIan.nextDoubleSplittingBy(s,punct), 0.001 ); assertEquals(true, UtilIan.hasNextDoubleSplittingBy(s,punct) ); assertEquals(23.0, UtilIan.nextDoubleSplittingBy(s,punct), 0.001 ); assertEquals(true, UtilIan.hasNextDoubleSplittingBy(s,punct) ); assertEquals( 4.0, UtilIan.nextDoubleSplittingBy(s,punct), 0.001 ); assertEquals(true, UtilIan.hasNextDoubleSplittingBy(s,punct) ); assertEquals(99.0, UtilIan.nextDoubleSplittingBy(s,punct), 0.001 ); assertEquals(false, UtilIan.hasNextDoubleSplittingBy(s,punct) ); s = new Scanner("23.0 23 +4. 99 --3"); punct = "#:?!<>\\(\\){}\\[\\]"; assertEquals(true, UtilIan.hasNextDoubleSplittingBy(s,punct) ); assertEquals(23.0, UtilIan.nextDoubleSplittingBy(s,punct), 0.001 ); assertEquals(true, UtilIan.hasNextDoubleSplittingBy(s,punct) ); assertEquals(23.0, UtilIan.nextDoubleSplittingBy(s,punct), 0.001 ); assertEquals(true, UtilIan.hasNextDoubleSplittingBy(s,punct) ); assertEquals( 4.0, UtilIan.nextDoubleSplittingBy(s,punct), 0.001 ); assertEquals(true, UtilIan.hasNextDoubleSplittingBy(s,punct) ); assertEquals(99.0, UtilIan.nextDoubleSplittingBy(s,punct), 0.001 ); assertEquals(false, UtilIan.hasNextDoubleSplittingBy(s,punct) ); System.out.println("done"); } //@Test public void equalsIgnoreWhitespaceTest() { System.out.print("starting equalsIgnoreWhitespaceTest..."); assertEquals( true, UtilIan.equalsIgnoreWhitespace( "hi", "hi" )); assertEquals( true, UtilIan.equalsIgnoreWhitespace( "", "" )); assertEquals( true, UtilIan.equalsIgnoreWhitespace( " ", " " )); assertEquals( true, UtilIan.equalsIgnoreWhitespace( "hi there", "hi there" )); assertEquals( true, UtilIan.equalsIgnoreWhitespace( " ", " \t\n \t " )); assertEquals( true, UtilIan.equalsIgnoreWhitespace( "", " \t\n \t " )); assertEquals( true, UtilIan.equalsIgnoreWhitespace( "hi", "\nhi" )); assertEquals( true, UtilIan.equalsIgnoreWhitespace( "hi", "hi\n" )); assertEquals( true, UtilIan.equalsIgnoreWhitespace( "hi", " hi " )); assertEquals( true, UtilIan.equalsIgnoreWhitespace( "hi there", " hi there " )); assertEquals( false, UtilIan.equalsIgnoreWhitespace( "", "z" )); assertEquals( false, UtilIan.equalsIgnoreWhitespace( "a", "z" )); assertEquals( false, UtilIan.equalsIgnoreWhitespace( "hi there", "hithere" )); assertEquals( false, UtilIan.equalsIgnoreWhitespace( "hi\nthere", "hithere" )); assertEquals( false, UtilIan.equalsIgnoreWhitespace( "hi\n\tthere", "hithere" )); System.out.println("done"); } //@Test public void equalsApproxTest() { System.out.print("starting equalsApproxTest..."); assertFalse( UtilIan.equalsApprox(3.14,22.0/7.0) ); assertTrue( UtilIan.equalsApprox(3.1415926535,3.1415926536) ); assertTrue( UtilIan.equalsApprox(3.1415926535e-300,3.1415926536e-300) ); assertTrue( UtilIan.equalsApprox(3.1415926535e+300,3.1415926536e+300) ); assertFalse( UtilIan.equalsApprox(3.0, 4.0, 0.01) ); assertTrue( UtilIan.equalsApprox(3.0, 3.02, 0.01) ); // within 1% assertTrue( UtilIan.equalsApprox(3.0, 4.0, 0.35) ); // within 35% of each other System.out.println("done"); } /** assert that `actual.equals(expected)`. * Comment out if actually using JUnit. */ static void assertEquals( Object actual, Object expected ) { boolean passed = (actual==null || expected==null) ? actual==expected : actual.equals(expected); if (!passed) throw new AssertionError(String.format("assertEquals Failed: %s ≠ %s.", actual, expected)); } static void assertTrue( boolean rslt ) { if (!rslt) throw new AssertionError(String.format("assertTrue Failed.")); } static void assertFalse( boolean rslt ) { if (rslt) throw new AssertionError(String.format("asserFalse Failed.")); } /** assert that `actual` is within `tol`erance of `expected`. * Comment out if actually using JUnit. */ static void assertEquals( double actual, double expected, double tol ) { if (Math.abs(actual-expected) > tol) throw new AssertionError(String.format("assertEquals Failed: %g != %g (within %g).", actual, expected, tol)); } }