How to use assertRaises method in autotest

Best Python code snippet using autotest_python

test_getargs2.py

Source:test_getargs2.py Github

copy

Full Screen

...102class Unsigned_TestCase(unittest.TestCase):103 def test_b(self):104 from _testcapi import getargs_b105 # b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX)106 self.assertRaises(TypeError, getargs_b, 3.14)107 self.assertEqual(99, getargs_b(Int()))108 self.assertEqual(0, getargs_b(IntSubclass()))109 self.assertRaises(TypeError, getargs_b, BadInt())110 with self.assertWarns(DeprecationWarning):111 self.assertEqual(1, getargs_b(BadInt2()))112 self.assertEqual(0, getargs_b(BadInt3()))113 self.assertRaises(OverflowError, getargs_b, -1)114 self.assertEqual(0, getargs_b(0))115 self.assertEqual(UCHAR_MAX, getargs_b(UCHAR_MAX))116 self.assertRaises(OverflowError, getargs_b, UCHAR_MAX + 1)117 self.assertEqual(42, getargs_b(42))118 self.assertRaises(OverflowError, getargs_b, VERY_LARGE)119 def test_B(self):120 from _testcapi import getargs_B121 # B returns 'unsigned char', no range checking122 self.assertRaises(TypeError, getargs_B, 3.14)123 self.assertEqual(99, getargs_B(Int()))124 self.assertEqual(0, getargs_B(IntSubclass()))125 self.assertRaises(TypeError, getargs_B, BadInt())126 with self.assertWarns(DeprecationWarning):127 self.assertEqual(1, getargs_B(BadInt2()))128 self.assertEqual(0, getargs_B(BadInt3()))129 self.assertEqual(UCHAR_MAX, getargs_B(-1))130 self.assertEqual(0, getargs_B(0))131 self.assertEqual(UCHAR_MAX, getargs_B(UCHAR_MAX))132 self.assertEqual(0, getargs_B(UCHAR_MAX+1))133 self.assertEqual(42, getargs_B(42))134 self.assertEqual(UCHAR_MAX & VERY_LARGE, getargs_B(VERY_LARGE))135 def test_H(self):136 from _testcapi import getargs_H137 # H returns 'unsigned short', no range checking138 self.assertRaises(TypeError, getargs_H, 3.14)139 self.assertEqual(99, getargs_H(Int()))140 self.assertEqual(0, getargs_H(IntSubclass()))141 self.assertRaises(TypeError, getargs_H, BadInt())142 with self.assertWarns(DeprecationWarning):143 self.assertEqual(1, getargs_H(BadInt2()))144 self.assertEqual(0, getargs_H(BadInt3()))145 self.assertEqual(USHRT_MAX, getargs_H(-1))146 self.assertEqual(0, getargs_H(0))147 self.assertEqual(USHRT_MAX, getargs_H(USHRT_MAX))148 self.assertEqual(0, getargs_H(USHRT_MAX+1))149 self.assertEqual(42, getargs_H(42))150 self.assertEqual(VERY_LARGE & USHRT_MAX, getargs_H(VERY_LARGE))151 def test_I(self):152 from _testcapi import getargs_I153 # I returns 'unsigned int', no range checking154 self.assertRaises(TypeError, getargs_I, 3.14)155 self.assertEqual(99, getargs_I(Int()))156 self.assertEqual(0, getargs_I(IntSubclass()))157 self.assertRaises(TypeError, getargs_I, BadInt())158 with self.assertWarns(DeprecationWarning):159 self.assertEqual(1, getargs_I(BadInt2()))160 self.assertEqual(0, getargs_I(BadInt3()))161 self.assertEqual(UINT_MAX, getargs_I(-1))162 self.assertEqual(0, getargs_I(0))163 self.assertEqual(UINT_MAX, getargs_I(UINT_MAX))164 self.assertEqual(0, getargs_I(UINT_MAX+1))165 self.assertEqual(42, getargs_I(42))166 self.assertEqual(VERY_LARGE & UINT_MAX, getargs_I(VERY_LARGE))167 def test_k(self):168 from _testcapi import getargs_k169 # k returns 'unsigned long', no range checking170 # it does not accept float, or instances with __int__171 self.assertRaises(TypeError, getargs_k, 3.14)172 self.assertRaises(TypeError, getargs_k, Int())173 self.assertEqual(0, getargs_k(IntSubclass()))174 self.assertRaises(TypeError, getargs_k, BadInt())175 self.assertRaises(TypeError, getargs_k, BadInt2())176 self.assertEqual(0, getargs_k(BadInt3()))177 self.assertEqual(ULONG_MAX, getargs_k(-1))178 self.assertEqual(0, getargs_k(0))179 self.assertEqual(ULONG_MAX, getargs_k(ULONG_MAX))180 self.assertEqual(0, getargs_k(ULONG_MAX+1))181 self.assertEqual(42, getargs_k(42))182 self.assertEqual(VERY_LARGE & ULONG_MAX, getargs_k(VERY_LARGE))183class Signed_TestCase(unittest.TestCase):184 def test_h(self):185 from _testcapi import getargs_h186 # h returns 'short', and does range checking (SHRT_MIN ... SHRT_MAX)187 self.assertRaises(TypeError, getargs_h, 3.14)188 self.assertEqual(99, getargs_h(Int()))189 self.assertEqual(0, getargs_h(IntSubclass()))190 self.assertRaises(TypeError, getargs_h, BadInt())191 with self.assertWarns(DeprecationWarning):192 self.assertEqual(1, getargs_h(BadInt2()))193 self.assertEqual(0, getargs_h(BadInt3()))194 self.assertRaises(OverflowError, getargs_h, SHRT_MIN-1)195 self.assertEqual(SHRT_MIN, getargs_h(SHRT_MIN))196 self.assertEqual(SHRT_MAX, getargs_h(SHRT_MAX))197 self.assertRaises(OverflowError, getargs_h, SHRT_MAX+1)198 self.assertEqual(42, getargs_h(42))199 self.assertRaises(OverflowError, getargs_h, VERY_LARGE)200 def test_i(self):201 from _testcapi import getargs_i202 # i returns 'int', and does range checking (INT_MIN ... INT_MAX)203 self.assertRaises(TypeError, getargs_i, 3.14)204 self.assertEqual(99, getargs_i(Int()))205 self.assertEqual(0, getargs_i(IntSubclass()))206 self.assertRaises(TypeError, getargs_i, BadInt())207 with self.assertWarns(DeprecationWarning):208 self.assertEqual(1, getargs_i(BadInt2()))209 self.assertEqual(0, getargs_i(BadInt3()))210 self.assertRaises(OverflowError, getargs_i, INT_MIN-1)211 self.assertEqual(INT_MIN, getargs_i(INT_MIN))212 self.assertEqual(INT_MAX, getargs_i(INT_MAX))213 self.assertRaises(OverflowError, getargs_i, INT_MAX+1)214 self.assertEqual(42, getargs_i(42))215 self.assertRaises(OverflowError, getargs_i, VERY_LARGE)216 def test_l(self):217 from _testcapi import getargs_l218 # l returns 'long', and does range checking (LONG_MIN ... LONG_MAX)219 self.assertRaises(TypeError, getargs_l, 3.14)220 self.assertEqual(99, getargs_l(Int()))221 self.assertEqual(0, getargs_l(IntSubclass()))222 self.assertRaises(TypeError, getargs_l, BadInt())223 with self.assertWarns(DeprecationWarning):224 self.assertEqual(1, getargs_l(BadInt2()))225 self.assertEqual(0, getargs_l(BadInt3()))226 self.assertRaises(OverflowError, getargs_l, LONG_MIN-1)227 self.assertEqual(LONG_MIN, getargs_l(LONG_MIN))228 self.assertEqual(LONG_MAX, getargs_l(LONG_MAX))229 self.assertRaises(OverflowError, getargs_l, LONG_MAX+1)230 self.assertEqual(42, getargs_l(42))231 self.assertRaises(OverflowError, getargs_l, VERY_LARGE)232 def test_n(self):233 from _testcapi import getargs_n234 # n returns 'Py_ssize_t', and does range checking235 # (PY_SSIZE_T_MIN ... PY_SSIZE_T_MAX)236 self.assertRaises(TypeError, getargs_n, 3.14)237 self.assertRaises(TypeError, getargs_n, Int())238 self.assertEqual(0, getargs_n(IntSubclass()))239 self.assertRaises(TypeError, getargs_n, BadInt())240 self.assertRaises(TypeError, getargs_n, BadInt2())241 self.assertEqual(0, getargs_n(BadInt3()))242 self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MIN-1)243 self.assertEqual(PY_SSIZE_T_MIN, getargs_n(PY_SSIZE_T_MIN))244 self.assertEqual(PY_SSIZE_T_MAX, getargs_n(PY_SSIZE_T_MAX))245 self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MAX+1)246 self.assertEqual(42, getargs_n(42))247 self.assertRaises(OverflowError, getargs_n, VERY_LARGE)248class LongLong_TestCase(unittest.TestCase):249 def test_L(self):250 from _testcapi import getargs_L251 # L returns 'long long', and does range checking (LLONG_MIN252 # ... LLONG_MAX)253 self.assertRaises(TypeError, getargs_L, 3.14)254 self.assertRaises(TypeError, getargs_L, "Hello")255 self.assertEqual(99, getargs_L(Int()))256 self.assertEqual(0, getargs_L(IntSubclass()))257 self.assertRaises(TypeError, getargs_L, BadInt())258 with self.assertWarns(DeprecationWarning):259 self.assertEqual(1, getargs_L(BadInt2()))260 self.assertEqual(0, getargs_L(BadInt3()))261 self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1)262 self.assertEqual(LLONG_MIN, getargs_L(LLONG_MIN))263 self.assertEqual(LLONG_MAX, getargs_L(LLONG_MAX))264 self.assertRaises(OverflowError, getargs_L, LLONG_MAX+1)265 self.assertEqual(42, getargs_L(42))266 self.assertRaises(OverflowError, getargs_L, VERY_LARGE)267 def test_K(self):268 from _testcapi import getargs_K269 # K return 'unsigned long long', no range checking270 self.assertRaises(TypeError, getargs_K, 3.14)271 self.assertRaises(TypeError, getargs_K, Int())272 self.assertEqual(0, getargs_K(IntSubclass()))273 self.assertRaises(TypeError, getargs_K, BadInt())274 self.assertRaises(TypeError, getargs_K, BadInt2())275 self.assertEqual(0, getargs_K(BadInt3()))276 self.assertEqual(ULLONG_MAX, getargs_K(ULLONG_MAX))277 self.assertEqual(0, getargs_K(0))278 self.assertEqual(0, getargs_K(ULLONG_MAX+1))279 self.assertEqual(42, getargs_K(42))280 self.assertEqual(VERY_LARGE & ULLONG_MAX, getargs_K(VERY_LARGE))281class Float_TestCase(unittest.TestCase):282 def assertEqualWithSign(self, actual, expected):283 self.assertEqual(actual, expected)284 self.assertEqual(math.copysign(1, actual), math.copysign(1, expected))285 def test_f(self):286 from _testcapi import getargs_f287 self.assertEqual(getargs_f(4.25), 4.25)288 self.assertEqual(getargs_f(4), 4.0)289 self.assertRaises(TypeError, getargs_f, 4.25+0j)290 self.assertEqual(getargs_f(Float()), 4.25)291 self.assertEqual(getargs_f(FloatSubclass(7.5)), 7.5)292 self.assertEqual(getargs_f(FloatSubclass2(7.5)), 7.5)293 self.assertRaises(TypeError, getargs_f, BadFloat())294 with self.assertWarns(DeprecationWarning):295 self.assertEqual(getargs_f(BadFloat2()), 4.25)296 self.assertEqual(getargs_f(BadFloat3(7.5)), 7.5)297 for x in (FLT_MIN, -FLT_MIN, FLT_MAX, -FLT_MAX, INF, -INF):298 self.assertEqual(getargs_f(x), x)299 if FLT_MAX < DBL_MAX:300 self.assertEqual(getargs_f(DBL_MAX), INF)301 self.assertEqual(getargs_f(-DBL_MAX), -INF)302 if FLT_MIN > DBL_MIN:303 self.assertEqualWithSign(getargs_f(DBL_MIN), 0.0)304 self.assertEqualWithSign(getargs_f(-DBL_MIN), -0.0)305 self.assertEqualWithSign(getargs_f(0.0), 0.0)306 self.assertEqualWithSign(getargs_f(-0.0), -0.0)307 r = getargs_f(NAN)308 self.assertNotEqual(r, r)309 def test_d(self):310 from _testcapi import getargs_d311 self.assertEqual(getargs_d(4.25), 4.25)312 self.assertEqual(getargs_d(4), 4.0)313 self.assertRaises(TypeError, getargs_d, 4.25+0j)314 self.assertEqual(getargs_d(Float()), 4.25)315 self.assertEqual(getargs_d(FloatSubclass(7.5)), 7.5)316 self.assertEqual(getargs_d(FloatSubclass2(7.5)), 7.5)317 self.assertRaises(TypeError, getargs_d, BadFloat())318 with self.assertWarns(DeprecationWarning):319 self.assertEqual(getargs_d(BadFloat2()), 4.25)320 self.assertEqual(getargs_d(BadFloat3(7.5)), 7.5)321 for x in (DBL_MIN, -DBL_MIN, DBL_MAX, -DBL_MAX, INF, -INF):322 self.assertEqual(getargs_d(x), x)323 self.assertRaises(OverflowError, getargs_d, 1<<DBL_MAX_EXP)324 self.assertRaises(OverflowError, getargs_d, -1<<DBL_MAX_EXP)325 self.assertEqualWithSign(getargs_d(0.0), 0.0)326 self.assertEqualWithSign(getargs_d(-0.0), -0.0)327 r = getargs_d(NAN)328 self.assertNotEqual(r, r)329 def test_D(self):330 from _testcapi import getargs_D331 self.assertEqual(getargs_D(4.25+0.5j), 4.25+0.5j)332 self.assertEqual(getargs_D(4.25), 4.25+0j)333 self.assertEqual(getargs_D(4), 4.0+0j)334 self.assertEqual(getargs_D(Complex()), 4.25+0.5j)335 self.assertEqual(getargs_D(ComplexSubclass(7.5+0.25j)), 7.5+0.25j)336 self.assertEqual(getargs_D(ComplexSubclass2(7.5+0.25j)), 7.5+0.25j)337 self.assertRaises(TypeError, getargs_D, BadComplex())338 self.assertEqual(getargs_D(BadComplex2()), 4.25+0.5j)339 self.assertEqual(getargs_D(BadComplex3(7.5+0.25j)), 7.5+0.25j)340 for x in (DBL_MIN, -DBL_MIN, DBL_MAX, -DBL_MAX, INF, -INF):341 c = complex(x, 1.0)342 self.assertEqual(getargs_D(c), c)343 c = complex(1.0, x)344 self.assertEqual(getargs_D(c), c)345 self.assertEqualWithSign(getargs_D(complex(0.0, 1.0)).real, 0.0)346 self.assertEqualWithSign(getargs_D(complex(-0.0, 1.0)).real, -0.0)347 self.assertEqualWithSign(getargs_D(complex(1.0, 0.0)).imag, 0.0)348 self.assertEqualWithSign(getargs_D(complex(1.0, -0.0)).imag, -0.0)349class Paradox:350 "This statement is false."351 def __bool__(self):352 raise NotImplementedError353class Boolean_TestCase(unittest.TestCase):354 def test_p(self):355 from _testcapi import getargs_p356 self.assertEqual(0, getargs_p(False))357 self.assertEqual(0, getargs_p(None))358 self.assertEqual(0, getargs_p(0))359 self.assertEqual(0, getargs_p(0.0))360 self.assertEqual(0, getargs_p(0j))361 self.assertEqual(0, getargs_p(''))362 self.assertEqual(0, getargs_p(()))363 self.assertEqual(0, getargs_p([]))364 self.assertEqual(0, getargs_p({}))365 self.assertEqual(1, getargs_p(True))366 self.assertEqual(1, getargs_p(1))367 self.assertEqual(1, getargs_p(1.0))368 self.assertEqual(1, getargs_p(1j))369 self.assertEqual(1, getargs_p('x'))370 self.assertEqual(1, getargs_p((1,)))371 self.assertEqual(1, getargs_p([1]))372 self.assertEqual(1, getargs_p({1:2}))373 self.assertEqual(1, getargs_p(unittest.TestCase))374 self.assertRaises(NotImplementedError, getargs_p, Paradox())375class Tuple_TestCase(unittest.TestCase):376 def test_args(self):377 from _testcapi import get_args378 ret = get_args(1, 2)379 self.assertEqual(ret, (1, 2))380 self.assertIs(type(ret), tuple)381 ret = get_args(1, *(2, 3))382 self.assertEqual(ret, (1, 2, 3))383 self.assertIs(type(ret), tuple)384 ret = get_args(*[1, 2])385 self.assertEqual(ret, (1, 2))386 self.assertIs(type(ret), tuple)387 ret = get_args(*TupleSubclass([1, 2]))388 self.assertEqual(ret, (1, 2))389 self.assertIs(type(ret), tuple)390 ret = get_args()391 self.assertIn(ret, ((), None))392 self.assertIn(type(ret), (tuple, type(None)))393 ret = get_args(*())394 self.assertIn(ret, ((), None))395 self.assertIn(type(ret), (tuple, type(None)))396 def test_tuple(self):397 from _testcapi import getargs_tuple398 ret = getargs_tuple(1, (2, 3))399 self.assertEqual(ret, (1,2,3))400 # make sure invalid tuple arguments are handled correctly401 class seq:402 def __len__(self):403 return 2404 def __getitem__(self, n):405 raise ValueError406 self.assertRaises(TypeError, getargs_tuple, 1, seq())407class Keywords_TestCase(unittest.TestCase):408 def test_kwargs(self):409 from _testcapi import get_kwargs410 ret = get_kwargs(a=1, b=2)411 self.assertEqual(ret, {'a': 1, 'b': 2})412 self.assertIs(type(ret), dict)413 ret = get_kwargs(a=1, **{'b': 2, 'c': 3})414 self.assertEqual(ret, {'a': 1, 'b': 2, 'c': 3})415 self.assertIs(type(ret), dict)416 ret = get_kwargs(**DictSubclass({'a': 1, 'b': 2}))417 self.assertEqual(ret, {'a': 1, 'b': 2})418 self.assertIs(type(ret), dict)419 ret = get_kwargs()420 self.assertIn(ret, ({}, None))421 self.assertIn(type(ret), (dict, type(None)))422 ret = get_kwargs(**{})423 self.assertIn(ret, ({}, None))424 self.assertIn(type(ret), (dict, type(None)))425 def test_positional_args(self):426 # using all positional args427 self.assertEqual(428 getargs_keywords((1,2), 3, (4,(5,6)), (7,8,9), 10),429 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)430 )431 def test_mixed_args(self):432 # positional and keyword args433 self.assertEqual(434 getargs_keywords((1,2), 3, (4,(5,6)), arg4=(7,8,9), arg5=10),435 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)436 )437 def test_keyword_args(self):438 # all keywords439 self.assertEqual(440 getargs_keywords(arg1=(1,2), arg2=3, arg3=(4,(5,6)), arg4=(7,8,9), arg5=10),441 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)442 )443 def test_optional_args(self):444 # missing optional keyword args, skipping tuples445 self.assertEqual(446 getargs_keywords(arg1=(1,2), arg2=3, arg5=10),447 (1, 2, 3, -1, -1, -1, -1, -1, -1, 10)448 )449 def test_required_args(self):450 # required arg missing451 try:452 getargs_keywords(arg1=(1,2))453 except TypeError as err:454 self.assertEqual(str(err), "Required argument 'arg2' (pos 2) not found")455 else:456 self.fail('TypeError should have been raised')457 def test_too_many_args(self):458 try:459 getargs_keywords((1,2),3,(4,(5,6)),(7,8,9),10,111)460 except TypeError as err:461 self.assertEqual(str(err), "function takes at most 5 arguments (6 given)")462 else:463 self.fail('TypeError should have been raised')464 def test_invalid_keyword(self):465 # extraneous keyword arg466 try:467 getargs_keywords((1,2),3,arg5=10,arg666=666)468 except TypeError as err:469 self.assertEqual(str(err), "'arg666' is an invalid keyword argument for this function")470 else:471 self.fail('TypeError should have been raised')472 def test_surrogate_keyword(self):473 try:474 getargs_keywords((1,2), 3, (4,(5,6)), (7,8,9), **{'\uDC80': 10})475 except TypeError as err:476 self.assertEqual(str(err), "'\udc80' is an invalid keyword argument for this function")477 else:478 self.fail('TypeError should have been raised')479class KeywordOnly_TestCase(unittest.TestCase):480 def test_positional_args(self):481 # using all possible positional args482 self.assertEqual(483 getargs_keyword_only(1, 2),484 (1, 2, -1)485 )486 def test_mixed_args(self):487 # positional and keyword args488 self.assertEqual(489 getargs_keyword_only(1, 2, keyword_only=3),490 (1, 2, 3)491 )492 def test_keyword_args(self):493 # all keywords494 self.assertEqual(495 getargs_keyword_only(required=1, optional=2, keyword_only=3),496 (1, 2, 3)497 )498 def test_optional_args(self):499 # missing optional keyword args, skipping tuples500 self.assertEqual(501 getargs_keyword_only(required=1, optional=2),502 (1, 2, -1)503 )504 self.assertEqual(505 getargs_keyword_only(required=1, keyword_only=3),506 (1, -1, 3)507 )508 def test_required_args(self):509 self.assertEqual(510 getargs_keyword_only(1),511 (1, -1, -1)512 )513 self.assertEqual(514 getargs_keyword_only(required=1),515 (1, -1, -1)516 )517 # required arg missing518 with self.assertRaisesRegex(TypeError,519 r"Required argument 'required' \(pos 1\) not found"):520 getargs_keyword_only(optional=2)521 with self.assertRaisesRegex(TypeError,522 r"Required argument 'required' \(pos 1\) not found"):523 getargs_keyword_only(keyword_only=3)524 def test_too_many_args(self):525 with self.assertRaisesRegex(TypeError,526 r"Function takes at most 2 positional arguments \(3 given\)"):527 getargs_keyword_only(1, 2, 3)528 with self.assertRaisesRegex(TypeError,529 r"function takes at most 3 arguments \(4 given\)"):530 getargs_keyword_only(1, 2, 3, keyword_only=5)531 def test_invalid_keyword(self):532 # extraneous keyword arg533 with self.assertRaisesRegex(TypeError,534 "'monster' is an invalid keyword argument for this function"):535 getargs_keyword_only(1, 2, monster=666)536 def test_surrogate_keyword(self):537 with self.assertRaisesRegex(TypeError,538 "'\udc80' is an invalid keyword argument for this function"):539 getargs_keyword_only(1, 2, **{'\uDC80': 10})540class PositionalOnlyAndKeywords_TestCase(unittest.TestCase):541 from _testcapi import getargs_positional_only_and_keywords as getargs542 def test_positional_args(self):543 # using all possible positional args544 self.assertEqual(self.getargs(1, 2, 3), (1, 2, 3))545 def test_mixed_args(self):546 # positional and keyword args547 self.assertEqual(self.getargs(1, 2, keyword=3), (1, 2, 3))548 def test_optional_args(self):549 # missing optional args550 self.assertEqual(self.getargs(1, 2), (1, 2, -1))551 self.assertEqual(self.getargs(1, keyword=3), (1, -1, 3))552 def test_required_args(self):553 self.assertEqual(self.getargs(1), (1, -1, -1))554 # required positional arg missing555 with self.assertRaisesRegex(TypeError,556 r"Function takes at least 1 positional arguments \(0 given\)"):557 self.getargs()558 with self.assertRaisesRegex(TypeError,559 r"Function takes at least 1 positional arguments \(0 given\)"):560 self.getargs(keyword=3)561 def test_empty_keyword(self):562 with self.assertRaisesRegex(TypeError,563 "'' is an invalid keyword argument for this function"):564 self.getargs(1, 2, **{'': 666})565class Bytes_TestCase(unittest.TestCase):566 def test_c(self):567 from _testcapi import getargs_c568 self.assertRaises(TypeError, getargs_c, b'abc') # len > 1569 self.assertEqual(getargs_c(b'a'), 97)570 self.assertEqual(getargs_c(bytearray(b'a')), 97)571 self.assertRaises(TypeError, getargs_c, memoryview(b'a'))572 self.assertRaises(TypeError, getargs_c, 's')573 self.assertRaises(TypeError, getargs_c, 97)574 self.assertRaises(TypeError, getargs_c, None)575 def test_y(self):576 from _testcapi import getargs_y577 self.assertRaises(TypeError, getargs_y, 'abc\xe9')578 self.assertEqual(getargs_y(b'bytes'), b'bytes')579 self.assertRaises(ValueError, getargs_y, b'nul:\0')580 self.assertRaises(TypeError, getargs_y, bytearray(b'bytearray'))581 self.assertRaises(TypeError, getargs_y, memoryview(b'memoryview'))582 self.assertRaises(TypeError, getargs_y, None)583 def test_y_star(self):584 from _testcapi import getargs_y_star585 self.assertRaises(TypeError, getargs_y_star, 'abc\xe9')586 self.assertEqual(getargs_y_star(b'bytes'), b'bytes')587 self.assertEqual(getargs_y_star(b'nul:\0'), b'nul:\0')588 self.assertEqual(getargs_y_star(bytearray(b'bytearray')), b'bytearray')589 self.assertEqual(getargs_y_star(memoryview(b'memoryview')), b'memoryview')590 self.assertRaises(TypeError, getargs_y_star, None)591 def test_y_hash(self):592 from _testcapi import getargs_y_hash593 self.assertRaises(TypeError, getargs_y_hash, 'abc\xe9')594 self.assertEqual(getargs_y_hash(b'bytes'), b'bytes')595 self.assertEqual(getargs_y_hash(b'nul:\0'), b'nul:\0')596 self.assertRaises(TypeError, getargs_y_hash, bytearray(b'bytearray'))597 self.assertRaises(TypeError, getargs_y_hash, memoryview(b'memoryview'))598 self.assertRaises(TypeError, getargs_y_hash, None)599 def test_w_star(self):600 # getargs_w_star() modifies first and last byte601 from _testcapi import getargs_w_star602 self.assertRaises(TypeError, getargs_w_star, 'abc\xe9')603 self.assertRaises(TypeError, getargs_w_star, b'bytes')604 self.assertRaises(TypeError, getargs_w_star, b'nul:\0')605 self.assertRaises(TypeError, getargs_w_star, memoryview(b'bytes'))606 buf = bytearray(b'bytearray')607 self.assertEqual(getargs_w_star(buf), b'[ytearra]')608 self.assertEqual(buf, bytearray(b'[ytearra]'))609 buf = bytearray(b'memoryview')610 self.assertEqual(getargs_w_star(memoryview(buf)), b'[emoryvie]')611 self.assertEqual(buf, bytearray(b'[emoryvie]'))612 self.assertRaises(TypeError, getargs_w_star, None)613class String_TestCase(unittest.TestCase):614 def test_C(self):615 from _testcapi import getargs_C616 self.assertRaises(TypeError, getargs_C, 'abc') # len > 1617 self.assertEqual(getargs_C('a'), 97)618 self.assertEqual(getargs_C('\u20ac'), 0x20ac)619 self.assertEqual(getargs_C('\U0001f40d'), 0x1f40d)620 self.assertRaises(TypeError, getargs_C, b'a')621 self.assertRaises(TypeError, getargs_C, bytearray(b'a'))622 self.assertRaises(TypeError, getargs_C, memoryview(b'a'))623 self.assertRaises(TypeError, getargs_C, 97)624 self.assertRaises(TypeError, getargs_C, None)625 def test_s(self):626 from _testcapi import getargs_s627 self.assertEqual(getargs_s('abc\xe9'), b'abc\xc3\xa9')628 self.assertRaises(ValueError, getargs_s, 'nul:\0')629 self.assertRaises(TypeError, getargs_s, b'bytes')630 self.assertRaises(TypeError, getargs_s, bytearray(b'bytearray'))631 self.assertRaises(TypeError, getargs_s, memoryview(b'memoryview'))632 self.assertRaises(TypeError, getargs_s, None)633 def test_s_star(self):634 from _testcapi import getargs_s_star635 self.assertEqual(getargs_s_star('abc\xe9'), b'abc\xc3\xa9')636 self.assertEqual(getargs_s_star('nul:\0'), b'nul:\0')637 self.assertEqual(getargs_s_star(b'bytes'), b'bytes')638 self.assertEqual(getargs_s_star(bytearray(b'bytearray')), b'bytearray')639 self.assertEqual(getargs_s_star(memoryview(b'memoryview')), b'memoryview')640 self.assertRaises(TypeError, getargs_s_star, None)641 def test_s_hash(self):642 from _testcapi import getargs_s_hash643 self.assertEqual(getargs_s_hash('abc\xe9'), b'abc\xc3\xa9')644 self.assertEqual(getargs_s_hash('nul:\0'), b'nul:\0')645 self.assertEqual(getargs_s_hash(b'bytes'), b'bytes')646 self.assertRaises(TypeError, getargs_s_hash, bytearray(b'bytearray'))647 self.assertRaises(TypeError, getargs_s_hash, memoryview(b'memoryview'))648 self.assertRaises(TypeError, getargs_s_hash, None)649 def test_z(self):650 from _testcapi import getargs_z651 self.assertEqual(getargs_z('abc\xe9'), b'abc\xc3\xa9')652 self.assertRaises(ValueError, getargs_z, 'nul:\0')653 self.assertRaises(TypeError, getargs_z, b'bytes')654 self.assertRaises(TypeError, getargs_z, bytearray(b'bytearray'))655 self.assertRaises(TypeError, getargs_z, memoryview(b'memoryview'))656 self.assertIsNone(getargs_z(None))657 def test_z_star(self):658 from _testcapi import getargs_z_star659 self.assertEqual(getargs_z_star('abc\xe9'), b'abc\xc3\xa9')660 self.assertEqual(getargs_z_star('nul:\0'), b'nul:\0')661 self.assertEqual(getargs_z_star(b'bytes'), b'bytes')662 self.assertEqual(getargs_z_star(bytearray(b'bytearray')), b'bytearray')663 self.assertEqual(getargs_z_star(memoryview(b'memoryview')), b'memoryview')664 self.assertIsNone(getargs_z_star(None))665 def test_z_hash(self):666 from _testcapi import getargs_z_hash667 self.assertEqual(getargs_z_hash('abc\xe9'), b'abc\xc3\xa9')668 self.assertEqual(getargs_z_hash('nul:\0'), b'nul:\0')669 self.assertEqual(getargs_z_hash(b'bytes'), b'bytes')670 self.assertRaises(TypeError, getargs_z_hash, bytearray(b'bytearray'))671 self.assertRaises(TypeError, getargs_z_hash, memoryview(b'memoryview'))672 self.assertIsNone(getargs_z_hash(None))673 def test_es(self):674 from _testcapi import getargs_es675 self.assertEqual(getargs_es('abc\xe9'), b'abc\xc3\xa9')676 self.assertEqual(getargs_es('abc\xe9', 'latin1'), b'abc\xe9')677 self.assertRaises(UnicodeEncodeError, getargs_es, 'abc\xe9', 'ascii')678 self.assertRaises(LookupError, getargs_es, 'abc\xe9', 'spam')679 self.assertRaises(TypeError, getargs_es, b'bytes', 'latin1')680 self.assertRaises(TypeError, getargs_es, bytearray(b'bytearray'), 'latin1')681 self.assertRaises(TypeError, getargs_es, memoryview(b'memoryview'), 'latin1')682 self.assertRaises(TypeError, getargs_es, None, 'latin1')683 self.assertRaises(TypeError, getargs_es, 'nul:\0', 'latin1')684 def test_et(self):685 from _testcapi import getargs_et686 self.assertEqual(getargs_et('abc\xe9'), b'abc\xc3\xa9')687 self.assertEqual(getargs_et('abc\xe9', 'latin1'), b'abc\xe9')688 self.assertRaises(UnicodeEncodeError, getargs_et, 'abc\xe9', 'ascii')689 self.assertRaises(LookupError, getargs_et, 'abc\xe9', 'spam')690 self.assertEqual(getargs_et(b'bytes', 'latin1'), b'bytes')691 self.assertEqual(getargs_et(bytearray(b'bytearray'), 'latin1'), b'bytearray')692 self.assertRaises(TypeError, getargs_et, memoryview(b'memoryview'), 'latin1')693 self.assertRaises(TypeError, getargs_et, None, 'latin1')694 self.assertRaises(TypeError, getargs_et, 'nul:\0', 'latin1')695 self.assertRaises(TypeError, getargs_et, b'nul:\0', 'latin1')696 self.assertRaises(TypeError, getargs_et, bytearray(b'nul:\0'), 'latin1')697 def test_es_hash(self):698 from _testcapi import getargs_es_hash699 self.assertEqual(getargs_es_hash('abc\xe9'), b'abc\xc3\xa9')700 self.assertEqual(getargs_es_hash('abc\xe9', 'latin1'), b'abc\xe9')701 self.assertRaises(UnicodeEncodeError, getargs_es_hash, 'abc\xe9', 'ascii')702 self.assertRaises(LookupError, getargs_es_hash, 'abc\xe9', 'spam')703 self.assertRaises(TypeError, getargs_es_hash, b'bytes', 'latin1')704 self.assertRaises(TypeError, getargs_es_hash, bytearray(b'bytearray'), 'latin1')705 self.assertRaises(TypeError, getargs_es_hash, memoryview(b'memoryview'), 'latin1')706 self.assertRaises(TypeError, getargs_es_hash, None, 'latin1')707 self.assertEqual(getargs_es_hash('nul:\0', 'latin1'), b'nul:\0')708 buf = bytearray(b'x'*8)709 self.assertEqual(getargs_es_hash('abc\xe9', 'latin1', buf), b'abc\xe9')710 self.assertEqual(buf, bytearray(b'abc\xe9\x00xxx'))711 buf = bytearray(b'x'*5)712 self.assertEqual(getargs_es_hash('abc\xe9', 'latin1', buf), b'abc\xe9')713 self.assertEqual(buf, bytearray(b'abc\xe9\x00'))714 buf = bytearray(b'x'*4)715 self.assertRaises(ValueError, getargs_es_hash, 'abc\xe9', 'latin1', buf)716 self.assertEqual(buf, bytearray(b'x'*4))717 buf = bytearray()718 self.assertRaises(ValueError, getargs_es_hash, 'abc\xe9', 'latin1', buf)719 def test_et_hash(self):720 from _testcapi import getargs_et_hash721 self.assertEqual(getargs_et_hash('abc\xe9'), b'abc\xc3\xa9')722 self.assertEqual(getargs_et_hash('abc\xe9', 'latin1'), b'abc\xe9')723 self.assertRaises(UnicodeEncodeError, getargs_et_hash, 'abc\xe9', 'ascii')724 self.assertRaises(LookupError, getargs_et_hash, 'abc\xe9', 'spam')725 self.assertEqual(getargs_et_hash(b'bytes', 'latin1'), b'bytes')726 self.assertEqual(getargs_et_hash(bytearray(b'bytearray'), 'latin1'), b'bytearray')727 self.assertRaises(TypeError, getargs_et_hash, memoryview(b'memoryview'), 'latin1')728 self.assertRaises(TypeError, getargs_et_hash, None, 'latin1')729 self.assertEqual(getargs_et_hash('nul:\0', 'latin1'), b'nul:\0')730 self.assertEqual(getargs_et_hash(b'nul:\0', 'latin1'), b'nul:\0')731 self.assertEqual(getargs_et_hash(bytearray(b'nul:\0'), 'latin1'), b'nul:\0')732 buf = bytearray(b'x'*8)733 self.assertEqual(getargs_et_hash('abc\xe9', 'latin1', buf), b'abc\xe9')734 self.assertEqual(buf, bytearray(b'abc\xe9\x00xxx'))735 buf = bytearray(b'x'*5)736 self.assertEqual(getargs_et_hash('abc\xe9', 'latin1', buf), b'abc\xe9')737 self.assertEqual(buf, bytearray(b'abc\xe9\x00'))738 buf = bytearray(b'x'*4)739 self.assertRaises(ValueError, getargs_et_hash, 'abc\xe9', 'latin1', buf)740 self.assertEqual(buf, bytearray(b'x'*4))741 buf = bytearray()742 self.assertRaises(ValueError, getargs_et_hash, 'abc\xe9', 'latin1', buf)743 def test_u(self):744 from _testcapi import getargs_u745 self.assertEqual(getargs_u('abc\xe9'), 'abc\xe9')746 self.assertRaises(ValueError, getargs_u, 'nul:\0')747 self.assertRaises(TypeError, getargs_u, b'bytes')748 self.assertRaises(TypeError, getargs_u, bytearray(b'bytearray'))749 self.assertRaises(TypeError, getargs_u, memoryview(b'memoryview'))750 self.assertRaises(TypeError, getargs_u, None)751 def test_u_hash(self):752 from _testcapi import getargs_u_hash753 self.assertEqual(getargs_u_hash('abc\xe9'), 'abc\xe9')754 self.assertEqual(getargs_u_hash('nul:\0'), 'nul:\0')755 self.assertRaises(TypeError, getargs_u_hash, b'bytes')756 self.assertRaises(TypeError, getargs_u_hash, bytearray(b'bytearray'))757 self.assertRaises(TypeError, getargs_u_hash, memoryview(b'memoryview'))758 self.assertRaises(TypeError, getargs_u_hash, None)759 def test_Z(self):760 from _testcapi import getargs_Z761 self.assertEqual(getargs_Z('abc\xe9'), 'abc\xe9')762 self.assertRaises(ValueError, getargs_Z, 'nul:\0')763 self.assertRaises(TypeError, getargs_Z, b'bytes')764 self.assertRaises(TypeError, getargs_Z, bytearray(b'bytearray'))765 self.assertRaises(TypeError, getargs_Z, memoryview(b'memoryview'))766 self.assertIsNone(getargs_Z(None))767 def test_Z_hash(self):768 from _testcapi import getargs_Z_hash769 self.assertEqual(getargs_Z_hash('abc\xe9'), 'abc\xe9')770 self.assertEqual(getargs_Z_hash('nul:\0'), 'nul:\0')771 self.assertRaises(TypeError, getargs_Z_hash, b'bytes')772 self.assertRaises(TypeError, getargs_Z_hash, bytearray(b'bytearray'))773 self.assertRaises(TypeError, getargs_Z_hash, memoryview(b'memoryview'))774 self.assertIsNone(getargs_Z_hash(None))775class Object_TestCase(unittest.TestCase):776 def test_S(self):777 from _testcapi import getargs_S778 obj = b'bytes'779 self.assertIs(getargs_S(obj), obj)780 self.assertRaises(TypeError, getargs_S, bytearray(b'bytearray'))781 self.assertRaises(TypeError, getargs_S, 'str')782 self.assertRaises(TypeError, getargs_S, None)783 self.assertRaises(TypeError, getargs_S, memoryview(obj))784 def test_Y(self):785 from _testcapi import getargs_Y786 obj = bytearray(b'bytearray')787 self.assertIs(getargs_Y(obj), obj)788 self.assertRaises(TypeError, getargs_Y, b'bytes')789 self.assertRaises(TypeError, getargs_Y, 'str')790 self.assertRaises(TypeError, getargs_Y, None)791 self.assertRaises(TypeError, getargs_Y, memoryview(obj))792 def test_U(self):793 from _testcapi import getargs_U794 obj = 'str'795 self.assertIs(getargs_U(obj), obj)796 self.assertRaises(TypeError, getargs_U, b'bytes')797 self.assertRaises(TypeError, getargs_U, bytearray(b'bytearray'))798 self.assertRaises(TypeError, getargs_U, None)799if __name__ == "__main__":...

