Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(16640)

Side by Side Diff: Lib/test/test_struct.py

Issue 17804: streaming struct unpacking
Patch Set: Created 6 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/struct.py ('k') | Modules/_struct.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 from collections import abc
1 import array 2 import array
3 import operator
2 import unittest 4 import unittest
3 import struct 5 import struct
4 import sys 6 import sys
5 7
6 from test import support 8 from test import support
7 9
8 ISBIGENDIAN = sys.byteorder == "big" 10 ISBIGENDIAN = sys.byteorder == "big"
9 IS32BIT = sys.maxsize == 0x7fffffff 11 IS32BIT = sys.maxsize == 0x7fffffff
10 12
11 integer_codes = 'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q', 'n', 'N' 13 integer_codes = 'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q', 'n', 'N'
(...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 self.check_sizeof('BHILfdspP', 9) 588 self.check_sizeof('BHILfdspP', 9)
587 self.check_sizeof('B' * 1234, 1234) 589 self.check_sizeof('B' * 1234, 1234)
588 self.check_sizeof('fd', 2) 590 self.check_sizeof('fd', 2)
589 self.check_sizeof('xxxxxxxxxxxxxx', 0) 591 self.check_sizeof('xxxxxxxxxxxxxx', 0)
590 self.check_sizeof('100H', 100) 592 self.check_sizeof('100H', 100)
591 self.check_sizeof('187s', 1) 593 self.check_sizeof('187s', 1)
592 self.check_sizeof('20p', 1) 594 self.check_sizeof('20p', 1)
593 self.check_sizeof('0s', 1) 595 self.check_sizeof('0s', 1)
594 self.check_sizeof('0c', 0) 596 self.check_sizeof('0c', 0)
595 597
598
599 class UnpackIteratorTest(unittest.TestCase):
600 """
601 Tests for iterative unpacking (struct.Struct.iter_unpack).
602 """
603
604 def test_construct(self):
605 def _check_iterator(it):
606 self.assertIsInstance(it, abc.Iterator)
607 self.assertIsInstance(it, abc.Iterable)
608 s = struct.Struct('>ibcp')
609 it = s.iter_unpack(b"")
610 _check_iterator(it)
611 it = s.iter_unpack(b"1234567")
612 _check_iterator(it)
613 # Wrong bytes length
614 with self.assertRaises(struct.error):
615 s.iter_unpack(b"123456")
616 with self.assertRaises(struct.error):
617 s.iter_unpack(b"12345678")
618 # Zero-length struct
619 s = struct.Struct('>')
620 with self.assertRaises(struct.error):
621 s.iter_unpack(b"")
622 with self.assertRaises(struct.error):
623 s.iter_unpack(b"12")
624
625 def test_iterate(self):
626 s = struct.Struct('>IB')
627 b = bytes(range(1, 16))
628 it = s.iter_unpack(b)
629 self.assertEqual(next(it), (0x01020304, 5))
630 self.assertEqual(next(it), (0x06070809, 10))
631 self.assertEqual(next(it), (0x0b0c0d0e, 15))
632 self.assertRaises(StopIteration, next, it)
633 self.assertRaises(StopIteration, next, it)
634
635 def test_arbitrary_buffer(self):
636 s = struct.Struct('>IB')
637 b = bytes(range(1, 11))
638 it = s.iter_unpack(memoryview(b))
639 self.assertEqual(next(it), (0x01020304, 5))
640 self.assertEqual(next(it), (0x06070809, 10))
641 self.assertRaises(StopIteration, next, it)
642 self.assertRaises(StopIteration, next, it)
643
644 def test_length_hint(self):
645 lh = operator.length_hint
646 s = struct.Struct('>IB')
647 b = bytes(range(1, 16))
648 it = s.iter_unpack(b)
649 self.assertEqual(lh(it), 3)
650 next(it)
651 self.assertEqual(lh(it), 2)
652 next(it)
653 self.assertEqual(lh(it), 1)
654 next(it)
655 self.assertEqual(lh(it), 0)
656 self.assertRaises(StopIteration, next, it)
657 self.assertEqual(lh(it), 0)
658
659 def test_module_func(self):
660 # Sanity check for the global struct.iter_unpack()
661 it = struct.iter_unpack('>IB', bytes(range(1, 11)))
662 self.assertEqual(next(it), (0x01020304, 5))
663 self.assertEqual(next(it), (0x06070809, 10))
664 self.assertRaises(StopIteration, next, it)
665 self.assertRaises(StopIteration, next, it)
666
667
596 def test_main(): 668 def test_main():
597 support.run_unittest(StructTest) 669 support.run_unittest(__name__)
598 670
599 if __name__ == '__main__': 671 if __name__ == '__main__':
600 test_main() 672 test_main()
OLDNEW
« no previous file with comments | « Lib/struct.py ('k') | Modules/_struct.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+