|
6 | 6 | print_function, unicode_literals) |
7 | 7 | from future.builtins import * |
8 | 8 | from future.tests.base import unittest, expectedFailurePY2 |
9 | | -from future.utils import PY26 |
| 9 | +from future.utils import PY26, PY2, raise_from |
10 | 10 |
|
11 | 11 | import sys |
12 | 12 | import random |
| 13 | +import array |
13 | 14 |
|
14 | 15 | try: |
15 | 16 | import numpy as np |
@@ -778,5 +779,249 @@ def test_unary_operators(self): |
778 | 779 | self.assertEqual(-b, 3) |
779 | 780 | self.assertTrue(isinstance(-a, int)) |
780 | 781 |
|
| 782 | + def test_to_bytes(self): |
| 783 | + def check(tests, byteorder, signed=False): |
| 784 | + for test, expected in tests.items(): |
| 785 | + try: |
| 786 | + self.assertEqual( |
| 787 | + int(test).to_bytes(len(expected), byteorder, signed=signed), |
| 788 | + expected) |
| 789 | + except Exception as err: |
| 790 | + raise_from(AssertionError( |
| 791 | + "failed to convert {0} with byteorder={1} and signed={2}" |
| 792 | + .format(test, byteorder, signed)), err) |
| 793 | + |
| 794 | + # Convert integers to signed big-endian byte arrays. |
| 795 | + tests1 = { |
| 796 | + 0: bytes(b'\x00'), |
| 797 | + 1: bytes(b'\x01'), |
| 798 | + -1: bytes(b'\xff'), |
| 799 | + -127: bytes(b'\x81'), |
| 800 | + -128: bytes(b'\x80'), |
| 801 | + -129: bytes(b'\xff\x7f'), |
| 802 | + 127: bytes(b'\x7f'), |
| 803 | + 129: bytes(b'\x00\x81'), |
| 804 | + -255: bytes(b'\xff\x01'), |
| 805 | + -256: bytes(b'\xff\x00'), |
| 806 | + 255: bytes(b'\x00\xff'), |
| 807 | + 256: bytes(b'\x01\x00'), |
| 808 | + 32767: bytes(b'\x7f\xff'), |
| 809 | + -32768: bytes(b'\xff\x80\x00'), |
| 810 | + 65535: bytes(b'\x00\xff\xff'), |
| 811 | + -65536: bytes(b'\xff\x00\x00'), |
| 812 | + -8388608: bytes(b'\x80\x00\x00') |
| 813 | + } |
| 814 | + # check(tests1, 'big', signed=True) |
| 815 | + |
| 816 | + # Convert integers to signed little-endian byte arrays. |
| 817 | + tests2 = { |
| 818 | + 0: bytes(b'\x00'), |
| 819 | + 1: bytes(b'\x01'), |
| 820 | + -1: bytes(b'\xff'), |
| 821 | + -127: bytes(b'\x81'), |
| 822 | + -128: bytes(b'\x80'), |
| 823 | + -129: bytes(b'\x7f\xff'), |
| 824 | + 127: bytes(b'\x7f'), |
| 825 | + 129: bytes(b'\x81\x00'), |
| 826 | + -255: bytes(b'\x01\xff'), |
| 827 | + -256: bytes(b'\x00\xff'), |
| 828 | + 255: bytes(b'\xff\x00'), |
| 829 | + 256: bytes(b'\x00\x01'), |
| 830 | + 32767: bytes(b'\xff\x7f'), |
| 831 | + -32768: bytes(b'\x00\x80'), |
| 832 | + 65535: bytes(b'\xff\xff\x00'), |
| 833 | + -65536: bytes(b'\x00\x00\xff'), |
| 834 | + -8388608: bytes(b'\x00\x00\x80') |
| 835 | + } |
| 836 | + # check(tests2, 'little', signed=True) |
| 837 | + |
| 838 | + # Convert integers to unsigned big-endian byte arrays. |
| 839 | + tests3 = { |
| 840 | + 0: bytes(b'\x00'), |
| 841 | + 1: bytes(b'\x01'), |
| 842 | + 127: bytes(b'\x7f'), |
| 843 | + 128: bytes(b'\x80'), |
| 844 | + 255: bytes(b'\xff'), |
| 845 | + 256: bytes(b'\x01\x00'), |
| 846 | + 32767: bytes(b'\x7f\xff'), |
| 847 | + 32768: bytes(b'\x80\x00'), |
| 848 | + 65535: bytes(b'\xff\xff'), |
| 849 | + 65536: bytes(b'\x01\x00\x00') |
| 850 | + } |
| 851 | + check(tests3, 'big', signed=False) |
| 852 | + |
| 853 | + # Convert integers to unsigned little-endian byte arrays. |
| 854 | + tests4 = { |
| 855 | + 0: bytes(b'\x00'), |
| 856 | + 1: bytes(b'\x01'), |
| 857 | + 127: bytes(b'\x7f'), |
| 858 | + 128: bytes(b'\x80'), |
| 859 | + 255: bytes(b'\xff'), |
| 860 | + 256: bytes(b'\x00\x01'), |
| 861 | + 32767: bytes(b'\xff\x7f'), |
| 862 | + 32768: bytes(b'\x00\x80'), |
| 863 | + 65535: bytes(b'\xff\xff'), |
| 864 | + 65536: bytes(b'\x00\x00\x01') |
| 865 | + } |
| 866 | + check(tests4, 'little', signed=False) |
| 867 | + |
| 868 | + self.assertRaises(OverflowError, int(256).to_bytes, 1, 'big', signed=False) |
| 869 | + # self.assertRaises(OverflowError, int(256).to_bytes, 1, 'big', signed=True) |
| 870 | + self.assertRaises(OverflowError, int(256).to_bytes, 1, 'little', signed=False) |
| 871 | + # self.assertRaises(OverflowError, int(256).to_bytes, 1, 'little', signed=True) |
| 872 | + self.assertRaises(OverflowError, int(-1).to_bytes, 2, 'big', signed=False), |
| 873 | + self.assertRaises(OverflowError, int(-1).to_bytes, 2, 'little', signed=False) |
| 874 | + self.assertEqual(int(0).to_bytes(0, 'big'), b'') |
| 875 | + self.assertEqual(int(1).to_bytes(5, 'big'), b'\x00\x00\x00\x00\x01') |
| 876 | + self.assertEqual(int(0).to_bytes(5, 'big'), b'\x00\x00\x00\x00\x00') |
| 877 | + # self.assertEqual(int(-1).to_bytes(5, 'big', signed=True), |
| 878 | + # bytes(b'\xff\xff\xff\xff\xff')) |
| 879 | + self.assertRaises(OverflowError, int(1).to_bytes, 0, 'big') |
| 880 | + |
| 881 | + def test_from_bytes(self): |
| 882 | + def check(tests, byteorder, signed=False): |
| 883 | + for test, expected in tests.items(): |
| 884 | + try: |
| 885 | + self.assertEqual( |
| 886 | + int.from_bytes(test, byteorder, signed=signed), |
| 887 | + int(expected)) |
| 888 | + except Exception as err: |
| 889 | + raise_from(AssertionError( |
| 890 | + "failed to convert {0} with byteorder={1!r} and signed={2}" |
| 891 | + .format(test, byteorder, signed)), err) |
| 892 | + |
| 893 | + # Convert signed big-endian byte arrays to integers. |
| 894 | + tests1 = { |
| 895 | + bytes(b''): 0, |
| 896 | + bytes(b'\x00'): 0, |
| 897 | + bytes(b'\x00\x00'): 0, |
| 898 | + bytes(b'\x01'): 1, |
| 899 | + bytes(b'\x00\x01'): 1, |
| 900 | + bytes(b'\xff'): -1, |
| 901 | + bytes(b'\xff\xff'): -1, |
| 902 | + bytes(b'\x81'): -127, |
| 903 | + bytes(b'\x80'): -128, |
| 904 | + bytes(b'\xff\x7f'): -129, |
| 905 | + bytes(b'\x7f'): 127, |
| 906 | + bytes(b'\x00\x81'): 129, |
| 907 | + bytes(b'\xff\x01'): -255, |
| 908 | + bytes(b'\xff\x00'): -256, |
| 909 | + bytes(b'\x00\xff'): 255, |
| 910 | + bytes(b'\x01\x00'): 256, |
| 911 | + bytes(b'\x7f\xff'): 32767, |
| 912 | + bytes(b'\x80\x00'): -32768, |
| 913 | + bytes(b'\x00\xff\xff'): 65535, |
| 914 | + bytes(b'\xff\x00\x00'): -65536, |
| 915 | + bytes(b'\x80\x00\x00'): -8388608 |
| 916 | + } |
| 917 | + # check(tests1, 'big', signed=True) |
| 918 | + |
| 919 | + # Convert signed little-endian byte arrays to integers. |
| 920 | + tests2 = { |
| 921 | + bytes(b''): 0, |
| 922 | + bytes(b'\x00'): 0, |
| 923 | + bytes(b'\x00\x00'): 0, |
| 924 | + bytes(b'\x01'): 1, |
| 925 | + bytes(b'\x00\x01'): 256, |
| 926 | + bytes(b'\xff'): -1, |
| 927 | + bytes(b'\xff\xff'): -1, |
| 928 | + bytes(b'\x81'): -127, |
| 929 | + bytes(b'\x80'): -128, |
| 930 | + bytes(b'\x7f\xff'): -129, |
| 931 | + bytes(b'\x7f'): 127, |
| 932 | + bytes(b'\x81\x00'): 129, |
| 933 | + bytes(b'\x01\xff'): -255, |
| 934 | + bytes(b'\x00\xff'): -256, |
| 935 | + bytes(b'\xff\x00'): 255, |
| 936 | + bytes(b'\x00\x01'): 256, |
| 937 | + bytes(b'\xff\x7f'): 32767, |
| 938 | + bytes(b'\x00\x80'): -32768, |
| 939 | + bytes(b'\xff\xff\x00'): 65535, |
| 940 | + bytes(b'\x00\x00\xff'): -65536, |
| 941 | + bytes(b'\x00\x00\x80'): -8388608 |
| 942 | + } |
| 943 | + # check(tests2, 'little', signed=True) |
| 944 | + |
| 945 | + # Convert unsigned big-endian byte arrays to integers. |
| 946 | + tests3 = { |
| 947 | + bytes(b''): 0, |
| 948 | + bytes(b'\x00'): 0, |
| 949 | + bytes(b'\x01'): 1, |
| 950 | + bytes(b'\x7f'): 127, |
| 951 | + bytes(b'\x80'): 128, |
| 952 | + bytes(b'\xff'): 255, |
| 953 | + bytes(b'\x01\x00'): 256, |
| 954 | + bytes(b'\x7f\xff'): 32767, |
| 955 | + bytes(b'\x80\x00'): 32768, |
| 956 | + bytes(b'\xff\xff'): 65535, |
| 957 | + bytes(b'\x01\x00\x00'): 65536, |
| 958 | + } |
| 959 | + check(tests3, 'big', signed=False) |
| 960 | + |
| 961 | + # Convert integers to unsigned little-endian byte arrays. |
| 962 | + tests4 = { |
| 963 | + bytes(b''): 0, |
| 964 | + bytes(b'\x00'): 0, |
| 965 | + bytes(b'\x01'): 1, |
| 966 | + bytes(b'\x7f'): 127, |
| 967 | + bytes(b'\x80'): 128, |
| 968 | + bytes(b'\xff'): 255, |
| 969 | + bytes(b'\x00\x01'): 256, |
| 970 | + bytes(b'\xff\x7f'): 32767, |
| 971 | + bytes(b'\x00\x80'): 32768, |
| 972 | + bytes(b'\xff\xff'): 65535, |
| 973 | + bytes(b'\x00\x00\x01'): 65536, |
| 974 | + } |
| 975 | + check(tests4, 'little', signed=False) |
| 976 | + |
| 977 | + class myint(int): |
| 978 | + pass |
| 979 | + |
| 980 | + if PY2: |
| 981 | + import __builtin__ |
| 982 | + oldbytes = __builtin__.bytes |
| 983 | + types = (bytes, oldbytes) |
| 984 | + else: |
| 985 | + types = (bytes,) |
| 986 | + for mytype in types: |
| 987 | + self.assertIs(type(myint.from_bytes(mytype(b'\x00'), 'big')), myint) |
| 988 | + self.assertEqual(myint.from_bytes(mytype(b'\x01'), 'big'), 1) |
| 989 | + self.assertIs( |
| 990 | + type(myint.from_bytes(mytype(b'\x00'), 'big', signed=False)), myint) |
| 991 | + self.assertEqual(myint.from_bytes(mytype(b'\x01'), 'big', signed=False), 1) |
| 992 | + self.assertIs(type(myint.from_bytes(mytype(b'\x00'), 'little')), myint) |
| 993 | + self.assertEqual(myint.from_bytes(mytype(b'\x01'), 'little'), 1) |
| 994 | + self.assertIs(type(myint.from_bytes( |
| 995 | + mytype(b'\x00'), 'little', signed=False)), myint) |
| 996 | + self.assertEqual(myint.from_bytes(mytype(b'\x01'), 'little', signed=False), 1) |
| 997 | + # self.assertEqual( |
| 998 | + # int.from_bytes([255, 0, 0], 'big', signed=True), -65536) |
| 999 | + # self.assertEqual( |
| 1000 | + # int.from_bytes((255, 0, 0), 'big', signed=True), -65536) |
| 1001 | + # self.assertEqual(int.from_bytes( |
| 1002 | + # bytearray(mytype(b'\xff\x00\x00')), 'big', signed=True), -65536) |
| 1003 | + # self.assertEqual(int.from_bytes( |
| 1004 | + # bytearray(mytype(b'\xff\x00\x00')), 'big', signed=True), -65536) |
| 1005 | + # self.assertEqual(int.from_bytes( |
| 1006 | + # array.array('B', mytype(b'\xff\x00\x00')), 'big', signed=True), -65536) |
| 1007 | + # self.assertEqual(int.from_bytes( |
| 1008 | + # memoryview(mytype(b'\xff\x00\x00')), 'big', signed=True), -65536) |
| 1009 | + |
| 1010 | + self.assertRaises(TypeError, int.from_bytes, u"", 'big') |
| 1011 | + self.assertRaises(TypeError, int.from_bytes, u"\x00", 'big') |
| 1012 | + self.assertRaises(TypeError, myint.from_bytes, u"", 'big') |
| 1013 | + self.assertRaises(TypeError, myint.from_bytes, u"\x00", 'big') |
| 1014 | + |
| 1015 | + types = (int, lambda x: x) if PY2 else (lambda x: x,) |
| 1016 | + for mytype in types: |
| 1017 | + self.assertRaises(ValueError, int.from_bytes, [mytype(256)], 'big') |
| 1018 | + self.assertRaises(ValueError, int.from_bytes, [mytype(0)], 'big\x00') |
| 1019 | + self.assertRaises(ValueError, int.from_bytes, [mytype(0)], 'little\x00') |
| 1020 | + self.assertRaises(TypeError, int.from_bytes, mytype(0), 'big') |
| 1021 | + # self.assertRaises(TypeError, int.from_bytes, mytype(0), 'big', True) |
| 1022 | + self.assertRaises(TypeError, myint.from_bytes, mytype(0), 'big') |
| 1023 | + # self.assertRaises(TypeError, int.from_bytes, mytype(0), 'big', True) |
| 1024 | + |
| 1025 | + |
781 | 1026 | if __name__ == "__main__": |
782 | 1027 | unittest.main() |
0 commit comments