summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_bool.py4
-rw-r--r--Lib/test/test_enum.py12
-rw-r--r--Lib/test/test_float.py18
-rw-r--r--Lib/test/test_long.py17
4 files changed, 51 insertions, 0 deletions
diff --git a/Lib/test/test_bool.py b/Lib/test/test_bool.py
index 2507439..d30a3b9 100644
--- a/Lib/test/test_bool.py
+++ b/Lib/test/test_bool.py
@@ -314,6 +314,10 @@ class BoolTest(unittest.TestCase):
return -1
self.assertRaises(ValueError, bool, Eggs())
+ def test_from_bytes(self):
+ self.assertIs(bool.from_bytes(b'\x00'*8, 'big'), False)
+ self.assertIs(bool.from_bytes(b'abcd', 'little'), True)
+
def test_sane_len(self):
# this test just tests our assumptions about __len__
# this will start failing if __len__ changes assertions
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index 4b14e7f..e970a26 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -541,6 +541,18 @@ class TestEnum(unittest.TestCase):
self.assertEqual([k for k,v in WeekDay.__members__.items()
if v.name != k], ['TEUSDAY', ])
+ def test_intenum_from_bytes(self):
+ self.assertIs(IntStooges.from_bytes(b'\x00\x03', 'big'), IntStooges.MOE)
+ with self.assertRaises(ValueError):
+ IntStooges.from_bytes(b'\x00\x05', 'big')
+
+ def test_floatenum_fromhex(self):
+ h = float.hex(FloatStooges.MOE.value)
+ self.assertIs(FloatStooges.fromhex(h), FloatStooges.MOE)
+ h = float.hex(FloatStooges.MOE.value + 0.01)
+ with self.assertRaises(ValueError):
+ FloatStooges.fromhex(h)
+
def test_pickle_enum(self):
if isinstance(Stooges, Exception):
raise Stooges
diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py
index 48f7a70..cb1f6db 100644
--- a/Lib/test/test_float.py
+++ b/Lib/test/test_float.py
@@ -1355,6 +1355,24 @@ class HexFloatTestCase(unittest.TestCase):
else:
self.identical(x, fromHex(toHex(x)))
+ def test_subclass(self):
+ class F(float):
+ def __new__(cls, value):
+ return float.__new__(cls, value + 1)
+
+ f = F.fromhex((1.5).hex())
+ self.assertIs(type(f), F)
+ self.assertEqual(f, 2.5)
+
+ class F2(float):
+ def __init__(self, value):
+ self.foo = 'bar'
+
+ f = F2.fromhex((1.5).hex())
+ self.assertIs(type(f), F2)
+ self.assertEqual(f, 1.5)
+ self.assertEqual(getattr(f, 'foo', 'none'), 'bar')
+
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py
index 62e69a9..b2d008b 100644
--- a/Lib/test/test_long.py
+++ b/Lib/test/test_long.py
@@ -1203,6 +1203,23 @@ class LongTest(unittest.TestCase):
self.assertRaises(TypeError, myint.from_bytes, 0, 'big')
self.assertRaises(TypeError, int.from_bytes, 0, 'big', True)
+ class myint2(int):
+ def __new__(cls, value):
+ return int.__new__(cls, value + 1)
+
+ i = myint2.from_bytes(b'\x01', 'big')
+ self.assertIs(type(i), myint2)
+ self.assertEqual(i, 2)
+
+ class myint3(int):
+ def __init__(self, value):
+ self.foo = 'bar'
+
+ i = myint3.from_bytes(b'\x01', 'big')
+ self.assertIs(type(i), myint3)
+ self.assertEqual(i, 1)
+ self.assertEqual(getattr(i, 'foo', 'none'), 'bar')
+
def test_access_to_nonexistent_digit_0(self):
# http://bugs.python.org/issue14630: A bug in _PyLong_Copy meant that
# ob_digit[0] was being incorrectly accessed for instances of a