Advertisement
Nattack

Untitled

Jul 20th, 2017
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. import java.io.*;
  2. import java.nio.file.*;
  3. import java.util.*;
  4. import java.util.concurrent.Callable;
  5. import java.util.stream.IntStream;
  6. import java.util.stream.Stream;
  7.  
  8. public class CalcTester {
  9.     public static void main(String[] args) {
  10.         new TestRunner(CalcTester.class).run();
  11.         try {
  12.             testGivenTests();
  13.             testCalc();
  14.             testOperate();
  15.         } catch ( IOException e ) {
  16.             System.out.println(e.getMessage());
  17.         }
  18.     }
  19.  
  20.     // test building new Calc objects and running them
  21.     public static void testCalc() {
  22.         Calc test;
  23.  
  24.         //Typical
  25.         test = new Calc(new Scanner( "1 2 + 3 -" )); // with scanner, once
  26.         assert test.call() == 0;
  27.  
  28.         //boundary
  29.         test = new Calc("0"); // single entry
  30.         assert test.call() == 0;
  31.  
  32.         //Special
  33.         test = new Calc("1 1 test"); // invalid input to operator
  34.         try {
  35.             test.call();
  36.             assert false;
  37.         } catch (IllegalArgumentException e) {
  38.             assert "java.lang.IllegalArgumentException: test".equals(e.getMessage()) : "msg: "+ e.getMessage();
  39.         }
  40.  
  41.         //Special
  42.         test = new Calc("1 +"); // stack out of range, empty prematurely
  43.         try {
  44.             test.call();
  45.             assert false;
  46.         } catch (IllegalStateException e) {
  47.             assert "java.util.NoSuchElementException".equals(e.getMessage()) : "msg: "+ e.getMessage();
  48.         }
  49.  
  50.         //Special
  51.         test = new Calc("1 1 + 1"); // stack out of range, too many elements
  52.         try {
  53.             test.call();
  54.             assert false;
  55.         } catch (IllegalStateException e) {
  56.             assert "java.lang.IllegalStateException: List elements not equal to one".equals(e.getMessage()) : "msg: "+ e.getMessage();
  57.         }
  58.     }
  59.  
  60.     // test operations
  61.     public static void testOperate() {
  62.         Calc test;
  63.  
  64.         // typical
  65.         test = new Calc("1 1 +");
  66.         assert test.call() == 2;
  67.  
  68.         test = new Calc("1 1 -");
  69.         assert test.call() == 0;
  70.  
  71.         test = new Calc("2 2 *");
  72.         assert test.call() == 4;
  73.  
  74.         test = new Calc("2 2 /");
  75.         assert test.call() == 1;
  76.  
  77.         test = new Calc("4 2 /"); // be sure that lhs and rhs are in the right place
  78.         assert test.call() == 2;
  79.  
  80.         test = new Calc("2 2 %");
  81.         assert test.call() == 0;
  82.  
  83.         test = new Calc("4 2 ^");
  84.         assert test.call() == 16;
  85.  
  86.         // boundary
  87.         test = new Calc("-1 -1 +"); // negatives
  88.         assert test.call() == -2;
  89.  
  90.         test = new Calc("0 0 +"); // zero
  91.         assert test.call() == 0;
  92.  
  93.         // special
  94.         test = new Calc("1.0 1 +"); // floating point should fail
  95.         try {
  96.             test.call();
  97.             assert false;
  98.         }
  99.         catch (IllegalStateException e) {
  100.             assert "java.util.NoSuchElementException".equals(e.getMessage()) : "msg: "+ e.getMessage();
  101.         }
  102.  
  103.     }
  104.  
  105.     public static void testGivenTests() throws IOException {
  106.         Calc calculator;
  107.  
  108.         // open test files
  109.         try (Scanner test = new Scanner( Files.newInputStream(Paths.get("test.in"))) ; Scanner expected = new Scanner( Files.newInputStream(Paths.get("test.expected"))) ) {
  110.             //compare all tests
  111.             while (test.hasNextLine()) {
  112.                 calculator = new Calc(test.nextLine());
  113.                 assert expected.nextInt() == calculator.call();
  114.             }
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement