| 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 net.sourceforge.combean.except.GModelException; |
| 21 | |
|
| 22 | |
import net.sourceforge.combean.mathprog.grooml.GIndexBinding; |
| 23 | |
import net.sourceforge.combean.mathprog.grooml.GSet; |
| 24 | |
import net.sourceforge.combean.mathprog.grooml.GProductSet; |
| 25 | |
|
| 26 | |
class TestGIndexBinding extends GroovyTestCase { |
| 27 | |
|
| 28 | |
private static def outerValues = 2..4; |
| 29 | |
private static def innerValues = ["aaa", "bbb"]; |
| 30 | |
|
| 31 | |
private env = null; |
| 32 | |
|
| 33 | |
void setUp() { |
| 34 | 27 | this.env = [:]; |
| 35 | |
} |
| 36 | |
|
| 37 | |
void testNumericRangeSingleBinding() { |
| 38 | 3 | GSet values = GSet.convert(2..4); |
| 39 | 3 | GIndexBinding binding = new GIndexBinding("foo", values); |
| 40 | 3 | auxTestSingleBinding(2..4, binding); |
| 41 | |
} |
| 42 | |
|
| 43 | |
void testStringListSingleBinding() { |
| 44 | 3 | GSet values = GSet.convert(["aaa", "bbb", "ccc"]); |
| 45 | 3 | GIndexBinding binding = new GIndexBinding("foo", values); |
| 46 | 3 | auxTestSingleBinding(["aaa", "bbb", "ccc"], binding); |
| 47 | |
} |
| 48 | |
|
| 49 | |
void testConstructIndexBindingForProductSet() { |
| 50 | 3 | GSet s = new GProductSet([outerValues, innerValues]); |
| 51 | 3 | GIndexBinding binding = new GIndexBinding(["outer", "inner"], s); |
| 52 | |
|
| 53 | 3 | auxTestDoubleBinding(binding); |
| 54 | |
} |
| 55 | |
|
| 56 | |
void testConstructIndexBindingForListOfSets() { |
| 57 | 3 | GIndexBinding binding = new GIndexBinding(["outer", "inner"], |
| 58 | |
[outerValues, innerValues]); |
| 59 | |
|
| 60 | 3 | auxTestDoubleBinding(binding); |
| 61 | |
} |
| 62 | |
|
| 63 | |
void testIndexBindingWithWrongCardinality() { |
| 64 | 3 | shouldFail (GModelException) { |
| 65 | 3 | GIndexBinding foo = new GIndexBinding(["x"], [1..2, 3..4]); |
| 66 | |
} |
| 67 | |
} |
| 68 | |
|
| 69 | |
void testCreateSimpleBinding() { |
| 70 | 3 | GIndexBinding binding = GIndexBinding.convert([foo:2..4]); |
| 71 | |
|
| 72 | 3 | auxTestSingleBinding(2..4, binding); |
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | |
void testCreateDoubleBinding() { |
| 77 | |
GIndexBinding binding = |
| 78 | 3 | GIndexBinding.convert([outer:outerValues, inner:innerValues]); |
| 79 | |
|
| 80 | 3 | auxTestDoubleBinding(binding); |
| 81 | |
} |
| 82 | |
|
| 83 | |
void testCreateBindingToProductSet() { |
| 84 | 3 | GSet s = new GProductSet([outerValues, innerValues]); |
| 85 | |
GIndexBinding binding = |
| 86 | 3 | GIndexBinding.convert(["outer", "inner"]:s); |
| 87 | |
|
| 88 | 3 | auxTestDoubleBinding(binding); |
| 89 | |
} |
| 90 | |
|
| 91 | |
void testProductOfBindings() { |
| 92 | 3 | GIndexBinding firstBind = GIndexBinding.convert([outer:outerValues]); |
| 93 | 3 | GIndexBinding doubleBind = firstBind * [inner:innerValues]; |
| 94 | |
|
| 95 | 3 | auxTestDoubleBinding(doubleBind); |
| 96 | |
} |
| 97 | |
|
| 98 | |
private void auxTestSingleBinding(setDef, GIndexBinding binding) { |
| 99 | 9 | assertNotNull(binding); |
| 100 | 9 | assertEquals(setDef, binding.evaluate(this.env).collect{it}); |
| 101 | |
} |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
private void auxTestDoubleBinding(GIndexBinding binding) { |
| 108 | 15 | (1..2).each { |
| 109 | 30 | Iterator bindIter = binding.evaluate(this.env); |
| 110 | 30 | outerValues.each{ outer-> |
| 111 | 90 | innerValues.each{ inner -> |
| 112 | 180 | assertTrue(bindIter.hasNext()); |
| 113 | 180 | bindIter.next(); |
| 114 | 180 | assertNotNull(this.env.outer); |
| 115 | 180 | assertEquals(outer, this.env.outer); |
| 116 | 180 | assertNotNull(this.env.inner); |
| 117 | 180 | assertEquals(inner, this.env.inner); |
| 118 | |
} |
| 119 | |
} |
| 120 | 30 | assertFalse(bindIter.hasNext()); |
| 121 | |
} |
| 122 | |
} |
| 123 | |
|
| 124 | |
} |