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
7f7386cf
Kaydet (Commit)
7f7386cf
authored
Haz 02, 2006
tarafından
Martin Blais
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed struct test to not use unittest.
üst
d21a7fff
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
84 additions
and
78 deletions
+84
-78
test_struct.py
Lib/test/test_struct.py
+84
-78
No files found.
Lib/test/test_struct.py
Dosyayı görüntüle @
7f7386cf
...
...
@@ -2,7 +2,6 @@ from test.test_support import TestFailed, verbose, verify
import
test.test_support
import
struct
import
array
import
unittest
import
warnings
import
sys
...
...
@@ -494,80 +493,87 @@ def test_1229380():
if
PY_STRUCT_RANGE_CHECKING
:
test_1229380
()
class
PackBufferTestCase
(
unittest
.
TestCase
):
"""
Test the packing methods that work on buffers.
"""
def
test_unpack_from
(
self
):
test_string
=
'abcd01234'
fmt
=
'4s'
s
=
struct
.
Struct
(
fmt
)
for
cls
in
(
str
,
buffer
):
data
=
cls
(
test_string
)
self
.
assertEquals
(
s
.
unpack_from
(
data
),
(
'abcd'
,))
self
.
assertEquals
(
s
.
unpack_from
(
data
,
2
),
(
'cd01'
,))
self
.
assertEquals
(
s
.
unpack_from
(
data
,
4
),
(
'0123'
,))
for
i
in
xrange
(
6
):
self
.
assertEquals
(
s
.
unpack_from
(
data
,
i
),
(
data
[
i
:
i
+
4
],))
for
i
in
xrange
(
6
,
len
(
test_string
)
+
1
):
simple_err
(
s
.
unpack_from
,
data
,
i
)
for
cls
in
(
str
,
buffer
):
data
=
cls
(
test_string
)
self
.
assertEquals
(
struct
.
unpack_from
(
fmt
,
data
),
(
'abcd'
,))
self
.
assertEquals
(
struct
.
unpack_from
(
fmt
,
data
,
2
),
(
'cd01'
,))
self
.
assertEquals
(
struct
.
unpack_from
(
fmt
,
data
,
4
),
(
'0123'
,))
for
i
in
xrange
(
6
):
self
.
assertEquals
(
struct
.
unpack_from
(
fmt
,
data
,
i
),
(
data
[
i
:
i
+
4
],))
for
i
in
xrange
(
6
,
len
(
test_string
)
+
1
):
simple_err
(
struct
.
unpack_from
,
fmt
,
data
,
i
)
def
test_pack_to
(
self
):
test_string
=
'Reykjavik rocks, eow!'
writable_buf
=
array
.
array
(
'c'
,
' '
*
100
)
fmt
=
'21s'
s
=
struct
.
Struct
(
fmt
)
# Test without offset
s
.
pack_to
(
writable_buf
,
0
,
test_string
)
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)]
self
.
assertEquals
(
from_buf
,
test_string
)
# Test with offset.
s
.
pack_to
(
writable_buf
,
10
,
test_string
)
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)
+
10
]
self
.
assertEquals
(
from_buf
,
(
test_string
[:
10
]
+
test_string
))
# Go beyond boundaries.
small_buf
=
array
.
array
(
'c'
,
' '
*
10
)
self
.
assertRaises
(
struct
.
error
,
s
.
pack_to
,
small_buf
,
0
,
test_string
)
self
.
assertRaises
(
struct
.
error
,
s
.
pack_to
,
small_buf
,
2
,
test_string
)
def
test_pack_to_fn
(
self
):
test_string
=
'Reykjavik rocks, eow!'
writable_buf
=
array
.
array
(
'c'
,
' '
*
100
)
fmt
=
'21s'
pack_to
=
lambda
*
args
:
struct
.
pack_to
(
fmt
,
*
args
)
# Test without offset
pack_to
(
writable_buf
,
0
,
test_string
)
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)]
self
.
assertEquals
(
from_buf
,
test_string
)
# Test with offset.
pack_to
(
writable_buf
,
10
,
test_string
)
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)
+
10
]
self
.
assertEquals
(
from_buf
,
(
test_string
[:
10
]
+
test_string
))
# Go beyond boundaries.
small_buf
=
array
.
array
(
'c'
,
' '
*
10
)
self
.
assertRaises
(
struct
.
error
,
pack_to
,
small_buf
,
0
,
test_string
)
self
.
assertRaises
(
struct
.
error
,
pack_to
,
small_buf
,
2
,
test_string
)
def
test_main
():
test
.
test_support
.
run_unittest
(
PackBufferTestCase
)
if
__name__
==
"__main__"
:
test_main
()
###########################################################################
# Packing and unpacking to/from buffers.
# Copied and modified from unittest.
def
assertRaises
(
excClass
,
callableObj
,
*
args
,
**
kwargs
):
try
:
callableObj
(
*
args
,
**
kwargs
)
except
excClass
:
return
else
:
raise
RuntimeError
(
"
%
s not raised."
%
excClass
)
def
test_unpack_from
():
test_string
=
'abcd01234'
fmt
=
'4s'
s
=
struct
.
Struct
(
fmt
)
for
cls
in
(
str
,
buffer
):
data
=
cls
(
test_string
)
assert
s
.
unpack_from
(
data
)
==
(
'abcd'
,)
assert
s
.
unpack_from
(
data
,
2
)
==
(
'cd01'
,)
assert
s
.
unpack_from
(
data
,
4
)
==
(
'0123'
,)
for
i
in
xrange
(
6
):
assert
s
.
unpack_from
(
data
,
i
)
==
(
data
[
i
:
i
+
4
],)
for
i
in
xrange
(
6
,
len
(
test_string
)
+
1
):
simple_err
(
s
.
unpack_from
,
data
,
i
)
for
cls
in
(
str
,
buffer
):
data
=
cls
(
test_string
)
assert
struct
.
unpack_from
(
fmt
,
data
)
==
(
'abcd'
,)
assert
struct
.
unpack_from
(
fmt
,
data
,
2
)
==
(
'cd01'
,)
assert
struct
.
unpack_from
(
fmt
,
data
,
4
)
==
(
'0123'
,)
for
i
in
xrange
(
6
):
assert
(
struct
.
unpack_from
(
fmt
,
data
,
i
)
==
(
data
[
i
:
i
+
4
],))
for
i
in
xrange
(
6
,
len
(
test_string
)
+
1
):
simple_err
(
struct
.
unpack_from
,
fmt
,
data
,
i
)
def
test_pack_to
():
test_string
=
'Reykjavik rocks, eow!'
writable_buf
=
array
.
array
(
'c'
,
' '
*
100
)
fmt
=
'21s'
s
=
struct
.
Struct
(
fmt
)
# Test without offset
s
.
pack_to
(
writable_buf
,
0
,
test_string
)
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)]
assert
from_buf
==
test_string
# Test with offset.
s
.
pack_to
(
writable_buf
,
10
,
test_string
)
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)
+
10
]
assert
from_buf
==
(
test_string
[:
10
]
+
test_string
)
# Go beyond boundaries.
small_buf
=
array
.
array
(
'c'
,
' '
*
10
)
assertRaises
(
struct
.
error
,
s
.
pack_to
,
small_buf
,
0
,
test_string
)
assertRaises
(
struct
.
error
,
s
.
pack_to
,
small_buf
,
2
,
test_string
)
def
test_pack_to_fn
():
test_string
=
'Reykjavik rocks, eow!'
writable_buf
=
array
.
array
(
'c'
,
' '
*
100
)
fmt
=
'21s'
pack_to
=
lambda
*
args
:
struct
.
pack_to
(
fmt
,
*
args
)
# Test without offset
pack_to
(
writable_buf
,
0
,
test_string
)
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)]
assert
from_buf
==
test_string
# Test with offset.
pack_to
(
writable_buf
,
10
,
test_string
)
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)
+
10
]
assert
from_buf
==
(
test_string
[:
10
]
+
test_string
)
# Go beyond boundaries.
small_buf
=
array
.
array
(
'c'
,
' '
*
10
)
assertRaises
(
struct
.
error
,
pack_to
,
small_buf
,
0
,
test_string
)
assertRaises
(
struct
.
error
,
pack_to
,
small_buf
,
2
,
test_string
)
# Test methods to pack and unpack from buffers rather than strings.
test_unpack_from
()
test_pack_to
()
test_pack_to_fn
()
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