Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
f472a90d
Commit
f472a90d
authored
Jan 10, 2013
by
Ezio Melotti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#16897: test_bisect now works with unittest test discovery. Initial patch by Zachary Ware.
parent
e212370f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
77 deletions
+38
-77
Lib/test/test_bisect.py
Lib/test/test_bisect.py
+35
-77
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/test/test_bisect.py
View file @
f472a90d
...
@@ -3,25 +3,8 @@ import unittest
...
@@ -3,25 +3,8 @@ import unittest
from
test
import
support
from
test
import
support
from
collections
import
UserList
from
collections
import
UserList
# We do a bit of trickery here to be able to test both the C implementation
py_bisect
=
support
.
import_fresh_module
(
'bisect'
,
blocked
=
[
'_bisect'
])
# and the Python implementation of the module.
c_bisect
=
support
.
import_fresh_module
(
'bisect'
,
fresh
=
[
'_bisect'
])
# Make it impossible to import the C implementation anymore.
sys
.
modules
[
'_bisect'
]
=
0
# We must also handle the case that bisect was imported before.
if
'bisect'
in
sys
.
modules
:
del
sys
.
modules
[
'bisect'
]
# Now we can import the module and get the pure Python implementation.
import
bisect
as
py_bisect
# Restore everything to normal.
del
sys
.
modules
[
'_bisect'
]
del
sys
.
modules
[
'bisect'
]
# This is now the module with the C implementation.
import
bisect
as
c_bisect
class
Range
(
object
):
class
Range
(
object
):
"""A trivial range()-like object without any integer width limitations."""
"""A trivial range()-like object without any integer width limitations."""
...
@@ -45,9 +28,7 @@ class Range(object):
...
@@ -45,9 +28,7 @@ class Range(object):
self
.
last_insert
=
idx
,
item
self
.
last_insert
=
idx
,
item
class
TestBisect
(
unittest
.
TestCase
):
class
TestBisect
:
module
=
None
def
setUp
(
self
):
def
setUp
(
self
):
self
.
precomputedCases
=
[
self
.
precomputedCases
=
[
(
self
.
module
.
bisect_right
,
[],
1
,
0
),
(
self
.
module
.
bisect_right
,
[],
1
,
0
),
...
@@ -218,17 +199,15 @@ class TestBisect(unittest.TestCase):
...
@@ -218,17 +199,15 @@ class TestBisect(unittest.TestCase):
self
.
module
.
insort
(
a
=
data
,
x
=
25
,
lo
=
1
,
hi
=
3
)
self
.
module
.
insort
(
a
=
data
,
x
=
25
,
lo
=
1
,
hi
=
3
)
self
.
assertEqual
(
data
,
[
10
,
20
,
25
,
25
,
25
,
30
,
40
,
50
])
self
.
assertEqual
(
data
,
[
10
,
20
,
25
,
25
,
25
,
30
,
40
,
50
])
class
TestBisectPython
(
TestBisect
):
class
TestBisectPython
(
TestBisect
,
unittest
.
TestCase
):
module
=
py_bisect
module
=
py_bisect
class
TestBisectC
(
TestBisect
):
class
TestBisectC
(
TestBisect
,
unittest
.
TestCase
):
module
=
c_bisect
module
=
c_bisect
#==============================================================================
#==============================================================================
class
TestInsort
(
unittest
.
TestCase
):
class
TestInsort
:
module
=
None
def
test_vsBuiltinSort
(
self
,
n
=
500
):
def
test_vsBuiltinSort
(
self
,
n
=
500
):
from
random
import
choice
from
random
import
choice
for
insorted
in
(
list
(),
UserList
()):
for
insorted
in
(
list
(),
UserList
()):
...
@@ -255,15 +234,14 @@ class TestInsort(unittest.TestCase):
...
@@ -255,15 +234,14 @@ class TestInsort(unittest.TestCase):
self
.
module
.
insort_right
(
lst
,
5
)
self
.
module
.
insort_right
(
lst
,
5
)
self
.
assertEqual
([
5
,
10
],
lst
.
data
)
self
.
assertEqual
([
5
,
10
],
lst
.
data
)
class
TestInsortPython
(
TestInsort
):
class
TestInsortPython
(
TestInsort
,
unittest
.
TestCase
):
module
=
py_bisect
module
=
py_bisect
class
TestInsortC
(
TestInsort
):
class
TestInsortC
(
TestInsort
,
unittest
.
TestCase
):
module
=
c_bisect
module
=
c_bisect
#==============================================================================
#==============================================================================
class
LenOnly
:
class
LenOnly
:
"Dummy sequence class defining __len__ but not __getitem__."
"Dummy sequence class defining __len__ but not __getitem__."
def
__len__
(
self
):
def
__len__
(
self
):
...
@@ -284,9 +262,7 @@ class CmpErr:
...
@@ -284,9 +262,7 @@ class CmpErr:
__eq__
=
__lt__
__eq__
=
__lt__
__ne__
=
__lt__
__ne__
=
__lt__
class
TestErrorHandling
(
unittest
.
TestCase
):
class
TestErrorHandling
:
module
=
None
def
test_non_sequence
(
self
):
def
test_non_sequence
(
self
):
for
f
in
(
self
.
module
.
bisect_left
,
self
.
module
.
bisect_right
,
for
f
in
(
self
.
module
.
bisect_left
,
self
.
module
.
bisect_right
,
self
.
module
.
insort_left
,
self
.
module
.
insort_right
):
self
.
module
.
insort_left
,
self
.
module
.
insort_right
):
...
@@ -313,58 +289,40 @@ class TestErrorHandling(unittest.TestCase):
...
@@ -313,58 +289,40 @@ class TestErrorHandling(unittest.TestCase):
self
.
module
.
insort_left
,
self
.
module
.
insort_right
):
self
.
module
.
insort_left
,
self
.
module
.
insort_right
):
self
.
assertRaises
(
TypeError
,
f
,
10
)
self
.
assertRaises
(
TypeError
,
f
,
10
)
class
TestErrorHandlingPython
(
TestErrorHandling
):
class
TestErrorHandlingPython
(
TestErrorHandling
,
unittest
.
TestCase
):
module
=
py_bisect
module
=
py_bisect
class
TestErrorHandlingC
(
TestErrorHandling
):
class
TestErrorHandlingC
(
TestErrorHandling
,
unittest
.
TestCase
):
module
=
c_bisect
module
=
c_bisect
#==============================================================================
#==============================================================================
libreftest
=
"""
class
TestDocExample
:
Example from the Library Reference: Doc/library/bisect.rst
def
test_grades
(
self
):
def
grade
(
score
,
breakpoints
=
[
60
,
70
,
80
,
90
],
grades
=
'FDCBA'
):
The bisect() function is generally useful for categorizing numeric data.
i
=
self
.
module
.
bisect
(
breakpoints
,
score
)
This example uses bisect() to look up a letter grade for an exam total
return
grades
[
i
]
(say) based on a set of ordered numeric breakpoints: 85 and up is an `A',
75..84 is a `B', etc.
result
=
[
grade
(
score
)
for
score
in
[
33
,
99
,
77
,
70
,
89
,
90
,
100
]]
self
.
assertEqual
(
result
,
[
'F'
,
'A'
,
'C'
,
'C'
,
'B'
,
'A'
,
'A'
])
>>> grades = "FEDCBA"
>>> breakpoints = [30, 44, 66, 75, 85]
def
test_colors
(
self
):
>>> from bisect import bisect
data
=
[(
'red'
,
5
),
(
'blue'
,
1
),
(
'yellow'
,
8
),
(
'black'
,
0
)]
>>> def grade(total):
data
.
sort
(
key
=
lambda
r
:
r
[
1
])
... return grades[bisect(breakpoints, total)]
keys
=
[
r
[
1
]
for
r
in
data
]
...
bisect_left
=
self
.
module
.
bisect_left
>>> grade(66)
self
.
assertEqual
(
data
[
bisect_left
(
keys
,
0
)],
(
'black'
,
0
))
'C'
self
.
assertEqual
(
data
[
bisect_left
(
keys
,
1
)],
(
'blue'
,
1
))
>>> list(map(grade, [33, 99, 77, 44, 12, 88]))
self
.
assertEqual
(
data
[
bisect_left
(
keys
,
5
)],
(
'red'
,
5
))
['E', 'A', 'B', 'D', 'F', 'A']
self
.
assertEqual
(
data
[
bisect_left
(
keys
,
8
)],
(
'yellow'
,
8
))
class
TestDocExamplePython
(
TestDocExample
,
unittest
.
TestCase
):
module
=
py_bisect
"""
class
TestDocExampleC
(
TestDocExample
,
unittest
.
TestCase
):
module
=
c_bisect
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
__test__
=
{
'libreftest'
:
libreftest
}
def
test_main
(
verbose
=
None
):
from
test
import
test_bisect
test_classes
=
[
TestBisectPython
,
TestBisectC
,
TestInsortPython
,
TestInsortC
,
TestErrorHandlingPython
,
TestErrorHandlingC
]
support
.
run_unittest
(
*
test_classes
)
support
.
run_doctest
(
test_bisect
,
verbose
)
# verify reference counting
if
verbose
and
hasattr
(
sys
,
"gettotalrefcount"
):
import
gc
counts
=
[
None
]
*
5
for
i
in
range
(
len
(
counts
)):
support
.
run_unittest
(
*
test_classes
)
gc
.
collect
()
counts
[
i
]
=
sys
.
gettotalrefcount
()
print
(
counts
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
test_main
(
verbose
=
True
)
unittest
.
main
(
)
Misc/NEWS
View file @
f472a90d
...
@@ -408,6 +408,9 @@ Library
...
@@ -408,6 +408,9 @@ Library
Tests
Tests
-----
-----
- Issue #16897: test_bisect now works with unittest test discovery.
Initial patch by Zachary Ware.
- Issue #16852: test_genericpath, test_posixpath, test_ntpath, and test_macpath
- Issue #16852: test_genericpath, test_posixpath, test_ntpath, and test_macpath
now work with unittest test discovery. Patch by Zachary Ware.
now work with unittest test discovery. Patch by Zachary Ware.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment