Best Python code snippet using Testify_python
test_solve.py
Source:test_solve.py
1import unittest, operator2from solve import scalar, vector, matrix, identitymatrix, trafo, identitytrafo, Solver3class ScalarTestCase(unittest.TestCase):4 def testInit(self):5 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), scalar, "")6 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), scalar, 1j)7 self.failUnlessEqual(str(scalar()), "unnamed_scalar")8 self.failUnlessEqual(str(scalar(name="s")), "s")9 self.failUnlessEqual(str(scalar(1)), "unnamed_scalar{=1.0}")10 self.failUnlessEqual(str(scalar(-1, name="s")), "s{=-1.0}")11 def testMath(self):12 self.failUnlessEqual(str(-scalar(name="s")), "unnamed_scalar{=-1.0} * s")13 self.failUnlessEqual(str(scalar(name="s") + scalar(name="t")), "s + t")14 self.failUnlessEqual(str(scalar(name="s") + 1), "s + unnamed_scalar{=1.0}")15 self.failUnlessEqual(str(1 + scalar(name="s")), "s + unnamed_scalar{=1.0}")16 self.failUnlessEqual(str(scalar(name="s") - scalar(name="t")), "unnamed_scalar{=-1.0} * t + s")17 self.failUnlessEqual(str(1 - scalar(name="s")), "unnamed_scalar{=-1.0} * s + unnamed_scalar{=1.0}")18 self.failUnlessEqual(str(2 * scalar(name="s")), "s * unnamed_scalar{=2.0}")19 self.failUnlessEqual(str(scalar(name="s") * 2), "s * unnamed_scalar{=2.0}")20 self.failUnlessEqual(str(scalar(name="s") * scalar(name="t")), "s * t")21 self.failUnlessEqual(str((scalar(name="s") + scalar(name="t")) * 2), "s * unnamed_scalar{=2.0} + t * unnamed_scalar{=2.0}")22 self.failUnlessEqual(str(scalar(name="s") / 2.0), "unnamed_scalar{=0.5} * s")23 self.failUnlessEqual(str(scalar(name="s") / 2), "unnamed_scalar{=0.0} * s") # integer logic!24 self.failUnlessEqual(str((scalar(name="s") + scalar(name="t")) / 2.0), "unnamed_scalar{=0.5} * s + unnamed_scalar{=0.5} * t")25 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, 2, scalar())26 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar(), scalar())27 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), scalar())28 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar() + scalar(), scalar())29 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1) + vector(1), scalar())30 def testAccess(self):31 s = scalar()32 self.failUnlessEqual(s.is_set(), 0)33 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), s.get)34 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), float, s)35 s.set(2)36 self.failUnlessEqual(s.is_set(), 1)37 self.failUnlessAlmostEqual(s.get(), 2.0)38 self.failUnlessAlmostEqual(float(s), 2.0)39 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), s.set, 3)40 self.failUnlessEqual(s.is_set(), 1)41 self.failUnlessAlmostEqual(s.get(), 2.0)42 self.failUnlessAlmostEqual(float(s), 2.0)43 s = scalar(2)44 self.failUnlessEqual(scalar(2).is_set(), 1)45 self.failUnlessAlmostEqual(s.get(), 2.0)46 self.failUnlessAlmostEqual(float(s), 2.0)47 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), s.set, 3)48 self.failUnlessEqual(s.is_set(), 1)49 self.failUnlessAlmostEqual(s.get(), 2.0)50 self.failUnlessAlmostEqual(float(s), 2.0)51class VectorTestCase(unittest.TestCase):52 def testInit(self):53 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), vector, 0, 0)54 self.failUnlessEqual(str(vector(2)), "unnamed_vector{=(unnamed_vector[0], unnamed_vector[1])}")55 self.failUnlessEqual(str(vector([1, 2])), "unnamed_vector{=(unnamed_vector[0]{=1.0}, unnamed_vector[1]{=2.0})}")56 self.failUnlessEqual(str(vector(3, "a")), "a{=(a[0], a[1], a[2])}")57 self.failUnlessEqual(str(vector([3, 2, 1], "a")), "a{=(a[0]{=3.0}, a[1]{=2.0}, a[2]{=1.0})}")58 def testAccess(self):59 a = vector(2)60 self.failUnlessEqual(str(a), "unnamed_vector{=(unnamed_vector[0], unnamed_vector[1])}")61 self.failUnlessEqual(a[0].is_set(), 0)62 self.failUnlessEqual(a.x.is_set(), 0)63 self.failUnlessEqual(a[1].is_set(), 0)64 self.failUnlessEqual(a.y.is_set(), 0)65 self.failUnlessRaises(IndexError, operator.__getitem__, a, 2)66 self.failUnlessRaises(IndexError, getattr, a, "z")67 a[0].set(2)68 self.failUnlessEqual(str(a), "unnamed_vector{=(unnamed_vector[0]{=2.0}, unnamed_vector[1])}")69 self.failUnlessAlmostEqual(a[0].get(), 2.0)70 self.failUnlessAlmostEqual(float(a[0]), 2.0)71 self.failUnlessAlmostEqual(a.x.get(), 2.0)72 self.failUnlessAlmostEqual(float(a.x), 2.0)73 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), a[1].get)74 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), float, a[1])75 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), a.y.get)76 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), float, a.y)77 self.failUnlessEqual(a[0].is_set(), 1)78 self.failUnlessEqual(a.x.is_set(), 1)79 self.failUnlessEqual(a[1].is_set(), 0)80 self.failUnlessEqual(a.y.is_set(), 0)81 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), a[0].set, 3)82 self.failUnlessAlmostEqual(a[0].get(), 2.0)83 self.failUnlessAlmostEqual(float(a[0]), 2.0)84 self.failUnlessAlmostEqual(a.x.get(), 2.0)85 self.failUnlessAlmostEqual(float(a.x), 2.0)86 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), a[1].get)87 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), float, a[1])88 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), a.y.get)89 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), float, a.y)90 self.failUnlessEqual(a[0].is_set(), 1)91 self.failUnlessEqual(a.x.is_set(), 1)92 self.failUnlessEqual(a[1].is_set(), 0)93 self.failUnlessEqual(a.y.is_set(), 0)94 a[1].set(3)95 self.failUnlessEqual(str(a), "unnamed_vector{=(unnamed_vector[0]{=2.0}, unnamed_vector[1]{=3.0})}")96 self.failUnlessAlmostEqual(a[0].get(), 2.0)97 self.failUnlessAlmostEqual(float(a[0]), 2.0)98 self.failUnlessAlmostEqual(a.x.get(), 2.0)99 self.failUnlessAlmostEqual(float(a.x), 2.0)100 self.failUnlessAlmostEqual(a[1].get(), 3.0)101 self.failUnlessAlmostEqual(float(a[1]), 3.0)102 self.failUnlessAlmostEqual(a.y.get(), 3.0)103 self.failUnlessAlmostEqual(float(a.y), 3.0)104 self.failUnlessEqual(a[0].is_set(), 1)105 self.failUnlessEqual(a.x.is_set(), 1)106 self.failUnlessEqual(a[1].is_set(), 1)107 self.failUnlessEqual(a.y.is_set(), 1)108 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), a[0].set, 4)109 self.failUnlessAlmostEqual(a[0].get(), 2.0)110 self.failUnlessAlmostEqual(float(a[0]), 2.0)111 self.failUnlessAlmostEqual(a.x.get(), 2.0)112 self.failUnlessAlmostEqual(float(a.x), 2.0)113 self.failUnlessAlmostEqual(a[1].get(), 3.0)114 self.failUnlessAlmostEqual(float(a[1]), 3.0)115 self.failUnlessAlmostEqual(a.y.get(), 3.0)116 self.failUnlessAlmostEqual(float(a.y), 3.0)117 self.failUnlessEqual(a[0].is_set(), 1)118 self.failUnlessEqual(a.x.is_set(), 1)119 self.failUnlessEqual(a[1].is_set(), 1)120 self.failUnlessEqual(a.y.is_set(), 1)121 a = vector([1, 2, 3])122 self.failUnlessEqual(str(a.x), "unnamed_vector[0]{=1.0}")123 self.failUnlessEqual(str(a.y), "unnamed_vector[1]{=2.0}")124 self.failUnlessEqual(str(a.z), "unnamed_vector[2]{=3.0}")125 def testLen(self):126 for i in range(1, 5):127 a = vector(i)128 self.failUnlessEqual(len(a), i)129 self.failUnlessEqual(str(a), "unnamed_vector{=(" + ", ".join(["unnamed_vector[%i]" % j for j in range(i)]) + ")}")130 def testMath(self):131 self.failUnlessEqual(str(-vector(2, "a")), "unnamed_vector{=(unnamed_scalar{=-1.0} * a[0], unnamed_scalar{=-1.0} * a[1])}")132 self.failUnlessEqual(str(vector(2, "a") + vector(2, "b")), "unnamed_vector{=(a[0] + b[0], a[1] + b[1])}")133 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, 1, vector(2))134 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(2), 1)135 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, scalar(), vector(2))136 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(2), scalar())137 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, scalar() + scalar(), vector(2))138 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(2), scalar() + scalar())139 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(2), vector(3))140 self.failUnlessEqual(str(vector(2, "a") - vector(2, "b")), "unnamed_vector{=(unnamed_scalar{=-1.0} * b[0] + a[0], unnamed_scalar{=-1.0} * b[1] + a[1])}")141 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, 1, vector(2))142 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(2), 1)143 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, scalar(), vector(2))144 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(2), scalar())145 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, scalar() + scalar(), vector(2))146 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(2), scalar() + scalar())147 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(2), vector(3))148 self.failUnlessEqual(str(2 * vector(2, "a")), "unnamed_vector{=(a[0] * unnamed_scalar{=2.0}, a[1] * unnamed_scalar{=2.0})}")149 self.failUnlessEqual(str(vector(2, "a") * 2), "unnamed_vector{=(a[0] * unnamed_scalar{=2.0}, a[1] * unnamed_scalar{=2.0})}")150 self.failUnlessEqual(str(scalar(name="s") * vector(2, "a")), "unnamed_vector{=(a[0] * s, a[1] * s)}")151 self.failUnlessEqual(str(scalar(name="s") * (vector(2, "a") + vector(2, "b"))), "unnamed_vector{=(a[0] * s + b[0] * s, a[1] * s + b[1] * s)}")152 self.failUnlessEqual(str((scalar(name="s") + scalar(name="t")) * vector(2, "a")), "unnamed_vector{=(a[0] * s + a[0] * t, a[1] * s + a[1] * t)}")153 self.failUnlessEqual(str((scalar(name="s") + scalar(name="t")) * (vector(2, "a") + vector(2, "b"))), "unnamed_vector{=(a[0] * s + b[0] * s + a[0] * t + b[0] * t, a[1] * s + b[1] * s + a[1] * t + b[1] * t)}")154 self.failUnlessEqual(str(vector(2, "a") * scalar(name="s")), "unnamed_vector{=(a[0] * s, a[1] * s)}")155 self.failUnlessEqual(str(vector(2, "a") * vector(2, "b")), "a[0] * b[0] + a[1] * b[1]")156 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, vector(2, "a"), vector(3))157 self.failUnlessEqual(str(vector(2, "a") / 2.0), "unnamed_vector{=(unnamed_scalar{=0.5} * a[0], unnamed_scalar{=0.5} * a[1])}")158 self.failUnlessEqual(str(vector(2, "a") / 2), "unnamed_vector{=(unnamed_scalar{=0.0} * a[0], unnamed_scalar{=0.0} * a[1])}") # integer logic!159 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar(), vector(1))160 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar() + scalar(), vector(1))161 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), scalar())162 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), scalar() + scalar())163 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), vector(1))164class MatrixTestCase(unittest.TestCase):165 def testInit(self):166 self.failUnlessEqual(str(matrix((2, 3))), "unnamed_matrix{=((unnamed_matrix[0, 0], unnamed_matrix[0, 1], unnamed_matrix[0, 2]), (unnamed_matrix[1, 0], unnamed_matrix[1, 1], unnamed_matrix[1, 2]))}")167 self.failUnlessEqual(str(matrix([[1, 2, 3], [4, 5, 6]])), "unnamed_matrix{=((unnamed_matrix[0, 0]{=1.0}, unnamed_matrix[0, 1]{=2.0}, unnamed_matrix[0, 2]{=3.0}), (unnamed_matrix[1, 0]{=4.0}, unnamed_matrix[1, 1]{=5.0}, unnamed_matrix[1, 2]{=6.0}))}")168 self.failUnlessEqual(str(matrix((2, 3), "a")), "a{=((a[0, 0], a[0, 1], a[0, 2]), (a[1, 0], a[1, 1], a[1, 2]))}")169 self.failUnlessEqual(str(matrix([[1, 2, 3], [4, 5, 6]], "a")), "a{=((a[0, 0]{=1.0}, a[0, 1]{=2.0}, a[0, 2]{=3.0}), (a[1, 0]{=4.0}, a[1, 1]{=5.0}, a[1, 2]{=6.0}))}")170 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), matrix, [[1, 2, 3], [4, 5]])171 def testLen(self):172 for rows in range(1, 5):173 for cols in range(1, 5):174 A = matrix((rows, cols))175 self.failUnlessEqual(A.getnumberofrows(), rows)176 self.failUnlessEqual(A.getnumberofcols(), cols)177 self.failUnlessEqual(str(A), "unnamed_matrix{=(" + ", ".join(["(" + ", ".join(["unnamed_matrix[%i, %i]" % (i, j) for j in range(cols)]) + ")" for i in range(rows)]) + ")}")178 def testAccess(self):179 A = matrix([2, 3])180 self.failUnlessEqual(str(A), "unnamed_matrix{=((unnamed_matrix[0, 0], unnamed_matrix[0, 1], unnamed_matrix[0, 2]), (unnamed_matrix[1, 0], unnamed_matrix[1, 1], unnamed_matrix[1, 2]))}")181 self.failUnlessEqual(A[0, 0].is_set(), 0)182 self.failUnlessEqual(A[1, 0].is_set(), 0)183 self.failUnlessRaises(IndexError, operator.__getitem__, A, (2, 0))184 self.failUnlessEqual(A[0, 1].is_set(), 0)185 self.failUnlessEqual(A[1, 1].is_set(), 0)186 self.failUnlessEqual(A[0, 2].is_set(), 0)187 self.failUnlessEqual(A[1, 2].is_set(), 0)188 self.failUnlessRaises(IndexError, operator.__getitem__, A, (0, 3))189 A[0, 0].set(1)190 self.failUnlessEqual(A[0, 0].is_set(), 1)191 self.failUnlessEqual(A[0, 1].is_set(), 0)192 self.failUnlessEqual(A[1, 0].is_set(), 0)193 self.failUnlessAlmostEqual(A[0, 0].get(), 1.0)194 self.failUnlessAlmostEqual(float(A[0, 0]), 1.0)195 def testMath(self):196 self.failUnlessEqual(str(-matrix([2, 3], "A")), "unnamed_matrix{=((unnamed_scalar{=-1.0} * A[0, 0], unnamed_scalar{=-1.0} * A[0, 1], unnamed_scalar{=-1.0} * A[0, 2]), (unnamed_scalar{=-1.0} * A[1, 0], unnamed_scalar{=-1.0} * A[1, 1], unnamed_scalar{=-1.0} * A[1, 2]))}")197 self.failUnlessEqual(str(matrix([2, 3], "A") + matrix([2, 3], "B")), "unnamed_matrix{=((A[0, 0] + B[0, 0], A[0, 1] + B[0, 1], A[0, 2] + B[0, 2]), (A[1, 0] + B[1, 0], A[1, 1] + B[1, 1], A[1, 2] + B[1, 2]))}")198 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), 1)199 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), scalar())200 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), scalar() + scalar())201 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), vector(2))202 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), vector(3))203 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), matrix([2, 4]))204 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), matrix([3, 3]))205 self.failUnlessEqual(str(matrix([2, 3], "A") - matrix([2, 3], "B")), "unnamed_matrix{=((unnamed_scalar{=-1.0} * B[0, 0] + A[0, 0], unnamed_scalar{=-1.0} * B[0, 1] + A[0, 1], unnamed_scalar{=-1.0} * B[0, 2] + A[0, 2]), (unnamed_scalar{=-1.0} * B[1, 0] + A[1, 0], unnamed_scalar{=-1.0} * B[1, 1] + A[1, 1], unnamed_scalar{=-1.0} * B[1, 2] + A[1, 2]))}")206 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), 1)207 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), scalar())208 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), scalar() + scalar())209 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), vector(2))210 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), vector(3))211 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), matrix([2, 4]))212 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), matrix([3, 3]))213 self.failUnlessEqual(str(2 * matrix([2, 3], "A")), "unnamed_matrix{=((A[0, 0] * unnamed_scalar{=2.0}, A[0, 1] * unnamed_scalar{=2.0}, A[0, 2] * unnamed_scalar{=2.0}), (A[1, 0] * unnamed_scalar{=2.0}, A[1, 1] * unnamed_scalar{=2.0}, A[1, 2] * unnamed_scalar{=2.0}))}")214 self.failUnlessEqual(str(matrix([2, 3], "A") * 2), "unnamed_matrix{=((A[0, 0] * unnamed_scalar{=2.0}, A[0, 1] * unnamed_scalar{=2.0}, A[0, 2] * unnamed_scalar{=2.0}), (A[1, 0] * unnamed_scalar{=2.0}, A[1, 1] * unnamed_scalar{=2.0}, A[1, 2] * unnamed_scalar{=2.0}))}")215 self.failUnlessEqual(str(matrix([2, 3], "A") * vector(3, "a")), "unnamed_vector{=(A[0, 0] * a[0] + A[0, 1] * a[1] + A[0, 2] * a[2], A[1, 0] * a[0] + A[1, 1] * a[1] + A[1, 2] * a[2])}")216 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, vector(2, "a"), matrix([2, 3], "A"))217 self.failUnlessEqual(str(matrix([2, 3], "A") * matrix([3, 2], "B")), "unnamed_matrix{=((A[0, 0] * B[0, 0] + A[0, 1] * B[1, 0] + A[0, 2] * B[2, 0], A[0, 0] * B[0, 1] + A[0, 1] * B[1, 1] + A[0, 2] * B[2, 1]), (A[1, 0] * B[0, 0] + A[1, 1] * B[1, 0] + A[1, 2] * B[2, 0], A[1, 0] * B[0, 1] + A[1, 1] * B[1, 1] + A[1, 2] * B[2, 1]))}")218 self.failUnlessEqual(str(matrix([2, 3], "A") / 2.0), "unnamed_matrix{=((unnamed_scalar{=0.5} * A[0, 0], unnamed_scalar{=0.5} * A[0, 1], unnamed_scalar{=0.5} * A[0, 2]), (unnamed_scalar{=0.5} * A[1, 0], unnamed_scalar{=0.5} * A[1, 1], unnamed_scalar{=0.5} * A[1, 2]))}")219 self.failUnlessEqual(str(matrix([2, 3], "A") / 2), "unnamed_matrix{=((unnamed_scalar{=0.0} * A[0, 0], unnamed_scalar{=0.0} * A[0, 1], unnamed_scalar{=0.0} * A[0, 2]), (unnamed_scalar{=0.0} * A[1, 0], unnamed_scalar{=0.0} * A[1, 1], unnamed_scalar{=0.0} * A[1, 2]))}")220 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar(), matrix([2, 3]))221 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar() + scalar(), matrix([2, 3]))222 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), matrix([2, 3]))223 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), scalar())224 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), scalar() + scalar())225 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), vector(1))226 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), matrix([2, 3]))227class TrafoTestCase(unittest.TestCase):228 def testInit(self):229 self.failUnlessEqual(str(trafo((2, 3))), "unnamed_trafo{=(matrix: unnamed_trafo_matrix{=((unnamed_trafo_matrix[0, 0], unnamed_trafo_matrix[0, 1], unnamed_trafo_matrix[0, 2]), (unnamed_trafo_matrix[1, 0], unnamed_trafo_matrix[1, 1], unnamed_trafo_matrix[1, 2]))}, vector: unnamed_trafo_vector{=(unnamed_trafo_vector[0], unnamed_trafo_vector[1])})}")230 self.failUnlessEqual(str(trafo((2, 3), "A")), "A{=(matrix: A_matrix{=((A_matrix[0, 0], A_matrix[0, 1], A_matrix[0, 2]), (A_matrix[1, 0], A_matrix[1, 1], A_matrix[1, 2]))}, vector: A_vector{=(A_vector[0], A_vector[1])})}")231 self.failUnlessEqual(str(trafo((matrix([2, 3], "B"), vector(2, "c")), "A")), "A{=(matrix: B{=((B[0, 0], B[0, 1], B[0, 2]), (B[1, 0], B[1, 1], B[1, 2]))}, vector: c{=(c[0], c[1])})}")232 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), trafo, (matrix([2, 3]), vector(3)))233 def testAccess(self):234 m = matrix([2, 3])235 v = vector(2)236 t = trafo((m, v))237 self.failUnlessEqual(t.getmatrix(), m)238 self.failUnlessEqual(t.getvector(), v)239 def testMath(self):240 self.failUnlessEqual(str(-trafo([2, 3], "A")), str(trafo((-matrix([2, 3], "A_matrix"), -vector(2, "A_vector")))))241 self.failUnlessEqual(str(trafo([2, 3], "A") + trafo([2, 3], "B")), str(trafo((matrix([2, 3], "A_matrix") + matrix([2, 3], "B_matrix"), vector(2, "A_vector") + vector(2, "B_vector")))))242 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, 1, trafo([2, 3]))243 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), 1)244 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, scalar(), trafo([2, 3]))245 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), scalar())246 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, scalar() + scalar(), trafo([2, 3]))247 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), scalar() + scalar())248 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(2), trafo([2, 3]))249 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), vector(2))250 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, vector(3), trafo([2, 3]))251 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), vector(3))252 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, matrix([2, 3]), trafo([2, 3]))253 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), matrix([2, 3]))254 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), trafo([2, 4]))255 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__add__, trafo([2, 3]), trafo([3, 3]))256 self.failUnlessEqual(str(trafo([2, 3], "A") - trafo([2, 3], "B")), str(trafo((matrix([2, 3], "A_matrix") - matrix([2, 3], "B_matrix"), vector(2, "A_vector") - vector(2, "B_vector")))))257 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, 1, trafo([2, 3]))258 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), 1)259 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, scalar(), trafo([2, 3]))260 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), scalar())261 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, scalar() + scalar(), trafo([2, 3]))262 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), scalar() + scalar())263 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(2), trafo([2, 3]))264 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), vector(2))265 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, vector(3), trafo([2, 3]))266 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), vector(3))267 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, matrix([2, 3]), trafo([2, 3]))268 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), matrix([2, 3]))269 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), trafo([2, 4]))270 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__sub__, trafo([2, 3]), trafo([3, 3]))271 self.failUnlessEqual(str(trafo([2, 3], "A") * trafo([3, 4], "B")), str(trafo((matrix([2, 3], "A_matrix") * matrix([3, 4], "B_matrix"), vector(2, "A_vector") + matrix([2, 3], "A_matrix") * vector(3, "B_vector")))))272 self.failUnlessEqual(str(2 * trafo([2, 3], "A")), str(trafo((2 * matrix([2, 3], "A_matrix"), 2 * vector(2, "A_vector")))))273 self.failUnlessEqual(str(trafo([2, 3], "A") * 2), str(trafo((matrix([2, 3], "A_matrix") * 2, vector(2, "A_vector") * 2))))274 self.failUnlessEqual(str(scalar() * trafo([2, 3], "A")), str(trafo((scalar() * matrix([2, 3], "A_matrix"), scalar() * vector(2, "A_vector")))))275 self.failUnlessEqual(str(trafo([2, 3], "A") * scalar()), str(trafo((matrix([2, 3], "A_matrix") * scalar(), vector(2, "A_vector") * scalar()))))276 self.failUnlessEqual(str((scalar() + scalar()) * trafo([2, 3], "A")), str(trafo(((scalar() + scalar()) * matrix([2, 3], "A_matrix"), (scalar() + scalar()) * vector(2, "A_vector")))))277 self.failUnlessEqual(str(trafo([2, 3], "A") * (scalar() + scalar())), str(trafo((matrix([2, 3], "A_matrix") * (scalar() + scalar()), vector(2, "A_vector") * (scalar() + scalar())))))278 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, vector(2), trafo([2, 3]))279 self.failUnlessEqual(str(trafo([2, 3], "A") * vector(3, "a")), str(matrix([2, 3], "A_matrix") * vector(3, "a") + vector(2, "A_vector")))280 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, trafo([2, 3]), vector(2))281 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, matrix([3, 2]), trafo([2, 3]))282 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__mul__, trafo([2, 3]), matrix([3, 2]))283 self.failUnlessEqual(str(trafo([2, 3], "A") / 2.0), str(trafo((matrix([2, 3], "A_matrix") / 2.0, vector(2, "A_vector") / 2.0))))284 self.failUnlessEqual(str(trafo([2, 3], "A") / 2), str(trafo((matrix([2, 3], "A_matrix") / 2, vector(2, "A_vector") / 2))))285 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar(), trafo([2, 3]))286 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, scalar() + scalar(), trafo([2, 3]))287 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, vector(1), trafo([2, 3]))288 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, matrix([2, 3]), trafo([2, 3]))289 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), scalar())290 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), scalar() + scalar())291 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), vector(1))292 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), matrix([2, 3]))293 self.failUnlessRaises((RuntimeError, AttributeError, TypeError), operator.__div__, trafo([2, 3]), trafo([2, 3]))294class SolverTestCase(unittest.TestCase):295 def testScalars(self):296 solver = Solver()297 a = scalar()298 solver.eq(2 + a, 3)299 self.failUnlessAlmostEqual(float(a), 1)300 self.failUnlessRaises(RuntimeError, solver.eq, 2 + a, 3) # the solver is broken after that301 solver = Solver()302 a = scalar()303 b = scalar()304 solver.eq(2*a, b)305 solver.eq(2 + a + b, 5)306 self.failUnlessAlmostEqual(float(a), 1)307 self.failUnlessAlmostEqual(float(b), 2)308 a = scalar()309 b = scalar()310 solver.eq(a, b)311 self.failUnlessRaises(Exception, solver.eq, a, b)312 def testVector(self):313 solver = Solver()314 a = vector(2)315 solver.eq(vector([1, 2]) + a, vector([3, 3]))316 self.failUnlessAlmostEqual(float(a.x), 2)317 self.failUnlessAlmostEqual(float(a.y), 1)318 b = vector(2)319 solver.eq(a*b) # => solver.eq(a*b, 0)320 solver.eq(b.y, 2)321 self.failUnlessAlmostEqual(float(b.x), -1)322 def testMatrix(self):323 solver = Solver()324 A = matrix([2, 2])325 solver.eq(A*vector([1, -1]), vector([2, 0]))...
test_operator.py
Source:test_operator.py
...333435class OperatorTestCase(unittest.TestCase):36 def test_lt(self):37 self.failUnlessRaises(TypeError, operator.lt)38 self.failUnlessRaises(TypeError, operator.lt, 1j, 2j)39 self.failIf(operator.lt(1, 0))40 self.failIf(operator.lt(1, 0.0))41 self.failIf(operator.lt(1, 1))42 self.failIf(operator.lt(1, 1.0))43 self.failUnless(operator.lt(1, 2))44 self.failUnless(operator.lt(1, 2.0))4546 def test_le(self):47 self.failUnlessRaises(TypeError, operator.le)48 self.failUnlessRaises(TypeError, operator.le, 1j, 2j)49 self.failIf(operator.le(1, 0))50 self.failIf(operator.le(1, 0.0))51 self.failUnless(operator.le(1, 1))52 self.failUnless(operator.le(1, 1.0))53 self.failUnless(operator.le(1, 2))54 self.failUnless(operator.le(1, 2.0))5556 def test_eq(self):57 class C(object):58 def __eq__(self, other):59 raise SyntaxError60 self.failUnlessRaises(TypeError, operator.eq)61 self.failUnlessRaises(SyntaxError, operator.eq, C(), C())62 self.failIf(operator.eq(1, 0))63 self.failIf(operator.eq(1, 0.0))64 self.failUnless(operator.eq(1, 1))65 self.failUnless(operator.eq(1, 1.0))66 self.failIf(operator.eq(1, 2))67 self.failIf(operator.eq(1, 2.0))6869 def test_ne(self):70 class C(object):71 def __ne__(self, other):72 raise SyntaxError73 self.failUnlessRaises(TypeError, operator.ne)74 self.failUnlessRaises(SyntaxError, operator.ne, C(), C())75 self.failUnless(operator.ne(1, 0))76 self.failUnless(operator.ne(1, 0.0))77 self.failIf(operator.ne(1, 1))78 self.failIf(operator.ne(1, 1.0))79 self.failUnless(operator.ne(1, 2))80 self.failUnless(operator.ne(1, 2.0))8182 def test_ge(self):83 self.failUnlessRaises(TypeError, operator.ge)84 self.failUnlessRaises(TypeError, operator.ge, 1j, 2j)85 self.failUnless(operator.ge(1, 0))86 self.failUnless(operator.ge(1, 0.0))87 self.failUnless(operator.ge(1, 1))88 self.failUnless(operator.ge(1, 1.0))89 self.failIf(operator.ge(1, 2))90 self.failIf(operator.ge(1, 2.0))9192 def test_gt(self):93 self.failUnlessRaises(TypeError, operator.gt)94 self.failUnlessRaises(TypeError, operator.gt, 1j, 2j)95 self.failUnless(operator.gt(1, 0))96 self.failUnless(operator.gt(1, 0.0))97 self.failIf(operator.gt(1, 1))98 self.failIf(operator.gt(1, 1.0))99 self.failIf(operator.gt(1, 2))100 self.failIf(operator.gt(1, 2.0))101102 def test_abs(self):103 self.failUnlessRaises(TypeError, operator.abs)104 self.failUnlessRaises(TypeError, operator.abs, None)105 self.failUnless(operator.abs(-1) == 1)106 self.failUnless(operator.abs(1) == 1)107108 def test_add(self):109 self.failUnlessRaises(TypeError, operator.add)110 self.failUnlessRaises(TypeError, operator.add, None, None)111 self.failUnless(operator.add(3, 4) == 7)112113 def test_bitwise_and(self):114 self.failUnlessRaises(TypeError, operator.and_)115 self.failUnlessRaises(TypeError, operator.and_, None, None)116 self.failUnless(operator.and_(0xf, 0xa) == 0xa)117118 def test_concat(self):119 self.failUnlessRaises(TypeError, operator.concat)120 self.failUnlessRaises(TypeError, operator.concat, None, None)121 self.failUnless(operator.concat('py', 'thon') == 'python')122 self.failUnless(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])123 self.failUnless(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])124 self.failUnless(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])125 self.failUnlessRaises(TypeError, operator.concat, 13, 29)126127 def test_countOf(self):128 self.failUnlessRaises(TypeError, operator.countOf)129 self.failUnlessRaises(TypeError, operator.countOf, None, None)130 self.failUnless(operator.countOf([1, 2, 1, 3, 1, 4], 3) == 1)131 self.failUnless(operator.countOf([1, 2, 1, 3, 1, 4], 5) == 0)132133 def test_delitem(self):134 a = [4, 3, 2, 1]135 self.failUnlessRaises(TypeError, operator.delitem, a)136 self.failUnlessRaises(TypeError, operator.delitem, a, None)137 self.failUnless(operator.delitem(a, 1) is None)138 self.assert_(a == [4, 2, 1])139140 def test_delslice(self):141 a = range(10)142 self.failUnlessRaises(TypeError, operator.delslice, a)143 self.failUnlessRaises(TypeError, operator.delslice, a, None, None)144 self.failUnless(operator.delslice(a, 2, 8) is None)145 self.assert_(a == [0, 1, 8, 9])146147 def test_div(self):148 self.failUnlessRaises(TypeError, operator.div, 5)149 self.failUnlessRaises(TypeError, operator.div, None, None)150 self.failUnless(operator.floordiv(5, 2) == 2)151152 def test_floordiv(self):153 self.failUnlessRaises(TypeError, operator.floordiv, 5)154 self.failUnlessRaises(TypeError, operator.floordiv, None, None)155 self.failUnless(operator.floordiv(5, 2) == 2)156157 def test_truediv(self):158 self.failUnlessRaises(TypeError, operator.truediv, 5)159 self.failUnlessRaises(TypeError, operator.truediv, None, None)160 self.failUnless(operator.truediv(5, 2) == 2.5)161162 def test_getitem(self):163 a = range(10)164 self.failUnlessRaises(TypeError, operator.getitem)165 self.failUnlessRaises(TypeError, operator.getitem, a, None)166 self.failUnless(operator.getitem(a, 2) == 2)167168 def test_getslice(self):169 a = range(10)170 self.failUnlessRaises(TypeError, operator.getslice)171 self.failUnlessRaises(TypeError, operator.getslice, a, None, None)172 self.failUnless(operator.getslice(a, 4, 6) == [4, 5])173174 def test_indexOf(self):175 self.failUnlessRaises(TypeError, operator.indexOf)176 self.failUnlessRaises(TypeError, operator.indexOf, None, None)177 self.failUnless(operator.indexOf([4, 3, 2, 1], 3) == 1)178 self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0)179180 def test_invert(self):181 self.failUnlessRaises(TypeError, operator.invert)182 self.failUnlessRaises(TypeError, operator.invert, None)183 self.failUnless(operator.inv(4) == -5)184185 def test_isCallable(self):186 self.failUnlessRaises(TypeError, operator.isCallable)187 class C:188 pass189 def check(self, o, v):190 self.assert_(operator.isCallable(o) == callable(o) == v)191 check(self, 4, 0)192 check(self, operator.isCallable, 1)193 check(self, C, 1)194 check(self, C(), 0)195196 def test_isMappingType(self):197 self.failUnlessRaises(TypeError, operator.isMappingType)198 self.failIf(operator.isMappingType(1))199 self.failIf(operator.isMappingType(operator.isMappingType))200 self.failUnless(operator.isMappingType(operator.__dict__))201 self.failUnless(operator.isMappingType({}))202203 def test_isNumberType(self):204 self.failUnlessRaises(TypeError, operator.isNumberType)205 self.failUnless(operator.isNumberType(8))206 self.failUnless(operator.isNumberType(8j))207 self.failUnless(operator.isNumberType(8L))208 self.failUnless(operator.isNumberType(8.3))209 self.failIf(operator.isNumberType(dir()))210211 def test_isSequenceType(self):212 self.failUnlessRaises(TypeError, operator.isSequenceType)213 self.failUnless(operator.isSequenceType(dir()))214 self.failUnless(operator.isSequenceType(()))215 self.failUnless(operator.isSequenceType(xrange(10)))216 self.failUnless(operator.isSequenceType('yeahbuddy'))217 self.failIf(operator.isSequenceType(3))218219 def test_lshift(self):220 self.failUnlessRaises(TypeError, operator.lshift)221 self.failUnlessRaises(TypeError, operator.lshift, None, 42)222 self.failUnless(operator.lshift(5, 1) == 10)223 self.failUnless(operator.lshift(5, 0) == 5)224 self.assertRaises(ValueError, operator.lshift, 2, -1)225226 def test_mod(self):227 self.failUnlessRaises(TypeError, operator.mod)228 self.failUnlessRaises(TypeError, operator.mod, None, 42)229 self.failUnless(operator.mod(5, 2) == 1)230231 def test_mul(self):232 self.failUnlessRaises(TypeError, operator.mul)233 self.failUnlessRaises(TypeError, operator.mul, None, None)234 self.failUnless(operator.mul(5, 2) == 10)235236 def test_neg(self):237 self.failUnlessRaises(TypeError, operator.neg)238 self.failUnlessRaises(TypeError, operator.neg, None)239 self.failUnless(operator.neg(5) == -5)240 self.failUnless(operator.neg(-5) == 5)241 self.failUnless(operator.neg(0) == 0)242 self.failUnless(operator.neg(-0) == 0)243244 def test_bitwise_or(self):245 self.failUnlessRaises(TypeError, operator.or_)246 self.failUnlessRaises(TypeError, operator.or_, None, None)247 self.failUnless(operator.or_(0xa, 0x5) == 0xf)248249 def test_pos(self):250 self.failUnlessRaises(TypeError, operator.pos)251 self.failUnlessRaises(TypeError, operator.pos, None)252 self.failUnless(operator.pos(5) == 5)253 self.failUnless(operator.pos(-5) == -5)254 self.failUnless(operator.pos(0) == 0)255 self.failUnless(operator.pos(-0) == 0)256257 def test_pow(self):258 self.failUnlessRaises(TypeError, operator.pow)259 self.failUnlessRaises(TypeError, operator.pow, None, None)260 self.failUnless(operator.pow(3,5) == 3**5)261 self.failUnless(operator.__pow__(3,5) == 3**5)262 self.assertRaises(TypeError, operator.pow, 1)263 self.assertRaises(TypeError, operator.pow, 1, 2, 3)264265 def test_repeat(self):266 a = range(3)267 self.failUnlessRaises(TypeError, operator.repeat)268 self.failUnlessRaises(TypeError, operator.repeat, a, None)269 self.failUnless(operator.repeat(a, 2) == a+a)270 self.failUnless(operator.repeat(a, 1) == a)271 self.failUnless(operator.repeat(a, 0) == [])272 a = (1, 2, 3)273 self.failUnless(operator.repeat(a, 2) == a+a)274 self.failUnless(operator.repeat(a, 1) == a)275 self.failUnless(operator.repeat(a, 0) == ())276 a = '123'277 self.failUnless(operator.repeat(a, 2) == a+a)278 self.failUnless(operator.repeat(a, 1) == a)279 self.failUnless(operator.repeat(a, 0) == '')280 a = Seq1([4, 5, 6])281 self.failUnless(operator.repeat(a, 2) == [4, 5, 6, 4, 5, 6])282 self.failUnless(operator.repeat(a, 1) == [4, 5, 6])283 self.failUnless(operator.repeat(a, 0) == [])284 a = Seq2([4, 5, 6])285 self.failUnless(operator.repeat(a, 2) == [4, 5, 6, 4, 5, 6])286 self.failUnless(operator.repeat(a, 1) == [4, 5, 6])287 self.failUnless(operator.repeat(a, 0) == [])288 self.failUnlessRaises(TypeError, operator.repeat, 6, 7)289290 def test_rshift(self):291 self.failUnlessRaises(TypeError, operator.rshift)292 self.failUnlessRaises(TypeError, operator.rshift, None, 42)293 self.failUnless(operator.rshift(5, 1) == 2)294 self.failUnless(operator.rshift(5, 0) == 5)295 self.assertRaises(ValueError, operator.rshift, 2, -1)296297 def test_contains(self):298 self.failUnlessRaises(TypeError, operator.contains)299 self.failUnlessRaises(TypeError, operator.contains, None, None)300 self.failUnless(operator.contains(range(4), 2))301 self.failIf(operator.contains(range(4), 5))302 self.failUnless(operator.sequenceIncludes(range(4), 2))303 self.failIf(operator.sequenceIncludes(range(4), 5))304305 def test_setitem(self):306 a = range(3)307 self.failUnlessRaises(TypeError, operator.setitem, a)308 self.failUnlessRaises(TypeError, operator.setitem, a, None, None)309 self.failUnless(operator.setitem(a, 0, 2) is None)310 self.assert_(a == [2, 1, 2])311 self.assertRaises(IndexError, operator.setitem, a, 4, 2)312313 def test_setslice(self):314 a = range(4)315 self.failUnlessRaises(TypeError, operator.setslice, a)316 self.failUnlessRaises(TypeError, operator.setslice, a, None, None, None)317 self.failUnless(operator.setslice(a, 1, 3, [2, 1]) is None)318 self.assert_(a == [0, 2, 1, 3])319320 def test_sub(self):321 self.failUnlessRaises(TypeError, operator.sub)322 self.failUnlessRaises(TypeError, operator.sub, None, None)323 self.failUnless(operator.sub(5, 2) == 3)324325 def test_truth(self):326 class C(object):327 def __nonzero__(self):328 raise SyntaxError329 self.failUnlessRaises(TypeError, operator.truth)330 self.failUnlessRaises(SyntaxError, operator.truth, C())331 self.failUnless(operator.truth(5))332 self.failUnless(operator.truth([0]))333 self.failIf(operator.truth(0))334 self.failIf(operator.truth([]))335336 def test_bitwise_xor(self):337 self.failUnlessRaises(TypeError, operator.xor)338 self.failUnlessRaises(TypeError, operator.xor, None, None)339 self.failUnless(operator.xor(0xb, 0xc) == 0x7)340341 def test_is(self):342 a = b = 'xyzpdq'343 c = a[:3] + b[3:]344 self.failUnlessRaises(TypeError, operator.is_)345 self.failUnless(operator.is_(a, b))346 self.failIf(operator.is_(a,c))347348 def test_is_not(self):349 a = b = 'xyzpdq'350 c = a[:3] + b[3:]351 self.failUnlessRaises(TypeError, operator.is_not)352 self.failIf(operator.is_not(a, b))353 self.failUnless(operator.is_not(a,c))354355 def test_attrgetter(self):356 class A:357 pass358 a = A()359 a.name = 'arthur'360 f = operator.attrgetter('name')361 self.assertEqual(f(a), 'arthur')362 f = operator.attrgetter('rank')363 self.assertRaises(AttributeError, f, a)364 f = operator.attrgetter(2)365 self.assertRaises(TypeError, f, a)366 self.assertRaises(TypeError, operator.attrgetter)367368 # multiple gets369 record = A()370 record.x = 'X'371 record.y = 'Y'372 record.z = 'Z'373 self.assertEqual(operator.attrgetter('x','z','y')(record), ('X', 'Z', 'Y'))374 self.assertRaises(TypeError, operator.attrgetter('x', (), 'y'), record)375376 class C(object):377 def __getattr(self, name):378 raise SyntaxError379 self.failUnlessRaises(AttributeError, operator.attrgetter('foo'), C())380381 def test_itemgetter(self):382 a = 'ABCDE'383 f = operator.itemgetter(2)384 self.assertEqual(f(a), 'C')385 f = operator.itemgetter(10)386 self.assertRaises(IndexError, f, a)387388 class C(object):389 def __getitem(self, name):390 raise SyntaxError391 self.failUnlessRaises(TypeError, operator.itemgetter(42), C())392393 f = operator.itemgetter('name')394 self.assertRaises(TypeError, f, a)395 self.assertRaises(TypeError, operator.itemgetter)396397 d = dict(key='val')398 f = operator.itemgetter('key')399 self.assertEqual(f(d), 'val')400 f = operator.itemgetter('nonkey')401 self.assertRaises(KeyError, f, d)402403 # example used in the docs404 inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]405 getcount = operator.itemgetter(1)
...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!