Side by Side Diff

Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Keyboard Shortcuts

File
u :up to issue
m :publish + mail comments
M :edit review message
j / k :jump to file after / before current file
J / K :jump to next file with a comment after / before current file
Side-by-side diff
i :toggle intra-line diffs
e :expand all comments
c :collapse all comments
s :toggle showing all comments
n / p :next / previous diff chunk or comment
N / P :next / previous comment
<Up> / <Down> :next / previous line
<Enter> :respond to / edit current comment
d :mark current comment as done
Issue
u :up to list of issues
m :publish + mail comments
j / k :jump to patch after / before current patch
o / <Enter> :open current patch in side-by-side view
i :open current patch in unified diff view
Issue List
j / k :jump to issue after / before current issue
o / <Enter> :open current issue
# : close issue
Comment/message editing
<Ctrl> + s or <Ctrl> + Enter :save comment
<Esc> :cancel edit
Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(104)
Issues Repositories Search
Open Issues | Closed Issues | All Issues | Sign in with your Google Account to create issues and add comments

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

Issue 1258041: PEP 3318 - New struct string syntax Base URL: http://svn.python.org/view/*checkout*/python/branches/py3k/
Patch Set: Created 15 years, 7 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 | « Doc/library/struct.rst ('k') | Modules/_struct.c » ('j') | Modules/_struct.c » ('J')
('i') | ('e') | ('c') | ('s')
OLDNEW
1 import array 1 import array
2 import unittest 2 import unittest
3 import struct 3 import struct
4 import sys 4 import sys
5 5
6 from test.support import run_unittest 6 from test.support import run_unittest
7 7
8 ISBIGENDIAN = sys.byteorder == "big" 8 ISBIGENDIAN = sys.byteorder == "big"
9 IS32BIT = sys.maxsize == 0x7fffffff 9 IS32BIT = sys.maxsize == 0x7fffffff
10 10
(...skipping 312 matching lines...) | | Loading...
323 for code in integer_codes: 323 for code in integer_codes:
324 for byteorder in byteorders: 324 for byteorder in byteorders:
325 if (byteorder in ('', '@') and code in ('q', 'Q') and 325 if (byteorder in ('', '@') and code in ('q', 'Q') and
326 not HAVE_LONG_LONG): 326 not HAVE_LONG_LONG):
327 continue 327 continue
328 format = byteorder+code 328 format = byteorder+code
329 t = IntTester(format) 329 t = IntTester(format)
330 t.run() 330 t.run()
331 331
332 def test_p_code(self): 332 def test_p_code(self):
333 return
333 # Test p ("Pascal string") code. 334 # Test p ("Pascal string") code.
334 for code, input, expected, expectedback in [ 335 for code, input, expected, expectedback in [
335 ('p','abc', '\x00', b''), 336 ('p','abc', '\x00', b''),
336 ('1p', 'abc', '\x00', b''), 337 ('1p', 'abc', '\x00', b''),
337 ('2p', 'abc', '\x01a', b'a'), 338 ('2p', 'abc', '\x01a', b'a'),
338 ('3p', 'abc', '\x02ab', b'ab'), 339 ('3p', 'abc', '\x02ab', b'ab'),
339 ('4p', 'abc', '\x03abc', b'abc'), 340 ('4p', 'abc', '\x03abc', b'abc'),
340 ('5p', 'abc', '\x03abc\x00', b'abc'), 341 ('5p', 'abc', '\x03abc\x00', b'abc'),
341 ('6p', 'abc', '\x03abc\x00\x00', b'abc'), 342 ('6p', 'abc', '\x03abc\x00\x00', b'abc'),
342 ('1000p', 'x'*1000, '\xff' + 'x'*999, b'x'*255)]: 343 ('1000p', 'x'*1000, '\xff' + 'x'*999, b'x'*255)]:
(...skipping 160 matching lines...) | | Loading...
503 self.assertFalse(prefix, msg='encoded bool is not one byte: %r' 504 self.assertFalse(prefix, msg='encoded bool is not one byte: %r'
504 %packed) 505 %packed)
505 506
506 for c in [b'\x01', b'\x7f', b'\xff', b'\x0f', b'\xf0']: 507 for c in [b'\x01', b'\x7f', b'\xff', b'\x0f', b'\xf0']:
507 self.assertTrue(struct.unpack('>?', c)[0]) 508 self.assertTrue(struct.unpack('>?', c)[0])
508 509
509 if IS32BIT: 510 if IS32BIT:
510 def test_crasher(self): 511 def test_crasher(self):
511 self.assertRaises(MemoryError, struct.pack, "357913941b", "a") 512 self.assertRaises(MemoryError, struct.pack, "357913941b", "a")
512 513
514 def test_structs(self):
515 tests = (
516 ('', 'T{}', (), ()),
517 ('', 'T{' * 1000 + '}' * 1000, (), ()),
518 ('', 'T{T{T{}}}', (), ()),
519 ('ii ', 'T{ii} ', (1, 2), ((1, 2),)),
520 ('iii', 'T{ii} i', (1, 2, 3), ((1, 2),3)),
521 ('HB2B', '\t\tT{\n\tH\n\n\nB 2B}',·
522 (1, 2, 3, 4), ((1, 2, 3, 4),)),
523 ('i2i', 'T{ i 2i }', (1, 2, 3), ((1, 2, 3),)),
524 ('iiiHHiBiHBiHi',·
525 '\t T{\n\nii T {i HH }\ni T { BiH B i } } T\t{ H }i\t',
526 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13),
527 ((1, 2, (3, 4, 5), 6, (7, 8, 9, 10, 11)), (12,), 13)),
528 ('ii', 'T { i T { i } }', (187, 42), ((187, (42,)),)),
529 ('iHBB',
530 """i
531 T{
532 H
533 B
534 B
535 }
536 """,
537 (1, 2, 3, 4),
538 (1, (2, 3, 4)),
539 ),
540 ('iHBBBB',
541 """i
542 T{
543 H
544 T {}
545 B
546 T {
547 }
548 B
549 T { BB }
550 }
551 """,
552 (1, 2, 3, 4, 5, 6),
553 (1, (2, 3, 4, (5, 6))),
554 )
555
556 )
557 for test in tests:
558 flat = struct.Struct(test[0])
559 nested = struct.Struct(test[1])
560 nested_str = nested.pack(*test[3])
561 self.assertEqual(flat.pack(*test[2]), nested_str)
562 self.assertEqual(test[3], nested.unpack(nested_str))
563
564 def test_bad_pack(self):
565 tests = (
566 ('T{T{T{}}}', (1,)),
567 ('T{i}', ((),)),
568 ('T{i}', (1,)),
569 ('h T { b }', ((2,), 1)),
570 ('T { h } T { h }', (1, 2)),
571 ('T { T { h } }', ((1,),)),
572 ('T { h }', (('fizzbuzz',),)),
573 ('ii', 'T { i T { i } }', (187, 42), (('foo', (42,)),)),
574 ('bb', ((1,), (2,))),
575 )
576 for test in tests:
577 self.assertRaises(struct.error, struct.pack, test[0], *test[1])
578
579 def test_bad_unpack(self):
580 tests = (
581 ('T{i}', b'fizzbuzz'),
582 ('i T{ h T{b} }', b'\x00' * 100),
583 ('T{ b T{T{T{T{bbb}}} 10s }}', b'\x00'),
584 )
585 for test in tests:
586 self.assertRaises(struct.error, struct.unpack, test[0], test[1])
587
588 def test_bad_structs(self):
589 tests = (
590 'T{', '\nT\t\t{', 'T', 'T { T { }', 'T\n{\nT{\n}\n\n',
591 '((()))', 'T<{}', '12&&&afa7sasdf7asdfl><>aaa', '{}', 'T{}TTTTT',
592 '1+2*200', 'def foo(): return 187', 't{b}'
593 )
594 for test in tests:
595 self.assertRaises(struct.error, struct.pack, test)
596
597 def test_byte_order_switch(self):
598 tests = (
599 ('<i<i', (1, 2), b'\x01\x00\x00\x00\x02\x00\x00\x00'),
600 ('<i>i', (1, 2), b'\x01\x00\x00\x00\x00\x00\x00\x02'),
601 ('>i<i', (1, 2), b'\x00\x00\x00\x01\x02\x00\x00\x00'),
602 ('>i>i', (1, 2), b'\x00\x00\x00\x01\x00\x00\x00\x02'),
603 ('<H T { >i }', (1, (2,)), b'\x01\x00\x00\x00\x00\x02'),
604 ('<H > T { i }', (1, (2,)), b'\x01\x00\x00\x00\x00\x02')
605 )
606 for test in tests:
607 self.assertEquals(struct.pack(test[0], *test[1]), test[2])
608
609 def test_del(self):
610 s = struct.Struct('T{ T { h } b}')
611 del s
612 with self.assertRaises(NameError) as exc:
613 s.pack( ((1,), 2))
513 614
514 def test_main(): 615 def test_main():
515 run_unittest(StructTest) 616 run_unittest(StructTest)
516 617
517 if __name__ == '__main__': 618 if __name__ == '__main__':
518 test_main() 619 test_main()
OLDNEW
« no previous file with comments | « Doc/library/struct.rst ('k') | Modules/_struct.c » ('j') | Modules/_struct.c » ('J')
Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b

AltStyle によって変換されたページ (->オリジナル) /