From: <kk...@us...> - 2011-02-08 22:19:29
|
Revision: 116 http://python-control.svn.sourceforge.net/python-control/?rev=116&view=rev Author: kkchen Date: 2011-02-08 22:19:23 +0000 (Tue, 08 Feb 2011) Log Message: ----------- Upgraded the testing further to avoid manually updates I changed some things in test.py and the individual tests. In test.py, in no circumstance do you need to add the test modules manually. It finds them itself based on their name. I made some of the tests more unittest-oriented as well but a lot still to be done on that. bb...@ra... Modified Paths: -------------- branches/control-0.4a/src/TestConvert.py branches/control-0.4a/src/TestFreqRsp.py branches/control-0.4a/src/TestModelsimp.py branches/control-0.4a/src/TestStatefbk.py branches/control-0.4a/src/test.py Modified: branches/control-0.4a/src/TestConvert.py =================================================================== --- branches/control-0.4a/src/TestConvert.py 2011-02-08 22:19:18 UTC (rev 115) +++ branches/control-0.4a/src/TestConvert.py 2011-02-08 22:19:23 UTC (rev 116) @@ -25,13 +25,13 @@ """Set up testing parameters.""" # Number of times to run each of the randomized tests. - self.numTests = 10 + self.numTests = 10 #almost guarantees failure # Maximum number of states to test + 1 - self.maxStates = 3 + self.maxStates = 20 # Maximum number of inputs and outputs to test + 1 - self.maxIO = 3 + self.maxIO = 20 # Set to True to print systems to the output. - self.debug = True + self.debug = False def printSys(self, sys, ind): """Print system to the standard output.""" @@ -42,23 +42,47 @@ def testConvert(self): """Test state space to transfer function conversion.""" + #Currently it only tests that a TF->SS->TF generates an unchanged TF - print __doc__ + #print __doc__ for states in range(1, self.maxStates): for inputs in range(1, self.maxIO): for outputs in range(1, self.maxIO): - sys1 = matlab.rss(states, inputs, outputs) - self.printSys(sys1, 1) + #start with a random SS system and transform to TF + #then back to SS, check that the matrices are the same. + ssOriginal = matlab.rss(states, inputs, outputs) + self.printSys(ssOriginal, 1) - sys2 = matlab.tf(sys1) - self.printSys(sys2, 2) - - sys3 = matlab.ss(sys2) - self.printSys(sys3, 3) + tfOriginal = matlab.tf(ssOriginal) + self.printSys(tfOriginal, 2) + + ssTransformed = matlab.ss(tfOriginal) + self.printSys(ssTransformed, 3) - sys4 = matlab.tf(sys3) - self.printSys(sys4, 4) + tfTransformed = matlab.tf(ssTransformed) + self.printSys(tfTransformed, 4) + + for inputNum in range(inputs): + for outputNum in range(outputs): + np.testing.assert_array_almost_equal(\ + tfOriginal.num[outputNum][inputNum],\ + tfTransformed.num[outputNum][inputNum]) + + np.testing.assert_array_almost_equal(\ + tfOriginal.den[outputNum][inputNum],\ + tfTransformed.den[outputNum][inputNum]) + + #To test the ss systems is harder because they aren't the same + #realization. This could be done with checking that they have the + #same freq response with bode, but apparently it doesn't work + #the way it should right now: + ## Bode should work like this: + #[mag,phase,freq]=bode(sys) + #it doesn't seem to...... + #This should be added. + + def suite(): return unittest.TestLoader().loadTestsFromTestCase(TestConvert) Modified: branches/control-0.4a/src/TestFreqRsp.py =================================================================== --- branches/control-0.4a/src/TestFreqRsp.py 2011-02-08 22:19:18 UTC (rev 115) +++ branches/control-0.4a/src/TestFreqRsp.py 2011-02-08 22:19:23 UTC (rev 116) @@ -56,5 +56,8 @@ #plt.figure(4) #bode(sysMIMO,omega) + def suite(): - return unittest.TestLoader().loadTestsFromTestCase(TestConvert) + pass + #Uncomment this once it is a real unittest + #return unittest.TestLoader().loadTestsFromTestCase(TestFreqRsp) Modified: branches/control-0.4a/src/TestModelsimp.py =================================================================== --- branches/control-0.4a/src/TestModelsimp.py 2011-02-08 22:19:18 UTC (rev 115) +++ branches/control-0.4a/src/TestModelsimp.py 2011-02-08 22:19:23 UTC (rev 116) @@ -88,6 +88,8 @@ np.testing.assert_array_almost_equal(rsys.C, Crtrue,decimal=4) np.testing.assert_array_almost_equal(rsys.D, Drtrue,decimal=4) +def suite(): + return unittest.TestLoader().loadTestsFromTestCase(TestModelsimp) if __name__ == '__main__': Modified: branches/control-0.4a/src/TestStatefbk.py =================================================================== --- branches/control-0.4a/src/TestStatefbk.py 2011-02-08 22:19:18 UTC (rev 115) +++ branches/control-0.4a/src/TestStatefbk.py 2011-02-08 22:19:23 UTC (rev 116) @@ -80,5 +80,9 @@ self.assertRaises(ValueError, gram, sys, 'o') self.assertRaises(ValueError, gram, sys, 'c') +def suite(): + return unittest.TestLoader().loadTestsFromTestCase(TestStatefbk) + + if __name__ == '__main__': unittest.main() Modified: branches/control-0.4a/src/test.py =================================================================== --- branches/control-0.4a/src/test.py 2011-02-08 22:19:18 UTC (rev 115) +++ branches/control-0.4a/src/test.py 2011-02-08 22:19:23 UTC (rev 116) @@ -17,6 +17,45 @@ print SP.Popen(['./'+test],stdout=SP.PIPE).communicate()[0] print 'Completed',test + +############################################################################### + +def getFileList(dir,fileExtension=''): + """ Finds all files in the given directory that have the given file extension""" + filesRaw = SP.Popen(['ls',dir],stdout=SP.PIPE).communicate()[0] + #files separated by endlines + filename= '' + fileList=[] + #print 'filesRaw is ',filesRaw + for c in filesRaw: + if c!='\n': + filename+=c + else: #completed file name + if fileExtension != '' and filename[-len(fileExtension):] == fileExtension: + fileList.append(filename) + else: + pass #fileList.append(dir+filename) + filename='' + return fileList + +############################################################################### + + +def findTests(testdir='./'): + """Since python <2.7 doesn't have test discovery, this finds tests in the + provided directory. The default is to check the current directory. Any files + that match test* or Test* are considered unittest modules and checked for + a module.suite() function (in tests()).""" + fileList = getFileList(testdir,fileExtension='.py') + testModules= [] + for fileName in fileList: + if (fileName[:4] =='test' or fileName[:4]=='Test') and fileName!='test.py': + testModules.append(fileName[:-len('.py')]) + return testModules + +############################################################################### + + def tests(): import unittest try: #auto test discovery is only implemented in python 2.7+ @@ -43,21 +82,29 @@ print 'Tests may be incomplete' t=unittest.TextTestRunner() - - testModules = ['TestBDAlg','TestConvert','TestFreqRsp','TestMatlab','TestModelsimp',\ - 'TestStateSp','TestStatefbk','TestXferFcn'] #add additional tests here, or discovery? + + testModules = findTests() + suite = unittest.TestSuite() + suiteList=[] for mod in testModules: exec('import '+mod+' as currentModule') - print 'TEST',mod - suite = currentModule.suite() - t.run(suite) #After tests have been debugged and made into unittests, remove #the above (except the import) and replace with something like this: - #suiteList.append(ts.suite()) - #alltests = unittest.TestSuite(suiteList) - #t.run(alltests) - + try: + currentSuite = currentModule.suite() + if isinstance(currentSuite,unittest.TestSuite): + suiteList.append(currentModule.suite()) + else: + print mod+'.suite() doesnt return a unittest.TestSuite!, please fix!' + except: + print 'The test module '+mod+' doesnt have '+\ + 'a proper suite() function that returns a unittest.TestSuite object'+\ + ' Please fix this!' + alltests = unittest.TestSuite(suiteList) + t.run(unittest.TestSuite(alltests)) + +############################################################################### if __name__=='__main__': tests() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |