| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 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 | |
} |