22
33import static org .junit .jupiter .api .Assertions .*;
44
5- import java .util .ArrayList ;
6-
75import liquidjava .rj_language .ast .BinaryExpression ;
86import liquidjava .rj_language .ast .Expression ;
9- import liquidjava .rj_language .ast .FunctionInvocation ;
107import liquidjava .rj_language .ast .LiteralBoolean ;
118import liquidjava .rj_language .ast .LiteralInt ;
129import liquidjava .rj_language .ast .UnaryExpression ;
@@ -113,7 +110,7 @@ void testSimpleComparison() {
113110
114111 // Then
115112 assertNotNull (result , "Result should not be null" );
116- assertTrue ( result .getValue () instanceof LiteralBoolean , "Result should be a boolean" );
113+ assertInstanceOf ( LiteralBoolean . class , result .getValue (), "Result should be a boolean" );
117114 assertFalse (((LiteralBoolean ) result .getValue ()).isBooleanTrue (), "Expected result to befalse" );
118115
119116 // (y || true) && y == false => false || true = true
@@ -249,8 +246,8 @@ void testComplexArithmeticWithMultipleOperations() {
249246 // Then
250247 assertNotNull (result , "Result should not be null" );
251248 assertNotNull (result .getValue (), "Result value should not be null" );
252- assertTrue ( result .getValue () instanceof LiteralBoolean , "Result should be a boolean literal" );
253- assertTrue ((( LiteralBoolean ) result .getValue () ).isBooleanTrue (), "Expected result to be true" );
249+ assertInstanceOf ( LiteralBoolean . class , result .getValue (), "Result should be a boolean literal" );
250+ assertTrue (result .getValue ().isBooleanTrue (), "Expected result to be true" );
254251
255252 // 5 * 2 + 7 - 3
256253 ValDerivationNode val5 = new ValDerivationNode (new LiteralInt (5 ), new VarDerivationNode ("a" ));
@@ -311,6 +308,7 @@ void testComplexArithmeticWithMultipleOperations() {
311308 void testFixedPointSimplification () {
312309 // Given: x == -y && y == a / b && a == 6 && b == 3
313310 // Expected: x == -2
311+
314312 Expression varX = new Var ("x" );
315313 Expression varY = new Var ("y" );
316314 Expression varA = new Var ("a" );
@@ -354,7 +352,7 @@ void testSingleEqualityShouldNotSimplify() {
354352 "Single equality should not be simplified to a boolean literal" );
355353
356354 // The result should be the original expression unchanged
357- assertTrue ( result .getValue () instanceof BinaryExpression , "Result should still be a binary expression" );
355+ assertInstanceOf ( BinaryExpression . class , result .getValue (), "Result should still be a binary expression" );
358356 BinaryExpression resultExpr = (BinaryExpression ) result .getValue ();
359357 assertEquals ("==" , resultExpr .getOperator (), "Operator should still be ==" );
360358 assertEquals ("x" , resultExpr .getFirstOperand ().toString (), "Left operand should be x" );
@@ -373,7 +371,6 @@ void testTwoEqualitiesShouldNotSimplify() {
373371 Expression varY = new Var ("y" );
374372 Expression two = new LiteralInt (2 );
375373 Expression yEquals2 = new BinaryExpression (varY , "==" , two );
376-
377374 Expression fullExpression = new BinaryExpression (xEquals1 , "&&" , yEquals2 );
378375
379376 // When
@@ -385,7 +382,7 @@ void testTwoEqualitiesShouldNotSimplify() {
385382 "Two equalities should not be simplified to a boolean literal" );
386383
387384 // The result should be the original expression unchanged
388- assertTrue ( result .getValue () instanceof BinaryExpression , "Result should still be a binary expression" );
385+ assertInstanceOf ( BinaryExpression . class , result .getValue (), "Result should still be a binary expression" );
389386 BinaryExpression resultExpr = (BinaryExpression ) result .getValue ();
390387 assertEquals ("&&" , resultExpr .getOperator (), "Operator should still be &&" );
391388 assertEquals ("x == 1" , resultExpr .getFirstOperand ().toString (), "Left operand should be x == 1" );
@@ -396,11 +393,14 @@ void testTwoEqualitiesShouldNotSimplify() {
396393 void testSameVarTwiceShouldSimplifyToSingle () {
397394 // Given: x && x
398395 // Expected: x
396+
399397 Expression varX = new Var ("x" );
400398 Expression fullExpression = new BinaryExpression (varX , "&&" , varX );
401399 // When
400+
402401 ValDerivationNode result = ExpressionSimplifier .simplify (fullExpression );
403402 // Then
403+
404404 assertNotNull (result , "Result should not be null" );
405405 assertEquals ("x" , result .getValue ().toString (),
406406 "Same variable twice should be simplified to a single variable" );
@@ -410,6 +410,7 @@ void testSameVarTwiceShouldSimplifyToSingle() {
410410 void testSameEqualityTwiceShouldSimplifyToSingle () {
411411 // Given: x == 1 && x == 1
412412 // Expected: x == 1
413+
413414 Expression varX = new Var ("x" );
414415 Expression one = new LiteralInt (1 );
415416 Expression xEquals1First = new BinaryExpression (varX , "==" , one );
@@ -429,6 +430,7 @@ void testSameEqualityTwiceShouldSimplifyToSingle() {
429430 void testSameExpressionTwiceShouldSimplifyToSingle () {
430431 // Given: a + b == 1 && a + b == 1
431432 // Expected: a + b == 1
433+
432434 Expression varA = new Var ("a" );
433435 Expression varB = new Var ("b" );
434436 Expression sum = new BinaryExpression (varA , "+" , varB );
@@ -466,7 +468,7 @@ void testCircularDependencyShouldNotSimplify() {
466468 "Circular dependency should not be simplified to a boolean literal" );
467469
468470 // The result should be the original expression unchanged
469- assertTrue ( result .getValue () instanceof BinaryExpression , "Result should still be a binary expression" );
471+ assertInstanceOf ( BinaryExpression . class , result .getValue (), "Result should still be a binary expression" );
470472 BinaryExpression resultExpr = (BinaryExpression ) result .getValue ();
471473 assertEquals ("&&" , resultExpr .getOperator (), "Operator should still be &&" );
472474 assertEquals ("x == y" , resultExpr .getFirstOperand ().toString (), "Left operand should be x == y" );
@@ -475,9 +477,9 @@ void testCircularDependencyShouldNotSimplify() {
475477
476478 @ Test
477479 void testRealExpression () {
478- // Given: #a_5 == (-#fresh_4) && #fresh_4 == #x_2 / #y_3 && #x_2 == #x_0 && #x_0 == 6 && #y_3 == #y_1 && #y_1 ==
479- // 3
480+ // Given: #a_5 == -#fresh_4 && #fresh_4 == #x_2 / #y_3 && #x_2 == #x_0 && #x_0 == 6 && #y_3 == #y_1 && #y_1 == 3
480481 // Expected: #a_5 == -2
482+
481483 Expression varA5 = new Var ("#a_5" );
482484 Expression varFresh4 = new Var ("#fresh_4" );
483485 Expression varX2 = new Var ("#x_2" );
@@ -498,8 +500,10 @@ void testRealExpression() {
498500 Expression thirdAnd = new BinaryExpression (y3EqualsY1 , "&&" , y1Equals3 );
499501 Expression firstBigAnd = new BinaryExpression (firstAnd , "&&" , secondAnd );
500502 Expression fullExpression = new BinaryExpression (firstBigAnd , "&&" , thirdAnd );
503+
501504 // When
502505 ValDerivationNode result = ExpressionSimplifier .simplify (fullExpression );
506+
503507 // Then
504508 assertNotNull (result , "Result should not be null" );
505509 assertEquals ("#a_5 == -2" , result .getValue ().toString (), "Expected result to be #a_5 == -2" );
@@ -510,18 +514,20 @@ void testRealExpression() {
510514 void testTransitive () {
511515 // Given: a == b && b == 1
512516 // Expected: a == 1
517+
513518 Expression varA = new Var ("a" );
514519 Expression varB = new Var ("b" );
515520 Expression one = new LiteralInt (1 );
516521 Expression aEqualsB = new BinaryExpression (varA , "==" , varB );
517522 Expression bEquals1 = new BinaryExpression (varB , "==" , one );
518523 Expression fullExpression = new BinaryExpression (aEqualsB , "&&" , bEquals1 );
524+
519525 // When
520526 ValDerivationNode result = ExpressionSimplifier .simplify (fullExpression );
527+
521528 // Then
522529 assertNotNull (result , "Result should not be null" );
523530 assertEquals ("a == 1" , result .getValue ().toString (), "Expected result to be a == 1" );
524-
525531 }
526532
527533 /**
@@ -531,25 +537,22 @@ private void assertDerivationEquals(DerivationNode expected, DerivationNode actu
531537 if (expected == null && actual == null )
532538 return ;
533539
540+ assertNotNull (expected );
534541 assertEquals (expected .getClass (), actual .getClass (), message + ": node types should match" );
535- if (expected instanceof ValDerivationNode ) {
536- ValDerivationNode expectedVal = (ValDerivationNode ) expected ;
542+ if (expected instanceof ValDerivationNode expectedVal ) {
537543 ValDerivationNode actualVal = (ValDerivationNode ) actual ;
538544 assertEquals (expectedVal .getValue ().toString (), actualVal .getValue ().toString (),
539545 message + ": values should match" );
540546 assertDerivationEquals (expectedVal .getOrigin (), actualVal .getOrigin (), message + " > origin" );
541- } else if (expected instanceof BinaryDerivationNode ) {
542- BinaryDerivationNode expectedBin = (BinaryDerivationNode ) expected ;
547+ } else if (expected instanceof BinaryDerivationNode expectedBin ) {
543548 BinaryDerivationNode actualBin = (BinaryDerivationNode ) actual ;
544549 assertEquals (expectedBin .getOp (), actualBin .getOp (), message + ": operators should match" );
545550 assertDerivationEquals (expectedBin .getLeft (), actualBin .getLeft (), message + " > left" );
546551 assertDerivationEquals (expectedBin .getRight (), actualBin .getRight (), message + " > right" );
547- } else if (expected instanceof VarDerivationNode ) {
548- VarDerivationNode expectedVar = (VarDerivationNode ) expected ;
552+ } else if (expected instanceof VarDerivationNode expectedVar ) {
549553 VarDerivationNode actualVar = (VarDerivationNode ) actual ;
550554 assertEquals (expectedVar .getVar (), actualVar .getVar (), message + ": variables should match" );
551- } else if (expected instanceof UnaryDerivationNode ) {
552- UnaryDerivationNode expectedUnary = (UnaryDerivationNode ) expected ;
555+ } else if (expected instanceof UnaryDerivationNode expectedUnary ) {
553556 UnaryDerivationNode actualUnary = (UnaryDerivationNode ) actual ;
554557 assertEquals (expectedUnary .getOp (), actualUnary .getOp (), message + ": operators should match" );
555558 assertDerivationEquals (expectedUnary .getOperand (), actualUnary .getOperand (), message + " > operand" );
0 commit comments