@@ -304,6 +304,35 @@ void testComplexArithmeticWithMultipleOperations() {
304304 assertDerivationEquals (expected , result , "" );
305305 }
306306
307+ @ Test
308+ void testFixedPointSimplification () {
309+ // Given: x == -y && y == a / b && a == 6 && b == 3
310+ // Expected: x == -2
311+ Expression varX = new Var ("x" );
312+ Expression varY = new Var ("y" );
313+ Expression varA = new Var ("a" );
314+ Expression varB = new Var ("b" );
315+
316+ Expression aDivB = new BinaryExpression (varA , "/" , varB );
317+ Expression yEqualsADivB = new BinaryExpression (varY , "==" , aDivB );
318+ Expression negY = new UnaryExpression ("-" , varY );
319+ Expression xEqualsNegY = new BinaryExpression (varX , "==" , negY );
320+ Expression six = new LiteralInt (6 );
321+ Expression aEquals6 = new BinaryExpression (varA , "==" , six );
322+ Expression three = new LiteralInt (3 );
323+ Expression bEquals3 = new BinaryExpression (varB , "==" , three );
324+ Expression firstAnd = new BinaryExpression (xEqualsNegY , "&&" , yEqualsADivB );
325+ Expression secondAnd = new BinaryExpression (aEquals6 , "&&" , bEquals3 );
326+ Expression fullExpression = new BinaryExpression (firstAnd , "&&" , secondAnd );
327+
328+ // When
329+ ValDerivationNode result = ExpressionSimplifier .simplify (fullExpression );
330+
331+ // Then
332+ assertNotNull (result , "Result should not be null" );
333+ assertEquals ("x == -2" , result .getValue ().toString (), "Expected result to be x == -2" );
334+ }
335+
307336 @ Test
308337 void testSingleEqualityShouldNotSimplify () {
309338 // Given: x == 1
@@ -330,32 +359,61 @@ void testSingleEqualityShouldNotSimplify() {
330359 }
331360
332361 @ Test
333- void testFixedPointSimplification () {
334- // Given: x == -y && y == a / b && a == 6 && b == 3
335- // Expected: x == -2
362+ void testTwoEqualitiesShouldNotSimplify () {
363+ // Given: x == 1 && y == 2
364+ // Expected: x == 1 && y == 2 (should not be simplified to "true")
365+
336366 Expression varX = new Var ("x" );
367+ Expression one = new LiteralInt (1 );
368+ Expression xEquals1 = new BinaryExpression (varX , "==" , one );
369+
337370 Expression varY = new Var ("y" );
338- Expression varA = new Var ( "a" );
339- Expression varB = new Var ( "b" );
371+ Expression two = new LiteralInt ( 2 );
372+ Expression yEquals2 = new BinaryExpression ( varY , "==" , two );
340373
341- Expression aDivB = new BinaryExpression (varA , "/" , varB );
342- Expression yEqualsADivB = new BinaryExpression (varY , "==" , aDivB );
343- Expression negY = new UnaryExpression ("-" , varY );
344- Expression xEqualsNegY = new BinaryExpression (varX , "==" , negY );
345- Expression six = new LiteralInt (6 );
346- Expression aEquals6 = new BinaryExpression (varA , "==" , six );
347- Expression three = new LiteralInt (3 );
348- Expression bEquals3 = new BinaryExpression (varB , "==" , three );
349- Expression firstAnd = new BinaryExpression (xEqualsNegY , "&&" , yEqualsADivB );
350- Expression secondAnd = new BinaryExpression (aEquals6 , "&&" , bEquals3 );
351- Expression fullExpression = new BinaryExpression (firstAnd , "&&" , secondAnd );
374+ Expression fullExpression = new BinaryExpression (xEquals1 , "&&" , yEquals2 );
352375
353376 // When
354377 ValDerivationNode result = ExpressionSimplifier .simplify (fullExpression );
355378
356379 // Then
357380 assertNotNull (result , "Result should not be null" );
358- assertEquals ("x == -2" , result .getValue ().toString (), "Expected result to be x == -2" );
381+ assertEquals ("x == 1 && y == 2" , result .getValue ().toString (),
382+ "Two equalities should not be simplified to a boolean literal" );
383+
384+ // The result should be the original expression unchanged
385+ assertTrue (result .getValue () instanceof BinaryExpression , "Result should still be a binary expression" );
386+ BinaryExpression resultExpr = (BinaryExpression ) result .getValue ();
387+ assertEquals ("&&" , resultExpr .getOperator (), "Operator should still be &&" );
388+ assertEquals ("x == 1" , resultExpr .getFirstOperand ().toString (), "Left operand should be x == 1" );
389+ assertEquals ("y == 2" , resultExpr .getSecondOperand ().toString (), "Right operand should be y == 2" );
390+ }
391+
392+ @ Test
393+ void testCircularDependencyShouldNotSimplify () {
394+ // Given: x == y && y == x
395+ // Expected: x == y && y == x (should not be simplified to "true")
396+
397+ Expression varX = new Var ("x" );
398+ Expression varY = new Var ("y" );
399+ Expression xEqualsY = new BinaryExpression (varX , "==" , varY );
400+ Expression yEqualsX = new BinaryExpression (varY , "==" , varX );
401+ Expression fullExpression = new BinaryExpression (xEqualsY , "&&" , yEqualsX );
402+
403+ // When
404+ ValDerivationNode result = ExpressionSimplifier .simplify (fullExpression );
405+
406+ // Then
407+ assertNotNull (result , "Result should not be null" );
408+ assertEquals ("x == y && y == x" , result .getValue ().toString (),
409+ "Circular dependency should not be simplified to a boolean literal" );
410+
411+ // The result should be the original expression unchanged
412+ assertTrue (result .getValue () instanceof BinaryExpression , "Result should still be a binary expression" );
413+ BinaryExpression resultExpr = (BinaryExpression ) result .getValue ();
414+ assertEquals ("&&" , resultExpr .getOperator (), "Operator should still be &&" );
415+ assertEquals ("x == y" , resultExpr .getFirstOperand ().toString (), "Left operand should be x == y" );
416+ assertEquals ("y == x" , resultExpr .getSecondOperand ().toString (), "Right operand should be y == x" );
359417 }
360418
361419 /**
0 commit comments