Full Screen

Full Screen

test_operator.py

Source:test_operator.py Github

copy

Full Screen

...32 return other * self.lst33class OperatorTestCase:34 def test_lt(self):35 operator = self.module36 self.assertRaises(TypeError, operator.lt)37 self.assertRaises(TypeError, operator.lt, 1j, 2j)38 self.assertFalse(operator.lt(1, 0))39 self.assertFalse(operator.lt(1, 0.0))40 self.assertFalse(operator.lt(1, 1))41 self.assertFalse(operator.lt(1, 1.0))42 self.assertTrue(operator.lt(1, 2))43 self.assertTrue(operator.lt(1, 2.0))44 def test_le(self):45 operator = self.module46 self.assertRaises(TypeError, operator.le)47 self.assertRaises(TypeError, operator.le, 1j, 2j)48 self.assertFalse(operator.le(1, 0))49 self.assertFalse(operator.le(1, 0.0))50 self.assertTrue(operator.le(1, 1))51 self.assertTrue(operator.le(1, 1.0))52 self.assertTrue(operator.le(1, 2))53 self.assertTrue(operator.le(1, 2.0))54 def test_eq(self):55 operator = self.module56 class C(object):57 def __eq__(self, other):58 raise SyntaxError59 self.assertRaises(TypeError, operator.eq)60 self.assertRaises(SyntaxError, operator.eq, C(), C())61 self.assertFalse(operator.eq(1, 0))62 self.assertFalse(operator.eq(1, 0.0))63 self.assertTrue(operator.eq(1, 1))64 self.assertTrue(operator.eq(1, 1.0))65 self.assertFalse(operator.eq(1, 2))66 self.assertFalse(operator.eq(1, 2.0))67 def test_ne(self):68 operator = self.module69 class C(object):70 def __ne__(self, other):71 raise SyntaxError72 self.assertRaises(TypeError, operator.ne)73 self.assertRaises(SyntaxError, operator.ne, C(), C())74 self.assertTrue(operator.ne(1, 0))75 self.assertTrue(operator.ne(1, 0.0))76 self.assertFalse(operator.ne(1, 1))77 self.assertFalse(operator.ne(1, 1.0))78 self.assertTrue(operator.ne(1, 2))79 self.assertTrue(operator.ne(1, 2.0))80 def test_ge(self):81 operator = self.module82 self.assertRaises(TypeError, operator.ge)83 self.assertRaises(TypeError, operator.ge, 1j, 2j)84 self.assertTrue(operator.ge(1, 0))85 self.assertTrue(operator.ge(1, 0.0))86 self.assertTrue(operator.ge(1, 1))87 self.assertTrue(operator.ge(1, 1.0))88 self.assertFalse(operator.ge(1, 2))89 self.assertFalse(operator.ge(1, 2.0))90 def test_gt(self):91 operator = self.module92 self.assertRaises(TypeError, operator.gt)93 self.assertRaises(TypeError, operator.gt, 1j, 2j)94 self.assertTrue(operator.gt(1, 0))95 self.assertTrue(operator.gt(1, 0.0))96 self.assertFalse(operator.gt(1, 1))97 self.assertFalse(operator.gt(1, 1.0))98 self.assertFalse(operator.gt(1, 2))99 self.assertFalse(operator.gt(1, 2.0))100 def test_abs(self):101 operator = self.module102 self.assertRaises(TypeError, operator.abs)103 self.assertRaises(TypeError, operator.abs, None)104 self.assertEqual(operator.abs(-1), 1)105 self.assertEqual(operator.abs(1), 1)106 def test_add(self):107 operator = self.module108 self.assertRaises(TypeError, operator.add)109 self.assertRaises(TypeError, operator.add, None, None)110 self.assertEqual(operator.add(3, 4), 7)111 def test_bitwise_and(self):112 operator = self.module113 self.assertRaises(TypeError, operator.and_)114 self.assertRaises(TypeError, operator.and_, None, None)115 self.assertEqual(operator.and_(0xf, 0xa), 0xa)116 def test_concat(self):117 operator = self.module118 self.assertRaises(TypeError, operator.concat)119 self.assertRaises(TypeError, operator.concat, None, None)120 self.assertEqual(operator.concat('py', 'thon'), 'python')121 self.assertEqual(operator.concat([1, 2], [3, 4]), [1, 2, 3, 4])122 self.assertEqual(operator.concat(Seq1([5, 6]), Seq1([7])), [5, 6, 7])123 self.assertEqual(operator.concat(Seq2([5, 6]), Seq2([7])), [5, 6, 7])124 self.assertRaises(TypeError, operator.concat, 13, 29)125 def test_countOf(self):126 operator = self.module127 self.assertRaises(TypeError, operator.countOf)128 self.assertRaises(TypeError, operator.countOf, None, None)129 self.assertEqual(operator.countOf([1, 2, 1, 3, 1, 4], 3), 1)130 self.assertEqual(operator.countOf([1, 2, 1, 3, 1, 4], 5), 0)131 def test_delitem(self):132 operator = self.module133 a = [4, 3, 2, 1]134 self.assertRaises(TypeError, operator.delitem, a)135 self.assertRaises(TypeError, operator.delitem, a, None)136 self.assertIsNone(operator.delitem(a, 1))137 self.assertEqual(a, [4, 2, 1])138 def test_floordiv(self):139 operator = self.module140 self.assertRaises(TypeError, operator.floordiv, 5)141 self.assertRaises(TypeError, operator.floordiv, None, None)142 self.assertEqual(operator.floordiv(5, 2), 2)143 def test_truediv(self):144 operator = self.module145 self.assertRaises(TypeError, operator.truediv, 5)146 self.assertRaises(TypeError, operator.truediv, None, None)147 self.assertEqual(operator.truediv(5, 2), 2.5)148 def test_getitem(self):149 operator = self.module150 a = range(10)151 self.assertRaises(TypeError, operator.getitem)152 self.assertRaises(TypeError, operator.getitem, a, None)153 self.assertEqual(operator.getitem(a, 2), 2)154 def test_indexOf(self):155 operator = self.module156 self.assertRaises(TypeError, operator.indexOf)157 self.assertRaises(TypeError, operator.indexOf, None, None)158 self.assertEqual(operator.indexOf([4, 3, 2, 1], 3), 1)159 self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0)160 def test_invert(self):161 operator = self.module162 self.assertRaises(TypeError, operator.invert)163 self.assertRaises(TypeError, operator.invert, None)164 self.assertEqual(operator.inv(4), -5)165 def test_lshift(self):166 operator = self.module167 self.assertRaises(TypeError, operator.lshift)168 self.assertRaises(TypeError, operator.lshift, None, 42)169 self.assertEqual(operator.lshift(5, 1), 10)170 self.assertEqual(operator.lshift(5, 0), 5)171 self.assertRaises(ValueError, operator.lshift, 2, -1)172 def test_mod(self):173 operator = self.module174 self.assertRaises(TypeError, operator.mod)175 self.assertRaises(TypeError, operator.mod, None, 42)176 self.assertEqual(operator.mod(5, 2), 1)177 def test_mul(self):178 operator = self.module179 self.assertRaises(TypeError, operator.mul)180 self.assertRaises(TypeError, operator.mul, None, None)181 self.assertEqual(operator.mul(5, 2), 10)182 def test_matmul(self):183 operator = self.module184 self.assertRaises(TypeError, operator.matmul)185 self.assertRaises(TypeError, operator.matmul, 42, 42)186 class M:187 def __matmul__(self, other):188 return other - 1189 self.assertEqual(M() @ 42, 41)190 def test_neg(self):191 operator = self.module192 self.assertRaises(TypeError, operator.neg)193 self.assertRaises(TypeError, operator.neg, None)194 self.assertEqual(operator.neg(5), -5)195 self.assertEqual(operator.neg(-5), 5)196 self.assertEqual(operator.neg(0), 0)197 self.assertEqual(operator.neg(-0), 0)198 def test_bitwise_or(self):199 operator = self.module200 self.assertRaises(TypeError, operator.or_)201 self.assertRaises(TypeError, operator.or_, None, None)202 self.assertEqual(operator.or_(0xa, 0x5), 0xf)203 def test_pos(self):204 operator = self.module205 self.assertRaises(TypeError, operator.pos)206 self.assertRaises(TypeError, operator.pos, None)207 self.assertEqual(operator.pos(5), 5)208 self.assertEqual(operator.pos(-5), -5)209 self.assertEqual(operator.pos(0), 0)210 self.assertEqual(operator.pos(-0), 0)211 def test_pow(self):212 operator = self.module213 self.assertRaises(TypeError, operator.pow)214 self.assertRaises(TypeError, operator.pow, None, None)215 self.assertEqual(operator.pow(3,5), 3**5)216 self.assertRaises(TypeError, operator.pow, 1)217 self.assertRaises(TypeError, operator.pow, 1, 2, 3)218 def test_rshift(self):219 operator = self.module220 self.assertRaises(TypeError, operator.rshift)221 self.assertRaises(TypeError, operator.rshift, None, 42)222 self.assertEqual(operator.rshift(5, 1), 2)223 self.assertEqual(operator.rshift(5, 0), 5)224 self.assertRaises(ValueError, operator.rshift, 2, -1)225 def test_contains(self):226 operator = self.module227 self.assertRaises(TypeError, operator.contains)228 self.assertRaises(TypeError, operator.contains, None, None)229 self.assertTrue(operator.contains(range(4), 2))230 self.assertFalse(operator.contains(range(4), 5))231 def test_setitem(self):232 operator = self.module233 a = list(range(3))234 self.assertRaises(TypeError, operator.setitem, a)235 self.assertRaises(TypeError, operator.setitem, a, None, None)236 self.assertIsNone(operator.setitem(a, 0, 2))237 self.assertEqual(a, [2, 1, 2])238 self.assertRaises(IndexError, operator.setitem, a, 4, 2)239 def test_sub(self):240 operator = self.module241 self.assertRaises(TypeError, operator.sub)242 self.assertRaises(TypeError, operator.sub, None, None)243 self.assertEqual(operator.sub(5, 2), 3)244 def test_truth(self):245 operator = self.module246 class C(object):247 def __bool__(self):248 raise SyntaxError249 self.assertRaises(TypeError, operator.truth)250 self.assertRaises(SyntaxError, operator.truth, C())251 self.assertTrue(operator.truth(5))252 self.assertTrue(operator.truth([0]))253 self.assertFalse(operator.truth(0))254 self.assertFalse(operator.truth([]))255 def test_bitwise_xor(self):256 operator = self.module257 self.assertRaises(TypeError, operator.xor)258 self.assertRaises(TypeError, operator.xor, None, None)259 self.assertEqual(operator.xor(0xb, 0xc), 0x7)260 def test_is(self):261 operator = self.module262 a = b = 'xyzpdq'263 c = a[:3] + b[3:]264 self.assertRaises(TypeError, operator.is_)265 self.assertTrue(operator.is_(a, b))266 self.assertFalse(operator.is_(a,c))267 def test_is_not(self):268 operator = self.module269 a = b = 'xyzpdq'270 c = a[:3] + b[3:]271 self.assertRaises(TypeError, operator.is_not)272 self.assertFalse(operator.is_not(a, b))273 self.assertTrue(operator.is_not(a,c))274 def test_attrgetter(self):275 operator = self.module276 class A:277 pass278 a = A()279 a.name = 'arthur'280 f = operator.attrgetter('name')281 self.assertEqual(f(a), 'arthur')282 self.assertRaises(TypeError, f)283 self.assertRaises(TypeError, f, a, 'dent')284 self.assertRaises(TypeError, f, a, surname='dent')285 f = operator.attrgetter('rank')286 self.assertRaises(AttributeError, f, a)287 self.assertRaises(TypeError, operator.attrgetter, 2)288 self.assertRaises(TypeError, operator.attrgetter)289 # multiple gets290 record = A()291 record.x = 'X'292 record.y = 'Y'293 record.z = 'Z'294 self.assertEqual(operator.attrgetter('x','z','y')(record), ('X', 'Z', 'Y'))295 self.assertRaises(TypeError, operator.attrgetter, ('x', (), 'y'))296 class C(object):297 def __getattr__(self, name):298 raise SyntaxError299 self.assertRaises(SyntaxError, operator.attrgetter('foo'), C())300 # recursive gets301 a = A()302 a.name = 'arthur'303 a.child = A()304 a.child.name = 'thomas'305 f = operator.attrgetter('child.name')306 self.assertEqual(f(a), 'thomas')307 self.assertRaises(AttributeError, f, a.child)308 f = operator.attrgetter('name', 'child.name')309 self.assertEqual(f(a), ('arthur', 'thomas'))310 f = operator.attrgetter('name', 'child.name', 'child.child.name')311 self.assertRaises(AttributeError, f, a)312 f = operator.attrgetter('child.')313 self.assertRaises(AttributeError, f, a)314 f = operator.attrgetter('.child')315 self.assertRaises(AttributeError, f, a)316 a.child.child = A()317 a.child.child.name = 'johnson'318 f = operator.attrgetter('child.child.name')319 self.assertEqual(f(a), 'johnson')320 f = operator.attrgetter('name', 'child.name', 'child.child.name')321 self.assertEqual(f(a), ('arthur', 'thomas', 'johnson'))322 def test_itemgetter(self):323 operator = self.module324 a = 'ABCDE'325 f = operator.itemgetter(2)326 self.assertEqual(f(a), 'C')327 self.assertRaises(TypeError, f)328 self.assertRaises(TypeError, f, a, 3)329 self.assertRaises(TypeError, f, a, size=3)330 f = operator.itemgetter(10)331 self.assertRaises(IndexError, f, a)332 class C(object):333 def __getitem__(self, name):334 raise SyntaxError335 self.assertRaises(SyntaxError, operator.itemgetter(42), C())336 f = operator.itemgetter('name')337 self.assertRaises(TypeError, f, a)338 self.assertRaises(TypeError, operator.itemgetter)339 d = dict(key='val')340 f = operator.itemgetter('key')341 self.assertEqual(f(d), 'val')342 f = operator.itemgetter('nonkey')343 self.assertRaises(KeyError, f, d)344 # example used in the docs345 inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]346 getcount = operator.itemgetter(1)347 self.assertEqual(list(map(getcount, inventory)), [3, 2, 5, 1])348 self.assertEqual(sorted(inventory, key=getcount),349 [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)])350 # multiple gets351 data = list(map(str, range(20)))352 self.assertEqual(operator.itemgetter(2,10,5)(data), ('2', '10', '5'))353 self.assertRaises(TypeError, operator.itemgetter(2, 'x', 5), data)354 def test_methodcaller(self):355 operator = self.module356 self.assertRaises(TypeError, operator.methodcaller)357 self.assertRaises(TypeError, operator.methodcaller, 12)358 class A:359 def foo(self, *args, **kwds):360 return args[0] + args[1]361 def bar(self, f=42):362 return f363 def baz(*args, **kwds):364 return kwds['name'], kwds['self']365 a = A()366 f = operator.methodcaller('foo')367 self.assertRaises(IndexError, f, a)368 f = operator.methodcaller('foo', 1, 2)369 self.assertEqual(f(a), 3)370 self.assertRaises(TypeError, f)371 self.assertRaises(TypeError, f, a, 3)372 self.assertRaises(TypeError, f, a, spam=3)373 f = operator.methodcaller('bar')374 self.assertEqual(f(a), 42)375 self.assertRaises(TypeError, f, a, a)376 f = operator.methodcaller('bar', f=5)377 self.assertEqual(f(a), 5)378 f = operator.methodcaller('baz', name='spam', self='eggs')379 self.assertEqual(f(a), ('spam', 'eggs'))380 def test_inplace(self):381 operator = self.module382 class C(object):383 def __iadd__ (self, other): return "iadd"384 def __iand__ (self, other): return "iand"385 def __ifloordiv__(self, other): return "ifloordiv"386 def __ilshift__ (self, other): return "ilshift"387 def __imod__ (self, other): return "imod"388 def __imul__ (self, other): return "imul"389 def __imatmul__ (self, other): return "imatmul"390 def __ior__ (self, other): return "ior"391 def __ipow__ (self, other): return "ipow"392 def __irshift__ (self, other): return "irshift"393 def __isub__ (self, other): return "isub"394 def __itruediv__ (self, other): return "itruediv"395 def __ixor__ (self, other): return "ixor"396 def __getitem__(self, other): return 5 # so that C is a sequence397 c = C()398 self.assertEqual(operator.iadd (c, 5), "iadd")399 self.assertEqual(operator.iand (c, 5), "iand")400 self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv")401 self.assertEqual(operator.ilshift (c, 5), "ilshift")402 self.assertEqual(operator.imod (c, 5), "imod")403 self.assertEqual(operator.imul (c, 5), "imul")404 self.assertEqual(operator.imatmul (c, 5), "imatmul")405 self.assertEqual(operator.ior (c, 5), "ior")406 self.assertEqual(operator.ipow (c, 5), "ipow")407 self.assertEqual(operator.irshift (c, 5), "irshift")408 self.assertEqual(operator.isub (c, 5), "isub")409 self.assertEqual(operator.itruediv (c, 5), "itruediv")410 self.assertEqual(operator.ixor (c, 5), "ixor")411 self.assertEqual(operator.iconcat (c, c), "iadd")412 def test_length_hint(self):413 operator = self.module414 class X(object):415 def __init__(self, value):416 self.value = value417 def __length_hint__(self):418 if type(self.value) is type:419 raise self.value420 else:421 return self.value422 self.assertEqual(operator.length_hint([], 2), 0)423 self.assertEqual(operator.length_hint(iter([1, 2, 3])), 3)424 self.assertEqual(operator.length_hint(X(2)), 2)425 self.assertEqual(operator.length_hint(X(NotImplemented), 4), 4)426 self.assertEqual(operator.length_hint(X(TypeError), 12), 12)427 with self.assertRaises(TypeError):428 operator.length_hint(X("abc"))429 with self.assertRaises(ValueError):430 operator.length_hint(X(-2))431 with self.assertRaises(LookupError):432 operator.length_hint(X(LookupError))433 def test_dunder_is_original(self):434 operator = self.module435 names = [name for name in dir(operator) if not name.startswith('_')]436 for name in names:437 orig = getattr(operator, name)438 dunder = getattr(operator, '__' + name.strip('_') + '__', None)439 if dunder:440 self.assertIs(dunder, orig)441class PyOperatorTestCase(OperatorTestCase, unittest.TestCase):442 module = py_operator443@unittest.skipUnless(c_operator, 'requires _operator')444class COperatorTestCase(OperatorTestCase, unittest.TestCase):445 module = c_operator...

