#
# Unit Tests for Set() Objects
#
# PyomoModel            Base test class
# SimpleSetA            Testing simple set of integers
# SimpleSetAordered     Testing simple set of ordered integers
# TestRangeSet          Testing the RangeSet class
# TestRangeSet1         More testing of the RangeSet class
# SimpleSetB            Testing simple set of string values
# SimpleSetC            Testing simple set of tuples
# ArraySet              Testing arrays of sets
# RealSetTests          Testing the RealSet class
# IntegerSetTests       Testing the IntegerSet class
# SetArgs1              Testing arguments for simple set
# SetArgs2              Testing arguments for arrays of sets
# Misc                  Misc tests
# SetIO                 Testing Set IO formats
#

import unittest
import os
import sys
from os.path import abspath, dirname
sys.path.insert(0, dirname(dirname(abspath(__file__)))+"/../..")
from coopr.pyomo import *
import coopr.pyomo.base


class PyomoModel(unittest.TestCase):

    def setUp(self):
        self.model = Model()

    def construct(self,filename):
        self.instance = self.model.create(filename)


class SimpleSetA(PyomoModel):

    def setUp(self):
        #
        # Create Model
        #
        PyomoModel.setUp(self)
        #
        # Create Set 'A' data file
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; set A := 1 3 5 7; end;"
        OUTPUT.close()
        #
        # Create model instance
        #
        self.model.A = Set()
        #
        # Misc datasets
        #
        self.model.tmpset1 = Set(initialize=[1,3,5,7])
        self.model.tmpset2 = Set(initialize=[1,2,3,5,7])
        self.model.tmpset3 = Set(initialize=[2,3,5,7,9])

        self.model.setunion = Set(initialize=[1,2,3,5,7,9])
        self.model.setintersection = Set(initialize=[3,5,7])
        self.model.setxor = Set(initialize=[1,2,9])
        self.model.setdiff = Set(initialize=[1])
        self.model.setmul = Set(initialize=[(1,2), (1,3), (1,5), (1,7), (1,9),
                                      (3,2), (3,3), (3,5), (3,7), (3,9),
                                      (5,2), (5,3), (5,5), (5,7), (5,9),
                                      (7,2), (7,3), (7,5), (7,7), (7,9)])

        self.instance = self.model.create("setA.dat")

        self.e1=1
        self.e2=2
        self.e3=3
        self.e4=4
        self.e5=5
        self.e6=6

    def tearDown(self):
        #
        # Remove Set 'A' data file
        #
        if os.path.exists("setA.dat"):
           os.remove("setA.dat")

    def test_len(self):
        """Check that a simple set of numeric elements has the right size"""
        self.failUnlessEqual( len(self.instance.A), 4)

    def test_data(self):
        """Check that we can access the underlying set data"""
        self.failUnlessEqual( len(self.instance.A.data()), 4)

    def test_dim(self):
        """Check that a simple set has dimension zero for its indexing"""
        self.failUnlessEqual( self.instance.A.dim(), 0)

    def test_clear(self):
        """Check the clear() method empties the set"""
        self.instance.A.clear()
        self.failUnlessEqual( len(self.instance.A), 0)

    def test_virtual(self):
        """Check if this is not a virtual set"""
        self.failUnlessEqual( self.instance.A.virtual, False)

    def test_bounds(self):
        """Verify the bounds on this set"""
        self.failUnlessEqual( self.instance.A.bounds(), (1,7))

    def test_check_values(self):
        """Check if the values added to this set are valid"""
        #
        # This should not throw an exception here
        #
        self.instance.A.check_values()

    def test_first(self):
        """Check that we can get the 'first' value in the set"""
        self.tmp = self.instance.A.first()
        self.failIfEqual( self.tmp, None )

    def test_addValid(self):
        """Check that we can add valid set elements"""
        self.instance.A.add(self.e2,self.e4)
        try:
          self.instance.A.add(self.e3)
        except ValueError:
          pass
        else:
          self.fail("Should fail when adding an element that already exists in a set.")
        self.failUnlessEqual( len(self.instance.A), 6)
        self.failIf( self.e2 not in self.instance.A, "Cannot find new element in A")
        self.failIf( self.e4 not in self.instance.A, "Cannot find new element in A")

    def test_addInvalid(self):
        """Check that we get an error when adding invalid set elements"""
        #
        # This verifies that by default, all set elements are valid.  That
        # is, the default within is None
        #
        self.failUnlessEqual( self.instance.A.within, None)
        self.instance.A.add('2','3','4')
        self.failIf( '2' not in self.instance.A, "Found invalid new element in A")
        
    def test_removeValid(self):
        """Check that we can remove a valid set element"""
        self.instance.A.remove(self.e3)
        self.failUnlessEqual( len(self.instance.A), 3)
        self.failIf( 3 in self.instance.A, "Found element in A that we removed")

    def test_removeInvalid(self):
        """Check that we fail to remove an invalid set element"""
        self.failUnlessRaises(KeyError, self.instance.A.remove, 2)
        self.failUnlessEqual( len(self.instance.A), 4)

    def test_discardValid(self):
        """Check that we can discard a valid set element"""
        self.instance.A.discard(self.e3)
        self.failUnlessEqual( len(self.instance.A), 3)
        self.failIf( 3 in self.instance.A, "Found element in A that we removed")

    def test_discardInvalid(self):
        """Check that we fail to remove an invalid set element without an exception"""
        self.instance.A.discard(self.e2)
        self.failUnlessEqual( len(self.instance.A), 4)

    def test_iterator(self):
        """Check that we can iterate through the set"""
        self.tmp = set()
        for val in self.instance.A:
          self.tmp.add(val)
        self.failIf( self.tmp != self.instance.A.data(), "Set values found by the iterator appear to be different from the underlying set")

    def test_eq1(self):
        """Various checks for set equality and inequality (1)"""
        self.failUnlessEqual( self.instance.A == self.instance.tmpset1, True)
        self.failUnlessEqual( self.instance.tmpset1 == self.instance.A, True)
        self.failUnlessEqual( self.instance.A != self.instance.tmpset1, False)
        self.failUnlessEqual( self.instance.tmpset1 != self.instance.A, False)

    def test_eq2(self):
        """Various checks for set equality and inequality (2)"""
        self.failUnlessEqual( self.instance.A == self.instance.tmpset2, False)
        self.failUnlessEqual( self.instance.tmpset2 == self.instance.A, False)
        self.failUnlessEqual( self.instance.A != self.instance.tmpset2, True)
        self.failUnlessEqual( self.instance.tmpset2 != self.instance.A, True)

    def test_le1(self):
        """Various checks for set subset (1)"""
        self.failUnlessEqual( self.instance.A < self.instance.tmpset1, False)
        self.failUnlessEqual( self.instance.A <= self.instance.tmpset1, True)
        self.failUnlessEqual( self.instance.A > self.instance.tmpset1, False)
        self.failUnlessEqual( self.instance.A >= self.instance.tmpset1, True)
        self.failUnlessEqual( self.instance.tmpset1 < self.instance.A, False)
        self.failUnlessEqual( self.instance.tmpset1 <= self.instance.A, True)
        self.failUnlessEqual( self.instance.tmpset1 > self.instance.A, False)
        self.failUnlessEqual( self.instance.tmpset1 >= self.instance.A, True)

    def test_le2(self):
        """Various checks for set subset (2)"""
        self.failUnlessEqual( self.instance.A < self.instance.tmpset2, True)
        self.failUnlessEqual( self.instance.A <= self.instance.tmpset2, True)
        self.failUnlessEqual( self.instance.A > self.instance.tmpset2, False)
        self.failUnlessEqual( self.instance.A >= self.instance.tmpset2, False)
        self.failUnlessEqual( self.instance.tmpset2 < self.instance.A, False)
        self.failUnlessEqual( self.instance.tmpset2 <= self.instance.A, False)
        self.failUnlessEqual( self.instance.tmpset2 > self.instance.A, True)
        self.failUnlessEqual( self.instance.tmpset2 >= self.instance.A, True)

    def test_le3(self):
        """Various checks for set subset (3)"""
        self.failUnlessEqual( self.instance.A < self.instance.tmpset3, False)
        self.failUnlessEqual( self.instance.A <= self.instance.tmpset3, False)
        self.failUnlessEqual( self.instance.A > self.instance.tmpset3, False)
        self.failUnlessEqual( self.instance.A >= self.instance.tmpset3, False)
        self.failUnlessEqual( self.instance.tmpset3 < self.instance.A, False)
        self.failUnlessEqual( self.instance.tmpset3 <= self.instance.A, False)
        self.failUnlessEqual( self.instance.tmpset3 > self.instance.A, False)
        self.failUnlessEqual( self.instance.tmpset3 >= self.instance.A, False)

    def test_contains(self):
        """Various checks for contains() method"""
        self.failUnlessEqual( self.e1 in self.instance.A, True)
        self.failUnlessEqual( self.e2 in self.instance.A, False)
        self.failUnlessEqual( '2' in self.instance.A, False)

    def test_or(self):
        """Check that set union works"""
        self.instance.tmp = self.instance.A | self.instance.tmpset3
        self.instance.tmp.construct()
        self.failUnlessEqual( self.instance.tmp == self.instance.setunion, True)

    def test_and(self):
        """Check that set intersection works"""
        self.instance.tmp = self.instance.A & self.instance.tmpset3
        self.instance.tmp.construct()
        self.failUnlessEqual( self.instance.tmp == self.instance.setintersection, True)

    def test_xor(self):
        """Check that set exclusive or works"""
        self.instance.tmp = self.instance.A ^ self.instance.tmpset3
        self.instance.tmp.construct()
        self.failUnlessEqual( self.instance.tmp == self.instance.setxor, True)

    def test_diff(self):
        """Check that set difference works"""
        self.instance.tmp = self.instance.A - self.instance.tmpset3
        self.instance.tmp.construct()
        self.failUnlessEqual( self.instance.tmp == self.instance.setdiff, True)

    def test_mul(self):
        """Check that set cross-product works"""
        self.instance.tmp = self.instance.A * self.instance.tmpset3
        self.instance.tmp.construct()
        self.failUnlessEqual( self.instance.tmp == self.instance.setmul, True)


class SimpleSetAordered(SimpleSetA):

    def setUp(self):
        #
        # Create Model
        #
        PyomoModel.setUp(self)
        #
        # Create Set 'A' data file
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; set A := 1 3 5 7; end;"
        OUTPUT.close()
        #
        # Create model instance
        #
        self.model.A = Set(ordered=True)
        #
        # Misc datasets
        #
        self.model.tmpset1 = Set(initialize=[1,3,5,7])
        self.model.tmpset2 = Set(initialize=[1,2,3,5,7])
        self.model.tmpset3 = Set(initialize=[2,3,5,7,9])

        self.model.setunion = Set(initialize=[1,2,3,5,7,9])
        self.model.setintersection = Set(initialize=[3,5,7])
        self.model.setxor = Set(initialize=[1,2,9])
        self.model.setdiff = Set(initialize=[1])
        self.model.setmul = Set(initialize=[(1,2), (1,3), (1,5), (1,7), (1,9),
                                      (3,2), (3,3), (3,5), (3,7), (3,9),
                                      (5,2), (5,3), (5,5), (5,7), (5,9),
                                      (7,2), (7,3), (7,5), (7,7), (7,9)])

        self.instance = self.model.create("setA.dat")

        self.e1=1
        self.e2=2
        self.e3=3
        self.e4=4
        self.e5=5
        self.e6=6

    def test_first(self):
        """Check that we can get the 'first' value in the set"""
        self.tmp = self.instance.A.first()
        self.failIfEqual( self.tmp, None )
        self.failUnlessEqual( self.tmp, 1 )

    def test_ordered(self):
        tmp=[]
        for val in self.instance.A:
          tmp.append(val)
        self.failUnlessEqual( tmp, [1,3,5,7] )


class TestRangeSet(SimpleSetA):

    def setUp(self):
        #
        # Create Model
        #
        PyomoModel.setUp(self)
        #
        # Create model instance
        #
        self.model.A = RangeSet(1,5)
        #
        # Misc datasets
        #
        self.model.tmpset1 = Set(initialize=[1,2,3,4,5])
        self.model.tmpset2 = Set(initialize=[1,2,3,4,5,7])
        self.model.tmpset3 = Set(initialize=[2,3,5,7,9])

        self.model.setunion = Set(initialize=[1,2,3,4,5,7,9])
        self.model.setintersection = Set(initialize=[2,3,5])
        self.model.setxor = Set(initialize=[1,4,7,9])
        self.model.setdiff = Set(initialize=[1,4])
        self.model.setmul = Set(initialize=[(1,2), (1,3), (1,5), (1,7), (1,9),
                                      (2,2), (2,3), (2,5), (2,7), (2,9),
                                      (3,2), (3,3), (3,5), (3,7), (3,9),
                                      (4,2), (4,3), (4,5), (4,7), (4,9),
                                      (5,2), (5,3), (5,5), (5,7), (5,9)])

        self.instance = self.model.create()

        self.e1=1
        self.e2=2
        self.e3=3
        self.e4=4
        self.e5=5
        self.e6=6

    def test_bounds(self):
        """Verify the bounds on this set"""
        self.failUnlessEqual( self.instance.A.bounds(), (1,5))

    def test_addValid(self):
        """Check that we can add valid set elements"""
        try:
          self.instance.A.add(self.e6)
          self.instance.A.add(self.e3)
        except ValueError:
          pass
        else:
          self.fail("Should fail when adding an element that already exists in a set.")
        self.failUnlessEqual( len(self.instance.A), 5)

    def test_addInvalid(self):
        """Check that we get an error when adding invalid set elements"""
        #
        # This verifies that by default, all set elements are valid.  That
        # is, the default within is None
        #
        self.failUnlessEqual( self.instance.A.within, None)
        try:
          self.instance.A.add('2','3','4')
        except ValueError:
          pass
        else:
          self.fail("test_addInvalid")
        self.failIf( '2' in self.instance.A, "Found invalid new element in A")
        
    def test_removeValid(self):
        """Check that we can remove a valid set element"""
        self.instance.A.remove(self.e3)
        self.failUnlessEqual( len(self.instance.A), 4)
        self.failIf( self.e3 in self.instance.A, "Found element in A that we removed")

    def test_removeInvalid(self):
        """Check that we fail to remove an invalid set element"""
        self.failUnlessRaises(KeyError, self.instance.A.remove, 6)
        self.failUnlessEqual( len(self.instance.A), 5)

    def test_discardValid(self):
        """Check that we can discard a valid set element"""
        self.instance.A.discard(self.e3)
        self.failUnlessEqual( len(self.instance.A), 4)
        self.failIf( self.e3 in self.instance.A, "Found element in A that we removed")

    def test_discardInvalid(self):
        """Check that we fail to remove an invalid set element without an exception"""
        self.instance.A.discard(self.e6)
        self.failUnlessEqual( len(self.instance.A), 5)

    def test_contains(self):
        """Various checks for contains() method"""
        self.failUnlessEqual( self.e1 in self.instance.A, True)
        self.failUnlessEqual( self.e2 in self.instance.A, True)
        self.failUnlessEqual( '2' in self.instance.A, False)

    def test_len(self):
        """Check that a simple set of numeric elements has the right size"""
        self.failUnlessEqual( len(self.instance.A), 5)

    def test_data(self):
        """Check that we can access the underlying set data"""
        self.failUnlessEqual( len(self.instance.A.data()), 5)


class TestRangeSet2(TestRangeSet):

    def setUp(self):
        #
        # Create Model
        #
        PyomoModel.setUp(self)
        #
        # Create model instance
        #
        def validate_fn(val,model):
            return (val >= 1) and (val <= 5)

        self.model.A = RangeSet(1,10, validate=validate_fn)
        #
        # Misc datasets
        #
        self.model.tmpset1 = Set(initialize=[1,2,3,4,5])
        self.model.tmpset2 = Set(initialize=[1,2,3,4,5,7])
        self.model.tmpset3 = Set(initialize=[2,3,5,7,9])

        self.model.setunion = Set(initialize=[1,2,3,4,5,7,9])
        self.model.setintersection = Set(initialize=[2,3,5])
        self.model.setxor = Set(initialize=[1,4,7,9])
        self.model.setdiff = Set(initialize=[1,4])
        self.model.setmul = Set(initialize=[(1,2), (1,3), (1,5), (1,7), (1,9),
                                      (2,2), (2,3), (2,5), (2,7), (2,9),
                                      (3,2), (3,3), (3,5), (3,7), (3,9),
                                      (4,2), (4,3), (4,5), (4,7), (4,9),
                                      (5,2), (5,3), (5,5), (5,7), (5,9)])

        self.instance = self.model.create()

        self.e1=1
        self.e2=2
        self.e3=3
        self.e4=4
        self.e5=5
        self.e6=6


class TestRangeSetMisc(unittest.TestCase):

    def test_constructor1(self):
        a=RangeSet(10)
        a.construct()
        tmp=[]
        for i in a:
          tmp.append(i)
        self.failUnlessEqual(tmp, range(1,11))
        self.failUnlessEqual( a.bounds(), (1,10))


    def test_constructor2(self):
        a=RangeSet(1,10,2)
        a.construct()
        tmp=[]
        for i in a:
          tmp.append(i)
        self.failUnlessEqual(tmp, range(1,11,2))
        self.failUnlessEqual( a.bounds(), (1,9))

    def test_constructor3(self):
        model=Model()
        model.a=Param(initialize=1)
        model.b=Param(initialize=2)
        model.c=Param(initialize=10)
        model.d=RangeSet( model.a*model.a, model.c*model.a, model.a*model.b)
        instance=model.create()
        tmp=[]
        for i in instance.d:
          tmp.append(i)
        self.failUnlessEqual(tmp, range(1,11,2))
        try:
            instance.d.add(2)
            self.fail("Only odd values allowed")
        except ValueError:
            pass
        try:
            instance.d.add(0)
            self.fail("Only positive values allowed")
        except ValueError:
            pass
        self.failUnlessEqual( instance.d.bounds(), (1,9))

class SimpleSetB(SimpleSetA):

    def setUp(self):
        #
        # Create Model
        #
        PyomoModel.setUp(self)
        #
        # Create Set 'A' data file
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; set A := A1 A3 A5 A7; end;"
        OUTPUT.close()
        #
        # Create model instance
        #
        self.model.A = Set()
        #
        # Debugging
        #
        # Misc datasets
        #
        self.model.tmpset1 = Set(initialize=['A1','A3','A5','A7'])
        self.model.tmpset2 = Set(initialize=['A1','A2','A3','A5','A7'])
        self.model.tmpset3 = Set(initialize=['A2','A3','A5','A7','A9'])

        self.model.setunion = Set(initialize=['A1','A2','A3','A5','A7','A9'])
        self.model.setintersection = Set(initialize=['A3','A5','A7'])
        self.model.setxor = Set(initialize=['A1','A2','A9'])
        self.model.setdiff = Set(initialize=['A1'])
        self.model.setmul = Set(initialize=[('A1','A2'), ('A1','A3'), ('A1','A5'), ('A1','A7'), ('A1','A9'),
                                      ('A3','A2'), ('A3','A3'), ('A3','A5'), ('A3','A7'), ('A3','A9'),
                                      ('A5','A2'), ('A5','A3'), ('A5','A5'), ('A5','A7'), ('A5','A9'),
                                      ('A7','A2'), ('A7','A3'), ('A7','A5'), ('A7','A7'), ('A7','A9')])

        self.instance = self.model.create("setA.dat")
        self.e1='A1'
        self.e2='A2'
        self.e3='A3'
        self.e4='A4'
        self.e5='A5'
        self.e6='A6'

    def test_bounds(self):
        self.failUnlessEqual( self.instance.A.bounds(), None)


class SimpleSetC(SimpleSetA):

    def setUp(self):
        #
        # Create Model
        #
        PyomoModel.setUp(self)
        #
        # Create Set 'A' data file
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; set A := (A1,1) (A3,1) (A5,1) (A7,1); end;"
        OUTPUT.close()
        #
        # Create model instance
        #
        self.model.A = Set(dimen=2)
        #
        # Debugging
        #
        # Misc datasets
        #
        self.model.tmpset1 = Set(initialize=[('A1',1), ('A3',1), ('A5',1), ('A7',1)])
        self.model.tmpset2 = Set(initialize=[('A1',1),('A2',1),('A3',1),('A5',1),('A7',1)])
        self.model.tmpset3 = Set(initialize=[('A2',1),('A3',1),('A5',1),('A7',1),('A9',1)])

        self.model.setunion = Set(initialize=[('A1',1),('A2',1),('A3',1),('A5',1),('A7',1),('A9',1)])
        self.model.setintersection = Set(initialize=[('A3',1),('A5',1),('A7',1)])
        self.model.setxor = Set(initialize=[('A1',1),('A2',1),('A9',1)])
        self.model.setdiff = Set(initialize=[('A1',1)])
        self.model.setmul = Set(initialize=[(('A1',1,'A2',1)), (('A1',1,'A3',1)), (('A1',1,'A5',1)), (('A1',1,'A7',1)), (('A1',1,'A9',1)),
                                      (('A3',1,'A2',1)), (('A3',1,'A3',1)), (('A3',1,'A5',1)), (('A3',1,'A7',1)), (('A3',1,'A9',1)),
                                      (('A5',1,'A2',1)), (('A5',1,'A3',1)), (('A5',1,'A5',1)), (('A5',1,'A7',1)), (('A5',1,'A9',1)),
                                      (('A7',1,'A2',1)), (('A7',1,'A3',1)), (('A7',1,'A5',1)), (('A7',1,'A7',1)), (('A7',1,'A9',1))])

        self.instance = self.model.create("setA.dat")
        self.e1=('A1',1)
        self.e2=('A2',1)
        self.e3=('A3',1)
        self.e4=('A4',1)
        self.e5=('A5',1)
        self.e6=('A6',1)

    def tearDown(self):
        #
        # Remove Set 'A' data file
        #
        os.remove("setA.dat")

    def test_bounds(self):
        self.failUnlessEqual( self.instance.A.bounds(), None)

    def test_addInvalid(self):
        """Check that we get an error when adding invalid set elements"""
        #
        # This verifies that by default, all set elements are valid.  That
        # is, the default within is None
        #
        self.failUnlessEqual( self.instance.A.within, None)
        try:
          self.instance.A.add('2','3','4')
        except ValueError:
          pass
        else:
          self.fail("fail test_addInvalid")
        self.failIf( '2' in self.instance.A, "Found invalid new element in A")
        self.instance.A.add(('2','3'))
        

class ArraySet(PyomoModel):

    def setUp(self):
        #
        # Create Model
        #
        PyomoModel.setUp(self)
        #
        # Create Set 'A' data file
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; set Z := A C; set A[A] := 1 3 5 7; set A[C] := 3 5 7 9; end;"
        OUTPUT.close()
        #
        # Create model instance
        #
        self.model.Z = Set()
        self.model.A = Set(self.model.Z)
        #
        # Debugging
        #
        # Misc datasets
        #
        self.model.tmpset1 = Set()
        self.model.tmpset2 = Set()
        self.model.tmpset3 = Set()

        self.instance = self.model.create("setA.dat")
        self.e1=('A1',1)

    def test_bounds(self):
        self.failUnlessEqual( self.instance.A.bounds(), None)

    def test_getitem(self):
        """Check the access to items"""
        try:
          tmp=[]
          for val in self.instance.A['A']:
            tmp.append(val)
          tmp.sort()
        except:
          self.fail("Problems getting a valid set from a set array")
        self.failUnlessEqual( tmp, [1,3,5,7])
        try:
          tmp = self.instance.A['D']
        except KeyError:
          pass
        else:
          self.fail("Problems getting an invalid set from a set array")

    def test_setitem(self):
        """Check the access to items"""
        try:
          self.model.Z = Set(initialize=['A','C'])
          self.model.A = Set(self.model.Z,initialize={'A':[1]})
          self.instance = self.model.create()
          tmp=[1,6,9]
          self.instance.A['A'] = tmp
          self.instance.A['C'] = tmp
        except:
          self.fail("Problems setting a valid set into a set array")
        try:
          self.instance.A['D'] = tmp
        except KeyError:
          pass
        else:
          self.fail("Problems setting an invalid set into a set array")

    def test_keys(self):
        """Check the keys for the array"""
        tmp=self.instance.A.keys()
        tmp.sort()
        self.failUnlessEqual(tmp, ['A', 'C'])

    def test_len(self):
        """Check that a simple set of numeric elements has the right size"""
        try:
           len(self.instance.A)
        except TypeError:
           self.fail("fail test_len")            
        else:
           pass            


    def test_data(self):
        """Check that we can access the underlying set data"""
        try:
          self.instance.A.data()
        except TypeError:
          pass
        else:
          self.fail("fail test_data")

    def test_dim(self):
        """Check that a simple set has dimension zero for its indexing"""
        self.failUnlessEqual( self.instance.A.dim(), 1)

    def test_clear(self):
        """Check the clear() method empties the set"""
        self.instance.A.clear()
        for key in self.instance.A:
          self.failUnlessEqual( len(self.instance.A[key]), 0)

    def test_virtual(self):
        """Check if this is not a virtual set"""
        self.failUnlessEqual( self.instance.A.virtual, False)

    def test_check_values(self):
        """Check if the values added to this set are valid"""
        #
        # This should not throw an exception here
        #
        self.instance.A.check_values()

    def test_first(self):
        """Check that we can get the 'first' value in the set"""
        pass

    def test_removeValid(self):
        """Check that we can remove a valid set element"""
        pass

    def test_removeInvalid(self):
        """Check that we fail to remove an invalid set element"""
        pass

    def test_discardValid(self):
        """Check that we can discard a valid set element"""
        pass

    def test_discardInvalid(self):
        """Check that we fail to remove an invalid set element without an exception"""
        pass

    def test_iterator(self):
        """Check that we can iterate through the set"""
        tmp = 0
        for key in self.instance.A:
          tmp += len(self.instance.A[key])
        self.failUnlessEqual( tmp, 8)

    def test_eq1(self):
        """Various checks for set equality and inequality (1)"""
        try:
          self.failUnlessEqual( self.instance.A == self.instance.tmpset1, True)
          self.failUnlessEqual( self.instance.tmpset1 == self.instance.A, True)
          self.failUnlessEqual( self.instance.A != self.instance.tmpset1, False)
          self.failUnlessEqual( self.instance.tmpset1 != self.instance.A, False)
        except TypeError:
          pass
        else:
          self.fail("fail test_eq1")

    def test_eq2(self):
        """Various checks for set equality and inequality (2)"""
        try:
          self.failUnlessEqual( self.instance.A == self.instance.tmpset2, False)
          self.failUnlessEqual( self.instance.tmpset2 == self.instance.A, False)
          self.failUnlessEqual( self.instance.A != self.instance.tmpset2, True)
          self.failUnlessEqual( self.instance.tmpset2 != self.instance.A, True)
        except TypeError:
          pass
        else:
          self.fail("fail test_eq2")

    def test_contains(self):
        """Various checks for contains() method"""
        tmp = self.e1 in self.instance.A
        self.failUnlessEqual( tmp, False )

    def test_or(self):
        """Check that set union works"""
        try:
          self.instance.A | self.instance.tmpset3
        except TypeError:
          pass
        else:
          self.fail("fail test_or")

    def test_and(self):
        """Check that set intersection works"""
        try:
          self.instance.tmp = self.instance.A & self.instance.tmpset3
        except TypeError:
          pass
        else:
          self.fail("fail test_and")

    def test_xor(self):
        """Check that set exclusive or works"""
        try:
          self.instance.A ^ self.instance.tmpset3
        except TypeError:
          pass
        else:
          self.fail("fail test_xor")

    def test_diff(self):
        """Check that set difference works"""
        try:
          self.instance.A - self.instance.tmpset3
        except TypeError:
          pass
        else:
          self.fail("fail test_diff")

    def test_mul(self):
        """Check that set cross-product works"""
        try:
          self.instance.A * self.instance.tmpset3
        except TypeError:
          pass
        else:
          self.fail("fail test_mul")


class ArraySet2(PyomoModel):

    def setUp(self):
        #
        # Create Model
        #
        PyomoModel.setUp(self)
        #
        # Create Set 'A' data file
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; set Z := A C; set Y := 1 2 ; set A[A,1] := 1 3 5 7; set A[C,2] := 3 5 7 9; end;"
        OUTPUT.close()
        #
        # Create model instance
        #
        self.model.Z = Set()
        self.model.Y = Set()
        self.model.A = Set(self.model.Z,self.model.Y)
        #
        # Debugging
        #
        # Misc datasets
        #
        self.model.tmpset1 = Set()
        self.model.tmpset2 = Set()
        self.model.tmpset3 = Set()

        self.instance = self.model.create("setA.dat")
        self.e1=('A1',1)

    def test_bounds(self):
        self.failUnlessEqual( self.instance.A.bounds(), None)

    def test_getitem(self):
        """Check the access to items"""
        try:
          tmp=[]
          for val in self.instance.A['A',1]:
            tmp.append(val)
          tmp.sort()
        except:
          self.fail("Problems getting a valid set from a set array")
        self.failUnlessEqual( tmp, [1,3,5,7])
        try:
          tmp = self.instance.A['A',2]
        except KeyError:
          pass
        else:
          self.fail("Problems getting an invalid set from a set array")

    def Xtest_setitem(self):
        """Check the access to items"""
        try:
          self.model.Y = Set(initialize=[1,2])
          self.model.Z = Set(initialize=['A','C'])
          self.model.A = Set(self.model.Z,self.model.Y,initialize={'A':[1]})
          self.instance = self.model.create()
          tmp=[1,6,9]
          self.instance.A['A'] = tmp
          self.instance.A['C'] = tmp
        except:
          self.fail("Problems setting a valid set into a set array")
        try:
          self.instance.A['D'] = tmp
        except KeyError:
          pass
        else:
          self.fail("Problems setting an invalid set into a set array")

    def Xtest_keys(self):
        """Check the keys for the array"""
        tmp=self.instance.A.keys()
        tmp.sort()
        self.failUnlessEqual(tmp, ['A', 'C'])

    def Xtest_len(self):
        """Check that a simple set of numeric elements has the right size"""
        try:
          len(self.instance.A)
        except TypeError:
          pass
        else:
          self.fail("fail test_len")

    def Xtest_data(self):
        """Check that we can access the underlying set data"""
        try:
          self.instance.A.data()
        except TypeError:
          pass
        else:
          self.fail("fail test_data")

    def Xtest_dim(self):
        """Check that a simple set has dimension zero for its indexing"""
        self.failUnlessEqual( self.instance.A.dim(), 1)

    def Xtest_clear(self):
        """Check the clear() method empties the set"""
        self.instance.A.clear()
        for key in self.instance.A:
          self.failUnlessEqual( len(self.instance.A[key]), 0)

    def Xtest_virtual(self):
        """Check if this is not a virtual set"""
        self.failUnlessEqual( self.instance.A.virtual, False)

    def Xtest_check_values(self):
        """Check if the values added to this set are valid"""
        #
        # This should not throw an exception here
        #
        self.instance.A.check_values()

    def Xtest_first(self):
        """Check that we can get the 'first' value in the set"""
        pass

    def Xtest_removeValid(self):
        """Check that we can remove a valid set element"""
        pass

    def Xtest_removeInvalid(self):
        """Check that we fail to remove an invalid set element"""
        pass

    def Xtest_discardValid(self):
        """Check that we can discard a valid set element"""
        pass

    def Xtest_discardInvalid(self):
        """Check that we fail to remove an invalid set element without an exception"""
        pass

    def Xtest_iterator(self):
        """Check that we can iterate through the set"""
        tmp = 0
        for key in self.instance.A:
          tmp += len(self.instance.A[key])
        self.failUnlessEqual( tmp, 8)

    def Xtest_eq1(self):
        """Various checks for set equality and inequality (1)"""
        try:
          self.failUnlessEqual( self.instance.A == self.instance.tmpset1, True)
          self.failUnlessEqual( self.instance.tmpset1 == self.instance.A, True)
          self.failUnlessEqual( self.instance.A != self.instance.tmpset1, False)
          self.failUnlessEqual( self.instance.tmpset1 != self.instance.A, False)
        except TypeError:
          pass
        else:
          self.fail("fail test_eq1")

    def Xtest_eq2(self):
        """Various checks for set equality and inequality (2)"""
        try:
          self.failUnlessEqual( self.instance.A == self.instance.tmpset2, False)
          self.failUnlessEqual( self.instance.tmpset2 == self.instance.A, False)
          self.failUnlessEqual( self.instance.A != self.instance.tmpset2, True)
          self.failUnlessEqual( self.instance.tmpset2 != self.instance.A, True)
        except TypeError:
          pass
        else:
          self.fail("fail test_eq2")

    def Xtest_contains(self):
        """Various checks for contains() method"""
        tmp = self.e1 in self.instance.A
        self.failUnlessEqual( tmp, False )

    def Xtest_or(self):
        """Check that set union works"""
        try:
          self.instance.A | self.instance.tmpset3
        except TypeError:
          pass
        else:
          self.fail("fail test_or")

    def Xtest_and(self):
        """Check that set intersection works"""
        try:
          self.instance.tmp = self.instance.A & self.instance.tmpset3
        except TypeError:
          pass
        else:
          self.fail("fail test_and")

    def Xtest_xor(self):
        """Check that set exclusive or works"""
        try:
          self.instance.A ^ self.instance.tmpset3
        except TypeError:
          pass
        else:
          self.fail("fail test_xor")

    def Xtest_diff(self):
        """Check that set difference works"""
        try:
          self.instance.A - self.instance.tmpset3
        except TypeError:
          pass
        else:
          self.fail("fail test_diff")

    def Xtest_mul(self):
        """Check that set cross-product works"""
        try:
          self.instance.A * self.instance.tmpset3
        except TypeError:
          pass
        else:
          self.fail("fail test_mul")


class RealSetTests(SimpleSetA):

    def setUp(self):
        #
        # Create Model
        #
        PyomoModel.setUp(self)
        #
        # Create model instance
        #
        self.model.A = Reals
        #
        # Misc datasets
        #
        self.model.tmpset1 = Set(initialize=[1.1,3.1,5.1,7.1])
        self.model.tmpset2 = Set(initialize=[1.1,2.1,3.1,5.1,7.1])
        self.model.tmpset3 = Set(initialize=[2.1,3.1,5.1,7.1,9.1])

        self.model.setunion = Reals
        self.model.setintersection = Set(initialize=[1.1,3.1,5.1,7.1])
        self.model.setxor = Set(initialize=[])
        self.model.setdiff = Set(initialize=[])
        self.model.setmul = None

        self.instance = self.model.create()
        self.e1=1.1
        self.e2=2.1
        self.e3=3.1
        self.e4=4.1
        self.e5=5.1
        self.e6=6.1

    def tearDown(self):
        pass

    def test_bounds(self):
        self.failUnlessEqual( self.instance.A.bounds(), (None,None))

    def test_len(self):
        """Check that the set has the right size"""
        try:
          len(self.instance.A)
        except ValueError:
          pass
        else:
          self.fail("test_len failure")

    def test_data(self):
        """Check that we can access the underlying set data"""
        try:
          self.instance.A.data()
        except TypeError:
          pass
        else:
          self.fail("test_data failure")

    def test_clear(self):
        """Check that the clear() method generates an exception"""
        self.failUnlessRaises(TypeError, self.instance.A.clear)

    def test_virtual(self):
        """Check if this is not a virtual set"""
        self.failUnlessEqual( self.instance.A.virtual, True)

    def test_discardValid(self):
        """Check that we fail to remove an invalid set element without an exception"""
        self.failUnlessRaises(KeyError, self.instance.A.discard, self.e2)

    def test_discardInvalid(self):
        """Check that we fail to remove an invalid set element without an exception"""
        pass

    def test_removeValid(self):
        """Check that we can remove a valid set element"""
        self.failUnlessRaises(KeyError, self.instance.A.remove, self.e3)

    def test_removeInvalid(self):
        pass

    def test_addInvalid(self):
        """Check that we get an error when adding invalid set elements"""
        pass
        
    def test_first(self):
        """Check that we can get the 'first' value in the set"""
        self.failUnlessRaises(TypeError,self.instance.A.first)

    def test_addValid(self):
        """Check that we can add valid set elements"""
        self.failUnlessEqual( self.instance.A.within, None)
        self.failUnlessRaises(TypeError,self.instance.A.add,2)

    def test_iterator(self):
        """Check that we can iterate through the set"""
        try:
          for val in self.instance.A:
            tmp=val
        except TypeError:
          pass
        else:
          fail("test_iterator failure")

    def test_eq1(self):
        """Various checks for set equality and inequality (1)"""
        self.failUnless(not(self.instance.A == self.instance.tmpset1))
        self.failUnless(not(self.instance.tmpset1 == self.instance.A))
        self.failUnless(self.instance.A != self.instance.tmpset1)
        self.failUnless(self.instance.tmpset1 != self.instance.A)


    def test_eq2(self):
        """Various checks for set equality and inequality (2)"""
        self.failUnless(not(self.instance.A == self.instance.tmpset2))
        self.failUnless(not(self.instance.tmpset2 == self.instance.A))
        self.failUnless(self.instance.A != self.instance.tmpset2)
        self.failUnless(self.instance.tmpset2 != self.instance.A)

    def test_le1(self):
        """Various checks for set subset (1)"""
        try:
          self.instance.A < self.instance.tmpset1
          self.instance.A <= self.instance.tmpset1
          self.instance.A > self.instance.tmpset1
          self.instance.A >= self.instance.tmpset1
          self.instance.tmpset1 < self.instance.A
          self.instance.tmpset1 <= self.instance.A
          self.instance.tmpset1 > self.instance.A
          self.instance.tmpset1 >= self.instance.A
        except TypeError:
          pass
        else:
          self.fail("test_le1 failure")

    def test_le2(self):
        """Various checks for set subset (2)"""
        try:
          self.instance.A < self.instance.tmpset2
          self.instance.A <= self.instance.tmpset2
          self.instance.A > self.instance.tmpset2
          self.instance.A >= self.instance.tmpset2
          self.instance.tmpset2 < self.instance.A
          self.instance.tmpset2 <= self.instance.A
          self.instance.tmpset2 > self.instance.A
          self.instance.tmpset2 >= self.instance.A
        except TypeError:
          pass
        else:
          self.fail("test_le2 failure")

    def test_le3(self):
        """Various checks for set subset (3)"""
        try:
          self.instance.A < self.instance.tmpset3
          self.instance.A <= self.instance.tmpset3
          self.instance.A > self.instance.tmpset3
          self.instance.A >= self.instance.tmpset3
          self.instance.tmpset3 < self.instance.A
          self.instance.tmpset3 <= self.instance.A
          self.instance.tmpset3 > self.instance.A
          self.instance.tmpset3 >= self.instance.A
        except TypeError:
          pass
        else:
          self.fail("test_le3 failure")

    def test_contains(self):
        """Various checks for contains() method"""
        self.failUnlessEqual( self.e1 in self.instance.A, True)
        self.failUnlessEqual( self.e2 in self.instance.A, True)
        self.failUnlessEqual( '2' in self.instance.A, False)

    def test_or(self):
        """Check that set union works"""
        try:
          self.instance.tmp = self.instance.A | self.instance.tmpset3
        except TypeError:
          pass
        else:
          fail("Operator __or__ should have failed.")

    def test_and(self):
        """Check that set intersection works"""
        try:
          self.instance.tmp = self.instance.A & self.instance.tmpset3
        except TypeError:
          pass
        else:
          fail("Operator __and__ should have failed.")

    def test_xor(self):
        """Check that set exclusive or works"""
        try:
          self.tmp = self.instance.A ^ self.instance.tmpset3
        except:
          pass
        else:
          fail("Operator __xor__ should have failed.")

    def test_diff(self):
        """Check that set difference works"""
        try:
          self.tmp = self.instance.A - self.instance.tmpset3
        except:
          pass
        else:
          fail("Operator __diff__ should have failed.")

    def test_mul(self):
        """Check that set cross-product works"""
        try:
          self.instance.tmp = self.instance.A * self.instance.tmpset3
        except:
          pass
        else:
          fail("Operator __mul__ should have failed.")


class IntegerSetTests(RealSetTests):

    def setUp(self):
        #
        # Create Model
        #
        PyomoModel.setUp(self)
        #
        # Create model instance
        #
        self.model.A = Integers
        #
        # Misc datasets
        #
        self.model.tmpset1 = Set(initialize=[1,3,5,7])
        self.model.tmpset2 = Set(initialize=[1,2,3,5,7])
        self.model.tmpset3 = Set(initialize=[2,3,5,7,9])

        self.model.setunion = Reals
        self.model.setintersection = Set(initialize=[1,3,5,7])
        self.model.setxor = Set(initialize=[])
        self.model.setdiff = Set(initialize=[])
        self.model.setmul = None

        self.instance = self.model.create()
        self.e1=1
        self.e2=2
        self.e3=3
        self.e4=4
        self.e5=5
        self.e6=6

    def test_bounds(self):
        self.failUnlessEqual( self.instance.A.bounds(), (None,None))


class AnySetTests(RealSetTests):

    def setUp(self):
        #
        # Create Model
        #
        PyomoModel.setUp(self)
        #
        # Create model instance
        #
        self.model.A = Any
        #
        # Misc datasets
        #
        self.model.tmpset1 = Set(initialize=[1,'3',5,7])
        self.model.tmpset2 = Set(initialize=[1,2,'3',5,7])
        self.model.tmpset3 = Set(initialize=[2,'3',5,7,9])

        self.model.setunion = Any
        self.model.setintersection = Set(initialize=[1,'3',5,7])
        self.model.setxor = Set(initialize=[])
        self.model.setdiff = Set(initialize=[])
        self.model.setmul = None

        self.instance = self.model.create()
        self.e1=1
        self.e2=2
        self.e3='3'
        self.e4=4
        self.e5=5
        self.e6=6

    def test_bounds(self):
        self.failUnlessEqual( self.instance.A.bounds(), None)

    def test_contains(self):
        """Various checks for contains() method"""
        self.failUnlessEqual( self.e1 in self.instance.A, True)
        self.failUnlessEqual( self.e2 in self.instance.A, True)
        self.failUnlessEqual( '2' in self.instance.A, True)


class SetArgs1(PyomoModel):

    def setUp(self):
        #
        # Create Model
        #
        PyomoModel.setUp(self)

    def test_initialize1(self):
        self.model.A = Set(initialize=[1,2,3,'A'])
        self.instance = self.model.create()
        self.failUnlessEqual(len(self.instance.A),4)
        
    def test_initialize2(self):
        self.model.A = Set(initialize=[(i,j) for i in range(0,3) for j in range(1,4) if (i+j)%2 == 0])
        self.instance = self.model.create()
        self.failUnlessEqual(len(self.instance.A),4)

    def test_initialize3(self):
        self.model.A = Set(initialize=((i,j) for i in range(0,3) for j in range(1,4) if (i+j)%2 == 0))
        self.instance = self.model.create()
        self.failUnlessEqual(len(self.instance.A),4)
        
    def test_initialize4(self):
        self.model.A = Set(initialize=range(0,4))
        def B_index(model):
            return (i for i in model.A if i%2 == 0)
        def B_init(i,model):
            return range(i,2+i)
        self.model.B = Set(B_index, initialize=B_init)
        self.instance = self.model.create()
        #self.instance.pprint()
        self.failUnlessEqual(self.instance.B[0].data(),set([0,1]))
        self.failUnlessEqual(self.instance.B[2].data(),set([2,3]))
        self.failUnlessEqual(self.instance.B.keys(),[0,2])

    def test_initialize5(self):
        self.model.A = Set(initialize=range(0,4))
        def B_index(model):
            for i in model.A:
                if i%2 == 0:
                    yield i
        def B_init(i,model):
            return range(i,2+i)
        self.model.B = Set(B_index, initialize=B_init)
        self.instance = self.model.create()
        #self.instance.pprint()
        self.failUnlessEqual(self.instance.B[0].data(),set([0,1]))
        self.failUnlessEqual(self.instance.B[2].data(),set([2,3]))
        self.failUnlessEqual(self.instance.B.keys(),[0,2])

    def test_initialize6(self):
        self.model.A = Set(initialize=range(0,4))
        def B_index(model):
            for i in model.A:
                if i%2 == 0:
                    yield i
        def B_init(i,j,model):
            if j:
                return range(i,2+i)
            return []
        self.model.B = Set(B_index, [True,False], initialize=B_init)
        self.instance = self.model.create()
        #self.instance.pprint()
        self.failUnlessEqual(set(self.instance.B.keys()),set([(0,True),(2,True),(0,False),(2,False)]))
        self.failUnlessEqual(self.instance.B[0,True].data(),set([0,1]))
        self.failUnlessEqual(self.instance.B[2,True].data(),set([2,3]))

    def test_initialize7(self):
        self.model.A = Set(initialize=range(0,3))
        @set_options(dimen=3)
        def B_index(model):
            return [(i,i+1,i*i) for i in model.A]
        def B_init(i,ii,iii,j,model):
            if j:
                return range(i,2+i)
            return []
        self.model.B = Set(B_index, [True,False], initialize=B_init)
        self.instance = self.model.create()
        #self.instance.pprint()
        self.failUnlessEqual(set(self.instance.B.keys()),set([(0,1,0,True),(1,2,1,True),(2,3,4,True),(0,1,0,False),(1,2,1,False),(2,3,4,False)]))
        self.failUnlessEqual(self.instance.B[0,1,0,True].data(),set([0,1]))
        self.failUnlessEqual(self.instance.B[2,3,4,True].data(),set([2,3]))

    def test_initialize8(self):
        self.model.A = Set(initialize=range(0,3))
        def B_index(model):
            return [(i,i+1,i*i) for i in model.A]
        def B_init(i,ii,iii,j,model):
            if j:
                return range(i,2+i)
            return []
        self.model.B = Set(B_index, [True,False], initialize=B_init)
        try:
            self.instance = self.model.create()
            self.fail("Expected ValueError because B_index returns a tuple")
        except ValueError:
            pass

    def test_initialize9(self):
        self.model.A = Set(initialize=range(0,3))
        @set_options(domain=Integers)
        def B_index(model):
            return [i/2.0 for i in model.A]
        def B_init(i,j,model):
            if j:
                return range(int(i),int(2+i))
            return []
        self.model.B = Set(B_index, [True,False], initialize=B_init)
        self.instance = self.model.create()
        try:
            self.instance = self.model.create()
            self.fail("Expected ValueError because B_index returns invalid set values")
        except AssertionError:
            pass

    def test_dimen1(self):
        #
        # Create model instance
        #
        self.model.A = Set(initialize=[1,2,3], dimen=1)
        self.instance = self.model.create()
        #
        try:
          self.model.A = Set(initialize=[4,5,6], dimen=2)
          self.instance = self.model.create()
        except ValueError:
          pass
        else:
          self.fail("test_dimen")
        #
        self.model.A = Set(initialize=[(1,2), (2,3), (3,4)], dimen=2)
        self.instance = self.model.create()
        #
        try:
          self.model.A = Set(initialize=[(1,2), (2,3), (3,4)], dimen=1)
          self.instance = self.model.create()
        except ValueError:
          pass
        else:
          self.fail("test_dimen")
        #
        def f(model):
            return [(1,1), (2,2), (3,3)]
        self.model.A = Set(initialize=f, dimen=2)
        self.instance = self.model.create()
        #
        try:
            self.model.A = Set(initialize=f, dimen=3)
            self.instance = self.model.create()
        except ValueError:
          pass
        else:
          self.fail("test_dimen")
          
    def test_rule(self):
        #
        # Create model instance
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; param n := 5; end;"
        OUTPUT.close()
        def tmp_init(model):
            ##model.n.pprint()
            ##print "HERE",model.n,value(model.n)
            return range(0,value(model.n))
        self.model.n = Param()
        self.model.A = Set(initialize=tmp_init)
        self.instance = self.model.create("setA.dat")
        self.failUnlessEqual(len(self.instance.A),5)
          
    def test_rule2(self):
        #
        # Create model instance
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; param n := 5; end;"
        OUTPUT.close()
        def tmp_init(z,model):
            if z==value(model.n) or z == 10:
               return None
            return z
        self.model.n = Param()
        self.model.A = Set(initialize=tmp_init)
        self.instance = self.model.create("setA.dat")
        self.failUnlessEqual(len(self.instance.A),5)
          
    def test_within1(self):
        #
        # Create Set 'A' data file
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; set A := 1 3 5 7.5; end;"
        OUTPUT.close()
        #
        # Create A with an error
        #
        self.model.A = Set(within=Integers)
        try:
          self.instance = self.model.create("setA.dat")
        except ValueError:
          pass
        else:
          self.fail("fail test_within1")
        
    def test_within2(self):
        #
        # Create Set 'A' data file
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; set A := 1 3 5 7.5; end;"
        OUTPUT.close()
        #
        # Create A with an error
        #
        self.model.A = Set(within=Reals)
        try:
          self.instance = self.model.create("setA.dat")
        except ValueError:
          self.fail("fail test_within2")
        else:
          pass
        
    def test_validation1(self):
        #
        # Create Set 'A' data file
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; set A := 1 3 5 7.5; end;"
        OUTPUT.close()
        #
        # Create A with an error
        #
        self.model.A = Set(validate=lambda x,model:x<6)
        try:
          self.instance = self.model.create("setA.dat")
        except ValueError:
          pass
        else:
          self.fail("fail test_validation1")
        
    def test_validation2(self):
        #
        # Create Set 'A' data file
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; set A := 1 3 5 5.5; end;"
        OUTPUT.close()
        #
        # Create A with an error
        #
        self.model.A = Set(validate=lambda x,model:x<6)
        try:
          self.instance = self.model.create("setA.dat")
        except ValueError:
          self.fail("fail test_validation2")
        else:
          pass
        
    def test_other1(self):
        self.model.A = Set(initialize=[1,2,3,'A'], validate=lambda x,model:x in Integers)
        try:
          self.instance = self.model.create()
        except ValueError:
          pass
        else:
          self.fail("fail test_other1")

    def test_other2(self):
        self.model.A = Set(initialize=[1,2,3,'A'], within=Integers)
        try:
          self.instance = self.model.create()
        except ValueError:
          pass
        else:
          self.fail("fail test_other1")

    def test_other3(self):
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; param n := 5; end;"
        OUTPUT.close()
        def tmp_init(model):
            tmp=[]
            for i in range(0,value(model.n)):
              tmp.append(i/2.0)
            return tmp
        self.model.n = Param()
        self.model.A = Set(initialize=tmp_init, validate=lambda x,model:x in Integers)
        try:
          self.instance = self.model.create("setA.dat")
        except ValueError:
          pass
        else:
          self.fail("fail test_other1")

    def test_other4(self):
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; param n := 5; end;"
        OUTPUT.close()
        def tmp_init(model):
            tmp=[]
            for i in range(0,value(model.n)):
              tmp.append(i/2.0)
            return tmp
        self.model.n = Param()
        self.model.A = Set(initialize=tmp_init, within=Integers)
        try:
          self.instance = self.model.create("setA.dat")
        except ValueError:
          pass
        else:
          self.fail("fail test_other1")

    def tearDown(self):
        #
        # Remove Set 'A' data file
        #
        if os.path.exists("setA.dat"):
           os.remove("setA.dat")


class SetArgs2(PyomoModel):

    def setUp(self):
        #
        # Create Model
        #
        PyomoModel.setUp(self)

    def test_initialize(self):
        #
        # Create Set 'A' data file
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; set Z := A C; set A[A] := 1 3 5 7; end;"
        OUTPUT.close()
        #
        # Create model instance
        #
        self.model.Z = Set()
        self.model.A = Set(self.model.Z, initialize={'A':[1,2,3,'A']})
        self.instance = self.model.create()
        self.failUnlessEqual(len(self.instance.A['A']),4)
        
    def test_dimen(self):
        #
        # Create model instance
        #
        self.model.Z = Set(initialize=[1,2])
        self.model.A = Set(self.model.Z, initialize=[1,2,3], dimen=1)
        self.instance = self.model.create()
        try:
          self.model.A = Set(self.model.Z, initialize=[4,5,6], dimen=2)
          self.instance = self.model.create()
        except ValueError:
          pass
        else:
          self.fail("test_dimen")
        self.model.A = Set(self.model.Z, initialize=[(1,2), (2,3), (3,4)], dimen=2)
        self.instance = self.model.create()
        try:
          self.model.A = Set(self.model.Z, initialize=[(1,2), (2,3), (3,4)], dimen=1)
          self.instance = self.model.create()
        except ValueError:
          pass
        else:
          self.fail("test_dimen")
          
    def test_rule(self):
        #
        # Create Set 'A' data file
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; param n := 5; set Z := A C; end;"
        OUTPUT.close()
        def tmp_init(i,model):
            return range(0,value(model.n))
        self.model.n = Param()
        self.model.Z = Set()
        self.model.A = Set(self.model.Z, initialize=tmp_init)
        self.instance = self.model.create("setA.dat")
        self.failUnlessEqual(len(self.instance.A['A']),5)
          
    def test_rule2(self):
        #
        # Create Set 'A' data file
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; param n := 5; set Z := A C; end;"
        OUTPUT.close()
        def tmp_init(i,z,model):
            if z==value(model.n):
               return None
            return z
        self.model.n = Param()
        self.model.Z = Set()
        self.model.A = Set(self.model.Z, initialize=tmp_init)
        self.instance = self.model.create("setA.dat")
        self.failUnlessEqual(len(self.instance.A['A']),5)
          
    def test_within1(self):
        #
        # Create Set 'A' data file
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; set Z := A C; set A[A] := 1 3 5 7.5; end;"
        OUTPUT.close()
        #
        # Create A with an error
        #
        self.model.Z = Set()
        self.model.A = Set(self.model.Z, within=Integers)
        try:
          self.instance = self.model.create("setA.dat")
        except ValueError:
          pass
        else:
          self.fail("fail test_within1")
        
    def test_within2(self):
        #
        # Create Set 'A' data file
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; set Z := A C; set A[A] := 1 3 5 7.5; end;"
        OUTPUT.close()
        #
        # Create A with an error
        #
        self.model.Z = Set()
        self.model.A = Set(self.model.Z, within=Reals)
        try:
          self.instance = self.model.create("setA.dat")
        except ValueError:
          self.fail("fail test_within2")
        else:
          pass
        
    def test_validation1(self):
        #
        # Create Set 'A' data file
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; set Z := A C; set A[A] := 1 3 5 7.5; end;"
        OUTPUT.close()
        #
        # Create A with an error
        #
        self.model.Z = Set()
        self.model.A = Set(self.model.Z, validate=lambda x,model:x<6)
        try:
          self.instance = self.model.create("setA.dat")
        except ValueError:
          pass
        else:
          self.fail("fail test_within1")
        
    def test_validation2(self):
        #
        # Create Set 'A' data file
        #
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; set Z := A C; set A[A] := 1 3 5 5.5; end;"
        OUTPUT.close()
        #
        # Create A with an error
        #
        self.model.Z = Set()
        self.model.A = Set(self.model.Z, validate=lambda x,model:x<6)
        try:
          self.instance = self.model.create("setA.dat")
        except ValueError:
          self.fail("fail test_within2")
        else:
          pass
        
    def test_other1(self):
        self.model.Z = Set(initialize=['A'])
        self.model.A = Set(self.model.Z, initialize={'A':[1,2,3,'A']}, validate=lambda x,model:x in Integers)
        try:
          self.instance = self.model.create()
        except ValueError:
          pass
        else:
          self.fail("fail test_other1")

    def test_other2(self):
        self.model.Z = Set(initialize=['A'])
        self.model.A = Set(self.model.Z, initialize={'A':[1,2,3,'A']}, within=Integers)
        try:
          self.instance = self.model.create()
        except ValueError:
          pass
        else:
          self.fail("fail test_other1")

    def test_other3(self):
        def tmp_init(i,model):
            tmp=[]
            for i in range(0,value(model.n)):
              tmp.append(i/2.0)
            return tmp
        self.model.n = Param(initialize=5)
        self.model.Z = Set(initialize=['A'])
        self.model.A = Set(self.model.Z,initialize=tmp_init, validate=lambda x,model:x in Integers)
        try:
          self.instance = self.model.create()
        except ValueError:
          pass
        else:
          self.fail("fail test_other1")

    def test_other4(self):
        def tmp_init(i,model):
            tmp=[]
            for i in range(0,value(model.n)):
              tmp.append(i/2.0)
            return tmp
        self.model.n = Param(initialize=5)
        self.model.Z = Set(initialize=['A'])
        self.model.A = Set(self.model.Z, initialize=tmp_init, within=Integers)
        self.model.B = Set(self.model.Z, rule=tmp_init, within=Integers)
        try:
          self.instance = self.model.create()
        except ValueError:
          pass
        else:
          self.fail("fail test_other1")

    def tearDown(self):
        #
        # Remove Set 'A' data file
        #
        if os.path.exists("setA.dat"):
           os.remove("setA.dat")


class Misc(PyomoModel):

    def setUp(self):
        #
        # Create Model
        #
        PyomoModel.setUp(self)
        #
        #
        #
        self.model.A = Set(initialize=[1,2,3])
        self.model.B = Set(initialize=['a','b','c'])
        self.model.C = Set(initialize=[4,5,6])

    def tearDown(self):
        #
        # Remove Set 'A' data file
        #
        if os.path.exists("setA.dat"):
           os.remove("setA.dat")

    def test_cross_set(self):
         self.model.C = self.model.A * self.model.B
         self.instance = self.model.create()
         self.failUnlessEqual(len(self.instance.C),9)

    def test_tricross_set(self):
         self.model.D = self.model.A * self.model.B * self.model.C
         self.instance = self.model.create()
         self.failUnlessEqual(len(self.instance.D),27)

    def test_virtual_cross_set(self):
         self.model.C = self.model.A * self.model.B
         self.model.C.virtual = True
         self.instance = self.model.create()
         self.failUnlessEqual(len(self.instance.C),9)
         self.failUnlessEqual(len(self.instance.C.value),0)
         tmp=[]
         for item in self.instance.C:
           tmp.append(item)
         self.failUnlessEqual(len(tmp),9)


class SetIO(PyomoModel):

    def setUp(self):
        #
        # Create Model
        #
        PyomoModel.setUp(self)

    def tearDown(self):
        #
        # Remove Set 'A' data file
        #
        if os.path.exists("setA.dat"):
           os.remove("setA.dat")

    def test_io1(self):
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; set A := A1 A2 A3; end;"
        OUTPUT.close()
        self.model.A = Set()
        self.instance = self.model.create("setA.dat")
        self.failUnlessEqual( len(self.instance.A), 3)

    def test_io2(self):
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data; set B := 1 2 3 4; end;"
        OUTPUT.close()
        self.model.B = Set()
        self.instance = self.model.create("setA.dat")
        self.failUnlessEqual( len(self.instance.B), 4)

    def test_io3(self):
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data;"
        print >>OUTPUT, "set A := A1 A2 A3;"
        print >>OUTPUT, "set B := 1 2 3 4;"
        print >>OUTPUT, "set C := (A1,1) (A2,2) (A3,3);"
        print >>OUTPUT, "end;"
        OUTPUT.close()
        self.model.A = Set()
        self.model.B = Set()
        self.model.C = self.model.A * self.model.B
        self.instance = self.model.create("setA.dat")
        self.failUnlessEqual( len(self.instance.C), 3)

    def test_io4(self):
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data;"
        print >>OUTPUT, "set A := A1 A2 A3;"
        print >>OUTPUT, "set B := 1 2 3 4;"
        print >>OUTPUT, "set D := (A1,1) (A2,2) (A3,3);"
        print >>OUTPUT, "end;"
        OUTPUT.close()
        self.model.A = Set()
        self.model.B = Set()
        self.model.D = Set(within=self.model.A*self.model.B)
        self.instance = self.model.create("setA.dat")
        self.failUnlessEqual( len(self.instance.D), 3)

    def test_io5(self):
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data;"
        print >>OUTPUT, "set A := A1 A2 A3;"
        print >>OUTPUT, "set B := 1 2 3 4;"
        print >>OUTPUT, "set D : A1 A2 A3 :="
        print >>OUTPUT, "    1   +  -  +"
        print >>OUTPUT, "    2   +  -  +"
        print >>OUTPUT, "    3   +  -  +"
        print >>OUTPUT, "    4   +  -  +;"
        print >>OUTPUT, "end;"
        OUTPUT.close()
        self.model.A = Set()
        self.model.B = Set()
        self.model.D = Set(within=self.model.A*self.model.B)
        self.instance = self.model.create("setA.dat")
        self.failUnlessEqual( len(self.instance.D), 8)

    def test_io6(self):
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data;"
        print >>OUTPUT, "set A := A1 A2 A3;"
        print >>OUTPUT, "set B := 1 2 3 4;"
        print >>OUTPUT, "set E :="
        print >>OUTPUT, "(A1,1,*) A1 A2"
        print >>OUTPUT, "(A2,2,*) A2 A3"
        print >>OUTPUT, "(A3,3,*) A1 A3 ;"
        print >>OUTPUT, "end;"
        OUTPUT.close()
        self.model.A = Set()
        self.model.B = Set()
        self.model.E = Set(within=self.model.A*self.model.B*self.model.A)
        self.instance = self.model.create("setA.dat")
        self.failUnlessEqual( len(self.instance.E), 6)

    def test_io7(self):
        OUTPUT=open("setA.dat","w")
        print >>OUTPUT, "data;"
        print >>OUTPUT, "set A := A1 A2 A3;"
        print >>OUTPUT, "set B := 1 2 3 4;"
        print >>OUTPUT, "set F[A1] := 1 3 5;"
        print >>OUTPUT, "set F[A2] := 2 4 6;"
        print >>OUTPUT, "set F[A3] := 3 5 7;"
        print >>OUTPUT, "end;"
        OUTPUT.close()
        self.model.A = Set()
        self.model.B = Set()
        self.model.F = Set(self.model.A)
        self.instance = self.model.create("setA.dat")
        self.failUnlessEqual( self.instance.F.dim(), 1)
        self.failUnlessEqual( len(self.instance.F.keys()), 3)
        self.failUnlessEqual( len(self.instance.F['A1']), 3)


def init_fn(model):
    return []

def tmp_constructor(index,ctr,model):
    if ctr == 10:
        return None
    else:
        return ctr

class SetErrors(PyomoModel):

    def test_membership(self):
        self.failUnlessEqual( 0 in Boolean, True)
        self.failUnlessEqual( 1 in Boolean, True)
        self.failUnlessEqual( True in Boolean, True)
        self.failUnlessEqual( False in Boolean, True)
        self.failUnlessEqual( 1.1 in Boolean, False)
        self.failUnlessEqual( 2 in Boolean, False)

        self.failUnlessEqual( 0 in Integers, True)
        self.failUnlessEqual( 1 in Integers, True)
        self.failUnlessEqual( True in Integers, False)
        self.failUnlessEqual( False in Integers, False)
        self.failUnlessEqual( 1.1 in Integers, False)
        self.failUnlessEqual( 2 in Integers, True)

        self.failUnlessEqual( 0 in Reals, True)
        self.failUnlessEqual( 1 in Reals, True)
        self.failUnlessEqual( True in Reals, False)
        self.failUnlessEqual( False in Reals, False)
        self.failUnlessEqual( 1.1 in Reals, True)
        self.failUnlessEqual( 2 in Reals, True)

        self.failUnlessEqual( 0 in Any, True)
        self.failUnlessEqual( 1 in Any, True)
        self.failUnlessEqual( True in Any, True)
        self.failUnlessEqual( False in Any, True)
        self.failUnlessEqual( 1.1 in Any, True)
        self.failUnlessEqual( 2 in Any, True)

    def test_setargs1(self):
        try:
            a=Set()
            c=Set(a,foo=None)
            self.fail("test_setargs1 - expected error because of bad argument")
        except ValueError:
            pass

    def test_setargs2(self):
        try:
            a=Set()
            b=Set(a)
            c=Set(within=b, dimen=2)
            self.fail("test_setargs1 - expected error because of bad argument")
        except ValueError:
            pass
        a=Set()
        b=Set(a)
        c=Set(within=b, dimen=1)
        self.failUnlessEqual(c.within,b)
        c.within = a
        self.failUnlessEqual(c.within,a)

    def test_setargs3(self):
        a=Set(dimen=2, initialize=(1,2))
        try:
            a=Set(dimen=1, initialize=(1,2))
            self.fail("test_setargs3 - expected error because of bad argument")
        except ValueError:
            pass

    def test_verify(self):
        a=Set(initialize=[1,2,3])
        b=Set(within=a)
        try:
            b._verify(4)
            self.fail("test_verify - bad value was expected")
        except ValueError:
            pass
        self.failUnlessEqual(b._verify(4,False),False)
        #
        c=Set()
        try:
            c._verify( (1,2) )
            self.fail("test_verify - bad value was expected")
        except ValueError:
            pass
        self.failUnlessEqual(c._verify( (1,2),False),False)
        #
        c=Set(dimen=2)
        try:
            c._verify( (1,2,3) )
            self.fail("test_verify - bad value was expected")
        except ValueError:
            pass
        self.failUnlessEqual(c._verify( (1,2,3),False),False)

    def test_construct(self):
        a = Set(initialize={})
        try:
            a.construct()
            self.fail("test_construct - expected failure constructing with a dictionary")
        except ValueError:
            pass
        #
        a = Set(initialize=init_fn)
        try:
            a.construct()
            self.fail("test_construct - expected exception due to None model")
        except ValueError:
            pass

    def test_add(self):
        a=Set()
        a.add(1)
        a.add("a")
        try:
            a.add({})
            self.fail("test_add - expected type error because {} is unhashable")
        except:
            pass

    def test_getitem(self):
        a=Set(initialize=[1,2])
        try:
            a[0]
            self.fail("test_getitem - cannot index an unordered set")
        except IndexError:
            pass

    def test_eq(self):
        a=Set(dimen=1,name="a",initialize=[1,2])
        a.construct()
        b=Set(dimen=2)
        self.failUnlessEqual(a==b,False)
        self.failUnless(not a.__eq__(Boolean))
        self.failUnless(not Boolean.__eq__(a))

    def test_neq(self):
        a=Set(dimen=1,initialize=[1,2])
        a.construct()
        b=Set(dimen=2)
        self.failUnlessEqual(a!=b,True)
        self.failUnless(a.__ne__(Boolean))
        self.failUnless(Boolean.__ne__(a))

    def test_contains(self):
        a=Set(initialize=[1,3,5,7])
        a.construct()
        b=Set(initialize=[1,3])
        b.construct()
        self.failUnlessEqual(b in a, True)
        self.failUnlessEqual(a in b, False)
        self.failUnlessEqual(1 in Integers, True)
        self.failUnlessEqual(1 in NonNegativeIntegers, True)

    def test_subset(self):
        try:
            Integers in Reals
            #Integers.issubset(Reals)
            self.fail("test_subset - expected TypeError")
        except TypeError:
            pass
        try:
            a=Set(dimen=1)
            b=Set(dimen=2)
            a in b
            self.fail("test_subset - expected ValueError")
        except ValueError:
            pass

    def test_superset(self):
        try:
            Reals >= Integers
            #Integers.issubset(Reals)
            self.fail("test_subset - expected TypeError")
        except TypeError:
            pass
        a=Set(initialize=[1,3,5,7])
        a.construct()
        b=Set(initialize=[1,3])
        b.construct()
        self.failUnlessEqual(Reals >= b, True)
        self.failUnlessEqual(Reals >= [1,3,7], True)
        self.failUnlessEqual(Reals >= [1,3,7,"a"], False)
        self.failUnlessEqual(a >= b, True)

    def test_lt(self):
        try:
            Integers < Reals
            self.fail("test_subset - expected TypeError")
        except TypeError:
            pass
        a=Set(initialize=[1,3,5,7])
        a.construct()
        try:
            a < Reals
            self.fail("test_subset - expected TypeError")
        except TypeError:
            pass
        b=Set(initialize=[1,3,5])
        b.construct()
        self.failUnlessEqual(a<a,False)
        self.failUnlessEqual(b<a,True)
        c=Set(initialize=[(1,2)])
        c.construct()
        try:
            a<c
            self.fail("test_subset - expected ValueError")
        except ValueError:
            pass

    def test_gt(self):
        a=Set(initialize=[1,3,5,7])
        a.construct()
        c=Set(initialize=[(1,2)])
        c.construct()
        try:
            a>c
            self.fail("test_subset - expected ValueError")
        except ValueError:
            pass
            
    def test_or(self):
        a=Set(initialize=[1,2,3])
        c=Set(initialize=[(1,2)])
        c.construct()
        try:
            Reals | Integers
            self.fail("test_or - expected TypeError")
        except TypeError:
            pass
        try:
            a | Integers
            self.fail("test_or - expected TypeError")
        except TypeError:
            pass
        try:
            a | c
            self.fail("test_or - expected ValueError")
        except ValueError:
            pass

    def test_and(self):
        a=Set(initialize=[1,2,3])
        c=Set(initialize=[(1,2)])
        c.construct()
        try:
            Reals & Integers
            self.fail("test_and - expected TypeError")
        except TypeError:
            pass
        try:
            a & Integers
            self.fail("test_and - expected TypeError")
        except TypeError:
            pass
        try:
            a & c
            self.fail("test_and - expected ValueError")
        except ValueError:
            pass

    def test_xor(self):
        a=Set(initialize=[1,2,3])
        c=Set(initialize=[(1,2)])
        c.construct()
        try:
            Reals ^ Integers
            self.fail("test_xor - expected TypeError")
        except TypeError:
            pass
        try:
            a ^ Integers
            self.fail("test_xor - expected TypeError")
        except TypeError:
            pass
        try:
            a ^ c
            self.fail("test_xor - expected ValueError")
        except ValueError:
            pass

    def test_sub(self):
        a=Set(initialize=[1,2,3])
        c=Set(initialize=[(1,2)])
        c.construct()
        try:
            Reals - Integers
            self.fail("test_sub - expected TypeError")
        except TypeError:
            pass
        try:
            a - Integers
            self.fail("test_sub - expected TypeError")
        except TypeError:
            pass
        try:
            a - c
            self.fail("test_sub - expected ValueError")
        except ValueError:
            pass

    def test_mul(self):
        a=Set(initialize=[1,2,3])
        c=Set(initialize=[(1,2)])
        c.construct()
        try:
            Reals * Integers
            self.fail("test_mul - expected TypeError")
        except TypeError:
            pass
        try:
            a * Integers
            self.fail("test_mul - expected TypeError")
        except TypeError:
            pass
        try:
            a * 1
            self.fail("test_mul - expected TypeError")
        except TypeError:
            pass
        b = a * c

    def test_arrayset_construct(self):
        a=Set(initialize=[1,2,3])
        a.construct()
        b=Set(a, initialize=tmp_constructor)
        try:
            b.construct({4:None})
            self.fail("test_arrayset_construct - expected KeyError")
        except KeyError:
            pass
        try:
            b.construct()
            self.fail("test_arrayset_construct - expected ValueError")
        except ValueError:
            pass
        b=Set(a,a, initialize=tmp_constructor)
        for i in b:
          self.failUnlessEqual(i in a, True)
        try:
            b.construct()
            self.fail("test_arrayset_construct - expected ValueError")
        except ValueError:
            pass

    def test_prodset(self):
        a=Set(initialize=[1,2])
        a.construct()
        b=Set(initialize=[6,7])
        b.construct()
        c=a*b
        c.construct()
        self.failUnlessEqual((6,2) in c, False)
        c=coopr.pyomo.base.sets._ProductSet(a,b)
        c.virtual=True
        self.failUnlessEqual((6,2) in c, False)
        self.failUnlessEqual((1,7) in c, True)
        c=coopr.pyomo.base.sets._ProductSet()
        c.virtual=True
        c.construct()
        c=coopr.pyomo.base.sets._ProductSet(a,b,initialize={(1,7):None,(2,6):None})
        c.construct()
        c=coopr.pyomo.base.sets._ProductSet(a,b,initialize=(1,7))
        c.construct()

def virt_constructor(y,model):
    return RealSet(validate=lambda x,model: x>y)

class ArraySetVirtual(unittest.TestCase):

    def test_construct(self):
        a=Set(initialize=[1,2,3])
        a.construct()
        b=Set(a, initialize=virt_constructor)
        #b.construct()

if __name__ == "__main__":
   unittest.main()

