Skip to content

Commit

Permalink
refactoring and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Becker committed Jul 29, 2021
1 parent 43a6b5f commit a8004a0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
34 changes: 12 additions & 22 deletions reeds_shepp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,41 @@


class Steering(Enum):
LEFT = 1
RIGHT = 2
STRAIGHT = 3
LEFT = -1
RIGHT = 1
STRAIGHT = 0


class Gear(Enum):
FORWARD = 1
BACKWARD = 2
BACKWARD = -1


class PathElement():
class PathElement:
def __init__(self, param, steering, gear):
self.param = param
self.steering = steering
self.gear = gear

def __repr__(self):
if self.steering == Steering.LEFT: steering_str = "left"
elif self.steering == Steering.RIGHT: steering_str = "right"
else: steering_str = "straight"

if self.gear == Gear.FORWARD: gear_str = "forward"
else: gear_str = "backward"

s = "{ Steering: " + steering_str + "\tGear: " + gear_str \
s = "{ Steering: " + self.steering.name + "\tGear: " + self.gear.name \
+ "\tdistance: " + str(round(self.param, 2)) + " }"

return s

def reverse_steering(self):
if self.steering == Steering.LEFT:
self.steering = Steering.RIGHT
elif self.steering == Steering.RIGHT:
self.steering = Steering.LEFT
self.steering = Steering(-self.steering.value)

def reverse_gear(self):
if self.gear == Gear.FORWARD:
self.gear = Gear.BACKWARD
else:
self.gear = Gear.FORWARD
self.gear = Gear(-self.gear.value)


def path_length(path):
"""
this one's obvious
"""
return sum([e.param for e in path])


def get_optimal_path(start, end):
"""
Return the shortest path from start to end among those that exist
Expand Down
40 changes: 40 additions & 0 deletions test_reeds_shepp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import unittest
from reeds_shepp import PathElement, Steering, Gear, path_length


class TestPathElement(unittest.TestCase):
def setUp(self) -> None:
self.element = PathElement(13, Steering.LEFT, Gear.FORWARD)

def test_repr(self):
self.assertEqual(
repr(self.element),
"{ Steering: LEFT Gear: FORWARD distance: 13 }"
)

def test_reverse_gear(self):
self.element.reverse_gear()
self.assertEqual(
self.element.gear,
Gear.BACKWARD
)

def test_reverse_steering(self):
self.element.reverse_steering()
self.assertEqual(
self.element.steering,
Steering.RIGHT
)


class TestPathLength(unittest.TestCase):
def test_with_positive_path_elements(self):
path = [PathElement(1, Steering.LEFT, Gear.FORWARD) for _ in range(2)]
self.assertEqual(
path_length(path),
2
)


if __name__ == '__main__':
unittest.main()

0 comments on commit a8004a0

Please sign in to comment.