Пример #1
0
 @Test
 public void testAdd() {
   double value1 = 3;
   double value2 = 3;
   double result = value1 + value2;
   Calculator c = new Calculator();
   System.out.println("-> TESTING ADD...");
   Assert.assertTrue(c.add(value1, value2) == result);
 }
Пример #2
0
 @Test
 public void testDivide() {
   double value1 = 9;
   double value2 = 12;
   double result = value1 / value2;
   Calculator c = new Calculator();
   System.out.println("-> TESTING DIVIDE...");
   Assert.assertTrue(c.divide(value1, value2) == result);
 }
Пример #3
0
 @Test
 public void testMultiply() {
   double value1 = 3;
   double value2 = 4;
   double result = value1 * value2;
   Calculator c = new Calculator();
   System.out.println("-> TESTING MULTIPLY..");
   Assert.assertTrue(c.multiply(value1, value2) == result);
 }
Пример #4
0
 @Test
 public void testSubtract() {
   double value1 = 5;
   double value2 = 10;
   double result = value1 - value2;
   Calculator c = new Calculator();
   System.out.println("-> TESTING Subtract...");
   Assert.assertTrue(c.subtract(value1, value2) == result);
 }
Пример #5
0
 /**
  * Tests wumbo() method.
  *
  * @author kuczynskij
  */
 @Test
 public void testWumbo() {
   if (Calculator.getWumbo()) {
     Calculator.wumbo();
     assert (Calculator.getWumbo() == false);
     Calculator.wumbo();
     assert (Calculator.getWumbo() == true);
   } else {
     Calculator.wumbo();
     assert (Calculator.getWumbo() == true);
     Calculator.wumbo();
     assert (Calculator.getWumbo() == false);
   }
 }
Пример #6
0
 /**
  * Tests hist() method.
  *
  * @author chuna
  */
 @SuppressWarnings("serial")
 @Test
 public void testHist() {
   Calculator.clear();
   String expectedAns = "There is no history to display.";
   assert (Calculator.hist().equals(expectedAns));
   List<Integer> l =
       new ArrayList<Integer>() {
         {
           add(10);
           add(5);
           add(2);
         }
       };
   Calculator.add(l);
   Calculator.sub(l);
   Calculator.mult(l);
   try {
     Calculator.div(l);
   } catch (Exception e) {
     assert (false);
   }
   expectedAns =
       "1 | div | 10 5 2 | 1\n"
           + "2 | mult | 10 5 2 | 100\n"
           + "3 | sub | 10 5 2 | 3\n"
           + "4 | add | 10 5 2 | 17";
   assert (Calculator.hist().equals(expectedAns));
 }
Пример #7
0
  public static void main(String[] args) {
    int result = 0;

    Calculator myCalc = new Calculator();

    try {
      if (args.length != 1) {
        throw new IllegalArgumentException("There must be exactly one argument! e.g. add(1, 2)");
      }

      if (!myCalc.checkBrackets(args[0])) {
        throw new IllegalArgumentException(
            "Brackets of the input expression \"" + args[0] + "\" do NOT match!");
      }

      /* Normalize the expression by removing all the whitespaces around
       * opening/closing brackets and the comma separator.
       * Any remaining whitespaces after the normalization will indicate
       * an invalid expression during evaluation later on.
       */

      /* Remove all white spaces before and after the opening and closing brackets
       */
      String expr_v1 = args[0].replaceAll("(.)(\\s+)([()])(\\s+)(.)", "$1$3$5");

      /* Remove all spaces before and after the separator ,
       */
      String expr_v2 = expr_v1.replaceAll("(.)(\\s*)([,])(\\s*)(.)", "$1$3$5");

      String expr_v3 = expr_v2.replaceAll("(.)([,])(\\s+)(.)", "$1$2$4");

      myCalc.logger.setLevel(DEBUG);

      result = myCalc.evaluateExpression(expr_v3);

      /* Print the result only if the expression is valid. Otherwise, an error
       * message has already been logged.
       */
      System.out.println(result);
    } catch (Exception e) {
      myCalc.logger.log(ERROR, e.toString());
      return;
    }
  }
Пример #8
0
 /**
  * Tests div() method.
  *
  * @author kuczynskij
  * @throws Exception
  */
 @SuppressWarnings("serial")
 @Test
 public void testDiv() {
   try {
     List<Integer> l =
         new ArrayList<Integer>() {
           {
             add(4);
             add(2);
             add(3);
           }
         };
     assert (Calculator.div(l) == 0);
     l =
         new ArrayList<Integer>() {
           {
             add(Integer.MIN_VALUE);
             add(1);
           }
         };
     assert (Calculator.div(l) == Integer.MIN_VALUE);
     l =
         new ArrayList<Integer>() {
           {
             add(Integer.MAX_VALUE);
             add(1);
           }
         };
     assert (Calculator.div(l) == Integer.MAX_VALUE);
     l =
         new ArrayList<Integer>() {
           {
             add(4);
             add(-5);
             add(6);
           }
         };
     assert (Calculator.div(l) == 0);
   } catch (Exception e) {
     assert (false);
   }
 }
