Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.nio.file.*;
- import java.util.*;
- import java.util.concurrent.Callable;
- import java.util.stream.IntStream;
- import java.util.stream.Stream;
- public class CalcTester {
- public static void main(String[] args) {
- new TestRunner(CalcTester.class).run();
- try {
- testGivenTests();
- testCalc();
- testOperate();
- } catch ( IOException e ) {
- System.out.println(e.getMessage());
- }
- }
- // test building new Calc objects and running them
- public static void testCalc() {
- Calc test;
- //Typical
- test = new Calc(new Scanner( "1 2 + 3 -" )); // with scanner, once
- assert test.call() == 0;
- //boundary
- test = new Calc("0"); // single entry
- assert test.call() == 0;
- //Special
- test = new Calc("1 1 test"); // invalid input to operator
- try {
- test.call();
- assert false;
- } catch (IllegalArgumentException e) {
- assert "java.lang.IllegalArgumentException: test".equals(e.getMessage()) : "msg: "+ e.getMessage();
- }
- //Special
- test = new Calc("1 +"); // stack out of range, empty prematurely
- try {
- test.call();
- assert false;
- } catch (IllegalStateException e) {
- assert "java.util.NoSuchElementException".equals(e.getMessage()) : "msg: "+ e.getMessage();
- }
- //Special
- test = new Calc("1 1 + 1"); // stack out of range, too many elements
- try {
- test.call();
- assert false;
- } catch (IllegalStateException e) {
- assert "java.lang.IllegalStateException: List elements not equal to one".equals(e.getMessage()) : "msg: "+ e.getMessage();
- }
- }
- // test operations
- public static void testOperate() {
- Calc test;
- // typical
- test = new Calc("1 1 +");
- assert test.call() == 2;
- test = new Calc("1 1 -");
- assert test.call() == 0;
- test = new Calc("2 2 *");
- assert test.call() == 4;
- test = new Calc("2 2 /");
- assert test.call() == 1;
- test = new Calc("4 2 /"); // be sure that lhs and rhs are in the right place
- assert test.call() == 2;
- test = new Calc("2 2 %");
- assert test.call() == 0;
- test = new Calc("4 2 ^");
- assert test.call() == 16;
- // boundary
- test = new Calc("-1 -1 +"); // negatives
- assert test.call() == -2;
- test = new Calc("0 0 +"); // zero
- assert test.call() == 0;
- // special
- test = new Calc("1.0 1 +"); // floating point should fail
- try {
- test.call();
- assert false;
- }
- catch (IllegalStateException e) {
- assert "java.util.NoSuchElementException".equals(e.getMessage()) : "msg: "+ e.getMessage();
- }
- }
- public static void testGivenTests() throws IOException {
- Calc calculator;
- // open test files
- try (Scanner test = new Scanner( Files.newInputStream(Paths.get("test.in"))) ; Scanner expected = new Scanner( Files.newInputStream(Paths.get("test.expected"))) ) {
- //compare all tests
- while (test.hasNextLine()) {
- calculator = new Calc(test.nextLine());
- assert expected.nextInt() == calculator.call();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement