Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
41bade96
Kaydet (Commit)
41bade96
authored
Tem 26, 2011
tarafından
Éric Araujo
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Remove duplicates of cmp_to_key (#12542, reviewed by Raymond Hettinger)
üst
45dedaaf
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
26 deletions
+13
-26
list_tests.py
Lib/test/list_tests.py
+5
-11
test_sort.py
Lib/test/test_sort.py
+8
-15
No files found.
Lib/test/list_tests.py
Dosyayı görüntüle @
41bade96
...
...
@@ -4,17 +4,10 @@ Tests common to list and UserList.UserList
import
sys
import
os
from
functools
import
cmp_to_key
from
test
import
support
,
seq_tests
def
CmpToKey
(
mycmp
):
'Convert a cmp= function into a key= function'
class
K
(
object
):
def
__init__
(
self
,
obj
):
self
.
obj
=
obj
def
__lt__
(
self
,
other
):
return
mycmp
(
self
.
obj
,
other
.
obj
)
==
-
1
return
K
class
CommonTest
(
seq_tests
.
CommonTest
):
...
...
@@ -443,7 +436,7 @@ class CommonTest(seq_tests.CommonTest):
return
1
else
:
# a > b
return
-
1
u
.
sort
(
key
=
CmpToK
ey
(
revcmp
))
u
.
sort
(
key
=
cmp_to_k
ey
(
revcmp
))
self
.
assertEqual
(
u
,
self
.
type2test
([
2
,
1
,
0
,
-
1
,
-
2
]))
# The following dumps core in unpatched Python 1.5:
...
...
@@ -456,7 +449,7 @@ class CommonTest(seq_tests.CommonTest):
else
:
# xmod > ymod
return
1
z
=
self
.
type2test
(
range
(
12
))
z
.
sort
(
key
=
CmpToK
ey
(
myComparison
))
z
.
sort
(
key
=
cmp_to_k
ey
(
myComparison
))
self
.
assertRaises
(
TypeError
,
z
.
sort
,
2
)
...
...
@@ -468,7 +461,8 @@ class CommonTest(seq_tests.CommonTest):
return
-
1
else
:
# x > y
return
1
self
.
assertRaises
(
ValueError
,
z
.
sort
,
key
=
CmpToKey
(
selfmodifyingComparison
))
self
.
assertRaises
(
ValueError
,
z
.
sort
,
key
=
cmp_to_key
(
selfmodifyingComparison
))
self
.
assertRaises
(
TypeError
,
z
.
sort
,
42
,
42
,
42
,
42
)
...
...
Lib/test/test_sort.py
Dosyayı görüntüle @
41bade96
...
...
@@ -2,18 +2,11 @@ from test import support
import
random
import
sys
import
unittest
from
functools
import
cmp_to_key
verbose
=
support
.
verbose
nerrors
=
0
def
CmpToKey
(
mycmp
):
'Convert a cmp= function into a key= function'
class
K
(
object
):
def
__init__
(
self
,
obj
):
self
.
obj
=
obj
def
__lt__
(
self
,
other
):
return
mycmp
(
self
.
obj
,
other
.
obj
)
==
-
1
return
K
def
check
(
tag
,
expected
,
raw
,
compare
=
None
):
global
nerrors
...
...
@@ -23,7 +16,7 @@ def check(tag, expected, raw, compare=None):
orig
=
raw
[:]
# save input in case of error
if
compare
:
raw
.
sort
(
key
=
CmpToK
ey
(
compare
))
raw
.
sort
(
key
=
cmp_to_k
ey
(
compare
))
else
:
raw
.
sort
()
...
...
@@ -108,7 +101,7 @@ class TestBase(unittest.TestCase):
print
(
" Checking against an insane comparison function."
)
print
(
" If the implementation isn't careful, this may segfault."
)
s
=
x
[:]
s
.
sort
(
key
=
CmpToK
ey
(
lambda
a
,
b
:
int
(
random
.
random
()
*
3
)
-
1
))
s
.
sort
(
key
=
cmp_to_k
ey
(
lambda
a
,
b
:
int
(
random
.
random
()
*
3
)
-
1
))
check
(
"an insane function left some permutation"
,
x
,
s
)
if
len
(
x
)
>=
2
:
...
...
@@ -165,12 +158,12 @@ class TestBugs(unittest.TestCase):
L
.
pop
()
return
(
x
>
y
)
-
(
x
<
y
)
L
=
[
1
,
2
]
self
.
assertRaises
(
ValueError
,
L
.
sort
,
key
=
CmpToK
ey
(
mutating_cmp
))
self
.
assertRaises
(
ValueError
,
L
.
sort
,
key
=
cmp_to_k
ey
(
mutating_cmp
))
def
mutating_cmp
(
x
,
y
):
L
.
append
(
3
)
del
L
[:]
return
(
x
>
y
)
-
(
x
<
y
)
self
.
assertRaises
(
ValueError
,
L
.
sort
,
key
=
CmpToK
ey
(
mutating_cmp
))
self
.
assertRaises
(
ValueError
,
L
.
sort
,
key
=
cmp_to_k
ey
(
mutating_cmp
))
memorywaster
=
[
memorywaster
]
#==============================================================================
...
...
@@ -185,7 +178,7 @@ class TestDecorateSortUndecorate(unittest.TestCase):
def
my_cmp
(
x
,
y
):
xlower
,
ylower
=
x
.
lower
(),
y
.
lower
()
return
(
xlower
>
ylower
)
-
(
xlower
<
ylower
)
copy
.
sort
(
key
=
CmpToK
ey
(
my_cmp
))
copy
.
sort
(
key
=
cmp_to_k
ey
(
my_cmp
))
def
test_baddecorator
(
self
):
data
=
'The quick Brown fox Jumped over The lazy Dog'
.
split
()
...
...
@@ -261,8 +254,8 @@ class TestDecorateSortUndecorate(unittest.TestCase):
def
my_cmp_reversed
(
x
,
y
):
x0
,
y0
=
x
[
0
],
y
[
0
]
return
(
y0
>
x0
)
-
(
y0
<
x0
)
data
.
sort
(
key
=
CmpToK
ey
(
my_cmp
),
reverse
=
True
)
copy1
.
sort
(
key
=
CmpToK
ey
(
my_cmp_reversed
))
data
.
sort
(
key
=
cmp_to_k
ey
(
my_cmp
),
reverse
=
True
)
copy1
.
sort
(
key
=
cmp_to_k
ey
(
my_cmp_reversed
))
self
.
assertEqual
(
data
,
copy1
)
copy2
.
sort
(
key
=
lambda
x
:
x
[
0
],
reverse
=
True
)
self
.
assertEqual
(
data
,
copy2
)
...
...
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