mirror of
https://github.com/python/cpython.git
synced 2025-09-15 13:16:12 +00:00
Fixed struct test to not use unittest.
This commit is contained in:
parent
d21a7fffb1
commit
7f7386cfd2
1 changed files with 72 additions and 66 deletions
|
@ -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():
|
||||||
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.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def test_unpack_from( self ):
|
###########################################################################
|
||||||
test_string = 'abcd01234'
|
# Packing and unpacking to/from buffers.
|
||||||
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 ):
|
# Copied and modified from unittest.
|
||||||
test_string = 'Reykjavik rocks, eow!'
|
def assertRaises(excClass, callableObj, *args, **kwargs):
|
||||||
writable_buf = array.array('c', ' '*100)
|
try:
|
||||||
fmt = '21s'
|
callableObj(*args, **kwargs)
|
||||||
s = struct.Struct(fmt)
|
except excClass:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
raise RuntimeError("%s not raised." % excClass)
|
||||||
|
|
||||||
# Test without offset
|
def test_unpack_from():
|
||||||
s.pack_to(writable_buf, 0, test_string)
|
test_string = 'abcd01234'
|
||||||
from_buf = writable_buf.tostring()[:len(test_string)]
|
fmt = '4s'
|
||||||
self.assertEquals(from_buf, test_string)
|
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)
|
||||||
|
|
||||||
# Test with offset.
|
def test_pack_to():
|
||||||
s.pack_to(writable_buf, 10, test_string)
|
test_string = 'Reykjavik rocks, eow!'
|
||||||
from_buf = writable_buf.tostring()[:len(test_string)+10]
|
writable_buf = array.array('c', ' '*100)
|
||||||
self.assertEquals(from_buf, (test_string[:10] + test_string))
|
fmt = '21s'
|
||||||
|
s = struct.Struct(fmt)
|
||||||
|
|
||||||
# Go beyond boundaries.
|
# Test without offset
|
||||||
small_buf = array.array('c', ' '*10)
|
s.pack_to(writable_buf, 0, test_string)
|
||||||
self.assertRaises(struct.error, s.pack_to, small_buf, 0, test_string)
|
from_buf = writable_buf.tostring()[:len(test_string)]
|
||||||
self.assertRaises(struct.error, s.pack_to, small_buf, 2, test_string)
|
assert from_buf == test_string
|
||||||
|
|
||||||
def test_pack_to_fn( self ):
|
# Test with offset.
|
||||||
test_string = 'Reykjavik rocks, eow!'
|
s.pack_to(writable_buf, 10, test_string)
|
||||||
writable_buf = array.array('c', ' '*100)
|
from_buf = writable_buf.tostring()[:len(test_string)+10]
|
||||||
fmt = '21s'
|
assert from_buf == (test_string[:10] + test_string)
|
||||||
pack_to = lambda *args: struct.pack_to(fmt, *args)
|
|
||||||
|
|
||||||
# Test without offset
|
# Go beyond boundaries.
|
||||||
pack_to(writable_buf, 0, test_string)
|
small_buf = array.array('c', ' '*10)
|
||||||
from_buf = writable_buf.tostring()[:len(test_string)]
|
assertRaises(struct.error, s.pack_to, small_buf, 0, test_string)
|
||||||
self.assertEquals(from_buf, test_string)
|
assertRaises(struct.error, s.pack_to, small_buf, 2, test_string)
|
||||||
|
|
||||||
# Test with offset.
|
def test_pack_to_fn():
|
||||||
pack_to(writable_buf, 10, test_string)
|
test_string = 'Reykjavik rocks, eow!'
|
||||||
from_buf = writable_buf.tostring()[:len(test_string)+10]
|
writable_buf = array.array('c', ' '*100)
|
||||||
self.assertEquals(from_buf, (test_string[:10] + test_string))
|
fmt = '21s'
|
||||||
|
pack_to = lambda *args: struct.pack_to(fmt, *args)
|
||||||
|
|
||||||
# Go beyond boundaries.
|
# Test without offset
|
||||||
small_buf = array.array('c', ' '*10)
|
pack_to(writable_buf, 0, test_string)
|
||||||
self.assertRaises(struct.error, pack_to, small_buf, 0, test_string)
|
from_buf = writable_buf.tostring()[:len(test_string)]
|
||||||
self.assertRaises(struct.error, pack_to, small_buf, 2, 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)
|
||||||
|
|
||||||
def test_main():
|
# Go beyond boundaries.
|
||||||
test.test_support.run_unittest(PackBufferTestCase)
|
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()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
test_main()
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue