Skip to content

Commit

Permalink
fixes bugs in timeflip and reflect
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Becker committed Jul 29, 2021
1 parent a8004a0 commit de356da
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
14 changes: 8 additions & 6 deletions reeds_shepp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from utils import *
import math
from enum import Enum
from dataclasses import dataclass, replace


class Steering(Enum):
Expand All @@ -31,11 +32,11 @@ class Gear(Enum):
BACKWARD = -1


@dataclass
class PathElement:
def __init__(self, param, steering, gear):
self.param = param
self.steering = steering
self.gear = gear
param: float
steering: Steering
gear: Gear

def __repr__(self):
s = "{ Steering: " + self.steering.name + "\tGear: " + self.gear.name \
Expand Down Expand Up @@ -102,16 +103,17 @@ def timeflip(path):
"""
timeflip transform described around the end of the article
"""
new_path = path.copy()
new_path = [replace(e) for e in path]
for e in new_path:
e.reverse_gear()
return new_path


def reflect(path):
"""
reflect transform described around the end of the article
"""
new_path = path.copy()
new_path = [replace(e) for e in path]
for e in new_path:
e.reverse_steering()
return new_path
Expand Down
51 changes: 50 additions & 1 deletion test_reeds_shepp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from reeds_shepp import PathElement, Steering, Gear, path_length
from reeds_shepp import PathElement, Steering, Gear, path_length, timeflip, reflect


class TestPathElement(unittest.TestCase):
Expand Down Expand Up @@ -36,5 +36,54 @@ def test_with_positive_path_elements(self):
)


class TestTimeflip(unittest.TestCase):
def setUp(self) -> None:
self.path = [PathElement(1, Steering.LEFT, g) for g in (Gear.FORWARD, Gear.BACKWARD)]
self.timeflipped = timeflip(self.path)

def test_it_flips_forward_backward(self):
self.assertEqual(
self.timeflipped[0].gear,
Gear.BACKWARD
)
self.assertEqual(
self.timeflipped[1].gear,
Gear.FORWARD
)

def test_it_does_not_mutate_original_path(self):
self.assertEqual(
self.path[0].gear,
Gear.FORWARD,
)


class TestReflect(unittest.TestCase):
def setUp(self) -> None:
self.path = [PathElement(1, s, Gear.FORWARD) for s in (Steering.LEFT, Steering.STRAIGHT, Steering.RIGHT)]
self.reflected = reflect(self.path)

def test_it_reflects_steering(self):
self.assertEqual(
self.reflected[0].steering,
Steering.RIGHT
)
self.assertEqual(
self.reflected[1].steering,
Steering.STRAIGHT
)
self.assertEqual(
self.reflected[2].steering,
Steering.LEFT
)

def test_it_does_not_mutate_original_path(self):
self.assertEqual(
self.path[0].steering,
Steering.LEFT,
)



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

0 comments on commit de356da

Please sign in to comment.