}
@Test()
public void testSortingOperatorTwoRowsOnSecond() {
CellAddressFactory caf;
Relation inputRelation;
SortingAscendingOperator sortAscOper;
SortingDescendingOperator sortDescOper;
Formulas expectedResult;
Formulas resultFormulas;
Relation resRelation;
caf = new CellAddressFactory(1);
inputRelation = caf.getSpaceForNewRelation(2);//(a,b)
sortAscOper = new SortingAscendingOperator(caf, inputRelation, 2);
resultFormulas = sortAscOper.getFormulas();
expectedResult = new Formulas();
expectedResult.put(new Cell('C',1), "COUNTA($B:$B)-COUNTIF($B:$B,NA())");
expectedResult.put(new Cell('D',1), "IF(ISNA($B1),$C$1+1,IF(ISERR($B1),1+$C$1-COUNTIF($B:$B,$B1),COUNTIF($B:$B,\"<\"&$B1)+COUNTIF($B$1:$B1,$B1)))");
expectedResult.put(new Cell('E',1), "MATCH(ROW(),$D:$D,0)");
expectedResult.put(new Cell('F',1), "INDEX(A:A,$E1)");
expectedResult.put(new Cell('G',1), "INDEX(B:B,$E1)");
expectedResult.put(new Cell('D',2), "IF(ISNA($B2),$C$1+1,IF(ISERR($B2),1+$C$1-COUNTIF($B:$B,$B2),COUNTIF($B:$B,\"<\"&$B2)+COUNTIF($B$1:$B2,$B2)))");
expectedResult.put(new Cell('E',2), "MATCH(ROW(),$D:$D,0)");
expectedResult.put(new Cell('F',2), "INDEX(A:A,$E2)");
expectedResult.put(new Cell('G',2), "INDEX(B:B,$E2)");
// System.out.println("expected:");
// System.out.println(expectedResult.toString(caf));
// System.out.println("real");
// System.out.println(resultFormulas.toString(caf));
assertEquals(expectedResult, resultFormulas);
resRelation = sortAscOper.getResultRelation();
assertEquals(new Column('F'), resRelation.getFirstCol());
assertEquals(2, resRelation.getCardinality());
caf = new CellAddressFactory(1);
inputRelation = caf.getSpaceForNewRelation(2);//(a,b)
sortDescOper = new SortingDescendingOperator(caf, inputRelation, 2);
resultFormulas = sortDescOper.getFormulas();
expectedResult = new Formulas();
expectedResult.put(new Cell('C',1), "COUNTA($B:$B)-COUNTIF($B:$B,NA())");
expectedResult.put(new Cell('D',1), "IF(ISNA($B1),$C$1+1,IF(ISERR($B1),1+$C$1-COUNTIF($B:$B,$B1),COUNTIF($B:$B,\">\"&$B1)+COUNTIF($B$1:$B1,$B1)))");
expectedResult.put(new Cell('E',1), "MATCH(ROW(),$D:$D,0)");
expectedResult.put(new Cell('F',1), "INDEX(A:A,$E1)");
expectedResult.put(new Cell('G',1), "INDEX(B:B,$E1)");
expectedResult.put(new Cell('D',2), "IF(ISNA($B2),$C$1+1,IF(ISERR($B2),1+$C$1-COUNTIF($B:$B,$B2),COUNTIF($B:$B,\">\"&$B2)+COUNTIF($B$1:$B2,$B2)))");
expectedResult.put(new Cell('E',2), "MATCH(ROW(),$D:$D,0)");
expectedResult.put(new Cell('F',2), "INDEX(A:A,$E2)");
expectedResult.put(new Cell('G',2), "INDEX(B:B,$E2)");
// System.out.println("expected:");
// System.out.println(expectedResult.toString(caf));
// System.out.println("real");
// System.out.println(resultFormulas.toString(caf));
assertEquals(expectedResult, resultFormulas);
resRelation = sortDescOper.getResultRelation();
assertEquals(new Column('F'), resRelation.getFirstCol());
assertEquals(2, resRelation.getCardinality());
}