source: trunk/test/sucasa/unit/test_symb_info.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: 4.5 KB
Line 
1#
2# Unit Tests for MILPSymbInfo
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
13from nose.tools import nottest
14from coopr.sucasa import parse_mapfile,MILPSymbInfo
15import pyutilib.misc
16import pyutilib.th
17
18class ParseMapFileTester(pyutilib.th.TestCase):
19
20    def setUp(self):
21        self.info = MILPSymbInfo()
22
23    def tearDown(self):
24        if os.path.exists(currdir+"parsetab.py"):
25           os.remove(currdir+"parsetab.py")
26        if os.path.exists(currdir+"parsetab.pyc"):
27           os.remove(currdir+"parsetab.pyc")
28        del self.info
29
30    def test_parse1(self):
31        """Parser empty string"""
32        ans = parse_mapfile(self.info, "")
33        tmp = str(ans)
34        self.failUnlessEqual(tmp,"")
35
36    def test_parse2(self):
37        """Parser None"""
38        ans = parse_mapfile(self.info, None)
39        self.failUnlessEqual(ans,None)
40
41    def test_parse3(self):
42        """Parser sucasa1.map"""
43        ans = parse_mapfile(self.info, filename=currdir+"sucasa1.map")
44        pyutilib.misc.setup_redirect(currdir+"sucasa1.out")
45        ans.print_symbols()
46        pyutilib.misc.reset_redirect()
47        self.failUnlessFileEqualsBaseline(currdir+"sucasa1.out",currdir+"sucasa1.txt")
48
49    def test_mapfile_io(self):
50        """Read/write mapfile correctly"""
51        ans = parse_mapfile(self.info, filename=currdir+"sucasa1.map")
52        ans.write_mapfile(currdir+"tmp.map",quiet=True)
53        self.failUnlessFileEqualsBaseline(currdir+"tmp.map",currdir+"sucasa1.map")
54        self.info = MILPSymbInfo(currdir+"tmp")
55        self.info.read_mapfile(currdir+"sucasa1.map")
56        self.info.write_mapfile(quiet=True)
57        self.failUnlessFileEqualsBaseline(currdir+"tmp.map",currdir+"sucasa1.map")
58        self.info = MILPSymbInfo(currdir+"sucasa1")
59        self.info.read_mapfile()
60        self.info.write_mapfile(currdir+"tmp.map",quiet=True)
61        self.failUnlessFileEqualsBaseline(currdir+"tmp.map",currdir+"sucasa1.map")
62
63    def test_add1(self):
64        """Add symbols directly, and verify that they are created"""
65        self.info.add_symbol("set","a",dimen=0)
66        self.info.add_symbol("set","b",index=['A','B'],tmpsets=True)
67        self.info.add_symbol("set","c",index=['x in W','B'],tmpsets=True,quiet=True)
68        self.info.add_symbol("set","d",index=['x in W'],superset=["a","b"],tmpsets=True,dimen=2)
69        self.info.write_mapfile(currdir+"add1.out",quiet=True)
70        self.failUnlessFileEqualsBaseline(currdir+"add1.out",currdir+"add1.map")
71
72    def test_generate(self):
73        """Generate PICO wrapper code"""
74        self.info.name = currdir+"generate"
75        self.info.read_mapfile(filename=currdir+"sucasa1.map")
76        self.info.generate_milp_symbol_code()
77        os.remove(currdir+"generate_info.h")
78        os.remove(currdir+"generate_info.cpp")
79
80    def test_error1(self):
81        """Parse empty expression"""
82        try:
83            ans = parse_mapfile(self.info,";")
84            self.fail("test_error1")
85        except IOError:
86            pass
87
88    def test_error2(self):
89        """Parse duplicate declarations"""
90        try:
91            pyutilib.misc.setup_redirect(currdir+"error2.out")
92            ans = parse_mapfile(self.info,"set a within integers dimen 1;\nset a within integers dimen 1;")
93            self.fail("test_error2")
94        except IOError:
95            pass
96        pyutilib.misc.reset_redirect()
97        os.remove(currdir+"error2.out")
98
99    def test_error3(self):
100        """Read non-existent mapfile"""
101        try:
102            self.info.read_mapfile("unknown.map")
103            self.fail("test_error3")
104        except IOError:
105            pass
106
107    def test_error4(self):
108        """Verify that an error occurs if you add an unknown index"""
109        try:
110            self.info.add_symbol("set","a",index=['A'],dimen=0,quiet=True)
111            self.fail("test_error4")
112        except IOError:
113            pass
114
115    def test_error5(self):
116        """Verify that an error occurs if you specify a bad superset"""
117        try:
118            self.info.add_symbol("set","a",superset="A")
119            self.fail("test_error5")
120        except IOError:
121            pass
122
123    def test_error6(self):
124        """Verify that an error occurs if you specify a bad superset list"""
125        try:
126            self.info.add_symbol("set","a",superset=["A","B"])
127            self.fail("test_error6")
128        except IOError:
129            pass
130
131if __name__ == "__main__":
132   unittest.main()
Note: See TracBrowser for help on using the repository browser.