Coverage Report - net.sourceforge.combean.test.mathprog.grooml.TestGExpression
 
Classes in this File Line Coverage Branch Coverage Complexity
TestGExpression
100%
40/40
52%
43/82
0
TestGExpression$_checkTermsInExpr_closure3
100%
1/1
N/A
0
TestGExpression$_setUp_closure1
100%
1/1
N/A
0
TestGExpression$_testConstructFromString_closure2
100%
3/3
88%
7/8
0
TestGExpression$_testDynExprEvaluation_closure5
100%
1/1
N/A
0
TestGExpression$_testMultiIndexSum_closure8
100%
1/1
N/A
0
TestGExpression$_testNestedSums_closure7
100%
1/1
N/A
0
TestGExpression$_testSimpleDynExpr_closure4
100%
1/1
N/A
0
TestGExpression$_testSumWithVariableExpressionInClosure_closure6
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  
 import groovy.util.GroovyTestCase
 21  
 
 22  
 import net.sourceforge.combean.mathprog.grooml.GExpression;
 23  
 import net.sourceforge.combean.mathprog.grooml.GDynamicExpression;
 24  
 import net.sourceforge.combean.mathprog.grooml.GSimpleExpression;
 25  
 import net.sourceforge.combean.mathprog.grooml.GSumExpression;
 26  
 import net.sourceforge.combean.mathprog.grooml.GCombinedExpression;
 27  
 import net.sourceforge.combean.mathprog.grooml.GTerm;
 28  
 import net.sourceforge.combean.mathprog.grooml.GSet;
 29  
 
 30  
 class TestGExpression extends GroovyTestCase {
 31  
     
 32  
     private Object env;
 33  
     private GDynamicExpression simpleExp;
 34  
     final private String simpleExpAsString = "1.0 x[1]";  
 35  
     
 36  
     void setUp() {
 37  30
         this.env = [:];
 38  30
         this.simpleExp = new GDynamicExpression() { "x[1]" };
 39  
     }
 40  
 
 41  
     void testConstructFromString() {
 42  3
         static final stringsForConstruction =
 43  
             [
 44  
                     ["2x[1]", "2.0 x[1]"],
 45  
                 ["x[1]", "1.0 x[1]"],
 46  
                 ["foo[]", "1.0 foo[]"],
 47  
                 ["   3.5  *   bar[  2 , 1 ]", "3.5 bar[2,1]"],
 48  
             ];
 49  3
         stringsForConstruction.each{
 50  
             input, expected ->
 51  12
             GExpression expr = new GSimpleExpression(input) 
 52  12
             assertEquals(expected, expr.termsToString(env));
 53  
         }
 54  
     }
 55  
 
 56  
     private void checkTermsInExpr(GExpression expr, List termsAsStrings) {
 57  24
                    Iterator exprIt = expr.evaluate(this.env);
 58  24
                    def terms = exprIt.collect{ it.toString() };
 59  24
                    terms.sort();
 60  24
         assertArrayEquals(termsAsStrings.sort().toArray(), terms.toArray());
 61  
         }
 62  
 
 63  
     void testStringExpr() {
 64  3
         GSimpleExpression strexp = new GSimpleExpression("-2 foo [3]");
 65  3
         checkTermsInExpr(strexp, ["-2.0 foo[3]"]);
 66  
     }
 67  
 
 68  
     void testSimpleDynExpr() {
 69  3
         GDynamicExpression dynexp = new GDynamicExpression() { "3.0 foo [3]" };
 70  3
         checkTermsInExpr(dynexp, ["3.0 foo[3]"]);
 71  
     }
 72  
     
 73  
     void testDynExprEvaluation() {
 74  3
         this.env = [c : 3.0, v:"foo", i:2];
 75  
         GDynamicExpression dynexp =
 76  3
             new GDynamicExpression() { "$c $v[$i]" };
 77  3
         checkTermsInExpr(dynexp, ["3.0 foo[2]"]);
 78  
     }
 79  
 
 80  
     void testEmptySum() {
 81  3
         GSet s = GSet.convert(1..<1);
 82  3
         GSumExpression sumExp = new GSumExpression([i: s], simpleExp);
 83  
 
 84  3
         assertFalse(sumExp.evaluate(this.env).hasNext());
 85  
     }
 86  
 
 87  
     void testSumWithConstantExpression() {
 88  3
         GSet s = GSet.convert(1..2);
 89  3
         GSumExpression sumExp = new GSumExpression([i: s], simpleExp);
 90  
         
 91  3
         checkTermsInExpr(sumExp, [simpleExpAsString, simpleExpAsString]);
 92  
     }
 93  
 
 94  
     void testSumWithVariableExpressionInClosure() {
 95  3
         GDynamicExpression dynExp = new GDynamicExpression() { " ${2.5*bar} x[$bar]" };
 96  3
         GSet s = GSet.convert(1..2);
 97  3
         GSumExpression sumExp = new GSumExpression([bar: s], dynExp);
 98  
         
 99  3
         checkTermsInExpr(sumExp, ["2.5 x[1]", "5.0 x[2]"]);
 100  
     }
 101  
 
 102  
     /** TODO: change delegate of GString
 103  
     void testSumWithVariableExpressionInGString() {
 104  
         Map env = [:];
 105  
         GSimpleExpression strExp = new GSimpleExpression(" ${2.5*bar} x[$bar]", env);
 106  
         GSet s = GSet.convert(1..2);
 107  
         GSumExpression sumExp = new GSumExpression([bar: s], strExp, env);
 108  
 
 109  
         assertTrue(sumExp.hasNext());
 110  
         GTerm t = sumExp.next();
 111  
         assertToString(t, "2.5 x[1]");
 112  
 
 113  
         assertTrue(sumExp.hasNext());
 114  
         t = sumExp.next();
 115  
         assertToString(t, "5.0 x[2]");
 116  
         
 117  
         assertFalse(sumExp.hasNext());
 118  
     }
 119  
     */
 120  
 
 121  
     void testNestedSums() {
 122  3
         GDynamicExpression dynExp = new GDynamicExpression() { " ${i+j} x[$i,$j]" };
 123  3
         GSet rangeI = GSet.convert(1..2);
 124  3
         GSet rangeJ = GSet.convert(2..3);
 125  3
         GSumExpression innerSum = new GSumExpression([j: rangeJ], dynExp);
 126  3
         GSumExpression outerSum = new GSumExpression([i: rangeI], innerSum);
 127  
         
 128  3
         checkTermsInExpr(outerSum, ["3.0 x[1,2]",
 129  
                                     "4.0 x[1,3]",
 130  
                                     "4.0 x[2,2]",
 131  
                                     "5.0 x[2,3]"]);
 132  
     }
 133  
 
 134  
     void testMultiIndexSum() {
 135  3
         GDynamicExpression dynExp = new GDynamicExpression() { " ${i+j} x[$i,$j]" };
 136  3
         GSet rangeI = GSet.convert(1..2);
 137  3
         GSet rangeJ = GSet.convert(2..3);
 138  
         GSumExpression outerSum =
 139  3
             new GSumExpression([j: rangeJ, i:rangeI], dynExp);
 140  
 
 141  3
         checkTermsInExpr(outerSum, ["3.0 x[1,2]",
 142  
                                     "4.0 x[1,3]",
 143  
                                     "4.0 x[2,2]",
 144  
                                     "5.0 x[2,3]"]);
 145  
     }
 146  
     
 147  
     void testCombinedExpressions() {
 148  3
         GSimpleExpression strExp1 = new GSimpleExpression("1.0 x[1]");
 149  3
         GSimpleExpression strExp2 = new GSimpleExpression("2.0 x[2]");
 150  
         GCombinedExpression combExp =
 151  3
             new GCombinedExpression([strExp1, strExp2]);
 152  
         
 153  3
         checkTermsInExpr(combExp, ["1.0 x[1]", "2.0 x[2]"]);
 154  
     }
 155  
 }