def test_init_signature(self): """Test whether miscalling the constructor generates an error.""" with self.assertRaises(TypeError): v = vector.Vector() with self.assertRaises(TypeError): v = vector.Vector('Too many', 'arguments')
def test_init_bad_size(self): """Test whether an invalid vector size generates an error.""" with self.assertRaises(ArgumentError): v = vector.Vector(3.5) with self.assertRaises(MemoryError): # FIXME: This isn't really a MemoryError, but more detailed error # messages aren't being passed from GSL to Python... yet. v = vector.Vector(-1)
def test_init_default(self): """Test creation of a vector with default arguments.""" # Can we create a vector of the default type (double)? v = vector.Vector(self.VECTOR_SIZE) # Is it the right size? self.assertEqual(len(v), self.VECTOR_SIZE) # Is it iterable, and are the elements the right type and initialised # to zero? for x in v: self.assertIsInstance(x, float) self.assertEqual(x, 0.0)
def test_init_from_complex_iterable(self): """Test creation of an implicitly complex vector.""" values = (3.0 - 0.5j, 0.1j) # Can we create a vector from an iterable of complex numbers? v = vector.Vector(values) # Is it the right size? self.assertEqual(len(v), len(values)) # Is it iterable, and are the elements the right type and value? for expected, got in zip(values, v): self.assertIsInstance(got, complex) self.assertEqual(expected, got)
def test_init_from_iterable(self): """Test creation and initialisation of a vector.""" values = (-1.0, 3.0, 0.0) # Can we create and initialise a vector of the default type (double)? v = vector.Vector(values) # Is it the right size? self.assertEqual(len(v), len(values)) # Is it iterable, and are the elements the right type and value? for expected, got in zip(values, v): self.assertIsInstance(got, float) self.assertEqual(expected, got)
def test_init_by_type(self): """Test creation of a vector with a typecode.""" for typecode in typecodes: itemtype, zeroval = typecodes[typecode] # Can we create a vector of this type? v = vector.Vector(self.VECTOR_SIZE, typecode=typecode) # Is it the right size? self.assertEqual(len(v), self.VECTOR_SIZE) # Is it iterable, and are the elements the right type and # initialised to zero? for x in v: self.assertIsInstance(x, itemtype) self.assertEqual(x, zeroval)
def setUp(self): """Prepare two vectors for use in tests.""" # Two real-valued vectors... self.u = vector.Vector((3.0, 0.0, -1.0)) self.v = vector.Vector((-1.0, 1.0, 0.5)) # ...and two complex-valued vectors. self.w = vector.Vector((2 + 1j, -2 + 1j, 1 - 2j), typecode='C') self.x = vector.Vector((0 - 1j, -1 + 0j, 0 + 0j), typecode='C') # Plus two differently-sized vectors, for testing size constraints. self.y = vector.Vector((2.5, 0.5, -1.0, 1.0)) self.z = vector.Vector((-0.5j, 1 - 2j), typecode='C')
def test_init_bad_typecode(self): """Test whether a bad typecode generates an error.""" with self.assertRaises(ValueError): v = vector.Vector(5, typecode='This is not a valid type code.')