Full Screen

Full Screen

test_util.py

Source:test_util.py Github

copy

Full Screen

...23 def test_init(self):24 a = zeros(0)25 self.assertEqual(a, bitarray(''))26 self.assertEqual(a.endian(), 'big')27 self.assertRaises(TypeError, zeros, 1.0)28 for n in range(100):29 a = zeros(n)30 self.assertEqual(a, bitarray(n * '0'))31 self.assertRaises(TypeError, zeros) # no argument32 # wrong arguments33 self.assertRaises(TypeError, zeros, '')34 self.assertRaises(TypeError, zeros, bitarray())35 self.assertRaises(TypeError, zeros, [])36 self.assertRaises(ValueError, zeros, -1)37 self.assertRaises(TypeError, zeros, 0, 1) # endian not string38 self.assertRaises(ValueError, zeros, 0, 'foo') # endian wrong string39 def test_endian(self):40 for endian in 'big', 'little':41 a = zeros(1, endian)42 self.assertEqual(a, bitarray('0'))43 self.assertEqual(a.endian(), endian)44 a = zeros(1, endian=endian)45 self.assertEqual(a, bitarray('0'))46 self.assertEqual(a.endian(), endian)47tests.append(TestsZeros)48# ---------------------------------------------------------------------------49class TestsHelpers(unittest.TestCase, Util):50 def test_rindex(self):51 self.assertRaises(TypeError, rindex)52 self.assertRaises(TypeError, rindex, None)53 self.assertRaises(TypeError, rindex, bitarray(), 1, 2)54 for endian in 'big', 'little':55 a = bitarray('00010110000', endian)56 self.assertEqual(rindex(a), 6)57 self.assertEqual(rindex(a, 1), 6)58 self.assertEqual(rindex(a, 'A'), 6)59 self.assertEqual(rindex(a, True), 6)60 a = bitarray('00010110111', endian)61 self.assertEqual(rindex(a, 0), 7)62 self.assertEqual(rindex(a, None), 7)63 self.assertEqual(rindex(a, False), 7)64 a = frozenbitarray('00010110111', endian)65 self.assertEqual(rindex(a, 0), 7)66 self.assertEqual(rindex(a, None), 7)67 self.assertEqual(rindex(a, False), 7)68 for v in 0, 1:69 self.assertRaises(ValueError, rindex,70 bitarray(0, endian), v)71 self.assertRaises(ValueError, rindex,72 bitarray('000', endian), 1)73 self.assertRaises(ValueError, rindex,74 bitarray('11111', endian), 0)75 def test_rindex2(self):76 for a in self.randombitarrays():77 v = randint(0, 1)78 try:79 i = rindex(a, v)80 except ValueError:81 i = None82 s = a.to01()83 try:84 j = s.rindex(str(v))85 except ValueError:86 j = None87 self.assertEqual(i, j)88 def test_rindex3(self):89 for _ in range(100):90 n = randint(1, 100000)91 v = randint(0, 1)92 a = bitarray(n)93 a.setall(1 - v)94 lst = [randint(0, n - 1) for _ in range(100)]95 for i in lst:96 a[i] = v97 self.assertEqual(rindex(a, v), max(lst))98 def test_rindex4(self):99 for _ in range(100):100 N = randint(1, 10000)101 a = bitarray(N)102 a.setall(0)103 a[randint(0, N - 1)] = 1104 self.assertEqual(rindex(a), a.index(1))105 def test_strip1(self):106 self.assertRaises(TypeError, strip, bitarray(), 123)107 self.assertRaises(ValueError, strip, bitarray(), 'up')108 for endian in 'big', 'little':109 a = bitarray('00010110000', endian)110 self.assertEQUAL(strip(a), bitarray('0001011', endian))111 self.assertEQUAL(strip(a, 'left'), bitarray('10110000', endian))112 self.assertEQUAL(strip(a, 'both'), bitarray('1011', endian))113 for mode in 'left', 'right', 'both':114 self.assertEqual(strip(bitarray('000'), mode), bitarray())115 self.assertEqual(strip(bitarray(), mode), bitarray())116 def test_strip2(self):117 for a in self.randombitarrays():118 b = a.copy()119 s = a.to01()120 self.assertEqual(strip(a, 'left'), bitarray(s.lstrip('0')))121 self.assertEqual(strip(a, 'right'), bitarray(s.rstrip('0')))122 self.assertEqual(strip(a, 'both'), bitarray(s.strip('0')))123 self.assertEQUAL(a, b)124 def test_strip_both(self):125 for _ in range(100):126 N = randint(1, 10000)127 a = bitarray(N)128 a.setall(0)129 a[randint(0, N - 1)] = 1130 self.assertEqual(strip(a, 'both'), bitarray('1'))131 def check_result(self, a, n, i):132 self.assertEqual(a.count(1, 0, i), n)133 if i:134 self.assertTrue(a[i - 1])135 def test_count_n1(self):136 a = bitarray('111110111110111110111110011110111110111110111000')137 b = a.copy()138 self.assertEqual(len(a), 48)139 self.assertEqual(a.count(), 37)140 self.assertRaises(TypeError, count_n, '', 0)141 self.assertEqual(count_n(a, 0), 0)142 self.assertEqual(count_n(a, 20), 23)143 self.assertEqual(count_n(a, 37), 45)144 self.assertRaises(ValueError, count_n, a, -1) # n < 0145 self.assertRaises(ValueError, count_n, a, 38) # n > a.count()146 self.assertRaises(ValueError, count_n, a, 49) # n > len(a)147 self.assertRaises(TypeError, count_n, a, "7")148 for n in range(0, 37):149 i = count_n(a, n)150 self.check_result(a, n, i)151 self.assertEqual(a[:i].count(), n)152 self.assertEQUAL(a, b)153 def test_count_n1_frozen(self):154 a = frozenbitarray('001111101111101111101111100111100')155 self.assertEqual(len(a), 33)156 self.assertEqual(a.count(), 24)157 self.assertRaises(TypeError, count_n, '', 0)158 self.assertEqual(count_n(a, 0), 0)159 self.assertEqual(count_n(a, 10), 13)160 self.assertEqual(count_n(a, 24), 31)161 self.assertRaises(ValueError, count_n, a, -1) # n < 0162 self.assertRaises(ValueError, count_n, a, 25) # n > a.count()163 self.assertRaises(ValueError, count_n, a, 34) # n > len(a)164 self.assertRaises(TypeError, count_n, a, "7")165 def test_count_n2(self):166 for N in list(range(100)) + [1000, 10000, 100000]:167 a = bitarray(N)168 v = randint(0, 1)169 a.setall(v - 1)170 for _ in range(randint(0, min(N, 100))):171 a[randint(0, N - 1)] = v172 n = randint(0, a.count())173 self.check_result(a, n, count_n(a, n))174 # check for total count175 tc = a.count()176 self.assertTrue(count_n(a, tc) <= N)177 self.assertRaises(ValueError, count_n, a, tc + 1)178 def test_count_n3(self):179 N = 100000180 for _ in range(10):181 a = bitarray(N)182 a.setall(0)183 self.assertEqual(count_n(a, 0), 0)184 self.assertRaises(ValueError, count_n, a, 1)185 i = randint(0, N - 1)186 a[i] = 1187 self.assertEqual(count_n(a, 1), i + 1)188 self.assertRaises(ValueError, count_n, a, 2)189 def test_count_n4(self):190 for a in self.randombitarrays():191 n = a.count() // 2192 i = count_n(a, n)193 self.check_result(a, n, i)194tests.append(TestsHelpers)195# ---------------------------------------------------------------------------196class TestsBitwiseCount(unittest.TestCase, Util):197 def test_bit_count1(self):198 a = bitarray('001111')199 aa = a.copy()200 b = bitarray('010011')201 bb = b.copy()202 self.assertEqual(count_and(a, b), 2)203 self.assertEqual(count_or(a, b), 5)204 self.assertEqual(count_xor(a, b), 3)205 for f in count_and, count_or, count_xor:206 # not two arguments207 self.assertRaises(TypeError, f)208 self.assertRaises(TypeError, f, a)209 self.assertRaises(TypeError, f, a, b, 3)210 # wrong argument types211 self.assertRaises(TypeError, f, a, '')212 self.assertRaises(TypeError, f, '1', b)213 self.assertRaises(TypeError, f, a, 4)214 self.assertEQUAL(a, aa)215 self.assertEQUAL(b, bb)216 b.append(1)217 for f in count_and, count_or, count_xor:218 self.assertRaises(ValueError, f, a, b)219 self.assertRaises(ValueError, f,220 bitarray('110', 'big'),221 bitarray('101', 'little'))222 def test_bit_count_frozen(self):223 a = frozenbitarray('001111')224 b = frozenbitarray('010011')225 self.assertEqual(count_and(a, b), 2)226 self.assertEqual(count_or(a, b), 5)227 self.assertEqual(count_xor(a, b), 3)228 def test_bit_count2(self):229 for n in list(range(50)) + [randint(1000, 2000)]:230 a = bitarray()231 a.frombytes(os.urandom(bits2bytes(n)))232 del a[n:]233 b = bitarray()234 b.frombytes(os.urandom(bits2bytes(n)))235 del b[n:]236 self.assertEqual(count_and(a, b), (a & b).count())237 self.assertEqual(count_or(a, b), (a | b).count())238 self.assertEqual(count_xor(a, b), (a ^ b).count())239tests.append(TestsBitwiseCount)240# ---------------------------------------------------------------------------241class TestsSubset(unittest.TestCase, Util):242 def test_subset(self):243 a = frozenbitarray('0101')244 b = bitarray('0111')245 self.assertTrue(subset(a, b))246 self.assertFalse(subset(b, a))247 self.assertRaises(TypeError, subset)248 self.assertRaises(TypeError, subset, a, '')249 self.assertRaises(TypeError, subset, '1', b)250 self.assertRaises(TypeError, subset, a, 4)251 b.append(1)252 self.assertRaises(ValueError, subset, a, b)253 def subset_simple(self, a, b):254 return (a & b).count() == a.count()255 def test_subset_True(self):256 for a, b in [('', ''), ('0', '1'), ('0', '0'), ('1', '1'),257 ('000', '111'), ('0101', '0111'),258 ('000010111', '010011111')]:259 a, b = bitarray(a), bitarray(b)260 self.assertTrue(subset(a, b) is True)261 self.assertTrue(self.subset_simple(a, b) is True)262 def test_subset_False(self):263 for a, b in [('1', '0'), ('1101', '0111'),264 ('0000101111', '0100111011')]:265 a, b = bitarray(a), bitarray(b)266 self.assertTrue(subset(a, b) is False)267 self.assertTrue(self.subset_simple(a, b) is False)268 def test_many(self):269 for a in self.randombitarrays(start=1):270 b = a.copy()271 # we set one random bit in b to 1, so a is always a subset of b272 b[randint(0, len(a)-1)] = 1273 self.assertTrue(subset(a, b))274 # but b in not always a subset of a275 self.assertEqual(subset(b, a), self.subset_simple(b, a))276 # we set all bits in a, which ensures that b is a subset of a277 a.setall(1)278 self.assertTrue(subset(b, a))279tests.append(TestsSubset)280# ---------------------------------------------------------------------------281class TestsHexlify(unittest.TestCase, Util):282 def test_ba2hex(self):283 self.assertEqual(ba2hex(bitarray(0, 'big')), '')284 self.assertEqual(ba2hex(bitarray('1110', 'big')), 'e')285 self.assertEqual(ba2hex(bitarray('00000001', 'big')), '01')286 self.assertEqual(ba2hex(bitarray('10000000', 'big')), '80')287 self.assertEqual(ba2hex(frozenbitarray('11000111', 'big')), 'c7')288 # length not multiple of 4289 self.assertRaises(ValueError, ba2hex, bitarray('10'))290 self.assertRaises(ValueError, ba2hex, bitarray(endian='little'))291 self.assertRaises(TypeError, ba2hex, '101')292 c = ba2hex(bitarray('1101'))293 self.assertIsInstance(c, str)294 for n in range(7):295 a = bitarray(n * '1111', 'big')296 b = a.copy()297 self.assertEqual(ba2hex(a), n * 'f')298 # ensure original object wasn't altered299 self.assertEQUAL(a, b)300 def test_hex2ba(self):301 self.assertEqual(hex2ba(''), bitarray())302 for c in 'e', 'E', b'e', b'E', unicode('e'), unicode('E'):303 a = hex2ba(c)304 self.assertEqual(a.to01(), '1110')305 self.assertEqual(a.endian(), 'big')306 self.assertEQUAL(hex2ba('01'), bitarray('00000001', 'big'))307 self.assertRaises(Exception, hex2ba, '01a7x89')308 self.assertRaises(TypeError, hex2ba, 0)309 def test_explicit(self):310 for h, bs in [('', ''),311 ('0', '0000'),312 ('a', '1010'),313 ('f', '1111'),314 ('1a', '00011010'),315 ('2b', '00101011'),316 ('4c1', '010011000001'),317 ('a7d', '101001111101')]:318 a = bitarray(bs, 'big')319 self.assertEQUAL(hex2ba(h), a)320 self.assertEqual(ba2hex(a), h)321 def test_round_trip(self):322 for i in range(100):323 s = ''.join(choice(hexdigits) for _ in range(randint(0, 1000)))324 t = ba2hex(hex2ba(s))325 self.assertEqual(t, s.lower())326 def test_round_trip2(self):327 for a in self.randombitarrays():328 if len(a) % 4 or a.endian() != 'big':329 self.assertRaises(ValueError, ba2hex, a)330 continue331 b = hex2ba(ba2hex(a))332 self.assertEQUAL(b, a)333tests.append(TestsHexlify)334# ---------------------------------------------------------------------------335class TestsIntegerization(unittest.TestCase, Util):336 def test_ba2int(self):337 self.assertEqual(ba2int(bitarray('0')), 0)338 self.assertEqual(ba2int(bitarray('1')), 1)339 self.assertEqual(ba2int(bitarray('00101', 'big')), 5)340 self.assertEqual(ba2int(bitarray('00101', 'little')), 20)341 self.assertEqual(ba2int(frozenbitarray('11')), 3)342 self.assertRaises(ValueError, ba2int, bitarray())343 self.assertRaises(ValueError, ba2int, frozenbitarray())344 self.assertRaises(TypeError, ba2hex, '101')345 a = bitarray('111')346 b = a.copy()347 self.assertEqual(ba2int(a), 7)348 # ensure original object wasn't altered349 self.assertEQUAL(a, b)350 def test_int2ba(self):351 self.assertEqual(int2ba(0), bitarray('0'))352 self.assertEqual(int2ba(1), bitarray('1'))353 self.assertEqual(int2ba(5), bitarray('101'))354 self.assertEQUAL(int2ba(6), bitarray('110', 'big'))355 self.assertEQUAL(int2ba(6, endian='little'),356 bitarray('011', 'little'))357 self.assertRaises(ValueError, int2ba, -1)358 self.assertRaises(TypeError, int2ba, 1.0)359 self.assertRaises(TypeError, int2ba, 1, 3.0)360 self.assertRaises(ValueError, int2ba, 1, 0)361 self.assertRaises(TypeError, int2ba, 1, 10, 123)362 self.assertRaises(ValueError, int2ba, 1, 10, 'asd')363 def test_int2ba_length(self):364 self.assertRaises(TypeError, int2ba, 0, 1.0)365 self.assertRaises(ValueError, int2ba, 0, 0)366 self.assertEqual(int2ba(5, length=6), bitarray('000101'))367 self.assertRaises(OverflowError, int2ba, 3, 1)368 for n in range(1, 100):369 ab = int2ba(1, n, 'big')370 al = int2ba(1, n, 'little')371 self.assertEqual(ab.endian(), 'big')372 self.assertEqual(al.endian(), 'little')373 self.assertEqual(len(ab), n),374 self.assertEqual(len(al), n)375 self.assertEqual(ab, bitarray((n - 1) * '0') + bitarray('1'))376 self.assertEqual(al, bitarray('1') + bitarray((n - 1) * '0'))377 ab = int2ba(0, n, 'big')378 al = int2ba(0, n, 'little')379 self.assertEqual(len(ab), n)380 self.assertEqual(len(al), n)381 self.assertEqual(ab, bitarray(n * '0', 'big'))382 self.assertEqual(al, bitarray(n * '0', 'little'))383 self.assertRaises(OverflowError, int2ba, 2 ** n, n, 'big')384 self.assertRaises(OverflowError, int2ba, 2 ** n, n, 'little')385 self.assertEqual(int2ba(2 ** n - 1), bitarray(n * '1'))386 self.assertEqual(int2ba(2 ** n - 1, endian='little'),387 bitarray(n * '1'))388 def test_explicit(self):389 for i, sa in [( 0, '0'), (1, '1'),390 ( 2, '10'), (3, '11'),391 (25, '11001'), (265, '100001001'),392 (3691038, '1110000101001000011110')]:393 ab = bitarray(sa, 'big')394 al = bitarray(sa[::-1], 'little')395 self.assertEQUAL(int2ba(i), ab)396 self.assertEQUAL(int2ba(i, endian='little'), al)397 self.assertEqual(ba2int(ab), ba2int(al), i)398 if i == 0 or i >= 512:399 continue400 for n in range(9, 32):401 for endian in 'big', 'little':402 a = int2ba(i, length=n, endian=endian)403 self.assertEqual(a.endian(), endian)404 self.assertEqual(len(a), n)405 if endian == 'big':406 f = a.index(1)407 self.assertEqual(a[:f], bitarray(f * '0'))408 self.assertEqual(a[f:], ab)409 def check_round_trip(self, i):410 for endian in 'big', 'little':411 a = int2ba(i, endian=endian)412 self.assertEqual(a.endian(), endian)413 self.assertTrue(len(a) > 0)414 # ensure we have no leading zeros415 if a.endian == 'big':416 self.assertTrue(len(a) == 1 or a.index(1) == 0)417 self.assertEqual(ba2int(a), i)418 if i > 0 and sys.version_info[:2] >= (2, 7):419 self.assertEqual(i.bit_length(), len(a))420 # add a few / trailing leading zeros to bitarray421 if endian == 'big':422 a = zeros(randint(0, 3), endian) + a423 else:424 a = a + zeros(randint(0, 3), endian)425 self.assertEqual(a.endian(), endian)426 self.assertEqual(ba2int(a), i)427 def test_many(self):428 for i in range(1000):429 self.check_round_trip(i)430 self.check_round_trip(randint(0, 10 ** randint(3, 300)))431tests.append(TestsIntegerization)432# ---------------------------------------------------------------------------433class TestsHuffman(unittest.TestCase):434 def test_simple(self):435 freq = {0: 10, 'as': 2, None: 1.6}436 code = huffman_code(freq)437 self.assertEqual(len(code), 3)438 self.assertEqual(len(code[0]), 1)439 self.assertEqual(len(code['as']), 2)440 self.assertEqual(len(code[None]), 2)441 def test_tiny(self):442 code = huffman_code({0: 0})443 self.assertEqual(len(code), 1)444 self.assertEqual(code, {0: bitarray()})445 code = huffman_code({0: 0, 1: 0})446 self.assertEqual(len(code), 2)447 for i in range(2):448 self.assertEqual(len(code[i]), 1)449 def test_endianness(self):450 freq = {'A': 10, 'B': 2, 'C': 5}451 for endian in 'big', 'little':452 code = huffman_code(freq, endian)453 self.assertEqual(len(code), 3)454 for v in code.values():455 self.assertEqual(v.endian(), endian)456 def test_wrong_arg(self):457 self.assertRaises(TypeError, huffman_code, [('a', 1)])458 self.assertRaises(TypeError, huffman_code, 123)459 self.assertRaises(TypeError, huffman_code, None)460 # cannot compare 'a' with 1461 self.assertRaises(TypeError, huffman_code, {'A': 'a', 'B': 1})462 self.assertRaises(ValueError, huffman_code, {})463 def test_balanced(self):464 n = 6465 freq = {}466 for i in range(2 ** n):467 freq[i] = 1468 code = huffman_code(freq)469 self.assertEqual(len(code), 2 ** n)470 self.assertTrue(all(len(v) == n for v in code.values()))471 def test_unbalanced(self):472 N = 27473 freq = {}474 for i in range(N):475 freq[i] = 2 ** i476 code = huffman_code(freq)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful