コード例 #1
0
ファイル: test_rational.py プロジェクト: Othko97/tomath
def test_main_constructor():
    assert A.numerator / A.denominator == 0.5

    assert Rational(2) == Rational(2, 1)

    # Ensure denominator always positive
    assert A.denominator > 0 and A1.denominator > 0 and A2.denominator > 0 and A3.denominator > 0

    flag = False
    try:
        err = Rational(1, 0)
    except ZeroDivisionError:
        flag = True

    assert flag == True
コード例 #2
0
ファイル: test_rational.py プロジェクト: Othko97/tomath
def test_power():
    assert A**2 == Rational(1, 4)
    assert A**0 == 1
    assert A**-1 == 2
    assert A**-3 == 8
コード例 #3
0
ファイル: test_rational.py プロジェクト: Othko97/tomath
def test_division():
    assert A / B == Rational(3, 2)
    assert A / 2 == Rational(1, 4)
    assert 2 / A == 4
コード例 #4
0
ファイル: test_rational.py プロジェクト: Othko97/tomath
def test_multiplication():
    assert A * B == Rational(1, 6)
    assert B * A == A * B
    assert 2 * A == 1
    assert 2 * A == A * 2
コード例 #5
0
ファイル: test_rational.py プロジェクト: Othko97/tomath
def test_subtraction():
    assert A - B == Rational(1, 6)
    assert A - 1 == Rational(-1, 2)
    assert 1 - A == Rational(1, 2)
コード例 #6
0
ファイル: test_rational.py プロジェクト: Othko97/tomath
def test_addition():
    assert A + B == Rational(5, 6)
    assert A + B == B + A
    assert 1 + A == Rational(3, 2)
    assert A + 1 == 1 + A
コード例 #7
0
ファイル: test_rational.py プロジェクト: Othko97/tomath
def test_eq():
    assert A == A
    assert Rational(2, 1) == 2
コード例 #8
0
ファイル: test_rational.py プロジェクト: Othko97/tomath
def test_from_str():
    assert Rational.from_str("1/2") == A
コード例 #9
0
ファイル: test_rational.py プロジェクト: Othko97/tomath
def test_ipow():
    a = A
    a **= 4
    assert a == Rational(1, 16)
コード例 #10
0
ファイル: test_rational.py プロジェクト: Othko97/tomath
"""
Tests for Rational numbers defined by the class.
"""

from numtheory.types.Rational import Rational

#############

# Test Data #

#############

A = Rational(1, 2)  #Half
B = Rational(1, 3)  #Third

A1 = Rational(-1, 2)
A2 = Rational(1, -2)
A3 = Rational(-1, -2)

#####################

# Test Constructors #

#####################


def test_main_constructor():
    assert A.numerator / A.denominator == 0.5

    assert Rational(2) == Rational(2, 1)