MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_from_float

Method test_from_float

Lib/test/test_decimal.py:2716–2746  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2714 self.assertEqual(Decimal(math.trunc(d)), r)
2715
2716 def test_from_float(self):
2717
2718 Decimal = self.decimal.Decimal
2719
2720 class MyDecimal(Decimal):
2721 def __init__(self, _):
2722 self.x = 'y'
2723
2724 self.assertIsSubclass(MyDecimal, Decimal)
2725
2726 r = MyDecimal.from_float(0.1)
2727 self.assertEqual(type(r), MyDecimal)
2728 self.assertEqual(str(r),
2729 '0.1000000000000000055511151231257827021181583404541015625')
2730 self.assertEqual(r.x, 'y')
2731
2732 bigint = 12345678901234567890123456789
2733 self.assertEqual(MyDecimal.from_float(bigint), MyDecimal(bigint))
2734 self.assertTrue(MyDecimal.from_float(float('nan')).is_qnan())
2735 self.assertTrue(MyDecimal.from_float(float('inf')).is_infinite())
2736 self.assertTrue(MyDecimal.from_float(float('-inf')).is_infinite())
2737 self.assertEqual(str(MyDecimal.from_float(float('nan'))),
2738 str(Decimal('NaN')))
2739 self.assertEqual(str(MyDecimal.from_float(float('inf'))),
2740 str(Decimal('Infinity')))
2741 self.assertEqual(str(MyDecimal.from_float(float('-inf'))),
2742 str(Decimal('-Infinity')))
2743 self.assertRaises(TypeError, MyDecimal.from_float, 'abc')
2744 for i in range(200):
2745 x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0)
2746 self.assertEqual(x, float(MyDecimal.from_float(x))) # roundtrip
2747
2748 def test_create_decimal_from_float(self):
2749 Decimal = self.decimal.Decimal

Callers

nothing calls this directly

Calls 12

strFunction · 0.85
DecimalClass · 0.85
assertTrueMethod · 0.80
expovariateMethod · 0.80
MyDecimalClass · 0.70
assertIsSubclassMethod · 0.45
from_floatMethod · 0.45
assertEqualMethod · 0.45
is_qnanMethod · 0.45
is_infiniteMethod · 0.45
assertRaisesMethod · 0.45
randomMethod · 0.45

Tested by

no test coverage detected