Coverage Report - net.sourceforge.combean.test.mathprog.grooml.TestGIndexBinding
 
Classes in this File Line Coverage Branch Coverage Complexity
TestGIndexBinding
100%
26/26
50%
31/62
0
TestGIndexBinding$_auxTestDoubleBinding_closure3
100%
3/3
75%
9/12
0
TestGIndexBinding$_auxTestDoubleBinding_closure3_closure4
100%
1/1
100%
2/2
0
TestGIndexBinding$_auxTestDoubleBinding_closure3_closure4_closure5
100%
6/6
57%
17/30
0
TestGIndexBinding$_auxTestSingleBinding_closure2
100%
1/1
N/A
0
TestGIndexBinding$_testIndexBindingWithWrongCardinality_closure1
100%
2/2
50%
2/4
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 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  
          * Check that the binding contains two variables outer, inner that are bound
 105  
          * to the statically defines values outerValues, innerValues.
 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  
 }