source: trunk/test/core/base/test_soln.py @ 1657

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

Removing coopr.opt-depricated, and
renaming coopr.core to coopr.opt.

Unfortunately, plugins are still not being properly loaded, so
some of the tests are failing.

File size: 5.0 KB
Line 
1#
2# Unit Tests for coopr.opt.base.solution
3#
4#
5
6import os
7import sys
8from os.path import abspath, dirname
9sys.path.insert(0, dirname(dirname(abspath(__file__)))+"/../..")
10cooprdir = dirname(abspath(__file__))+os.sep+".."+os.sep+".."+os.sep
11currdir = dirname(abspath(__file__))+os.sep
12
13import unittest
14from nose.tools import nottest
15import coopr.opt
16import coopr
17import pyutilib_th
18import xml
19import pyutilib
20
21class OptSolnDebug(pyutilib_th.TestCase):
22
23    def setUp(self):
24        pyutilib.TempfileManager.tempdir = currdir
25        self.results = coopr.opt.SolverResults()
26        self.soln = self.results.solution.create()
27
28    def tmp(self):
29        self.soln.variable[0]=0.0
30        self.soln.variable[1]=0.0
31        self.soln.variable[3]=0.0
32        pyutilib.TempfileManager.clear_tempfiles()
33
34    def tearDown(self):
35        pass
36
37    def test_write_solution1(self):
38        """ Write a SolverResults Object with solutions """
39        self.results.write(filename=currdir+"write_solution1.txt")
40        if not os.path.exists(currdir+"write_solution1.txt"):
41           self.fail("test_write_solution - failed to write write_solution1.txt")
42        self.failUnlessFileEqualsBaseline(currdir+"write_solution1.txt", currdir+"test1_soln.txt")
43
44    def test_write_solution2(self):
45        """ Write a SolverResults Object without solutions """
46        self.results.write(num=None,filename=currdir+"write_solution2.txt")
47        if not os.path.exists(currdir+"write_solution2.txt"):
48           self.fail("test_write_solution - failed to write write_solution2.txt")
49        self.failUnlessFileEqualsBaseline(currdir+"write_solution2.txt", currdir+"test2_soln.txt")
50
51    def test_delete_solution(self):
52        """ Delete a solution from a SolverResults object """
53        self.results.solution.delete(0)
54        self.results.write(filename=currdir+"delete_solution.txt")
55        if not os.path.exists(currdir+"delete_solution.txt"):
56           self.fail("test_write_solution - failed to write delete_solution.txt")
57        self.failUnlessFileEqualsBaseline(currdir+"delete_solution.txt", currdir+"test4_soln.txt")
58
59    def test_get_solution(self):
60        """ Get a solution from a SolverResults object """
61        tmp = self.results.solution[0]
62        self.failUnlessEqual(tmp,self.soln)
63
64    def test_get_solution_attr_error(self):
65        """ Create an error with a solution suffix """
66        try:
67           tmp = self.soln.bad
68           self.fail("Expected attribute error failure for 'bad'")
69        except AttributeError:
70           pass
71
72    def test_set_solution_attr_error(self):
73        """ Create an error with a solution suffix """
74        try:
75           self.soln.variable = True
76           self.fail("Expected attribute error failure for 'var'")
77        except AttributeError:
78           pass
79
80    def test_soln_pprint1(self):
81        """ Write a solution with only zero values, using the results 'write()' method """
82        self.soln.variable.add(0,0.0)
83        self.soln.variable.add(1,0.0)
84        self.soln.variable.add(3,0.0)
85        self.results.write(filename=currdir+"soln_pprint.txt")
86        if not os.path.exists(currdir+"soln_pprint.txt"):
87           self.fail("test_write_solution - failed to write soln_pprint.txt")
88        self.failUnlessFileEqualsBaseline(currdir+"soln_pprint.txt", currdir+"test3_soln.txt")
89
90    def test_soln_pprint2(self):
91        """ Write a solution with only zero values, using the Solution.pprint() method """
92        self.soln.variable.add(0,0.0)
93        self.soln.variable.add(1,0.0)
94        self.soln.variable.add(3,0.0)
95        pyutilib.setup_redirect(currdir+"soln_pprint2.out")
96        self.soln.pprint()
97        self.soln._variable.pprint()
98        pyutilib.reset_redirect()
99        self.failUnlessFileEqualsBaseline(currdir+"soln_pprint2.out", currdir+"soln_pprint2.txt")
100
101    def test_soln_suffix_iter(self):
102        """ Test a suffix iterator """
103        self.soln.variable.add(0,0.0)
104        self.soln.variable.add(1,0.1)
105        self.soln.variable.add(3,0.3)
106        i=0
107        for val in self.soln.variable:
108          i=i+1
109          self.failUnlessEqual(val[1], val[0]/10.0)
110        self.failUnlessEqual(i,len(self.soln.variable))
111
112    def test_soln_suffix_getiter(self):
113        self.soln.variable.add("x0",0.0)
114        self.soln.variable.add(1,0.1)
115        self.soln.variable.add("x3",0.3)
116        #print "HERE", self.soln.variable._index.keys()
117        self.failUnlessEqual(self.soln.variable["x3"],0.3)
118        self.failUnlessEqual(self.soln.variable[1],0.1)
119
120    def test_soln_suffix_setattr(self):
121        self.soln.variable.add("x(0)",0.0)
122        self.soln.variable.add("x3",0.3)
123        self.soln.variable.add("y[1,ab]",0.5)
124        #self.soln.pprint()
125        #print "HERE", self.soln.variable._index.keys()
126        self.failUnlessEqual(self.soln.variable.x.keys(),[0])
127        self.failUnlessEqual(self.soln.variable.x[0],0.0)
128        self.failUnlessEqual(self.soln.variable.x3,0.3)
129        self.failUnlessEqual(self.soln.variable.y.keys(),[(1,'ab')])
130        self.failUnlessEqual(self.soln.variable.y[1,'ab'],0.5)
131
132
133
134if __name__ == "__main__":
135   unittest.main()
Note: See TracBrowser for help on using the repository browser.