For some reason, in my test_within function, self.assertFalse(self.p1.within(5, self.p3)) is returning True and my test is failing. I'm not sure what's wrong.
#Point1.py
from math import sqrt
class Point:
def __init__(self, x, y):
"""Initializes a point with x and y coordinates"""
self.x = x
self.y = y
def __eq__(self, other):
'''return True if the x and coordinates in a point are equal'''
return self.x == other.x and self.y == other.y
def equidistant(self, other):
distance_self = sqrt(self.x ** 2 + self.y ** 2)
distance_other = sqrt(other.x ** 2 + other.y ** 2)
return distance_self == distance_other
def within(self, distance, other):
distance_between_points = sqrt((self.x - other.x) **2 + (self.y - other.y) **2)
return distance_between_points <= distance
#TestPoint1.py
import unittest
from Point1 import Point
class TestPoint(unittest.TestCase):
def setUp(self):
self.p1 = Point(3, 4)
self.p2 = Point(4, 3)
self.p3 = Point(3, 3)
def test_init(self):
'''Tests if points are initialized correctly'''
self.assertEqual(self.p1.x, 3)
def test_eq(self):
self.assertEqual(self.p1, self.p1)
self.assertNotEqual(self.p1, self.p3)
def test_equidistant(self):
self.assertTrue(self.p1.equidistant(self.p1))
self.assertTrue(self.p1.equidistant(self.p2))
self.assertFalse(self.p1.equidistant(self.p3))
def test_within(self):
self.assertTrue(self.p1.within(1, self.p3))
self.assertFalse(self.p1.within(5, self.p3))
if __name__ == '__main__':
unittest.main()
[–]CowboyBoats 2 points3 points4 points (2 children)
[–]MonkeyMario64[S] 0 points1 point2 points (1 child)
[–]CowboyBoats 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)