| 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 | |
|
| 21 | |
import groovy.util.GroovyTestCase |
| 22 | |
|
| 23 | |
import net.sourceforge.combean.mathprog.grooml.GSet; |
| 24 | |
import net.sourceforge.combean.mathprog.grooml.GNumberedSet; |
| 25 | |
import net.sourceforge.combean.mathprog.grooml.GModelArithmeticCategory; |
| 26 | |
import net.sourceforge.combean.mathprog.grooml.GListSet; |
| 27 | |
import net.sourceforge.combean.mathprog.grooml.GProductSet; |
| 28 | |
import net.sourceforge.combean.mathprog.grooml.GDynamicSet; |
| 29 | |
import net.sourceforge.combean.mathprog.grooml.GNestedSet; |
| 30 | |
import net.sourceforge.combean.mathprog.grooml.GIndexBinding; |
| 31 | |
import net.sourceforge.combean.except.GModelException; |
| 32 | |
|
| 33 | |
|
| 34 | |
class TestGSet extends GroovyTestCase { |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
public void testIntegerRangeSet() { |
| 41 | 3 | Range r = 1..10; |
| 42 | 3 | GSet s = new GListSet(r); |
| 43 | 3 | assertTrue(s.isNumbered()); |
| 44 | 3 | auxTestList(r, s); |
| 45 | |
} |
| 46 | |
|
| 47 | |
public void testEmptyRangeSet() { |
| 48 | 3 | GListSet s = new GListSet(1..<1); |
| 49 | 3 | assertTrue(s.isNumbered()); |
| 50 | 3 | assertEquals(0, s.size()); |
| 51 | 3 | assertFalse(s.evaluate([:]).hasNext()); |
| 52 | |
} |
| 53 | |
|
| 54 | |
public void testGetElemInRangeSet() { |
| 55 | 3 | GListSet s = new GListSet(10..15); |
| 56 | 3 | for (it in 0..5) { |
| 57 | 18 | assertEquals(10 + it, s.getElem(it)); |
| 58 | |
} |
| 59 | |
} |
| 60 | |
|
| 61 | |
public void testStringListSet() { |
| 62 | 3 | List l = ["foo", "bar", "bla"]; |
| 63 | 3 | GListSet s = new GListSet(l); |
| 64 | 3 | assertTrue(s.isNumbered()); |
| 65 | 3 | auxTestList(l, s); |
| 66 | |
} |
| 67 | |
|
| 68 | |
public void testEmptyListSet() { |
| 69 | 3 | GListSet s = new GListSet([]); |
| 70 | 3 | assertTrue(s.isNumbered()); |
| 71 | 3 | assertEquals(0, s.size()); |
| 72 | 3 | assertFalse(s.evaluate([:]).hasNext()); |
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public void testProductSet() { |
| 80 | 3 | GProductSet s = new GProductSet([1..2, 3..4]); |
| 81 | 3 | assertTrue(s.isNumbered()); |
| 82 | 3 | assertEquals(4, s.size()); |
| 83 | 3 | assertEquals(2, s.indexOf([2,3])); |
| 84 | 3 | assertToString(s.getElem(1), "[1, 4]"); |
| 85 | 3 | auxTestList([[1,3], [1,4], [2,3], [2,4]], s); |
| 86 | |
} |
| 87 | |
|
| 88 | |
public void testProductOfSingleElements() { |
| 89 | 3 | GProductSet s = new GProductSet([1..1, 2..2]); |
| 90 | 3 | assertEquals(1, s.size()); |
| 91 | 3 | assertEquals(0, s.indexOf([1,2])); |
| 92 | 3 | assertToString(s.getElem(0), "[1, 2]"); |
| 93 | 3 | auxTestList([[1,2]], s); |
| 94 | |
} |
| 95 | |
|
| 96 | |
public void testProductWithSingleElement() { |
| 97 | 3 | GProductSet s = new GProductSet([1..2, 3..3]); |
| 98 | 3 | assertEquals(2, s.size()); |
| 99 | 3 | assertEquals(1, s.indexOf([2,3])); |
| 100 | 3 | assertToString(s.getElem(0), "[1, 3]"); |
| 101 | 3 | auxTestList([[1,3],[2,3]], s); |
| 102 | |
|
| 103 | |
} |
| 104 | |
|
| 105 | |
public void testEmptyProductSet() { |
| 106 | 3 | shouldFail(GModelException) { |
| 107 | 3 | GProductSet s = new GProductSet([1..2, [], 3..4]); |
| 108 | |
} |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public void testDynamicSet() { |
| 116 | 3 | GSet s = new GDynamicSet() { 1..i }; |
| 117 | 3 | assertFalse(s.isNumbered()); |
| 118 | 3 | auxTestList([1, 2, 3], s, [i:3]); |
| 119 | |
} |
| 120 | |
|
| 121 | |
public void testNestedDynamicSet() { |
| 122 | 3 | GSet outer = new GIndexBinding(i:1..2); |
| 123 | 3 | GSet inner = new GDynamicSet() { 1..i }; |
| 124 | 3 | GSet s = new GNestedSet(outer, inner); |
| 125 | 3 | assertFalse(s.isNumbered()); |
| 126 | 3 | auxTestList([[1,1], [2,1], [2,2]], s); |
| 127 | |
} |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
public void testConvertToListSet() { |
| 134 | 3 | GSet s = GSet.convert(1..2); |
| 135 | 3 | assertTrue(s instanceof GListSet); |
| 136 | 3 | auxTestList(1..2, s); |
| 137 | |
|
| 138 | 3 | s = GSet.convert(["a", "b"]); |
| 139 | 3 | assertTrue(s instanceof GListSet); |
| 140 | 3 | auxTestList(["a", "b"], s); |
| 141 | |
} |
| 142 | |
|
| 143 | |
public void testConvertToListSetWithSingleElement() { |
| 144 | 3 | GSet s = GSet.convert(1); |
| 145 | 3 | assertTrue(s instanceof GListSet); |
| 146 | 3 | auxTestList([1], s); |
| 147 | |
|
| 148 | 3 | s = GSet.convert("a"); |
| 149 | 3 | assertTrue(s instanceof GListSet); |
| 150 | 3 | auxTestList(["a"], s); |
| 151 | |
} |
| 152 | |
|
| 153 | |
public void testConvertMapToBinding() { |
| 154 | 3 | GSet s = GSet.convert(i:1..2); |
| 155 | 3 | assertTrue(s instanceof GIndexBinding); |
| 156 | 3 | auxTestList([1,2], s); |
| 157 | |
|
| 158 | 3 | s = GSet.convert(i:1..2, j:3..4); |
| 159 | 3 | assertTrue(s instanceof GIndexBinding); |
| 160 | 3 | auxTestList([[1,3],[1,4],[2,3],[2,4]], s); |
| 161 | |
} |
| 162 | |
|
| 163 | |
public void testMultiplyOperator() { |
| 164 | 3 | GSet prod = null; |
| 165 | |
|
| 166 | 3 | prod = GSet.convert(1..2) * GSet.convert(3..4); |
| 167 | 3 | auxTestList([[1,3], [1,4], [2,3], [2,4]], prod); |
| 168 | |
|
| 169 | 3 | prod = GSet.convert(1..1) * GSet.convert(3..3); |
| 170 | 3 | auxTestList([[1,3]], prod); |
| 171 | |
|
| 172 | 3 | prod = GSet.convert(1..2) * GSet.convert(3..4) * GSet.convert(["x"]); |
| 173 | 3 | auxTestList([[1,3,"x"], [1,4,"x"], [2,3,"x"], [2,4,"x"]], prod); |
| 174 | |
|
| 175 | 3 | prod = GSet.convert(["x"]) * GSet.convert(1..2) * GSet.convert(3..4); |
| 176 | 3 | auxTestList([["x",1,3], ["x",1,4], ["x",2,3], ["x",2,4]], prod); |
| 177 | |
|
| 178 | 3 | prod = GSet.convert(["x"]) * (GSet.convert(1..2) * GSet.convert(3..4)); |
| 179 | 3 | auxTestList([["x",1,3], ["x",1,4], ["x",2,3], ["x",2,4]], prod); |
| 180 | |
|
| 181 | 3 | prod = GSet.convert(1..2) * (3..4); |
| 182 | 3 | auxTestList([[1,3], [1,4], [2,3], [2,4]], prod); |
| 183 | |
|
| 184 | 3 | use (GModelArithmeticCategory) { |
| 185 | 3 | prod = (1..2) * (3..4); |
| 186 | 3 | auxTestList([[1,3], [1,4], [2,3], [2,4]], prod); |
| 187 | 3 | prod = (1..2) * ["a", "b"]; |
| 188 | 3 | auxTestList([[1,"a"], [1,"b"], [2,"a"], [2,"b"]], prod); |
| 189 | |
} |
| 190 | |
} |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
public void testProductOfBindingWithSimpleSet() { |
| 197 | 3 | GIndexBinding bind = GIndexBinding.convert(i:1..2); |
| 198 | 3 | GSet prod = bind * (3..4); |
| 199 | 3 | auxTestList([[1,3], [1,4], [2,3], [2,4]], prod); |
| 200 | |
|
| 201 | 3 | GSet prodReverse = GSet.convert(3..4) * [i:1..2]; |
| 202 | 3 | auxTestList([[3,1], [3,2], [4,1], [4,2]], prodReverse); |
| 203 | |
} |
| 204 | |
|
| 205 | |
public void testProductOfBindingWithDynamicSet() { |
| 206 | 3 | GIndexBinding bind = GIndexBinding.convert(i:1..2); |
| 207 | 3 | GSet prod = bind * {(1..i)}; |
| 208 | 3 | auxTestList([[1,1], [2,1], [2,2]], prod); |
| 209 | |
} |
| 210 | |
|
| 211 | |
public void testCreateFromClosure() { |
| 212 | 3 | GSet s = GSet.convert{ (1..i) * ["a", "b"] }; |
| 213 | 3 | assertTrue(s instanceof GDynamicSet); |
| 214 | 3 | auxTestList([[1,"a"], [1,"b"], [2,"a"], [2,"b"]], s, [i:2]); |
| 215 | |
} |
| 216 | |
|
| 217 | |
private void auxTestList(expectedValues, GSet setToTest, Object env=[:]) { |
| 218 | 75 | for (iter in [1, 2]) { |
| 219 | 150 | Iterator setIter = setToTest.evaluate(env); |
| 220 | 150 | expectedValues.each { |
| 221 | 492 | assertTrue(setIter.hasNext()); |
| 222 | 492 | def nextVal = setIter.next(); |
| 223 | 492 | assertEquals(it, nextVal); |
| 224 | |
} |
| 225 | 150 | assertFalse(setIter.hasNext()); |
| 226 | |
} |
| 227 | |
} |
| 228 | |
} |