@@ -1093,7 +1093,7 @@ void testEquivalentBoundsKeepOneSide() {
10931093 @ Test
10941094 void testSubstitutesVariableDefinedByArithmeticExpression () {
10951095 // Given: z == y - 2 && y == x + 1
1096- // Expected: z == x + 1 - 2
1096+ // Expected: z == x - 1
10971097
10981098 Expression z = new Var ("z" );
10991099 Expression y = new Var ("y" );
@@ -1108,6 +1108,29 @@ void testSubstitutesVariableDefinedByArithmeticExpression() {
11081108
11091109 // Then
11101110 assertNotNull (result , "Result should not be null" );
1111- assertEquals ("z == x + 1 - 2" , result .getValue ().toString (), "Expected variable definition to be substituted" );
1111+ assertEquals ("z == x - 1" , result .getValue ().toString (), "Expected variable definition to be substituted" );
1112+ }
1113+
1114+ @ Test
1115+ void testFoldsAdjacentIntegerConstantsInLeftAssociatedArithmetic () {
1116+ // Given: x + 1 - 2, x - 1 + 2, x + 1 + 2, and x + 1 - 1
1117+ // Expected: x - 1, x + 1, x + 3, and x
1118+
1119+ Expression x = new Var ("x" );
1120+
1121+ Expression xPlus1Minus2 = new BinaryExpression (new BinaryExpression (x , "+" , new LiteralInt (1 )), "-" ,
1122+ new LiteralInt (2 ));
1123+ Expression xMinus1Plus2 = new BinaryExpression (new BinaryExpression (x , "-" , new LiteralInt (1 )), "+" ,
1124+ new LiteralInt (2 ));
1125+ Expression xPlus1Plus2 = new BinaryExpression (new BinaryExpression (x , "+" , new LiteralInt (1 )), "+" ,
1126+ new LiteralInt (2 ));
1127+ Expression xPlus1Minus1 = new BinaryExpression (new BinaryExpression (x , "+" , new LiteralInt (1 )), "-" ,
1128+ new LiteralInt (1 ));
1129+
1130+ // When / Then
1131+ assertEquals ("x - 1" , ExpressionSimplifier .simplify (xPlus1Minus2 ).getValue ().toString ());
1132+ assertEquals ("x + 1" , ExpressionSimplifier .simplify (xMinus1Plus2 ).getValue ().toString ());
1133+ assertEquals ("x + 3" , ExpressionSimplifier .simplify (xPlus1Plus2 ).getValue ().toString ());
1134+ assertEquals ("x" , ExpressionSimplifier .simplify (xPlus1Minus1 ).getValue ().toString ());
11121135 }
11131136}
0 commit comments