Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testBasicUsage():
f = FractionValue(3, Fraction(5, 3))
assert f.number == 3
assert f.fraction == Fraction(5, 3)
f.number = 5.5
f.fraction = Fraction(6, 5)
assert f.number == 5.5
assert f.fraction == Fraction(6, 5)
with pytest.raises(TypeError):
f.SetNumber("hello")
with pytest.raises(TypeError):
f.SetFraction("hello")
with pytest.raises(ValueError):
f.SetFraction((1, 2, 3))
assert FractionValue(3).GetFraction() == Fraction(0, 1)
assert Fraction(5, 3) + Fraction(2, 3) == Fraction(7, 3)
assert Fraction(5, 3) + 5.0 == Fraction(20, 3)
assert 5.0 + Fraction(5, 3) == Fraction(20, 3)
# sub
assert Fraction(5, 3) - Fraction(2, 3) == Fraction(3, 3)
assert Fraction(5, 3) - 1.0 == Fraction(2, 3)
assert 1.0 - Fraction(5, 3) == Fraction(-2, 3)
# mul
assert Fraction(5, 3) * Fraction(4, 3) == Fraction(20, 9)
assert 3 * Fraction(5, 3) == Fraction(15, 3)
assert Fraction(5, 3) * 3 == Fraction(15, 3)
# div
assert Fraction(5, 3) / Fraction(5, 3) == Fraction(15, 15)
assert Fraction(5, 3) / 3 == Fraction(5, 9)
assert 3 / Fraction(5, 3) == Fraction(9, 5)
# inv
assert Fraction(5, 3).inv() == Fraction(3, 5)
# neg
assert -Fraction(5, 3) == Fraction(-5, 3)
assert Fraction(5, 3) + 5.0 == Fraction(20, 3)
assert 5.0 + Fraction(5, 3) == Fraction(20, 3)
# sub
assert Fraction(5, 3) - Fraction(2, 3) == Fraction(3, 3)
assert Fraction(5, 3) - 1.0 == Fraction(2, 3)
assert 1.0 - Fraction(5, 3) == Fraction(-2, 3)
# mul
assert Fraction(5, 3) * Fraction(4, 3) == Fraction(20, 9)
assert 3 * Fraction(5, 3) == Fraction(15, 3)
assert Fraction(5, 3) * 3 == Fraction(15, 3)
# div
assert Fraction(5, 3) / Fraction(5, 3) == Fraction(15, 15)
assert Fraction(5, 3) / 3 == Fraction(5, 9)
assert 3 / Fraction(5, 3) == Fraction(9, 5)
# inv
assert Fraction(5, 3).inv() == Fraction(3, 5)
# neg
assert -Fraction(5, 3) == Fraction(-5, 3)
def testCopy():
f = Fraction(5, 3)
cf = copy.copy(f)
assert f == cf
assert not f != cf
assert f is not cf
assert f != Fraction(3, 5)
assert not f == Fraction(3, 5)
assert 5.0 + Fraction(5, 3) == Fraction(20, 3)
# sub
assert Fraction(5, 3) - Fraction(2, 3) == Fraction(3, 3)
assert Fraction(5, 3) - 1.0 == Fraction(2, 3)
assert 1.0 - Fraction(5, 3) == Fraction(-2, 3)
# mul
assert Fraction(5, 3) * Fraction(4, 3) == Fraction(20, 9)
assert 3 * Fraction(5, 3) == Fraction(15, 3)
assert Fraction(5, 3) * 3 == Fraction(15, 3)
# div
assert Fraction(5, 3) / Fraction(5, 3) == Fraction(15, 15)
assert Fraction(5, 3) / 3 == Fraction(5, 9)
assert 3 / Fraction(5, 3) == Fraction(9, 5)
# inv
assert Fraction(5, 3).inv() == Fraction(3, 5)
# neg
assert -Fraction(5, 3) == Fraction(-5, 3)
def testOperations():
# float operator
assert float(Fraction(5, 3)) == 5.0 / 3.0
# sum
assert Fraction(5, 3) + Fraction(2, 3) == Fraction(7, 3)
assert Fraction(5, 3) + 5.0 == Fraction(20, 3)
assert 5.0 + Fraction(5, 3) == Fraction(20, 3)
# sub
assert Fraction(5, 3) - Fraction(2, 3) == Fraction(3, 3)
assert Fraction(5, 3) - 1.0 == Fraction(2, 3)
assert 1.0 - Fraction(5, 3) == Fraction(-2, 3)
# mul
assert Fraction(5, 3) * Fraction(4, 3) == Fraction(20, 9)
assert 3 * Fraction(5, 3) == Fraction(15, 3)
assert Fraction(5, 3) * 3 == Fraction(15, 3)
# div
assert Fraction(5, 3) / Fraction(5, 3) == Fraction(15, 15)
assert Fraction(5, 3) / 3 == Fraction(5, 9)
assert 3 / Fraction(5, 3) == Fraction(9, 5)
def testStrFormat():
"""
Scalar field behavior. In this test we make sure that the
Fraction.__str__ method calls FormatFloat, which handles
the locale properly.
"""
import barril.basic.format_float
# By default, the numbers are formatted using "%g"
f = Fraction(5, 3)
assert str(f), "5/3"
# Test the use of FormatFloat | 5.6 is 28/5 | 5.6/3 is 28/15
f = Fraction(5.6, 3)
assert str(f) == "28/15"
original_format_float = barril.basic.format_float.FormatFloat
barril.basic.format_float.FormatFloat = lambda x, y: "X%.2fX" % y
try:
assert str(f) == "X28.00X/X15.00X"
finally:
barril.basic.format_float.FormatFloat = original_format_float
assert str(f) == "28/15"
def testBasicUsage():
f = FractionValue(3, Fraction(5, 3))
assert f.number == 3
assert f.fraction == Fraction(5, 3)
f.number = 5.5
f.fraction = Fraction(6, 5)
assert f.number == 5.5
assert f.fraction == Fraction(6, 5)
with pytest.raises(TypeError):
f.SetNumber("hello")
with pytest.raises(TypeError):
f.SetFraction("hello")
with pytest.raises(ValueError):
f.SetFraction((1, 2, 3))
assert FractionValue(3).GetFraction() == Fraction(0, 1)
def testOperations():
# float operator
assert float(Fraction(5, 3)) == 5.0 / 3.0
# sum
assert Fraction(5, 3) + Fraction(2, 3) == Fraction(7, 3)
assert Fraction(5, 3) + 5.0 == Fraction(20, 3)
assert 5.0 + Fraction(5, 3) == Fraction(20, 3)
# sub
assert Fraction(5, 3) - Fraction(2, 3) == Fraction(3, 3)
assert Fraction(5, 3) - 1.0 == Fraction(2, 3)
assert 1.0 - Fraction(5, 3) == Fraction(-2, 3)
# mul
assert Fraction(5, 3) * Fraction(4, 3) == Fraction(20, 9)
assert 3 * Fraction(5, 3) == Fraction(15, 3)
assert Fraction(5, 3) * 3 == Fraction(15, 3)
# div
assert Fraction(5, 3) / Fraction(5, 3) == Fraction(15, 15)
assert Fraction(5, 3) / 3 == Fraction(5, 9)
assert 3 / Fraction(5, 3) == Fraction(9, 5)
# inv
assert Fraction(5, 3).inv() == Fraction(3, 5)
# neg
assert -Fraction(5, 3) == Fraction(-5, 3)
def SetFraction(self, fraction):
"""
Sets the fractional part of this object.
:type fraction: Fraction or tuple(float, float)
:param fraction:
The fraction for this FractionValue object. A pair (numerator, denominator)
is also accepted.
"""
CheckType(fraction, (Fraction, tuple))
# convert a tuple
if isinstance(fraction, tuple):
if len(fraction) != 2:
raise ValueError(f"Expected a tuple (numerator, denominator), got {fraction!r}")
fraction = Fraction(*fraction)
self._fraction = fraction