Esempio n. 1
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
 }
Esempio n. 2
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));
 }
Esempio n. 3
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);
 }
Esempio n. 4
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);
 }