Skip to content

Commit

Permalink
Merge pull request #1092 from Jason27chan/more-soccer-tests
Browse files Browse the repository at this point in the history
More soccer tests
  • Loading branch information
Jason27chan committed Jan 24, 2018
2 parents 1c6c8d0 + fc72fef commit 9bf21fb
Show file tree
Hide file tree
Showing 5 changed files with 418 additions and 366 deletions.
152 changes: 71 additions & 81 deletions soccer/gameplay/tests/test_ball.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,88 +6,78 @@


class Moc_Ball:
def __init__(self, x, y):
self.pos = robocup.Point(x, y)
self.vel = robocup.Point(0, 0)
def __init__(self, x, y):
self.pos = robocup.Point(x, y)
self.vel = robocup.Point(0, 0)

def set_pos(self, x, y):
self.pos = robocup.Point(x, y)

def set_vel(self, x, y):
self.vel = robocup.Point(x, y)
def set_pos(self, x, y):
self.pos = robocup.Point(x, y)

def set_vel(self, x, y):
self.vel = robocup.Point(x, y)

class TestBall(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(TestBall, self).__init__(*args, **kwargs)
self.width = constants.Field.Width
self.length = constants.Field.Length

def setUp(self):
main.set_ball(Moc_Ball(0, constants.Field.Length / 2))

def test_is_moving_towards_our_goal(self):
# Easier than writing out whole function
def test_function():
return evaluation.ball.is_moving_towards_our_goal()

# Test velocity = 0
self.assertFalse(test_function(),
"function returns true when ball isn't moving")

# Test ball going straight to goal from center
main.ball().set_vel(0, -1)
self.assertTrue(
test_function(),
"function returns false when ball is going towards goal")

# Test ball going opposite to goal from center
main.ball().set_vel(0, 1)
self.assertFalse(
test_function(),
"function returns true when ball is going away from goal")

# Test ball going towards goal from right side of field
main.ball().set_pos(self.width / 4, self.length / 4)
main.ball().set_vel(-1, -1)
self.assertTrue(
test_function(),
"function returns false when ball is going towards goal")

# Test ball going towards the goal from left side of field
main.ball().set_pos(-self.width / 4, self.length / 4)
main.ball().set_vel(1, -1)
self.assertTrue(
test_function(),
"function returns false when ball is going towards goal")

def test_is_in_our_goalie_zone(self):
# easier than writing the function
def test_function():
return evaluation.ball.is_in_our_goalie_zone()

# Check center of field
self.assertFalse(test_function(
), "the center of the field is indicated to be inside the goalie zone")

# Check inside of goalie zone
main.ball().set_pos(0, 0)
self.assertTrue(test_function(),
"0, 0 is not indicated to be inside the goalie zone")

# Check another place inside goalie zone
main.ball().set_pos(0, self.length / 8)
self.assertTrue(test_function(
), "center of goalie zone is not indicated to be inside the goalie zone"
)

# Check just outside of goalie zone
main.ball().set_pos(0, self.length / 4)
self.assertFalse(test_function(
), "just outside of goalie zone is indicated to be inside the goalie zone"
)

# Check corner of field
main.ball().set_pos(self.width / 2, 0)
self.assertFalse(test_function(
), "the corner of the field is indicated to be inside the goalie zone")


def __init__(self, *args, **kwargs):
super(TestBall, self).__init__(*args, **kwargs)
self.width = constants.Field.Width
self.length = constants.Field.Length


def setUp(self):
main.set_ball(Moc_Ball(0, constants.Field.Length / 2))

def test_is_moving_towards_our_goal(self):
msg = "evaluation.ball.is_moving_towards_our_goal() isn't functioning correctly: "

# Easier than writing out whole function
def test_function():
return evaluation.ball.is_moving_towards_our_goal()

# Test velocity = 0
self.assertFalse(test_function(), msg + "function returns true when ball isn't moving")

# Test ball going straight to goal from center
main.ball().set_vel(0, -1)
self.assertTrue(test_function(), msg + "function returns false when ball is going towards goal")

# Test ball going opposite to goal from center
main.ball().set_vel(0, 1)
self.assertFalse(test_function(), msg + "function returns true when ball is going away from goal")

# Test ball going towards goal from right side of field
main.ball().set_pos(self.width / 4, self.length / 4)
main.ball().set_vel(-1, -1)
self.assertTrue(test_function(), msg + "function returns false when ball is going towards goal")

# Test ball going towards the goal from left side of field
main.ball().set_pos(-self.width / 4, self.length / 4)
main.ball().set_vel(1, -1)
self.assertTrue(test_function(), msg + "function returns false when ball is going towards goal")

def test_is_in_our_goalie_zone(self):
msg = "evaluation.ball.is_in_our_goalie_zone is not functioning as intended"

# easier than writing the function
def test_function():
return evaluation.ball.is_in_our_goalie_zone()

# Check center of field
self.assertFalse(test_function(), msg + "the center of the field is indicated to be inside the goalie zone")

# Check inside of goalie zone
main.ball().set_pos(0, 0)
self.assertTrue(test_function(), msg + "0, 0 is not indicated to be inside the goalie zone")

# Check another place inside goalie zone
main.ball().set_pos(0, self.length / 8)
self.assertTrue(test_function(), msg + "center of goalie zone is not indicated to be inside the goalie zone")

# Check just outside of goalie zone
main.ball().set_pos(0, self.length / 4)
self.assertFalse(test_function(), msg + "just outside of goalie zone is indicated to be inside the goalie zone")

# Check corner of field
main.ball().set_pos(self.width / 2, 0)
self.assertFalse(test_function(), msg + "the corner of the field is indicated to be inside the goalie zone")
150 changes: 66 additions & 84 deletions soccer/gameplay/tests/test_circle_near_ball.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,89 +7,71 @@


class Moc_Ball:
def __init__(self, x, y):
self.pos = robocup.Point(x, y)

def __init__(self, x, y):
self.pos = robocup.Point(x, y)

class TestCircleNearBall(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(TestCircleNearBall, self).__init__(*args, **kwargs)

def setUp(self):
self.circle_near_ball = tactics.stopped.circle_near_ball.CircleNearBall(
)

def test_get_circle_points(self):
width = constants.Field.Width
length = constants.Field.Length

# Checks that the points around the ball are within the bounds of the field
def assert_circle_points(self):
array = self.circle_near_ball.get_circle_points(6)
for point in array:
self.assertLessEqual(
point.x, width * 0.5,
"X position of points are outside the field.")
self.assertGreaterEqual(
point.x, width * -0.5,
"X position of points are outside the field.")

self.assertLessEqual(
point.y, length,
"Y positions of points are outside the field.")
self.assertGreaterEqual(
point.y, 0, "Y positions of points are outside the field.")

# Checks a point on the field to see if the circle around it is valid
def test_point(self, x, y):
main.set_ball(Moc_Ball(x, y))
assert_circle_points(self)

# Checks center of field
test_point(self, constants.Field.CenterPoint.x,
constants.Field.CenterPoint.y)

# Checks a point that should be clearly outside the field
test_point(self, width * -1, length * -1)

# Checks bottom left corner of field
test_point(self, width * -1 / 2, 0)

# Checks (0, 0). Should be right in front of our goal

# Checks bottom right corner of field
test_point(self, width / 2, 0)

# Checks middle right part of field
test_point(self, width / 2, length / 2)

# Checks a point just inside the field
test_point(self, width / 2 - constants.Robot.Radius,
constants.Robot.Radius)

# Checks a point just outside of the field
test_point(self, width / 2 + constants.Robot.Radius,
constants.Robot.Radius * -1)

def test_normalize_angle(self):
angle = -math.pi
while (angle < 0):
self.assertAlmostEqual(
self.circle_near_ball.normalize_angle(angle),
angle + 2 * math.pi,
msg="Unexpected value for normalize angle.")
angle += math.pi / 6
angle = 0
while (angle <= 2 * math.pi):
self.assertAlmostEqual(
angle,
self.circle_near_ball.normalize_angle(angle),
msg="Unexpected value for normalize angle.")
angle += math.pi / 6
angle = 3 * math.pi
while (angle < 4 * math.pi):
self.assertAlmostEqual(
angle - 2 * math.pi,
self.circle_near_ball.normalize_angle(angle),
msg="Unexpected value for normalize angle.")
angle += math.pi / 6
def __init__(self, *args, **kwargs):
super(TestCircleNearBall, self).__init__(*args, **kwargs)

def setUp(self):
self.circle_near_ball = tactics.stopped.circle_near_ball.CircleNearBall()

def test_get_circle_points(self):
width = constants.Field.Width
length = constants.Field.Length

# Checks that the points around the ball are within the bounds of the field
def assert_circle_points(self):
array = self.circle_near_ball.get_circle_points(6)
for point in array:
self.assertTrue(point.x >= width * -1 / 2 and point.x <= width / 2, msg="failure of get_circle_points. X position of points are outside the field. Point's x position is %d" %point.x)
self.assertTrue(point.y >= 0 and point.y <= length, msg="failure of get_circle_points. Y positions of points are outside the field. Point's y position is %d" %point.y )

# Checks a point on the field to see if the circle around it is valid
def test_point(self, x, y):
main.set_ball(Moc_Ball(x, y))
assert_circle_points(self)

# Checks center of field
test_point(self, constants.Field.CenterPoint.x, constants.Field.CenterPoint.y)

# Checks a point that should be clearly outside the field
test_point(self, width * -1, length * -1)

# Checks bottom left corner of field
test_point(self, width * -1 / 2, 0)

# Checks (0, 0). Should be right in front of our goal


# Checks bottom right corner of field
test_point(self, width / 2, 0)

# Checks middle right part of field
test_point(self, width / 2, length / 2)

# Checks a point just inside the field
test_point(self, width / 2 - constants.Robot.Radius, constants.Robot.Radius)

# Checks a point just outside of the field
test_point(self, width / 2 + constants.Robot.Radius, constants.Robot.Radius * -1)

def test_normalize_angle(self):
angle = -math.pi
while (angle < 0):
self.assertEqual(self.circle_near_ball.normalize_angle(angle),
angle + 2 * math.pi, msg="Unexpected value for normalize angle.")
angle += math.pi / 6
angle = 0
while (angle <= 2 * math.pi):
self.assertEqual(angle,
self.circle_near_ball.normalize_angle(angle),
msg="Unexpected value for normalize angle.")
angle += math.pi / 6
angle = 3 * math.pi
while (angle < 4 * math.pi):
self.assertEqual(angle - 2 * math.pi,
self.circle_near_ball.normalize_angle(angle),
msg="Unexpected value for normalize angle.")
angle += math.pi / 6

0 comments on commit 9bf21fb

Please sign in to comment.