Пример #9
0
 /**
  * Tests add() method.
  *
  * @author chuna
  * @throws Exception
  */
 @Test
 public void testGetUserInput() throws Exception {
   Calculator.clear();
   Scanner in =
       new Scanner(
           "add 1 2 3\n"
               + "sub 1 2 3\n"
               + "mult 1 2 3\n"
               + "div 10 5 2\n"
               + "wumbo\n"
               + "add 1 2 3\n"
               + "sub 1 2 3\n"
               + "mult 1 !2 3\n"
               + "div 10 5 2#\n"
               + "hist\n"
               + "clear\n"
               + "help\n"
               + "lsdjfl\n"
               + "quit");
   Calculator.setScanner(in);
   Calculator.getUserInput();
 }
Пример #10
0
 /**
  * Tests sub() method.
  *
  * @author kuczynskij
  */
 @SuppressWarnings("serial")
 @Test
 public void testSub() {
   List<Integer> l =
       new ArrayList<Integer>() {
         {
           add(1);
           add(2);
           add(3);
         }
       };
   assert (Calculator.sub(l) == -4);
   l =
       new ArrayList<Integer>() {
         {
           add(Integer.MAX_VALUE);
           add(-1);
         }
       };
   assert (Calculator.sub(l) == Integer.MAX_VALUE);
   l =
       new ArrayList<Integer>() {
         {
           add(Integer.MIN_VALUE);
           add(1);
         }
       };
   assert (Calculator.sub(l) == Integer.MIN_VALUE);
   l =
       new ArrayList<Integer>() {
         {
           add(4);
           add(-5);
           add(6);
         }
       };
   assert (Calculator.sub(l) == 3);
 }
Пример #11
0
 /**
  * Tests mult() method.
  *
  * @author chuna
  */
 @SuppressWarnings("serial")
 @Test
 public void testMult() {
   List<Integer> l =
       new ArrayList<Integer>() {
         {
           add(4);
           add(2);
           add(3);
         }
       };
   assert (Calculator.mult(l) == 24);
   l =
       new ArrayList<Integer>() {
         {
           add(Integer.MIN_VALUE);
           add(2);
         }
       };
   assert (Calculator.mult(l) == Integer.MIN_VALUE);
   l =
       new ArrayList<Integer>() {
         {
           add(Integer.MAX_VALUE);
           add(2);
         }
       };
   assert (Calculator.mult(l) == Integer.MAX_VALUE);
   l =
       new ArrayList<Integer>() {
         {
           add(4);
           add(-5);
           add(6);
         }
       };
   assert (Calculator.mult(l) == -120);
 }
Пример #12
0
 /**
  * Tests the div() for Exception (divide by zero).
  *
  * @author chuna
  * @throws Exception
  */
 @SuppressWarnings("serial")
 @Test(expectedExceptions = Exception.class)
 public void testZeroDivide() throws Exception {
   List<Integer> l =
       new ArrayList<Integer>() {
         {
           add(4);
           add(0);
           add(6);
         }
       };
   Calculator.div(l);
   assert (false);
 }
Пример #13
0
 /**
  * Tests clear() method.
  *
  * @author kuczynskij
  */
 @SuppressWarnings("serial")
 @Test
 public void testClear() {
   List<Integer> l =
       new ArrayList<Integer>() {
         {
           add(4);
           add(2);
           add(3);
         }
       };
   Calculator.add(l);
   Calculator.sub(l);
   Calculator.add(l);
   assert (Calculator.getHistory().size() > 0);
   Calculator.clear();
   assert (Calculator.getHistory().size() == 0);
 }
Пример #14
0
 /**
  * Tests reuse() method.
  *
  * @author chuna
  */
 @SuppressWarnings("serial")
 @Test
 public void testReuse() {
   Calculator.clear();
   List<Integer> l =
       new ArrayList<Integer>() {
         {
           add(10);
           add(5);
           add(2);
         }
       };
   Calculator.add(l);
   Calculator.sub(l);
   Calculator.mult(l);
   try {
     Calculator.div(l);
   } catch (Exception e) {
     assert (false);
   }
   assert (Calculator.reuse(4, null) == 17); // add
   assert (Calculator.reuse(3, null) == 3); // sub
   assert (Calculator.reuse(2, null) == 100); // mult
   assert (Calculator.reuse(1, null) == 1); // div
   // Now for the no spot in history found
   Calculation calc = new Calculation("add");
   System.out.println("History size is " + Calculator.getHistory().size());
   assert (Calculator.reuse(5, calc) == 0); // add id = 0
   calc = new Calculation("sub");
   assert (Calculator.reuse(6, calc) == 0); // sub id = 0
   calc = new Calculation("mult");
   assert (Calculator.reuse(0, calc) == 1); // mult id = 1
   calc = new Calculation("div");
   assert (Calculator.reuse(-1, calc) == 1); // div id = 1
 }