def test_init(): p = Point(0, 0) assert p.x == 0 assert p.y == 0 assert p.geometry == None assert_raises(TypeError, Point, Rational(0), 0)
def test_init_from_int(): r1 = Rational(1) assert r1.num == 1 assert r1.den == 1 r1 = Rational(10) assert r1.num == 10 assert r1.den == 1 r1 = Rational(-1) assert r1.num == -1 assert r1.den == 1 r1 = Rational(0) assert r1.num == 0 assert r1.den == 1
def test_init_from_pair(): r1 = Rational(1, 1) assert r1.num == 1 assert r1.den == 1 r1 = Rational(10, 1) assert r1.num == 10 assert r1.den == 1 r1 = Rational(-1, 1) assert r1.num == -1 assert r1.den == 1 r1 = Rational(0, 1) assert r1.num == 0 assert r1.den == 1 r1 = Rational(-1, -1) assert r1.num == 1 assert r1.den == 1 r1 = Rational(-10, -2) assert r1.num == 5 assert r1.den == 1 r1 = Rational(10, -3) assert r1.num == -10 assert r1.den == 3
def test_sub(): r1 = Rational(5) r2 = Rational(10) assert r1 - r2 == Rational(-5) assert r1 - r2 == -5 assert 5 - r2 == Rational(-5) assert 5 - r2 == -5 assert r1 - 10 == Rational(-5) assert r1 - 10 == -5 r1 = Rational(3, 2) r2 = Rational(5, 3) assert r1 - r2 == Rational(-1, 6) assert r2 - r1 == Rational(1, 6) assert r1 - r1 == 0 assert r1 - r1 == Rational(0) assert r1 - r1 == Rational(0, 1) assert r2 - r2 - r2 == -r2 assert r2 - r2 - r2 == Rational(-5, 3) assert r2 - r2 - r2 == Rational(-15, 9) assert_raises(TypeError, r1.__sub__, None) assert_raises(TypeError, r1.__sub__, 5.5) assert_raises(TypeError, r1.__rsub__, None) assert_raises(TypeError, r1.__rsub__, 5.5)
def test_add(): r1 = Rational(5) r2 = Rational(10) assert r1 + r2 == Rational(15) assert r1 + r2 == 15 assert 5 + r2 == Rational(15) assert 5 + r2 == 15 assert r1 + 10 == Rational(15) assert r1 + 10 == 15 r1 = Rational(3, 2) r2 = Rational(5, 3) assert r1 + r2 == Rational(19, 6) assert r2 + r1 == Rational(19, 6) assert r1 + r1 == 3 assert r1 + r1 == Rational(3) assert r1 + r1 == Rational(6, 2) assert r2 + r2 + r2 == 5 assert r2 + r2 + r2 == Rational(5) assert r2 + r2 + r2 == Rational(15, 3) assert_raises(TypeError, r1.__add__, None) assert_raises(TypeError, r1.__add__, 5.5) assert_raises(TypeError, r1.__radd__, None) assert_raises(TypeError, r1.__radd__, 5.5)
def test_eq(): r1 = Rational(5, 3) assert r1 == r1 assert_raises(TypeError, r1.__eq__, 5.0) assert_raises(TypeError, r1.__eq__, "fail") assert_raises(TypeError, r1.__eq__, None)
def test_div(): r2 = Rational(10) r1 = Rational(5) assert r1 / r2 == Rational(1, 2) assert 10 / r2 == Rational(1) assert 10 / r2 == 1 assert r1 / 5 == Rational(1) assert r1 / 5 == 1 r1 = Rational(3, 2) r2 = Rational(5, 3) assert r1 / r2 == Rational(9, 10) assert r2 / r1 == Rational(10, 9) assert 1 / r1 == Rational(2, 3) assert r1 / r1 == Rational(1) assert r2 / r2 / r2 == Rational(3, 5) assert_raises(TypeError, r1.__div__, None) assert_raises(TypeError, r1.__div__, 5.5) assert_raises(TypeError, r1.__rdiv__, None) assert_raises(TypeError, r1.__rdiv__, 5.5)
def test_mul(): r1 = Rational(5) r2 = Rational(10) assert r1 * r2 == Rational(50) assert r1 * r2 == 50 assert 5 * r2 == Rational(50) assert 5 * r2 == 50 assert r1 * 10 == Rational(50) assert r1 * 10 == 50 r1 = Rational(3, 2) r2 = Rational(5, 3) assert r1 * r2 == Rational(5, 2) assert r2 * r1 == Rational(5, 2) assert r1 * r1 == Rational(9, 4) assert r2 * r2 * r2 == Rational(125, 27) assert_raises(TypeError, r1.__mul__, None) assert_raises(TypeError, r1.__mul__, 5.5) assert_raises(TypeError, r1.__rmul__, None) assert_raises(TypeError, r1.__rmul__, 5.5) p = Point(r1, r2) p2 = p * r1 p3 = r1 * p
def test_midpoint(): f = FiniteField f.base = 11 X0 = Point(f(0), f(0), blue(f)) X1 = Point(f(2), f(2), blue(f)) X2 = Point(f(10), f(0), blue(f)) for (x0, x1) in [(X0, X1), (X1, X0), (X0, X2), (X2, X0), (X1, X2), (X2, X1)]: m = LineSegment(x0, x1).midpoint() assert LineSegment(x0, m).quadrance() == LineSegment(x1, m).quadrance() m = LineSegment(x1, x0).midpoint() assert LineSegment(x0, m).quadrance() == LineSegment(x1, m).quadrance() X0 = Point(Rational(1), Rational(7), red(Rational)) X1 = Point(Rational(3), Rational(2), red(Rational)) X2 = Point(Rational(10), Rational(1), red(Rational)) for (x0, x1) in [(X0, X1), (X1, X0), (X0, X2), (X2, X0), (X1, X2), (X2, X1)]: m = LineSegment(x0, x1).midpoint() assert LineSegment(x0, m).quadrance() == LineSegment(x1, m).quadrance() m = LineSegment(x1, x0).midpoint() assert LineSegment(x0, m).quadrance() == LineSegment(x1, m).quadrance() X0 = Point(Rational(3), Rational(0), green(Rational)) X1 = Point(Rational(2), Rational(5), green(Rational)) X2 = Point(Rational(10), Rational(1), green(Rational)) for (x0, x1) in [(X0, X1), (X1, X0), (X0, X2), (X2, X0), (X1, X2), (X2, X1)]: m = LineSegment(x0, x1).midpoint() assert LineSegment(x0, m).quadrance() == LineSegment(x1, m).quadrance() m = LineSegment(x1, x0).midpoint() assert LineSegment(x0, m).quadrance() == LineSegment(x1, m).quadrance()