@Test public void givenThreeDifferentExpressionWhenComparingThenResultingTermIsCorrectlyFormed() throws SetlException { FragmentList<AOperator> operatorsA = new FragmentList<>(); addNumber(operatorsA, 10); addNumber(operatorsA, 20); operatorsA.add(Sum.S); OperatorExpression expressionA = new OperatorExpression(operatorsA); FragmentList<AOperator> operatorsB = new FragmentList<>(); addNumber(operatorsB, 10); addNumber(operatorsB, 5); operatorsB.add(Difference.D); addNumber(operatorsB, 3); operatorsB.add(Sum.S); OperatorExpression expressionB = new OperatorExpression(operatorsB); FragmentList<AOperator> operatorsC = new FragmentList<>(); addNumber(operatorsC, 10); addNumber(operatorsC, 2); operatorsC.add(Sum.S); addNumber(operatorsC, 20); operatorsC.add(Product.P); OperatorExpression expressionC = new OperatorExpression(operatorsC); int compareAA = expressionA.compareTo(expressionA); int compareAB = expressionA.compareTo(expressionB); int compareAC = expressionA.compareTo(expressionC); int compareBA = expressionB.compareTo(expressionA); int compareBB = expressionB.compareTo(expressionB); int compareBC = expressionB.compareTo(expressionC); int compareCA = expressionC.compareTo(expressionA); int compareCB = expressionC.compareTo(expressionB); int compareCC = expressionC.compareTo(expressionC); assertEquals(0, compareAA); assertEquals(0, compareBB); assertEquals(0, compareCC); assertNotEquals(expressionA, expressionB); assertNotEquals(expressionA, expressionC); assertNotEquals(expressionB, expressionC); assertNotEquals(expressionB, expressionA); assertNotEquals(expressionC, expressionA); assertNotEquals(expressionC, expressionB); assertNotEquals(expressionA.hashCode(), expressionB.hashCode()); assertNotEquals(expressionA.hashCode(), expressionC.hashCode()); assertNotEquals(expressionB.hashCode(), expressionC.hashCode()); assertTrue(compareAB != 0); assertEquals(compareAB, -1 * compareBA); assertTrue(compareAC != 0); assertEquals(compareAC, -1 * compareCA); assertTrue(compareBC != 0); assertEquals(compareBC, -1 * compareCB); int implicationHoldingTrue = 0; if (compareAB < 0 && compareBC < 0) { assertTrue(compareAC < 0); implicationHoldingTrue++; } if (compareAB > 0 && compareBC > 0) { assertTrue(compareAC > 0); implicationHoldingTrue++; } if (compareAC < 0 && compareCB < 0) { assertTrue(compareAB < 0); implicationHoldingTrue++; } if (compareAC > 0 && compareCB > 0) { assertTrue(compareAB > 0); implicationHoldingTrue++; } if (compareBA < 0 && compareAC < 0) { assertTrue(compareBC < 0); implicationHoldingTrue++; } if (compareBA > 0 && compareAC > 0) { assertTrue(compareBC > 0); implicationHoldingTrue++; } assertEquals(1, implicationHoldingTrue); }
@Override public int compareTo(CodeFragment other) { if (this == other) { return 0; } else if (other.getClass() == OperatorExpression.class) { final OperatorExpression otr = (OperatorExpression) other; return operators.compareTo(otr.operators); } else { return (this.compareToOrdering() < other.compareToOrdering())? -1 : 1; } }
OperatorExpression extends Expression { @Override public int compareTo(CodeFragment other) { if (this == other) { return 0; } else if (other.getClass() == OperatorExpression.class) { final OperatorExpression otr = (OperatorExpression) other; return operators.compareTo(otr.operators); } else { return (this.compareToOrdering() < other.compareToOrdering())? -1 : 1; } } }
OperatorExpression extends Expression { @Override public int compareTo(CodeFragment other) { if (this == other) { return 0; } else if (other.getClass() == OperatorExpression.class) { final OperatorExpression otr = (OperatorExpression) other; return operators.compareTo(otr.operators); } else { return (this.compareToOrdering() < other.compareToOrdering())? -1 : 1; } } OperatorExpression(AOperator operator);  OperatorExpression(FragmentList<AOperator> operators);  OperatorExpression(OperatorExpression first, AOperator operator);  OperatorExpression(OperatorExpression first, OperatorExpression second);  OperatorExpression(OperatorExpression first, OperatorExpression second, AOperator operator); }
OperatorExpression extends Expression { @Override public int compareTo(CodeFragment other) { if (this == other) { return 0; } else if (other.getClass() == OperatorExpression.class) { final OperatorExpression otr = (OperatorExpression) other; return operators.compareTo(otr.operators); } else { return (this.compareToOrdering() < other.compareToOrdering())? -1 : 1; } } OperatorExpression(AOperator operator);  OperatorExpression(FragmentList<AOperator> operators);  OperatorExpression(OperatorExpression first, AOperator operator);  OperatorExpression(OperatorExpression first, OperatorExpression second);  OperatorExpression(OperatorExpression first, OperatorExpression second, AOperator operator); @Override boolean collectVariablesAndOptimize(State state, List<String> boundVariables, List<String> unboundVariables, List<String> usedVariables); boolean isConstant(); @Override Value evaluate(final State state); AAssignableExpression convertToAssignable(); @Override void appendString(State state, StringBuilder sb, int tabs); ArrayDeque<ExpressionFragment> computeExpressionFragmentStack(State state, int maxOperatorDepth); int precedence(); @Override Value toTerm(State state); static OperatorExpression createFromTerm(State state, Value value); static FragmentList<AOperator> appendFromTerm(State state, Value value, FragmentList<AOperator> operatorStack); @Override int compareTo(CodeFragment other); @Override long compareToOrdering(); @Override final boolean equals(final Object obj); @Override int computeHashCode(); }
OperatorExpression extends Expression { @Override public int compareTo(CodeFragment other) { if (this == other) { return 0; } else if (other.getClass() == OperatorExpression.class) { final OperatorExpression otr = (OperatorExpression) other; return operators.compareTo(otr.operators); } else { return (this.compareToOrdering() < other.compareToOrdering())? -1 : 1; } } OperatorExpression(AOperator operator);  OperatorExpression(FragmentList<AOperator> operators);  OperatorExpression(OperatorExpression first, AOperator operator);  OperatorExpression(OperatorExpression first, OperatorExpression second);  OperatorExpression(OperatorExpression first, OperatorExpression second, AOperator operator); @Override boolean collectVariablesAndOptimize(State state, List<String> boundVariables, List<String> unboundVariables, List<String> usedVariables); boolean isConstant(); @Override Value evaluate(final State state); AAssignableExpression convertToAssignable(); @Override void appendString(State state, StringBuilder sb, int tabs); ArrayDeque<ExpressionFragment> computeExpressionFragmentStack(State state, int maxOperatorDepth); int precedence(); @Override Value toTerm(State state); static OperatorExpression createFromTerm(State state, Value value); static FragmentList<AOperator> appendFromTerm(State state, Value value, FragmentList<AOperator> operatorStack); @Override int compareTo(CodeFragment other); @Override long compareToOrdering(); @Override final boolean equals(final Object obj); @Override int computeHashCode();  }