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
7f7386cf
Commit
7f7386cf
authored
Jun 02, 2006
by
Martin Blais
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed struct test to not use unittest.
parent
d21a7fff
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
84 additions
and
78 deletions
+84
-78
Lib/test/test_struct.py
Lib/test/test_struct.py
+84
-78
No files found.
Lib/test/test_struct.py
View file @
7f7386cf
...
@@ -2,7 +2,6 @@ from test.test_support import TestFailed, verbose, verify
...
@@ -2,7 +2,6 @@ from test.test_support import TestFailed, verbose, verify
import
test.test_support
import
test.test_support
import
struct
import
struct
import
array
import
array
import
unittest
import
warnings
import
warnings
import
sys
import
sys
...
@@ -494,80 +493,87 @@ def test_1229380():
...
@@ -494,80 +493,87 @@ def test_1229380():
if
PY_STRUCT_RANGE_CHECKING
:
if
PY_STRUCT_RANGE_CHECKING
:
test_1229380
()
test_1229380
()
class
PackBufferTestCase
(
unittest
.
TestCase
):
"""
###########################################################################
Test the packing methods that work on buffers.
# Packing and unpacking to/from buffers.
"""
# Copied and modified from unittest.
def
test_unpack_from
(
self
):
def
assertRaises
(
excClass
,
callableObj
,
*
args
,
**
kwargs
):
test_string
=
'abcd01234'
try
:
fmt
=
'4s'
callableObj
(
*
args
,
**
kwargs
)
s
=
struct
.
Struct
(
fmt
)
except
excClass
:
for
cls
in
(
str
,
buffer
):
return
data
=
cls
(
test_string
)
else
:
self
.
assertEquals
(
s
.
unpack_from
(
data
),
(
'abcd'
,))
raise
RuntimeError
(
"%s not raised."
%
excClass
)
self
.
assertEquals
(
s
.
unpack_from
(
data
,
2
),
(
'cd01'
,))
self
.
assertEquals
(
s
.
unpack_from
(
data
,
4
),
(
'0123'
,))
def
test_unpack_from
():
for
i
in
xrange
(
6
):
test_string
=
'abcd01234'
self
.
assertEquals
(
s
.
unpack_from
(
data
,
i
),
(
data
[
i
:
i
+
4
],))
fmt
=
'4s'
for
i
in
xrange
(
6
,
len
(
test_string
)
+
1
):
s
=
struct
.
Struct
(
fmt
)
simple_err
(
s
.
unpack_from
,
data
,
i
)
for
cls
in
(
str
,
buffer
):
for
cls
in
(
str
,
buffer
):
data
=
cls
(
test_string
)
data
=
cls
(
test_string
)
assert
s
.
unpack_from
(
data
)
==
(
'abcd'
,)
self
.
assertEquals
(
struct
.
unpack_from
(
fmt
,
data
),
(
'abcd'
,))
assert
s
.
unpack_from
(
data
,
2
)
==
(
'cd01'
,)
self
.
assertEquals
(
struct
.
unpack_from
(
fmt
,
data
,
2
),
(
'cd01'
,))
assert
s
.
unpack_from
(
data
,
4
)
==
(
'0123'
,)
self
.
assertEquals
(
struct
.
unpack_from
(
fmt
,
data
,
4
),
(
'0123'
,))
for
i
in
xrange
(
6
):
for
i
in
xrange
(
6
):
assert
s
.
unpack_from
(
data
,
i
)
==
(
data
[
i
:
i
+
4
],)
self
.
assertEquals
(
struct
.
unpack_from
(
fmt
,
data
,
i
),
for
i
in
xrange
(
6
,
len
(
test_string
)
+
1
):
(
data
[
i
:
i
+
4
],))
simple_err
(
s
.
unpack_from
,
data
,
i
)
for
i
in
xrange
(
6
,
len
(
test_string
)
+
1
):
for
cls
in
(
str
,
buffer
):
simple_err
(
struct
.
unpack_from
,
fmt
,
data
,
i
)
data
=
cls
(
test_string
)
assert
struct
.
unpack_from
(
fmt
,
data
)
==
(
'abcd'
,)
def
test_pack_to
(
self
):
assert
struct
.
unpack_from
(
fmt
,
data
,
2
)
==
(
'cd01'
,)
test_string
=
'Reykjavik rocks, eow!'
assert
struct
.
unpack_from
(
fmt
,
data
,
4
)
==
(
'0123'
,)
writable_buf
=
array
.
array
(
'c'
,
' '
*
100
)
for
i
in
xrange
(
6
):
fmt
=
'21s'
assert
(
struct
.
unpack_from
(
fmt
,
data
,
i
)
==
(
data
[
i
:
i
+
4
],))
s
=
struct
.
Struct
(
fmt
)
for
i
in
xrange
(
6
,
len
(
test_string
)
+
1
):
simple_err
(
struct
.
unpack_from
,
fmt
,
data
,
i
)
# Test without offset
s
.
pack_to
(
writable_buf
,
0
,
test_string
)
def
test_pack_to
():
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)]
test_string
=
'Reykjavik rocks, eow!'
self
.
assertEquals
(
from_buf
,
test_string
)
writable_buf
=
array
.
array
(
'c'
,
' '
*
100
)
fmt
=
'21s'
# Test with offset.
s
=
struct
.
Struct
(
fmt
)
s
.
pack_to
(
writable_buf
,
10
,
test_string
)
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)
+
10
]
# Test without offset
self
.
assertEquals
(
from_buf
,
(
test_string
[:
10
]
+
test_string
))
s
.
pack_to
(
writable_buf
,
0
,
test_string
)
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)]
# Go beyond boundaries.
assert
from_buf
==
test_string
small_buf
=
array
.
array
(
'c'
,
' '
*
10
)
self
.
assertRaises
(
struct
.
error
,
s
.
pack_to
,
small_buf
,
0
,
test_string
)
# Test with offset.
self
.
assertRaises
(
struct
.
error
,
s
.
pack_to
,
small_buf
,
2
,
test_string
)
s
.
pack_to
(
writable_buf
,
10
,
test_string
)
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)
+
10
]
def
test_pack_to_fn
(
self
):
assert
from_buf
==
(
test_string
[:
10
]
+
test_string
)
test_string
=
'Reykjavik rocks, eow!'
writable_buf
=
array
.
array
(
'c'
,
' '
*
100
)
# Go beyond boundaries.
fmt
=
'21s'
small_buf
=
array
.
array
(
'c'
,
' '
*
10
)
pack_to
=
lambda
*
args
:
struct
.
pack_to
(
fmt
,
*
args
)
assertRaises
(
struct
.
error
,
s
.
pack_to
,
small_buf
,
0
,
test_string
)
assertRaises
(
struct
.
error
,
s
.
pack_to
,
small_buf
,
2
,
test_string
)
# Test without offset
pack_to
(
writable_buf
,
0
,
test_string
)
def
test_pack_to_fn
():
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)]
test_string
=
'Reykjavik rocks, eow!'
self
.
assertEquals
(
from_buf
,
test_string
)
writable_buf
=
array
.
array
(
'c'
,
' '
*
100
)
fmt
=
'21s'
# Test with offset.
pack_to
=
lambda
*
args
:
struct
.
pack_to
(
fmt
,
*
args
)
pack_to
(
writable_buf
,
10
,
test_string
)
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)
+
10
]
# Test without offset
self
.
assertEquals
(
from_buf
,
(
test_string
[:
10
]
+
test_string
))
pack_to
(
writable_buf
,
0
,
test_string
)
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)]
# Go beyond boundaries.
assert
from_buf
==
test_string
small_buf
=
array
.
array
(
'c'
,
' '
*
10
)
self
.
assertRaises
(
struct
.
error
,
pack_to
,
small_buf
,
0
,
test_string
)
# Test with offset.
self
.
assertRaises
(
struct
.
error
,
pack_to
,
small_buf
,
2
,
test_string
)
pack_to
(
writable_buf
,
10
,
test_string
)
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)
+
10
]
assert
from_buf
==
(
test_string
[:
10
]
+
test_string
)
def
test_main
():
test
.
test_support
.
run_unittest
(
PackBufferTestCase
)
# Go beyond boundaries.
small_buf
=
array
.
array
(
'c'
,
' '
*
10
)
if
__name__
==
"__main__"
:
assertRaises
(
struct
.
error
,
pack_to
,
small_buf
,
0
,
test_string
)
test_main
()
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