Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. import static org.junit.Assert.assertEquals;
  2. import org.junit.Before;
  3. import org.junit.Rule;
  4. import org.junit.Test;
  5. import org.junit.rules.ExpectedException;
  6. import java.lang.Object;
  7. import java.util.Stack;
  8.  
  9. public class TestJunitRevPolishCalc {
  10.  
  11.   String[] splitArray;
  12.   int first, number;
  13.   LeafNode nodeLeaf, nodeLeaf0, nodeLeaf1;
  14.   Stack<LeafNode> myStack;
  15.   OperatorNode opNode;
  16.   SumVisitor sumVisitor;
  17.   // for tests
  18.   boolean bo;
  19.   String str;
  20.  
  21.   @Before
  22.   public void setUp() {
  23.     str = new String("3 4 +");
  24.     splitArray = new String[3];
  25.     myStack = new Stack<LeafNode>();
  26.   }
  27.  
  28.   public boolean isNumeric(String str) {
  29.     try {
  30.       double d = Double.parseDouble(str);
  31.     } catch (NumberFormatException nfe) {
  32.       return false;
  33.     }
  34.     return true;
  35.   }
  36.  
  37.   public boolean isSymbol(String str) {
  38.     String[] symbolArray = {"+", "-", "/", "*"};
  39.     for (int j = 0; j < symbolArray.length; j++) {
  40.       if (str == symbolArray[j]) {
  41.         return true;
  42.       }
  43.     }
  44.     return false;
  45.   }
  46.  
  47.   @Test
  48.  
  49.   public void testArray() {
  50.     splitArray = str.split(" ");
  51.     first = Integer.parseInt(splitArray[0]);
  52.     assertEquals(first, 3, 0f);
  53.   }
  54.  
  55.   @Test
  56.  
  57.   public void testIsNumeric() {
  58.     splitArray = str.split(" ");
  59.     assertEquals(isNumeric(splitArray[0]), true);
  60.   }
  61.  
  62.   @Test
  63.  
  64.   public void testIsSymbol() {
  65.     splitArray = str.split(" ");
  66.     for (int i = 0; i < splitArray.length; i++) {
  67.       bo = isSymbol(splitArray[i]);
  68.     }
  69.     assertEquals(bo, true);
  70.   }
  71.  
  72.   @Test
  73.  
  74.   public void testRevPolishCalc0() throws BadTypeException, EmptyStackException {
  75.     int i = 0;
  76.     splitArray = str.split(" ");
  77.     while (i < splitArray.length) {
  78.       if (isNumeric(splitArray[i]) == true) {
  79.         number = Integer.parseInt(splitArray[i]);
  80.         nodeLeaf = new LeafNode(number);
  81.         myStack.push(nodeLeaf);
  82.       }
  83.       if (isSymbol(splitArray[i]) == true) {
  84.         nodeLeaf0 = myStack.pop();
  85.         nodeLeaf1 = myStack.pop();
  86.         switch (splitArray[i]) {
  87.           case "+":
  88.             Operator pl = Plus.getInstance();
  89.             opNode = new OperatorNode(pl, nodeLeaf0, nodeLeaf1);
  90.             break;
  91.           case "-":
  92.             Operator mi = Minus.getInstance();
  93.             opNode = new OperatorNode(mi, nodeLeaf0, nodeLeaf1);
  94.             break;
  95.           case "*":
  96.             Operator ti = Times.getInstance();
  97.             opNode = new OperatorNode(ti, nodeLeaf0, nodeLeaf1);
  98.             break;
  99.           default:
  100.             break;
  101.         }
  102.         //in the class return element on top of the stack
  103.         sumVisitor = new SumVisitor();
  104.         sumVisitor.visit(opNode);
  105.       }
  106.     }
  107.     nodeLeaf = new LeafNode(sumVisitor.getAnswer());
  108.     myStack.push(nodeLeaf);
  109.     assertEquals(myStack.pop().getValue(),7,0f);
  110.   }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement