source: trunk/test/sucasa/unit/test_AmplInfo.py @ 1768

Last change on this file since 1768 was 1768, checked in by wehart, 11 years ago

Rework of Coopr to use the new PyUtilib? package decomposition.

NOTE: to use Coopr with this update, we need to work with a new version of coopr_install.

File size: 6.1 KB
Line 
1#
2# Unit Tests for ampl_info
3#
4
5
6import os
7import sys
8from os.path import abspath, dirname
9sys.path.insert(0, dirname(dirname(abspath(__file__)))+os.sep+".."+os.sep+"..")
10currdir = dirname(abspath(__file__))+os.sep
11
12import unittest
13import pyutilib.misc
14import pyutilib.th
15from nose.tools import nottest
16from coopr.sucasa.ampl_info import AmplInfo
17from coopr.sucasa.symb_info import MILPSymbInfo
18
19class AmplInfoTester(pyutilib.th.TestCase):
20
21    def setUp(self):
22        self.info = AmplInfo()
23
24    def test_add(self):
25        """Check that add inserts the appropriate information"""
26        self.info.add('con','f',['a'])
27        self.info.add('param','b',['a'])
28        self.info.add('var','c',['a','a'])
29        self.info.add('min','d',['a'])
30        self.info.add('set','a',[])
31        self.info.add('max','e',['a','a'])
32        tmp = str(self.info)
33        target = "con f ['a'] None\nparam b ['a'] None\nvar c ['a', 'a'] None\nmin d ['a'] None\nset a [] None\nmax e ['a', 'a'] None"
34        self.failUnlessEqual(tmp,target)
35        #
36        # Check order of the insertions
37        #
38        tmp=[]
39        for i in self.info:
40            tmp.append(i[1])
41        self.failUnlessEqual(tmp,['f','b','c','d','a','e'])
42
43    def test_initialize1(self):
44        """ Check that initialize correctly fills a SymbInfo object"""
45        self.info.add('con','f',['a'])
46        self.info.add('param','b',['a'])
47        self.info.add('var','c',['a','a'])
48        self.info.add('min','d',['a'])
49        self.info.add('set','a1',[])
50        self.info.add('set','a2',[],superset=['a1','a1'], dimen=2)
51        self.info.add('max','e',['a','a'])
52        #
53        # Initialize a SymbInfo object
54        #
55        sinfo = MILPSymbInfo()
56        self.info.initialize(sinfo,quiet=True)
57        pyutilib.misc.setup_redirect(currdir+"initialize1.out")
58        print str(sinfo)
59        pyutilib.misc.reset_redirect()
60        self.failUnlessFileEqualsBaseline(currdir+"initialize1.out",currdir+"initialize1.txt")
61
62    def test_initialize2(self):
63        """ Check that initialize correctly fills a SymbInfo object, when all symbols are exported"""
64        self.info.add('con','f',['a'])
65        self.info.add('param','b',['a'])
66        self.info.add('var','c',['a','a'])
67        self.info.add('min','d',['a'])
68        self.info.add('set','a1',[])
69        self.info.add('set','a2',[],superset=['a1','a1'], dimen=2)
70        self.info.add('max','e',['a','a'])
71        self.info.exported_symbols.add("*")
72        #
73        # Initialize a SymbInfo object
74        #
75        sinfo = MILPSymbInfo()
76        self.info.initialize(sinfo,quiet=True)
77        pyutilib.misc.setup_redirect(currdir+"initialize2.out")
78        print str(sinfo)
79        pyutilib.misc.reset_redirect()
80        self.failUnlessFileEqualsBaseline(currdir+"initialize2.out",currdir+"initialize2.txt")
81
82    def test_initialize3(self):
83        """ Check that initialize correctly fills a SymbInfo object, when symbols are defined by SUCASA declarations"""
84        self.info.add_mapfile_declaration("a","set a within literals dimen 1")
85        self.info.add('set','a',[])
86        self.info.add('con','f',['a'])
87        self.info.add_mapfile_declaration("b","param b[a] in literals")
88        self.info.add('param','b',['a'])
89        self.info.add('var','c',['a','a'])
90        self.info.add('min','d',['a'])
91        self.info.add_mapfile_declaration("a2","set a2 within literals cross a dimen 2")
92        self.info.add('set','a2',[],superset=['a1','a1'], dimen=2)
93        self.info.add('max','e',['a','a'])
94        self.info.exported_symbols.add("a")
95        self.info.exported_symbols.add("b")
96        self.info.exported_symbols.add("a2")
97        #
98        # Initialize a SymbInfo object
99        #
100        sinfo = MILPSymbInfo()
101        self.info.initialize(sinfo,quiet=True)
102        pyutilib.misc.setup_redirect(currdir+"initialize3.out")
103        print str(sinfo)
104        pyutilib.misc.reset_redirect()
105        self.failUnlessFileEqualsBaseline(currdir+"initialize3.out",currdir+"initialize3.txt")
106
107    def test_initialize_err1(self):
108        """ Check that initialize correctly identifies an invalid exported symbol"""
109        self.info.add('con','f',['a'])
110        self.info.add('param','b',['a'])
111        self.info.add('var','c',['a','a'])
112        self.info.add('min','d',['a'])
113        self.info.add('set','a1',[])
114        self.info.add('set','a2',[],superset=['a1','a1'], dimen=2)
115        self.info.add('max','e',['a','a'])
116        self.info.exported_symbols.add("a1")
117        self.info.exported_symbols.add("b1")
118        #
119        # Initialize a SymbInfo object
120        #
121        sinfo = MILPSymbInfo()
122        try:
123            pyutilib.misc.setup_redirect(currdir+"initialize_err1.out")
124            self.info.initialize(sinfo,quiet=True)
125            pyutilib.misc.reset_redirect()
126            self.fail("Expected IOError")
127        except IOError:
128            pyutilib.misc.reset_redirect()
129            os.remove(currdir+"initialize_err1.out")
130
131    def test_check(self):
132        self.info.add('set','a',[])
133        self.info.add('set','a1',[])
134        self.info.add('set','a2',[],superset=['a1','a1'], dimen=2)
135        self.info.add('param','b',['a'])
136        self.info.add('var','c',['a','a'])
137        self.info.add('min','d',['a'])
138        self.info.add('max','e',['a','a'])
139        self.info.add('con','f',['a'])
140        self.info.exported_symbols.add("a")
141        self.info.exported_symbols.add("a1")
142        self.info.exported_symbols.add("a2")
143        self.info.exported_symbols.add("b")
144        sinfo = MILPSymbInfo()
145        self.info.initialize(sinfo,quiet=True)
146        self.info.check(sinfo)
147        #
148        # Create errors
149        #
150        sinfo.add_symbol('param','SXb')
151        sinfo.add_symbol('set','Xa')
152        sinfo.add_symbol('param','Xb')
153        sinfo.add_symbol('var','Xc')
154        sinfo.add_symbol('min','Xd')
155        sinfo.add_symbol('max','Xe')
156        sinfo.add_symbol('con','Xf')
157        pyutilib.misc.setup_redirect(currdir+"check.out")
158        self.info.check(sinfo)
159        pyutilib.misc.reset_redirect()
160        self.failUnlessFileEqualsBaseline(currdir+"check.out",currdir+"check.txt")
161       
162if __name__ == "__main__":
163   unittest.main()
Note: See TracBrowser for help on using the repository browser.