Coverage Report - net.sourceforge.combean.test.mathprog.grooml.TestGroomlInterpreter
 
Classes in this File Line Coverage Branch Coverage Complexity
TestGroomlInterpreter
100%
94/94
52%
63/122
0
TestGroomlInterpreter$_auxCheckElemsInSet_closure34
100%
1/1
N/A
0
TestGroomlInterpreter$_auxCheckTermsInExpression_closure33
100%
1/1
N/A
0
TestGroomlInterpreter$_testAccessToNonExistingProperty_closure30
100%
2/2
50%
1/2
0
TestGroomlInterpreter$_testAccessToNonExistingProperty_closure30_closure79
100%
4/4
50%
3/6
0
TestGroomlInterpreter$_testAccessToNonExistingProperty_closure30_closure79_closure80
100%
1/1
N/A
0
TestGroomlInterpreter$_testAccessToNonExistingProperty_closure30_closure79_closure81
100%
2/2
50%
2/4
0
TestGroomlInterpreter$_testAccessToNonExistingProperty_closure30_closure79_closure81_closure82
100%
1/1
N/A
0
TestGroomlInterpreter$_testAccessToNonExistingVariableIndex_closure31
100%
2/2
50%
1/2
0
TestGroomlInterpreter$_testAccessToNonExistingVariableIndex_closure31_closure83
100%
4/4
50%
3/6
0
TestGroomlInterpreter$_testAccessToNonExistingVariableIndex_closure31_closure83_closure84
100%
1/1
N/A
0
TestGroomlInterpreter$_testAccessToNonExistingVariableIndex_closure31_closure83_closure85
100%
2/2
100%
4/4
0
TestGroomlInterpreter$_testAccessToNonExistingVariableIndex_closure31_closure83_closure85_closure86
100%
1/1
N/A
0
TestGroomlInterpreter$_testAccessToVariablesInEnvironmentWithIndexOperator_closure21
100%
15/15
50%
23/46
0
TestGroomlInterpreter$_testAccessToVariablesInEnvironmentWithIndexOperator_closure21_closure47
100%
1/1
N/A
0
TestGroomlInterpreter$_testAccessToVariablesInEnvironmentWithIndexOperator_closure21_closure48
100%
1/1
N/A
0
TestGroomlInterpreter$_testCreationOfTermsWithSingleVariablesInEnv_closure20
100%
9/9
50%
11/22
0
TestGroomlInterpreter$_testCreationOfTermsWithSingleVariablesInEnv_closure20_closure45
100%
1/1
N/A
0
TestGroomlInterpreter$_testCreationOfTermsWithSingleVariablesInEnv_closure20_closure46
100%
1/1
N/A
0
TestGroomlInterpreter$_testCreationOfVariables_closure19
100%
1/1
N/A
0
TestGroomlInterpreter$_testDoubleDefinedRow_closure29
100%
2/2
50%
1/2
0
TestGroomlInterpreter$_testDoubleDefinedRow_closure29_closure75
100%
4/4
50%
3/6
0
TestGroomlInterpreter$_testDoubleDefinedRow_closure29_closure75_closure76
100%
1/1
N/A
0
TestGroomlInterpreter$_testDoubleDefinedRow_closure29_closure75_closure77
100%
1/1
N/A
0
TestGroomlInterpreter$_testDoubleDefinedRow_closure29_closure75_closure78
100%
1/1
N/A
0
TestGroomlInterpreter$_testDoubleDefinedVariable_closure28
100%
2/2
50%
1/2
0
TestGroomlInterpreter$_testDoubleDefinedVariable_closure28_closure71
75%
3/4
33%
2/6
0
TestGroomlInterpreter$_testDoubleDefinedVariable_closure28_closure71_closure72
100%
1/1
N/A
0
TestGroomlInterpreter$_testDoubleDefinedVariable_closure28_closure71_closure73
100%
1/1
N/A
0
TestGroomlInterpreter$_testDoubleDefinedVariable_closure28_closure71_closure74
0%
0/1
N/A
0
TestGroomlInterpreter$_testExprOnClosure_closure8
100%
1/1
N/A
0
TestGroomlInterpreter$_testModelWithAccessToExternalDefinitions_closure27
100%
2/2
50%
2/4
0
TestGroomlInterpreter$_testModelWithAccessToExternalDefinitions_closure27_closure68
100%
1/1
100%
2/2
0
TestGroomlInterpreter$_testModelWithAccessToExternalDefinitions_closure27_closure69
100%
1/1
50%
2/4
0
TestGroomlInterpreter$_testModelWithAccessToExternalDefinitions_closure27_closure69_closure70
100%
1/1
75%
3/4
0
TestGroomlInterpreter$_testModelWithDoubleIndexedVariableAndMultipleRows_closure25
100%
3/3
50%
3/6
0
TestGroomlInterpreter$_testModelWithDoubleIndexedVariableAndMultipleRows_closure25_closure58
100%
1/1
N/A
0
TestGroomlInterpreter$_testModelWithDoubleIndexedVariableAndMultipleRows_closure25_closure59
100%
2/2
100%
2/2
0
TestGroomlInterpreter$_testModelWithDoubleIndexedVariableAndMultipleRows_closure25_closure59_closure60
100%
1/1
N/A
0
TestGroomlInterpreter$_testModelWithDoubleIndexedVariableAndSingleRow_closure24
100%
3/3
50%
3/6
0
TestGroomlInterpreter$_testModelWithDoubleIndexedVariableAndSingleRow_closure24_closure55
100%
1/1
N/A
0
TestGroomlInterpreter$_testModelWithDoubleIndexedVariableAndSingleRow_closure24_closure56
100%
2/2
50%
2/4
0
TestGroomlInterpreter$_testModelWithDoubleIndexedVariableAndSingleRow_closure24_closure56_closure57
100%
1/1
N/A
0
TestGroomlInterpreter$_testModelWithSingleRow_closure22
100%
3/3
50%
2/4
0
TestGroomlInterpreter$_testModelWithSingleRow_closure22_closure49
100%
1/1
N/A
0
TestGroomlInterpreter$_testModelWithSingleRow_closure22_closure50
100%
2/2
50%
2/4
0
TestGroomlInterpreter$_testModelWithSingleRow_closure22_closure50_closure51
100%
1/1
N/A
0
TestGroomlInterpreter$_testNestedSums_closure15
100%
1/1
50%
3/6
0
TestGroomlInterpreter$_testNestedSums_closure15_closure41
100%
1/1
N/A
0
TestGroomlInterpreter$_testPlusOperatorOnClosures_closure11
100%
2/2
50%
2/4
0
TestGroomlInterpreter$_testPlusOperatorOnClosures_closure11_closure39
100%
1/1
N/A
0
TestGroomlInterpreter$_testPlusOperatorOnClosures_closure11_closure40
100%
1/1
N/A
0
TestGroomlInterpreter$_testPlusOperatorOnExpression_closure9
100%
1/1
50%
1/2
0
TestGroomlInterpreter$_testPlusOperatorOnExpression_closure9_closure38
100%
1/1
N/A
0
TestGroomlInterpreter$_testPlusOperatorOnStrings_closure10
100%
2/2
50%
2/4
0
TestGroomlInterpreter$_testRowsWithOperatorBasedRowDefinition_closure26
100%
5/5
50%
5/10
0
TestGroomlInterpreter$_testRowsWithOperatorBasedRowDefinition_closure26_closure61
100%
1/1
N/A
0
TestGroomlInterpreter$_testRowsWithOperatorBasedRowDefinition_closure26_closure62
100%
2/2
100%
4/4
0
TestGroomlInterpreter$_testRowsWithOperatorBasedRowDefinition_closure26_closure62_closure65
100%
1/1
N/A
0
TestGroomlInterpreter$_testRowsWithOperatorBasedRowDefinition_closure26_closure63
100%
2/2
100%
4/4
0
TestGroomlInterpreter$_testRowsWithOperatorBasedRowDefinition_closure26_closure63_closure66
100%
1/1
N/A
0
TestGroomlInterpreter$_testRowsWithOperatorBasedRowDefinition_closure26_closure64
100%
2/2
100%
4/4
0
TestGroomlInterpreter$_testRowsWithOperatorBasedRowDefinition_closure26_closure64_closure67
100%
1/1
N/A
0
TestGroomlInterpreter$_testSetProductOperatorOnList_closure2
100%
1/1
N/A
0
TestGroomlInterpreter$_testSetProductOperatorOnMap_closure3
100%
1/1
N/A
0
TestGroomlInterpreter$_testSetProductOperatorOnMap_closure4
100%
1/1
50%
2/4
0
TestGroomlInterpreter$_testSetProductOperatorOnRange_closure1
100%
1/1
N/A
0
TestGroomlInterpreter$_testSetProductWithDynamics_closure5
100%
1/1
N/A
0
TestGroomlInterpreter$_testSetProductWithDynamics_closure5_closure35
100%
1/1
N/A
0
TestGroomlInterpreter$_testSetProductWithDynamics_closure6
100%
1/1
N/A
0
TestGroomlInterpreter$_testSetProductWithDynamics_closure6_closure36
100%
1/1
N/A
0
TestGroomlInterpreter$_testSetProductWithDynamics_closure7
100%
1/1
50%
3/6
0
TestGroomlInterpreter$_testSetProductWithDynamics_closure7_closure37
100%
1/1
N/A
0
TestGroomlInterpreter$_testSingleRowWithStringIndices_closure23
100%
4/4
50%
2/4
0
TestGroomlInterpreter$_testSingleRowWithStringIndices_closure23_closure52
100%
1/1
N/A
0
TestGroomlInterpreter$_testSingleRowWithStringIndices_closure23_closure53
100%
1/1
50%
2/4
0
TestGroomlInterpreter$_testSingleRowWithStringIndices_closure23_closure53_closure54
100%
1/1
N/A
0
TestGroomlInterpreter$_testSingleRow_closure17
100%
2/2
50%
1/2
0
TestGroomlInterpreter$_testSingleRow_closure17_closure43
100%
1/1
N/A
0
TestGroomlInterpreter$_testSingleRow_closure18
100%
2/2
100%
4/4
0
TestGroomlInterpreter$_testSingleRow_closure18_closure44
100%
1/1
N/A
0
TestGroomlInterpreter$_testSolveModelAndAccessSolution_closure32
100%
10/10
50%
14/28
0
TestGroomlInterpreter$_testSolveModelAndAccessSolution_closure32_closure87
100%
1/1
N/A
0
TestGroomlInterpreter$_testSolveModelAndAccessSolution_closure32_closure88
100%
1/1
N/A
0
TestGroomlInterpreter$_testSolveModelAndAccessSolution_closure32_closure89
100%
2/2
83%
5/6
0
TestGroomlInterpreter$_testSolveModelAndAccessSolution_closure32_closure89_closure90
100%
1/1
N/A
0
TestGroomlInterpreter$_testSumWithDoubleIndex_closure16
100%
1/1
50%
3/6
0
TestGroomlInterpreter$_testSumWithDoubleIndex_closure16_closure42
100%
1/1
N/A
0
TestGroomlInterpreter$_testSumWithStringExpression_closure12
100%
1/1
N/A
0
TestGroomlInterpreter$_testSumWithStringExpression_closure13
100%
1/1
100%
2/2
0
TestGroomlInterpreter$_testSumWithTwoIndices_closure14
100%
1/1
N/A
0
 
 1  
 /*
 2  
     This file is part of Combean.
 3  
 
 4  
     Combean is free software; you can redistribute it and/or modify
 5  
     it under the terms of the GNU General Public License as published by
 6  
     the Free Software Foundation; either version 2 of the License, or
 7  
     (at your option) any later version.
 8  
 
 9  
     Combean is distributed in the hope that it will be useful,
 10  
     but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  
     GNU General Public License for more details.
 13  
 
 14  
     You should have received a copy of the GNU General Public License
 15  
     along with Foobar; if not, write to the Free Software
 16  
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 17  
 */
 18  
 package net.sourceforge.combean.test.mathprog.grooml;
 19  
 
 20  
 
 21  
 import groovy.util.GroovyTestCase
 22  
 
 23  
 import net.sourceforge.combean.except.GModelException;
 24  
 import net.sourceforge.combean.mathprog.grooml.GroomlInterpreter;
 25  
 import net.sourceforge.combean.mathprog.grooml.GSet;
 26  
 import net.sourceforge.combean.mathprog.grooml.GExpression;
 27  
 import net.sourceforge.combean.mathprog.grooml.GDynamicExpression;
 28  
 import net.sourceforge.combean.mathprog.grooml.GRows;
 29  
 import net.sourceforge.combean.mathprog.grooml.GVariables;
 30  
 import net.sourceforge.combean.mathprog.grooml.GListSet;
 31  
 
 32  
 
 33  
 class TestGroomlInterpreter extends GroovyTestCase {
 34  
 
 35  
     private GroomlInterpreter builder;
 36  
     
 37  
     void setUp() {
 38  87
         this.builder = new GroomlInterpreter();
 39  
     }
 40  
     
 41  
     /* Handling of sets */
 42  
     
 43  
     void testSetCommandOnRange() {
 44  3
         GSet s = builder.set(1..2);
 45  3
                 auxCheckElemsInSet([1, 2], s);
 46  
     }
 47  
     
 48  
     void testSetProductOperatorOnRange() {
 49  3
         GSet s = builder.set{ (1..2) * ["a", "b"] };
 50  3
                 auxCheckElemsInSet([[1,"a"], [1,"b"], [2,"a"], [2,"b"]], s);
 51  
     }
 52  
     
 53  
     void testSetProductOperatorOnList() {
 54  3
         GSet s = builder.set{ ["a", "b"] * (1..2) };
 55  3
                 auxCheckElemsInSet([["a",1], ["a",2], ["b",1], ["b",2]], s);
 56  
     }
 57  
     
 58  
     void testSetProductOperatorOnMap() {
 59  3
         GSet s = builder.set{ [i:1..2] * [j:3..4] };
 60  3
                 auxCheckElemsInSet([[1,3], [1,4], [2,3], [2,4]], s);
 61  
                 
 62  3
         builder.exec{ s = [i:1..2] * [j:2..3] };
 63  3
                 auxCheckElemsInSet([[1,2], [1,3], [2,2], [2,3]], s);
 64  
     }
 65  
     
 66  
     void testSetProductWithDynamics() {
 67  3
         GSet s = builder.set{ bind(i:1..2) * {1..i} };
 68  3
                 auxCheckElemsInSet([[1,1], [2,1], [2,2]], s);
 69  
                 
 70  3
         s = builder.set{ [i:1..2] * {1..i} };
 71  3
                 auxCheckElemsInSet([[1,1], [2,1], [2,2]], s);
 72  
                 
 73  3
         builder.exec{ s = [i:1..2] * [j:2..3] * {i..j} };
 74  3
                 auxCheckElemsInSet([[1,2,1],[1,2,2],[1,3,1],[1,3,2],[1,3,3],
 75  
                                     [2,2,2],[2,3,2],[2,3,3]], s);
 76  
     }
 77  
     
 78  
     /* Simple expressions */
 79  
     
 80  
     void testExpr() {
 81  3
         GExpression e = builder.expr("2 x[1]");
 82  3
         assertEquals("2.0 x[1]", e.termsToString([:]));
 83  
     }
 84  
     
 85  
     void testExprOnClosure() {
 86  3
         GExpression e = builder.expr{ "2 x[$i]" };
 87  3
         assertTrue(e instanceof GDynamicExpression);
 88  3
         builder.i = 1;
 89  3
         assertEquals("2.0 x[1]", e.termsToString(this.builder));
 90  
     }
 91  
     
 92  
     /* Combined expressions */
 93  
     
 94  
     void testPlusOperatorOnExpression() {
 95  3
         GExpression e1 = builder.expr("1.0 x[1]");
 96  3
         GExpression e = builder.expr {
 97  3
             e1 + { "2.0 x[$i]" }
 98  
         }
 99  3
         builder.i = 2;
 100  3
         auxCheckTermsInExpression(["1.0 x[1]", "2.0 x[2]"], e);
 101  
     }
 102  
     
 103  
     void testPlusOperatorOnStrings() {
 104  3
         GExpression e = builder.expr {
 105  3
             "1.0 x[1]" + expr("2.0 x[2]")
 106  
         }
 107  3
         auxCheckTermsInExpression(["1.0 x[1]", "2.0 x[2]"], e);
 108  
     }
 109  
     
 110  
     void testPlusOperatorOnClosures() {
 111  3
         GExpression e = builder.expr {
 112  3
             expr { "1.0 x[$i]" } + { "2.0 x[$j]" }
 113  
         }
 114  3
         builder.i = 1;
 115  3
         builder.j = 2;
 116  3
         auxCheckTermsInExpression(["1.0 x[1]", "2.0 x[2]"], e);
 117  
     }
 118  
     
 119  
     /* Sums */
 120  
     
 121  
     void testSumWithStringExpression() {
 122  3
         GExpression expr = builder.sum(i:1..2) { "x[$i]" };
 123  
         
 124  3
         String terms = ""
 125  3
         expr.evaluate([:]).each{ terms += "$it, " };
 126  3
         assertEquals("1.0 x[1], 1.0 x[2], ", terms);
 127  
     }
 128  
     
 129  
     void testSumWithTwoIndices() {
 130  3
         GExpression expr = builder.sum(i:1..2, j:3..4) { "${j-i} x[$i,$j]" };
 131  
 
 132  3
         final expectedTerms = ["1.0 x[2,3]",
 133  
                                "2.0 x[1,3]",
 134  
                                "2.0 x[2,4]",
 135  
                                "3.0 x[1,4]"];
 136  3
         auxCheckTermsInExpression(expectedTerms, expr);
 137  
         
 138  
     }
 139  
     
 140  
     void testNestedSums() {
 141  3
         GExpression expr = null;
 142  3
         builder.exec{
 143  3
             expr = sum(i:1..2, sum(j:3..4) { "${j-i} x[$i,$j]" })
 144  
         }
 145  
         
 146  3
         final expectedTerms = ["1.0 x[2,3]",
 147  
                                "2.0 x[1,3]",
 148  
                                "2.0 x[2,4]",
 149  
                                "3.0 x[1,4]"];
 150  3
         auxCheckTermsInExpression(expectedTerms, expr);
 151  
     }
 152  
     
 153  
     void testSumWithDoubleIndex() {
 154  3
         GExpression expr = null;
 155  
         
 156  3
         builder.exec{
 157  3
             expr = sum(["i", "j"]:(1..2)*(3..4)) { "${j-i} x[$i,$j]" }
 158  
         }
 159  
         
 160  3
         final expectedTerms = ["1.0 x[2,3]",
 161  
                                "2.0 x[1,3]",
 162  
                                "2.0 x[2,4]",
 163  
                                "3.0 x[1,4]"];
 164  3
         auxCheckTermsInExpression(expectedTerms, expr);
 165  
     }
 166  
     
 167  
     /* Rows */
 168  
     
 169  
     void testSingleRow() {
 170  3
         builder.load{
 171  3
             vars("x", 10..12) { 1.0 };
 172  
         }
 173  3
         GRows r = builder.row("foo") {
 174  12
             sum(i:[10,12]) { "$i x[$i]" } | 10.0
 175  
         }
 176  3
         println r.toString();
 177  3
         assertEquals("rows 'foo' : [row { numIter 2 expr "+
 178  3
             "{ 10.0 x[10] + 12.0 x[12] } = 10.0 'foo[0]', ]", r.toString());
 179  
     }
 180  
 
 181  
    /* Variables */
 182  
 
 183  
     void testCreationOfVariables() {
 184  3
         GVariables vars = builder.vars("bar", 1..2) { [it/2, -it..it] };
 185  3
                 assertEquals("vars 'bar' : [0.5 | [-1.0; 1.0], " +
 186  3
                      "1.0 | [-2.0; 2.0]]", vars.toString())
 187  
     }
 188  
 
 189  
    void testCreationOfTermsWithSingleVariablesInEnv() {
 190  3
         builder.exec{
 191  3
                         var("x") { 1.0 }            
 192  3
                         var("y") { 1.0 }            
 193  3
             GExpression e = x
 194  3
                   auxCheckTermsInExpression(["1.0 x[]"], e)
 195  3
             e = 3 * x
 196  3
             auxCheckTermsInExpression(["3.0 x[]"], e);
 197  3
             e = -x + 2*y
 198  3
             auxCheckTermsInExpression(["-1.0 x[]", "2.0 y[]"], e);
 199  
         }
 200  
    }
 201  
    
 202  
    void testAccessToVariablesInEnvironmentWithIndexOperator() {
 203  3
         builder.exec{
 204  3
                         vars("x", 2..4) { 1.0 }            
 205  3
                         vars("y", (1..2)*(3..4)) { 1.0 }            
 206  3
             GExpression e = x[2]
 207  3
                   auxCheckTermsInExpression(["1.0 x[2]"], e)
 208  3
             e = 4 * x[3]
 209  3
             auxCheckTermsInExpression(["4.0 x[3]"], e);
 210  3
             e = -x[4]
 211  3
             auxCheckTermsInExpression(["-1.0 x[4]"], e);
 212  3
             e = y[1,3]*2
 213  3
             auxCheckTermsInExpression(["2.0 y[1,3]"], e);
 214  3
             e = x["foo"]
 215  3
             auxCheckTermsInExpression(["1.0 x[foo]"], e);
 216  3
             e = x["foo", "bar"]
 217  3
             auxCheckTermsInExpression(["1.0 x[foo,bar]"], e);
 218  
         }
 219  
    }
 220  
     
 221  
     /* Loading of models */
 222  
     
 223  
     void testModelWithSingleRow() {
 224  3
         builder.load{
 225  3
             vars("x", 1..2) { 1.0 };
 226  3
                  row("foo") {
 227  3
                 sum(i:1..2) { "$i x[$i]" } | 10
 228  
             }
 229  
         }
 230  3
             assertEquals("""
 231  
 vars  x[1.0, 1.0]
 232  
 foo:
 233  
   1.0 x[1] + 2.0 x[2] = 10.0
 234  3
 """, builder.getModel().toSummaryString());
 235  
     }
 236  
 
 237  
     void testSingleRowWithStringIndices() {
 238  3
         builder.load{
 239  3
                         def idxSet = ["bla", "fasel"];
 240  3
             vars("x", [it:idxSet]) { 1.0 };
 241  3
                  row("foo") {
 242  3
                 sum(i:idxSet) { "2.0 x[$i]" } | 10
 243  
             }
 244  
         }
 245  3
             assertEquals("""
 246  
 vars  x[1.0, 1.0]
 247  
 foo:
 248  
   2.0 x[bla] + 2.0 x[fasel] = 10.0
 249  3
 """, builder.getModel().toSummaryString());
 250  
     }
 251  
 
 252  
     void testModelWithDoubleIndexedVariableAndSingleRow() {
 253  3
         builder.load{
 254  3
             vars("x", [i:(1..2)] * [j:(3..4)] ) { i + j };
 255  3
                  row("foo") {
 256  3
                 sum(i:1..2) { "x[$i,3]" } | 1
 257  
             }
 258  
         }
 259  3
             assertEquals("""
 260  
 vars  x[4.0, 5.0, 5.0, 6.0]
 261  
 foo:
 262  
   1.0 x[1,3] + 1.0 x[2,3] = 1.0
 263  3
 """, builder.getModel().toSummaryString());
 264  
     }
 265  
     
 266  
     
 267  
     void testModelWithDoubleIndexedVariableAndMultipleRows() {
 268  3
         builder.load{
 269  3
             vars("x", (1..2)*(3..4)) { 1.0 };
 270  3
                      rows("foo", [i:1..2]) {
 271  6
                          [ sum([j:3..4]) { "x[$i,$j]" }, "= 1.0" ]
 272  
                      }
 273  
         }
 274  3
             assertEquals("""
 275  
 vars  x[1.0, 1.0, 1.0, 1.0]
 276  
 foo:
 277  
   1.0 x[1,3] + 1.0 x[1,4] = 1.0
 278  
   1.0 x[2,3] + 1.0 x[2,4] = 1.0
 279  3
 """, builder.getModel().toSummaryString());
 280  
     }
 281  
          
 282  
     void testRowsWithOperatorBasedRowDefinition() {
 283  3
         builder.load{
 284  3
             vars("x", (1..2)*(3..4)) { 1.0 };
 285  3
                      rows("foo", [i:1..2]) {
 286  6
                          sum([j:3..4]) { "x[$i,$j]" } << 1.0
 287  
                      }
 288  3
                      rows("bar", [i:1..2]) {
 289  6
                          sum([j:3..4]) { "x[$i,$j]" } >> -1
 290  
                      }
 291  3
                      rows("baz", [i:1..2]) {
 292  6
                          sum([j:3..4]) { "x[$i,$j]" } | 1.0
 293  
                      }
 294  
         }
 295  3
                 assertEquals("""
 296  
 vars  x[1.0, 1.0, 1.0, 1.0]
 297  
 foo:
 298  
   1.0 x[1,3] + 1.0 x[1,4] <= 1.0
 299  
   1.0 x[2,3] + 1.0 x[2,4] <= 1.0
 300  
 bar:
 301  
   1.0 x[1,3] + 1.0 x[1,4] >= -1.0
 302  
   1.0 x[2,3] + 1.0 x[2,4] >= -1.0
 303  
 baz:
 304  
   1.0 x[1,3] + 1.0 x[1,4] = 1.0
 305  
   1.0 x[2,3] + 1.0 x[2,4] = 1.0
 306  3
 """, builder.getModel().toSummaryString());
 307  
     }
 308  
     
 309  
     private int calculate(Number i) {
 310  12
         return i+1;
 311  
     }
 312  
     
 313  
     private static final double num = 10.0;
 314  
 
 315  
     void testModelWithAccessToExternalDefinitions() {
 316  3
         def coeff = [1: 2.0, 2: 4.0];
 317  3
         builder.load{
 318  6
             vars("x", 1..2) { coeff[it] };
 319  3
                  row("foo") {
 320  3
                 sum(i:1..2) { "${calculate(coeff[i])} x[$i]" } | num
 321  
             }
 322  
         }
 323  3
             assertEquals("""
 324  
 vars  x[2.0, 4.0]
 325  
 foo:
 326  
   3.0 x[1] + 5.0 x[2] = 10.0
 327  3
 """, builder.getModel().toSummaryString());
 328  
     }
 329  
 
 330  
     /* Errors during parsing/execution of model */
 331  
     
 332  
     void testDoubleDefinedVariable() {
 333  3
         shouldFail (GModelException) {
 334  3
             builder.load{
 335  3
                 vars("x", 1..2) { 1.0 };
 336  3
                 vars("x", 3..4) { 1.0 };
 337  0
                 row("foo") { "x[1]" | 10 }
 338  
             }
 339  
         }
 340  
     }
 341  
 
 342  
     void testDoubleDefinedRow() {
 343  3
         shouldFail (GModelException) {
 344  3
             builder.load{
 345  3
                 vars("x", 1..2) { 1.0 };
 346  3
                 row("foo") { "x[1]" | 10 }
 347  3
                 row("foo") { "x[2]" | 10 }
 348  
             }
 349  
         }
 350  
     }
 351  
     
 352  
     void testAccessToNonExistingProperty() {
 353  3
                 shouldFail (GModelException) {
 354  3
                 builder.load{
 355  3
                     vars("x", 1..2) { 1.0 };
 356  3
                            row("foo") {
 357  3
                                sum(i:1..2) { "$i x[$j]" } | 10
 358  
                      }
 359  3
                 solve()
 360  
                 }
 361  
          }
 362  
     }
 363  
     
 364  
     void testAccessToNonExistingVariableIndex() {
 365  3
                 def msg = shouldFail (GModelException) {
 366  3
                 builder.load{
 367  3
                     vars("x", 1..2) { 1.0 };
 368  3
                            row("foo") {
 369  9
                                sum(i:1..3) { "$i x[$i]" } | 10
 370  
                      }
 371  3
                 solve()
 372  
                 }
 373  
         }
 374  3
                 assertEquals("Trying to access undefined index '3' of variable 'x'", msg);
 375  
     }
 376  
     
 377  
     /**
 378  
      * Test solving models
 379  
      */
 380  
     void testSolveModelAndAccessSolution() {
 381  3
         builder.load {
 382  3
             max()
 383  3
             var("y") { 1.0 }
 384  3
             vars("x", 1..2) { [it, 0..1] }
 385  3
             row("foo") {
 386  9
                 sum([i:1..2]) { x[i] } + y << 1 
 387  
             }
 388  
             
 389  
             // run the LP-solver
 390  3
             solve()
 391  
             
 392  3
             assertEquals("{1=0.0, 2=1.0}", x().toString());
 393  3
             assertEquals(0.0, x(1));
 394  3
             assertEquals(1.0, x(2));
 395  3
             assertEquals(0.0, y());
 396  
         }
 397  
         
 398  
     }
 399  
     
 400  
     /* Helper functions */
 401  
     
 402  
     private void auxCheckTermsInExpression(List expectedTerms, GExpression expr) {
 403  45
         def terms = expr.evaluate(this.builder).collect{ it.toString() };
 404  45
         assertEquals(expectedTerms.size(), terms.size());
 405  45
         terms.sort();
 406  45
         expectedTerms.sort();
 407  45
         assertArrayEquals(expectedTerms.toArray(), terms.toArray());
 408  
     }
 409  
     
 410  
     private void auxCheckElemsInSet(List expectedElems, GSet set) {
 411  24
         def elems = set.evaluate(this.builder).collect{ it };
 412  24
         assertEquals(expectedElems.size(), elems.size());
 413  24
         elems.sort();
 414  24
         expectedElems.sort();
 415  24
         assertArrayEquals(expectedElems.toArray(), elems.toArray());
 416  
     }
 417  
 }