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
65a9f369
Kaydet (Commit)
65a9f369
authored
Ock 16, 2013
tarafından
Frank Wierzbicki
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Closed #16886: test_dictcomps no longer depends on dict order
üst
51b55517
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
77 additions
and
40 deletions
+77
-40
test_dictcomps.py
Lib/test/test_dictcomps.py
+77
-40
No files found.
Lib/test/test_dictcomps.py
Dosyayı görüntüle @
65a9f369
import
unittest
doctests
=
"""
from
test
import
test_support
as
support
>>> k = "old value"
>>> { k: None for k in range(10) }
{0: None, 1: None, 2: None, 3: None, 4: None, 5: None, 6: None, 7: None, 8: None, 9: None}
>>> k
'old value'
# For scope testing.
g
=
"Global variable"
>>> { k: k+10 for k in range(10) }
{0: 10, 1: 11, 2: 12, 3: 13, 4: 14, 5: 15, 6: 16, 7: 17, 8: 18, 9: 19}
>>> g = "Global variable"
>>> { k: g for k in range(10) }
{0: 'Global variable', 1: 'Global variable', 2: 'Global variable', 3: 'Global variable', 4: 'Global variable', 5: 'Global variable', 6: 'Global variable', 7: 'Global variable', 8: 'Global variable', 9: 'Global variable'}
class
DictComprehensionTest
(
unittest
.
TestCase
):
>>> { k: v for k in range(10) for v in range(10) if k == v }
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}
def
test_basics
(
self
):
expected
=
{
0
:
10
,
1
:
11
,
2
:
12
,
3
:
13
,
4
:
14
,
5
:
15
,
6
:
16
,
7
:
17
,
8
:
18
,
9
:
19
}
actual
=
{
k
:
k
+
10
for
k
in
range
(
10
)}
self
.
assertEqual
(
actual
,
expected
)
>>> { k: v for v in range(10) for k in range(v*9, v*10) }
{9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4, 38: 4, 39: 4, 45: 5, 46: 5, 47: 5, 48: 5, 49: 5, 54: 6, 55: 6, 56: 6, 57: 6, 58: 6, 59: 6, 63: 7, 64: 7, 65: 7, 66: 7, 67: 7, 68: 7, 69: 7, 72: 8, 73: 8, 74: 8, 75: 8, 76: 8, 77: 8, 78: 8, 79: 8, 81: 9, 82: 9, 83: 9, 84: 9, 85: 9, 86: 9, 87: 9, 88: 9, 89: 9}
expected
=
{
0
:
0
,
1
:
1
,
2
:
2
,
3
:
3
,
4
:
4
,
5
:
5
,
6
:
6
,
7
:
7
,
8
:
8
,
9
:
9
}
actual
=
{
k
:
v
for
k
in
range
(
10
)
for
v
in
range
(
10
)
if
k
==
v
}
self
.
assertEqual
(
actual
,
expected
)
>>> { x: y for y, x in ((1, 2), (3, 4)) } = 5 # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
SyntaxError: ...
def
test_scope_isolation
(
self
):
k
=
"Local Variable"
>>> { x: y for y, x in ((1, 2), (3, 4)) } += 5 # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
SyntaxError: ...
expected
=
{
0
:
None
,
1
:
None
,
2
:
None
,
3
:
None
,
4
:
None
,
5
:
None
,
6
:
None
,
7
:
None
,
8
:
None
,
9
:
None
}
actual
=
{
k
:
None
for
k
in
range
(
10
)}
self
.
assertEqual
(
actual
,
expected
)
self
.
assertEqual
(
k
,
"Local Variable"
)
"""
expected
=
{
9
:
1
,
18
:
2
,
19
:
2
,
27
:
3
,
28
:
3
,
29
:
3
,
36
:
4
,
37
:
4
,
38
:
4
,
39
:
4
,
45
:
5
,
46
:
5
,
47
:
5
,
48
:
5
,
49
:
5
,
54
:
6
,
55
:
6
,
56
:
6
,
57
:
6
,
58
:
6
,
59
:
6
,
63
:
7
,
64
:
7
,
65
:
7
,
66
:
7
,
67
:
7
,
68
:
7
,
69
:
7
,
72
:
8
,
73
:
8
,
74
:
8
,
75
:
8
,
76
:
8
,
77
:
8
,
78
:
8
,
79
:
8
,
81
:
9
,
82
:
9
,
83
:
9
,
84
:
9
,
85
:
9
,
86
:
9
,
87
:
9
,
88
:
9
,
89
:
9
}
actual
=
{
k
:
v
for
v
in
range
(
10
)
for
k
in
range
(
v
*
9
,
v
*
10
)}
self
.
assertEqual
(
k
,
"Local Variable"
)
self
.
assertEqual
(
actual
,
expected
)
__test__
=
{
'doctests'
:
doctests
}
def
test_scope_isolation_from_global
(
self
):
expected
=
{
0
:
None
,
1
:
None
,
2
:
None
,
3
:
None
,
4
:
None
,
5
:
None
,
6
:
None
,
7
:
None
,
8
:
None
,
9
:
None
}
actual
=
{
g
:
None
for
g
in
range
(
10
)}
self
.
assertEqual
(
actual
,
expected
)
self
.
assertEqual
(
g
,
"Global variable"
)
def
test_main
(
verbose
=
None
):
import
sys
from
test
import
test_support
from
test
import
test_dictcomps
test_support
.
run_doctest
(
test_dictcomps
,
verbose
)
expected
=
{
9
:
1
,
18
:
2
,
19
:
2
,
27
:
3
,
28
:
3
,
29
:
3
,
36
:
4
,
37
:
4
,
38
:
4
,
39
:
4
,
45
:
5
,
46
:
5
,
47
:
5
,
48
:
5
,
49
:
5
,
54
:
6
,
55
:
6
,
56
:
6
,
57
:
6
,
58
:
6
,
59
:
6
,
63
:
7
,
64
:
7
,
65
:
7
,
66
:
7
,
67
:
7
,
68
:
7
,
69
:
7
,
72
:
8
,
73
:
8
,
74
:
8
,
75
:
8
,
76
:
8
,
77
:
8
,
78
:
8
,
79
:
8
,
81
:
9
,
82
:
9
,
83
:
9
,
84
:
9
,
85
:
9
,
86
:
9
,
87
:
9
,
88
:
9
,
89
:
9
}
actual
=
{
g
:
v
for
v
in
range
(
10
)
for
g
in
range
(
v
*
9
,
v
*
10
)}
self
.
assertEqual
(
g
,
"Global variable"
)
self
.
assertEqual
(
actual
,
expected
)
# verify reference counting
if
verbose
and
hasattr
(
sys
,
"gettotalrefcount"
):
import
gc
counts
=
[
None
]
*
5
for
i
in
range
(
len
(
counts
)):
test_support
.
run_doctest
(
test_dictcomps
,
verbose
)
gc
.
collect
()
counts
[
i
]
=
sys
.
gettotalrefcount
()
print
(
counts
)
def
test_global_visibility
(
self
):
expected
=
{
0
:
'Global variable'
,
1
:
'Global variable'
,
2
:
'Global variable'
,
3
:
'Global variable'
,
4
:
'Global variable'
,
5
:
'Global variable'
,
6
:
'Global variable'
,
7
:
'Global variable'
,
8
:
'Global variable'
,
9
:
'Global variable'
}
actual
=
{
k
:
g
for
k
in
range
(
10
)}
self
.
assertEqual
(
actual
,
expected
)
def
test_local_visibility
(
self
):
v
=
"Local variable"
expected
=
{
0
:
'Local variable'
,
1
:
'Local variable'
,
2
:
'Local variable'
,
3
:
'Local variable'
,
4
:
'Local variable'
,
5
:
'Local variable'
,
6
:
'Local variable'
,
7
:
'Local variable'
,
8
:
'Local variable'
,
9
:
'Local variable'
}
actual
=
{
k
:
v
for
k
in
range
(
10
)}
self
.
assertEqual
(
actual
,
expected
)
self
.
assertEqual
(
v
,
"Local variable"
)
def
test_illegal_assignment
(
self
):
with
self
.
assertRaisesRegexp
(
SyntaxError
,
"can't assign"
):
compile
(
"{x: y for y, x in ((1, 2), (3, 4))} = 5"
,
"<test>"
,
"exec"
)
with
self
.
assertRaisesRegexp
(
SyntaxError
,
"can't assign"
):
compile
(
"{x: y for y, x in ((1, 2), (3, 4))} += 5"
,
"<test>"
,
"exec"
)
def
test_main
():
support
.
run_unittest
(
__name__
)
if
__name__
==
"__main__"
:
test_main
(
verbose
=
True
)
test_main
()
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