* Alex Martelli <[EMAIL PROTECTED]>: > [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > > Sorry Peter, > > > > Try this.... > > > > import unittest > > import Numeric > > > > class myTest(unittest.TestCase): > > def runTest(self): > > var1 = Numeric.array([1,22]) > > var2 = Numeric.array([1,33]) > > self.assertEqual(var1,var2) > > > > if __name__ == '__main__': > > unittest.main() > > > i.e., thanks to element-by-element evaluation, == will generally return > a true value for ANY comparison of Numeric arrays, causing a very > frequent beginner's bug to be sure. Try Numeric.alltrue(c), or > Numeric.allclose(a,b) ...
I extend unittest.TestCase as follows (uses numarray, not Numeric): class NumTestCase(unittest.TestCase): """Extends TestCase with equality tests for numarrays. """ def numAssertEqual(self, a1, a2): """Test for equality of numarray fields a1 and a2. """ self.assertEqual(a1.shape, a2.shape) self.assertEqual(a1.type(), a2.type()) self.assertTrue(N.alltrue(N.equal(a1.flat, a2.flat))) def numAssertAlmostEqual(self, a1, a2): """Test for approximately equality of numarray fields a1 and a2. """ self.assertEqual(a1.shape, a2.shape) self.assertEqual(a1.type(), a2.type()) if a1.type() == 'Float64' or a1.type() == 'Complex64': prec = 15 else: prec = 7 if isinstance(a1.type(), N.ComplexType): af1, af2 = a1.flat.real, a2.flat.real for ind in xrange(af1.nelements()): self.assertAlmostEqual(af1[ind], af2[ind], prec) af1, af2 = a1.flat.imag, a2.flat.imag for ind in xrange(af1.nelements()): self.assertAlmostEqual(af1[ind], af2[ind], prec) else: af1, af2 = a1.flat, a2.flat for ind in xrange(af1.nelements()): self.assertAlmostEqual(af1[ind], af2[ind], prec) -- http://mail.python.org/mailman/listinfo/python-list