def test_add(self): self.assertEqual(shape.sum_shapes([(3,4), (3,4)]), (3,4)) with self.assertRaises(Exception) as cm: shape.sum_shapes([(1,3), (4,3)]) self.assertEqual(str(cm.exception), "Incompatible dimensions (1, 3) (4, 3)") # Promotion self.assertEqual(shape.sum_shapes([(3,4), (1,1)]), (3,4)) self.assertEqual(shape.sum_shapes([(1,1), (3,4)]), (3,4))
def test_add(self): self.assertEqual(shape.sum_shapes([(3, 4), (3, 4)]), (3, 4)) with self.assertRaises(Exception) as cm: shape.sum_shapes([(1, 3), (4, 3)]) self.assertEqual(str(cm.exception), "Incompatible dimensions (1, 3) (4, 3)") # Promotion self.assertEqual(shape.sum_shapes([(3, 4), (1, 1)]), (3, 4)) self.assertEqual(shape.sum_shapes([(1, 1), (3, 4)]), (3, 4))
def test_add_incompatible(self) -> None: """Test addition of incompatible shapes raises a ValueError. """ with self.assertRaises(ValueError): shape.sum_shapes([(4, 2), (4, )])
def test_add_broadcasting(self) -> None: """Test broadcasting of shapes during addition. """ # Broadcasting with scalars is permitted. self.assertEqual(shape.sum_shapes([(3, 4), (1, 1)]), (3, 4)) self.assertEqual(shape.sum_shapes([(1, 1), (3, 4)]), (3, 4)) self.assertEqual(shape.sum_shapes([(1, ), (3, 4)]), (3, 4)) self.assertEqual(shape.sum_shapes([(3, 4), (1, )]), (3, 4)) self.assertEqual(shape.sum_shapes([tuple(), (3, 4)]), (3, 4)) self.assertEqual(shape.sum_shapes([(3, 4), tuple()]), (3, 4)) self.assertEqual(shape.sum_shapes([(1, 1), (4, )]), (1, 4)) self.assertEqual(shape.sum_shapes([(4, ), (1, 1)]), (1, 4)) # All other types of broadcasting is not permitted. with self.assertRaises(ValueError): shape.sum_shapes([(4, 1), (4, )]) with self.assertRaises(ValueError): shape.sum_shapes([(4, ), (4, 1)]) with self.assertRaises(ValueError): shape.sum_shapes([(4, 2), (2, )]) with self.assertRaises(ValueError): shape.sum_shapes([(2, ), (4, 2)]) with self.assertRaises(ValueError): shape.sum_shapes([(4, 2), (4, 1)]) with self.assertRaises(ValueError): shape.sum_shapes([(4, 1), (4, 2)])
def test_add_matching(self) -> None: """Test addition of matching shapes. """ self.assertEqual(shape.sum_shapes([(3, 4), (3, 4)]), (3, 4)) self.assertEqual(shape.sum_shapes([(3, 4)] * 5), (3, 4))