Beispiel #1
0
def test_martian_plateau():
    """Tests MartianPlateau constructor with good inputs
    """
    top_corners = ["1 1", "2 5", "9223372036854775807 9223372036854775807"]
    xy_list = [[1, 1], [2, 5], [9223372036854775807, 9223372036854775807]]
    # Call function:
    for corner, xy in zip(top_corners, xy_list):
        try:
            mp = MartianPlateau(corner)
            assert mp.get_x() == xy[0]
            assert mp.get_y() == xy[1]
        except ImproperPlateau as i:
            print i.message
            assert False
def test_martian_plateau():
    """Tests MartianPlateau constructor with good inputs
    """
    top_corners=["1 1", "2 5", "9223372036854775807 9223372036854775807"]
    xy_list = [[1, 1], [2, 5], [9223372036854775807, 9223372036854775807]]
    # Call function:
    for corner, xy in zip(top_corners, xy_list):
        try:
            mp = MartianPlateau(corner)
            assert mp.get_x() == xy[0]
            assert mp.get_y() == xy[1]
        except ImproperPlateau as i:
            print i.message
            assert False
def main(argv=None):
    # parse command line options
    if argv is None:
        argv = sys.argv

    count = 0
    if len(argv) == 1:
        count = 1
    elif len(argv) > 1:
        if argv[1] == "-h" or argv[1] == "--help":
            print(textwrap.dedent(__doc__))
        elif argv[1].isdigit():
            count = int(argv[1])
        else:
            print "Invalid syntax: " + "".join((" " + s) for s in argv)
            print "Use -h or --help for Help"

    if count > 0:
        final_positions = ""
        try:
            plateau = MartianPlateau(raw_input("Plateau:"))
            for c in range(1, count + 1):
                rover = Rover(raw_input("Rover" + str(c) + " Landing:"),
                              plateau)
                output = rover.instruct(
                    raw_input("Rover" + str(c) + " Instructions:"))

                # Put together the final output for the program
                final_positions += "\nRover" + str(c) + ":" + output

            print final_positions

        except Exception as e:
            print e.message
            raise e
Beispiel #4
0
def test_is_outside():
    """Tests calling inputting invalid coordinates to get_location()
    """

    mp = MartianPlateau("0 0")
    assert mp.is_outside([0, 0]) is False
    assert mp.is_outside([1, 0]) is True
    assert mp.is_outside([0, 1]) is True
    assert mp.is_outside([2, 2]) is True

    mp = MartianPlateau("5 5")
    assert mp.is_outside([0, 0]) is False
    assert mp.is_outside([1, 0]) is False
    assert mp.is_outside([0, 1]) is False
    assert mp.is_outside([5, 5]) is False
    assert mp.is_outside([5, 6]) is True
    assert mp.is_outside([6, 5]) is True
def test_rover_move_single():
    plateau = MartianPlateau("10 10")
    start_locs = ["5 5 N", "5 5 E", "5 5 S", "5 5 W"]
    new_locs = [[5, 6, "N"], [6, 5, "E"], [5, 4, "S"], [4, 5, "W"]]

    for s, n in zip(start_locs, new_locs):
        rvr = Rover(s, plateau)
        output = rvr.instruct("M")
        print output
        assert rvr.get_x() == n[0]
        assert rvr.get_y() == n[1]
        assert rvr.get_facing() == n[2]
Beispiel #6
0
def test_get_top_right_corner_error():
    """Tests MartianPlateau constructor with bad inputs
    """
    top_corners = ["a", "a b", "a, b", "1", "1,2", "1, 2", "-1 -3"]
    # Call function:
    for corner in top_corners:
        try:
            mp = MartianPlateau(corner)
        except ImproperPlateau:
            assert True
        else:
            assert False
def test_rover_right_turn():
    plateau = MartianPlateau("5 5")
    rvr = Rover("1 1 N", plateau)

    assert rvr.get_facing() == "N"
    rvr.instruct("R")
    assert rvr.get_facing() == "E"
    rvr.instruct("R")
    assert rvr.get_facing() == "S"
    rvr.instruct("R")
    assert rvr.get_facing() == "W"
    rvr.instruct("R")
    assert rvr.get_facing() == "N"
def test_rover_move_example():
    plateau = MartianPlateau("5 5")
    start_locs = ["1 2 N", "3 3 E", ]
    instructions = ["LMLMLMLMM","MMRMMRMRRM"]
    new_locs = [[1, 3, "N"], [5, 1, "E"]]

    for s, i, n in zip(start_locs,instructions, new_locs):
        rvr = Rover(s, plateau)
        output =rvr.instruct(i)

        assert rvr.get_x() == n[0]
        assert rvr.get_y() == n[1]
        assert rvr.get_facing() == n[2]
def test_rover_move_sequence():
    plateau = MartianPlateau("6 6")
    start_locs = ["0 0 N", "3 3 W", ]
    instructions = ["MRMLMRMLMRMLMRMLMRMLMRML","MMMLMMM"]
    new_locs = [[6, 6, "N"], [0, 0, "S"]]

    for s, i, n in zip(start_locs,instructions, new_locs):
        rvr = Rover(s, plateau)
        rvr.instruct(i)

        assert rvr.get_x() == n[0]
        assert rvr.get_y() == n[1]
        assert rvr.get_facing() == n[2]
def test_rover_move_edge():
    plateau = MartianPlateau("4 4")
    start_locs = ["2 2 N", "2 2 E", "2 2 S", "2 2 W"]
    instructions = "MMMM"
    new_locs = [[2, 4, "N"], [4, 2, "E"], [2, 0, "S"], [0, 2, "W"]]

    for s, n in zip(start_locs, new_locs):
        rvr = Rover(s, plateau)
        rvr.instruct(instructions)

        assert rvr.get_x() == n[0]
        assert rvr.get_y() == n[1]
        assert rvr.get_facing() == n[2]
def test_rover_constructor_error():
    """Tests calling inputting invalid coordinates to get_location()
    """
    landing_strings = ["a", "a a", "a a a",
                       "1", "1 1", "1 1 1",
                       "2 2 n", "2 2 N "]
    plateau = MartianPlateau("5 5")
    for landing in landing_strings:
        try:
            rvr = Rover(landing, plateau)
        except ImproperLanding:
            assert True
        else:
            print landing
            assert False
def test_get_instruction_error():
    """Tests calling inputting invalid coordinates to get_location()
    """
    instr_strings =["l", "m", "r", "La", "L M", "M R", " LM ", "aM", "q    R"]
    plateau = MartianPlateau("1000 1000")
    rvr = Rover("100 100 N", plateau)

    for instr in instr_strings:
        try:
            rvr = Rover("100 100 N", plateau)
            rvr.instruct(instr)
        except ImproperInstruction:
            assert True
        else:
            print instr
            assert False
def test_rover_constructor_correct():
    """Tests calling inputting invalid coordinates to get_location()
    """
    landing_strings = ["02 2 N", "0 0 S", "33 44 E", "777 888 W"]
    landing_list = [[2, 2, "N"], [0, 0, "S"], [33, 44, "E"], [777, 888, "W"]]
    plateau = MartianPlateau("1000 1000")

    for landing, landing_l in zip(landing_strings, landing_list):
        try:
            rvr = Rover(landing, plateau)
            assert rvr.get_x() == landing_l[0]
            assert rvr.get_y() == landing_l[1]
            assert rvr.get_facing() == landing_l[2]
        except ImproperLanding:
            print landing
            assert False
def test_is_outside():
    """Tests calling inputting invalid coordinates to get_location()
    """

    mp = MartianPlateau("0 0")
    assert mp.is_outside([0, 0]) is False
    assert mp.is_outside([1, 0]) is True
    assert mp.is_outside([0, 1]) is True
    assert mp.is_outside([2, 2]) is True

    mp = MartianPlateau("5 5")
    assert mp.is_outside([0, 0]) is False
    assert mp.is_outside([1, 0]) is False
    assert mp.is_outside([0, 1]) is False
    assert mp.is_outside([5, 5]) is False
    assert mp.is_outside([5, 6]) is True
    assert mp.is_outside([6, 5]) is True