From: <bao...@us...> - 2006-05-24 18:20:56
|
Revision: 155 Author: baoilleach Date: 2006-05-24 11:20:44 -0700 (Wed, 24 May 2006) ViewCVS: http://svn.sourceforge.net/cclib/?rev=155&view=rev Log Message: ----------- Oops. Thanks to Adam for pointing out an error in the unittests. Apparently all tests of equality between a Numeric array and pretty much anything else return True. Looking at the web, this is a so-called beginner's mistake. In any case, I've created a special assertArrayEquals() which should be used to compare arrays instead of assertEquals. Now there are failures are over the shop...it was nicer with our heads under the sand :-) Modified Paths: -------------- trunk/test/testGeoOpt.py Added Paths: ----------- trunk/test/bettertest.py Added: trunk/test/bettertest.py =================================================================== --- trunk/test/bettertest.py (rev 0) +++ trunk/test/bettertest.py 2006-05-24 18:20:44 UTC (rev 155) @@ -0,0 +1,25 @@ +import Numeric +import unittest + +class TestCase(unittest.TestCase): + """Create a class with extra 'asserts' for testing numerical data. + + It is not possible to test equality of Numeric arrays using assertEquals(). + Instead use assertArrayEquals() defined below. + (For the original solution see: + http://mail.python.org/pipermail/python-list/2005-November/311235.html) + + Also, for testing near equality of floats use assertInside. + (Taken from Python Cookbook 2nd Ed. Recipe 8.11) + """ + def assertInside(self,first,second,error,msg=None): + """Fail if the second number isn't within a certain error of the first.""" + if not (second-error) < first < (second+error): + raise self.failureException, (msg or '%r != %r (+-%r)' % (first,second,error)) + + def assertArrayEquals(self,first,second,msg=None): + """Fails unless two Numeric arrays are identical.""" + if not (first.shape==second.shape and + first.typecode()==second.typecode() and + Numeric.alltrue(first==second)): + raise self.failureException, (msg or 'These two arrays are not identical') Property changes on: trunk/test/bettertest.py ___________________________________________________________________ Name: svn:executable + * Modified: trunk/test/testGeoOpt.py =================================================================== --- trunk/test/testGeoOpt.py 2006-05-24 01:53:21 UTC (rev 154) +++ trunk/test/testGeoOpt.py 2006-05-24 18:20:44 UTC (rev 155) @@ -1,15 +1,16 @@ import os import math import unittest +import bettertest from Numeric import array from testall import getfile from cclib.parser import ADF, GAMESS, Gaussian, Jaguar -class GenericGeoOptTest(unittest.TestCase): +class GenericGeoOptTest(bettertest.TestCase): def testhomos(self): """Is the index of the homo equal to 34?""" - self.assertEquals(self.data.homos,array([34])) + self.assertArrayEquals(self.data.homos,array([34]),"%s != array([34])" % self.data.homos) def testatomcoords(self): """Are atomcoords consistent with natom and Angstroms?""" @@ -43,7 +44,7 @@ def testscfenergy(self): """Is the SCF energy within 40eV of -10365""" - self.assert_(abs(self.data.scfenergies[-1]-(-10365))<40,"Final scf energy: %f not -10365+-40eV" % self.data.scfenergies[-1]) + self.assertInside(self.data.scfenergies[-1],-10365,40,"Final scf energy: %f not -10365+-40eV" % self.data.scfenergies[-1]) def testnormalisesym(self): """Did this subclasses overwrite normalisesym?""" @@ -60,11 +61,13 @@ def testscfvaluetype(self): """Do the scf values have the right type?""" - self.assert_(type(self.data.scfvalues[0])==type(array([])) and type(self.data.scfvalues)==type([])) + self.assertEquals(type(self.data.scfvalues),type([])) + self.assertEquals(type(self.data.scfvalues[0]),type(array([]))) def testscfvaluedim(self): """Do the scf values have the right dimensions?""" - self.assert_(len(self.data.scfvalues)==len(self.data.geovalues) and len(self.data.scfvalues[0])==len(self.data.scftargets)) + self.assertEquals(len(self.data.scfvalues),len(self.data.geovalues)) + self.assertEquals(len(self.data.scfvalues[0]),len(self.data.scftargets)) class GaussianGeoOptTest(GenericGeoOptTest): def setUp(self): @@ -94,7 +97,7 @@ def testscfenergy(self): """Is the SCF energy within 1eV of -140eV""" - self.assert_(abs(self.data.scfenergies[-1]-(-140))<1,"Final scf energy: %f not -140+-1eV" % self.data.scfenergies[-1]) + self.assertInside(self.data.scfenergies[-1],-140,1,"Final scf energy: %f not -140+-1eV" % self.data.scfenergies[-1]) class JaguarGeoOptTest(GenericGeoOptTest): def setUp(self): This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |