Advertisement
JeffGrigg

SimplifyExpressionTest

Mar 4th, 2018
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.08 KB | None | 0 0
  1. import junit.framework.TestCase;
  2. import java.util.Stack;
  3.  
  4. public class SimplifyExpressionTest extends TestCase {
  5.  
  6.     private static String simplifyExpression(final String inputExpression) {
  7.         final StringBuilder simplifiedExpression = new StringBuilder();
  8.  
  9.         // Implementation logic here:
  10.         final Stack<Boolean> signStack = new Stack<>();
  11.         boolean positiveSign = true;
  12.         signStack.push(positiveSign);
  13.  
  14.         for (final char chr : inputExpression.toCharArray()) {
  15.  
  16.             // >>>---> Super Secret Implementation Logic Here. <---<<< //
  17.  
  18.         }
  19.  
  20.         return simplifiedExpression.toString();
  21.     }
  22.  
  23.     public void test1() {
  24.         assertSimpifiedExpressionEquals("-a-b-c", "-(a+b+c)");
  25.     }
  26.  
  27.     public void test2() {
  28.         assertSimpifiedExpressionEquals("a-b-c+d", "a-b-(c-d)");
  29.     }
  30.  
  31.     public void test3() {
  32.         assertSimpifiedExpressionEquals("a-b+c+d-e", "a-(b-(c+d)+e)");
  33.     }
  34.  
  35.     public void test4() {
  36.         assertSimpifiedExpressionEquals("a-b+c+d-e+f-g", "a-(b-(c+d)+(e-f)+g)");
  37.     }
  38.  
  39.     public void tests() {
  40.         assertSimpifiedExpressionEquals("a", "a");
  41.         assertSimpifiedExpressionEquals("a", "+a");
  42.         assertSimpifiedExpressionEquals("-a", "-a");
  43.         assertSimpifiedExpressionEquals("a+b", "a+b");
  44.         assertSimpifiedExpressionEquals("a-b", "a-b");
  45.  
  46.         assertSimpifiedExpressionEquals("-a-b", "-(a+b)");
  47.         assertSimpifiedExpressionEquals("-a+b", "-(a-b)");
  48.  
  49.         assertSimpifiedExpressionEquals("-a", "-(a)");
  50.         assertSimpifiedExpressionEquals("a", "-(-(a))");
  51.         assertSimpifiedExpressionEquals("-a", "-(-(-(a)))");
  52.  
  53.         assertSimpifiedExpressionEquals("-a+b-c+d-e", "-(a-(b-(c-(d-e))))");
  54.         assertSimpifiedExpressionEquals("-a+b-c+d", "-(a-(b)+(c-d))");
  55.     }
  56.  
  57.     private static void assertSimpifiedExpressionEquals(final String expectedSimpifiedExpression, final String inputExpression) {
  58.         assertEquals("For expression <" + inputExpression + ">;", expectedSimpifiedExpression, simplifyExpression(inputExpression));
  59.     }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement