Beispiel #1
0
def testTriangleArea():
    print('Testing triangleArea()... ', end='')
    assertAlmostEqual(triangleArea(3, 4, 5), 6)
    assertAlmostEqual(triangleArea(3, 4, 0), 0)
    assertAlmostEqual(triangleArea(3, 4, 7), 0)
    assertAlmostEqual(triangleArea(-3, -4, -5), 0)
    assertAlmostEqual(triangleArea(1, 2, 2.8), (2.9 * 1.9 * 0.9 * 0.1)**0.5)
    print('Passed.')
Beispiel #2
0
def testGetInRange():
    print('Testing getInRange()... ', end='')
    assertEqual(getInRange(5, 1, 10), 5)
    assertEqual(getInRange(5, 5, 10), 5)
    assertEqual(getInRange(5, 9, 10), 9)
    assertEqual(getInRange(5, 10, 10), 10)
    assertEqual(getInRange(5, 10, 1), 5)
    assertEqual(getInRange(5, 10, 5), 5)
    assertEqual(getInRange(5, 10, 9), 9)
    assertEqual(getInRange(0, -20, -30), -20)
    assertAlmostEqual(getInRange(0, -20.25, -30.33), -20.25)
    print('Passed.')
Beispiel #3
0
def testLineIntersection():
    print('Testing lineIntersection()... ', end='')
    assertEqual(lineIntersection(2.5, 3, 2.5, 11), None)
    assertEqual(lineIntersection(25, 3, 25, 11), None)
    # y=3x-5 and y=x+5 intersect at (5,10)
    assertAlmostEqual(lineIntersection(3, -5, 1, 5), 5)
    # y=10x and y=-4x+35 intersect at (2.5,25)
    assertAlmostEqual(lineIntersection(10, 0, -4, 35), 2.5)
    assertAlmostEqual(lineIntersection(10, 0, -4, 15), 1.0714285714285714)
    print('Passed.')
Beispiel #4
0
def testRiverCruiseUpstreamTime():
    print('Testing riverCruiseUpstreamTime()... ', end='')
    # example from the source notes:
    totalTime = 3 # hours
    totalDistance = 30 # 15km up, 15km back down
    riverCurrent = 2 # km/hour
    assertAlmostEqual(riverCruiseUpstreamTime(totalTime,
                                              totalDistance,
                                              riverCurrent),
                       1.7888736053508778) # 1.79 in notes
    # another simple example
    totalTime = 3 # hours
    totalDistance = 30 # 15km up, 15km back down
    riverCurrent = 0 # km/hour
    assertAlmostEqual(riverCruiseUpstreamTime(totalTime,
                                              totalDistance,
                                              riverCurrent),
                       1.5)
    assertAlmostEqual(riverCruiseUpstreamTime(4,56,2),2.2801098892805185)
    print('Passed.')
Beispiel #5
0
def testThreeLinesArea():
    print('Testing threeLinesArea()... ', end='')
    assertAlmostEqual(threeLinesArea(1, 2, 3, 4, 5, 6), 0)
    assertAlmostEqual(threeLinesArea(0, 7, 1, 0, -1, 2), 36)
    assertAlmostEqual(threeLinesArea(0, 3, -.5, -5, 1, 3), 42.66666666666666)
    assertAlmostEqual(threeLinesArea(1, -5, 0, -2, 2, 2), 25)
    assertAlmostEqual(threeLinesArea(0, -9.75, -6, 2.25, 1, -4.75), 21)
    assertAlmostEqual(threeLinesArea(1, -5, 0, -2, 2, 25), 272.25)
    print('Passed.')
Beispiel #6
0
def testTriangleAreaByCoordinates():
    print('Testing triangleAreaByCoordinates()... ', end='')
    assertAlmostEqual(triangleAreaByCoordinates(1, 1, 9, 1, 5, 5), 16)
    assertAlmostEqual(triangleAreaByCoordinates(0, 0, 10, 0, 0, 50), 250)
    assertAlmostEqual(
        triangleAreaByCoordinates(1, 3, 5, 3, 3, (3 + 2 * 3**.5)), 4 * 3**.5)
    assertAlmostEqual(triangleAreaByCoordinates(-6, 7, -3, 20, 0, 7), 39)
    assertAlmostEqual(triangleAreaByCoordinates(-2, 2, 2, -2, 5, 5), 20)
    assertAlmostEqual(triangleAreaByCoordinates(-2, 2, -2, 2, 5, 5), 0)
    print('Passed.')
Beispiel #7
0
def testDistance():
    print('Testing distance()... ', end='')
    assertAlmostEqual(distance(0, 0, 3, 4), 5)
    assertAlmostEqual(distance(-1, -2, 3, 1), 5)
    assertAlmostEqual(distance(-.5, .5, .5, -.5), 2**0.5)
    print('Passed.')