diff options
author | Giampaolo Rodola' <g.rodola@gmail.com> | 2012-05-16 14:03:07 (GMT) |
---|---|---|
committer | Giampaolo Rodola' <g.rodola@gmail.com> | 2012-05-16 14:03:07 (GMT) |
commit | 26fd8feb5e96276bc99d5682865d34deb190b731 (patch) | |
tree | cfef5fe0375c28cacbcc6228113c4baadbdd317c /Lib/test | |
parent | e1266782160b0eef3726658480b83fa9fbcb6f0e (diff) | |
parent | 7ca29507f69d6b35cd697f533bc754a7864e6292 (diff) | |
download | cpython-26fd8feb5e96276bc99d5682865d34deb190b731.zip cpython-26fd8feb5e96276bc99d5682865d34deb190b731.tar.gz cpython-26fd8feb5e96276bc99d5682865d34deb190b731.tar.bz2 |
merge heads
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_bisect.py | 47 | ||||
-rw-r--r-- | Lib/test/test_pkgutil.py | 51 |
2 files changed, 94 insertions, 4 deletions
diff --git a/Lib/test/test_bisect.py b/Lib/test/test_bisect.py index c24a1a2..2ac3a68 100644 --- a/Lib/test/test_bisect.py +++ b/Lib/test/test_bisect.py @@ -23,6 +23,28 @@ del sys.modules['bisect'] import bisect as c_bisect +class Range(object): + """A trivial range()-like object without any integer width limitations.""" + def __init__(self, start, stop): + self.start = start + self.stop = stop + self.last_insert = None + + def __len__(self): + return self.stop - self.start + + def __getitem__(self, idx): + n = self.stop - self.start + if idx < 0: + idx += n + if idx >= n: + raise IndexError(idx) + return self.start + idx + + def insert(self, idx, item): + self.last_insert = idx, item + + class TestBisect(unittest.TestCase): module = None @@ -125,9 +147,28 @@ class TestBisect(unittest.TestCase): def test_large_range(self): # Issue 13496 mod = self.module - data = range(sys.maxsize-1) - self.assertEqual(mod.bisect_left(data, sys.maxsize-3), sys.maxsize-3) - self.assertEqual(mod.bisect_right(data, sys.maxsize-3), sys.maxsize-2) + n = sys.maxsize + data = range(n-1) + self.assertEqual(mod.bisect_left(data, n-3), n-3) + self.assertEqual(mod.bisect_right(data, n-3), n-2) + self.assertEqual(mod.bisect_left(data, n-3, n-10, n), n-3) + self.assertEqual(mod.bisect_right(data, n-3, n-10, n), n-2) + + def test_large_pyrange(self): + # Same as above, but without C-imposed limits on range() parameters + mod = self.module + n = sys.maxsize + data = Range(0, n-1) + self.assertEqual(mod.bisect_left(data, n-3), n-3) + self.assertEqual(mod.bisect_right(data, n-3), n-2) + self.assertEqual(mod.bisect_left(data, n-3, n-10, n), n-3) + self.assertEqual(mod.bisect_right(data, n-3, n-10, n), n-2) + x = n - 100 + mod.insort_left(data, x, x - 50, x + 50) + self.assertEqual(data.last_insert, (x, x)) + x = n - 200 + mod.insort_right(data, x, x - 50, x + 50) + self.assertEqual(data.last_insert, (x + 1, x)) def test_random(self, n=25): from random import randrange diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py index f4e0323..6025bcd 100644 --- a/Lib/test/test_pkgutil.py +++ b/Lib/test/test_pkgutil.py @@ -137,8 +137,57 @@ class PkgutilPEP302Tests(unittest.TestCase): self.assertEqual(foo.loads, 1) del sys.modules['foo'] + +class ExtendPathTests(unittest.TestCase): + def create_init(self, pkgname): + dirname = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, dirname) + sys.path.insert(0, dirname) + + pkgdir = os.path.join(dirname, pkgname) + os.mkdir(pkgdir) + with open(os.path.join(pkgdir, '__init__.py'), 'w') as fl: + fl.write('from pkgutil import extend_path\n__path__ = extend_path(__path__, __name__)\n') + + return dirname + + def create_submodule(self, dirname, pkgname, submodule_name, value): + module_name = os.path.join(dirname, pkgname, submodule_name + '.py') + with open(module_name, 'w') as fl: + print('value={}'.format(value), file=fl) + + def setUp(self): + # Create 2 directories on sys.path + self.pkgname = 'foo' + self.dirname_0 = self.create_init(self.pkgname) + self.dirname_1 = self.create_init(self.pkgname) + + def tearDown(self): + del sys.path[0] + del sys.path[0] + del sys.modules['foo'] + del sys.modules['foo.bar'] + del sys.modules['foo.baz'] + + def test_simple(self): + self.create_submodule(self.dirname_0, self.pkgname, 'bar', 0) + self.create_submodule(self.dirname_1, self.pkgname, 'baz', 1) + import foo.bar + import foo.baz + # Ensure we read the expected values + self.assertEqual(foo.bar.value, 0) + self.assertEqual(foo.baz.value, 1) + + # Ensure the path is set up correctly + self.assertEqual(sorted(foo.__path__), + sorted([os.path.join(self.dirname_0, self.pkgname), + os.path.join(self.dirname_1, self.pkgname)])) + + # XXX: test .pkg files + + def test_main(): - run_unittest(PkgutilTests, PkgutilPEP302Tests) + run_unittest(PkgutilTests, PkgutilPEP302Tests, ExtendPathTests) # this is necessary if test is run repeated (like when finding leaks) import zipimport zipimport._zip_directory_cache.clear() |