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
Hide 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
from
test
import
support
from
collections
import
UserList
# We do a bit of trickery here to be able to test both the C implementation
# and the Python implementation of the module.
# 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
py_bisect
=
support
.
import_fresh_module
(
'bisect'
,
blocked
=
[
'_bisect'
])
c_bisect
=
support
.
import_fresh_module
(
'bisect'
,
fresh
=
[
'_bisect'
])
class
Range
(
object
):
"""A trivial range()-like object without any integer width limitations."""
...
...
@@ -45,9 +28,7 @@ class Range(object):
self
.
last_insert
=
idx
,
item
class
TestBisect
(
unittest
.
TestCase
):
module
=
None
class
TestBisect
:
def
setUp
(
self
):
self
.
precomputedCases
=
[
(
self
.
module
.
bisect_right
,
[],
1
,
0
),
...
...
@@ -218,17 +199,15 @@ class TestBisect(unittest.TestCase):
self
.
module
.
insort
(
a
=
data
,
x
=
25
,
lo
=
1
,
hi
=
3
)
self
.
assertEqual
(
data
,
[
10
,
20
,
25
,
25
,
25
,
30
,
40
,
50
])
class
TestBisectPython
(
TestBisect
):
class
TestBisectPython
(
TestBisect
,
unittest
.
TestCase
):
module
=
py_bisect
class
TestBisectC
(
TestBisect
):
class
TestBisectC
(
TestBisect
,
unittest
.
TestCase
):
module
=
c_bisect
#==============================================================================
class
TestInsort
(
unittest
.
TestCase
):
module
=
None
class
TestInsort
:
def
test_vsBuiltinSort
(
self
,
n
=
500
):
from
random
import
choice
for
insorted
in
(
list
(),
UserList
()):
...
...
@@ -255,15 +234,14 @@ class TestInsort(unittest.TestCase):
self
.
module
.
insort_right
(
lst
,
5
)
self
.
assertEqual
([
5
,
10
],
lst
.
data
)
class
TestInsortPython
(
TestInsort
):
class
TestInsortPython
(
TestInsort
,
unittest
.
TestCase
):
module
=
py_bisect
class
TestInsortC
(
TestInsort
):
class
TestInsortC
(
TestInsort
,
unittest
.
TestCase
):
module
=
c_bisect
#==============================================================================
class
LenOnly
:
"Dummy sequence class defining __len__ but not __getitem__."
def
__len__
(
self
):
...
...
@@ -284,9 +262,7 @@ class CmpErr:
__eq__
=
__lt__
__ne__
=
__lt__
class
TestErrorHandling
(
unittest
.
TestCase
):
module
=
None
class
TestErrorHandling
:
def
test_non_sequence
(
self
):
for
f
in
(
self
.
module
.
bisect_left
,
self
.
module
.
bisect_right
,
self
.
module
.
insort_left
,
self
.
module
.
insort_right
):
...
...
@@ -313,58 +289,40 @@ class TestErrorHandling(unittest.TestCase):
self
.
module
.
insort_left
,
self
.
module
.
insort_right
):
self
.
assertRaises
(
TypeError
,
f
,
10
)
class
TestErrorHandlingPython
(
TestErrorHandling
):
class
TestErrorHandlingPython
(
TestErrorHandling
,
unittest
.
TestCase
):
module
=
py_bisect
class
TestErrorHandlingC
(
TestErrorHandling
):
class
TestErrorHandlingC
(
TestErrorHandling
,
unittest
.
TestCase
):
module
=
c_bisect
#==============================================================================
libreftest
=
"""
Example from the Library Reference: Doc/library/bisect.rst
The bisect() function is generally useful for categorizing numeric data.
This example uses bisect() to look up a letter grade for an exam total
(say) based on a set of ordered numeric breakpoints: 85 and up is an `A',
75..84 is a `B', etc.
>>> grades = "FEDCBA"
>>> breakpoints = [30, 44, 66, 75, 85]
>>> from bisect import bisect
>>> def grade(total):
... return grades[bisect(breakpoints, total)]
...
>>> grade(66)
'C'
>>> list(map(grade, [33, 99, 77, 44, 12, 88]))
['E', 'A', 'B', 'D', 'F', 'A']
class
TestDocExample
:
def
test_grades
(
self
):
def
grade
(
score
,
breakpoints
=
[
60
,
70
,
80
,
90
],
grades
=
'FDCBA'
):
i
=
self
.
module
.
bisect
(
breakpoints
,
score
)
return
grades
[
i
]
result
=
[
grade
(
score
)
for
score
in
[
33
,
99
,
77
,
70
,
89
,
90
,
100
]]
self
.
assertEqual
(
result
,
[
'F'
,
'A'
,
'C'
,
'C'
,
'B'
,
'A'
,
'A'
])
def
test_colors
(
self
):
data
=
[(
'red'
,
5
),
(
'blue'
,
1
),
(
'yellow'
,
8
),
(
'black'
,
0
)]
data
.
sort
(
key
=
lambda
r
:
r
[
1
])
keys
=
[
r
[
1
]
for
r
in
data
]
bisect_left
=
self
.
module
.
bisect_left
self
.
assertEqual
(
data
[
bisect_left
(
keys
,
0
)],
(
'black'
,
0
))
self
.
assertEqual
(
data
[
bisect_left
(
keys
,
1
)],
(
'blue'
,
1
))
self
.
assertEqual
(
data
[
bisect_left
(
keys
,
5
)],
(
'red'
,
5
))
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__"
:
test_main
(
verbose
=
True
)
unittest
.
main
(
)
Misc/NEWS
View file @
f472a90d
...
...
@@ -408,6 +408,9 @@ Library
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
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