コード例 #1
0
 def test_small(self):
     for points in (
         [Point(1, 0), Point(1, 1)],
         [Point(2,3), Point(10,3), Point(3,3)]
     ):
         self.assertAlmostEqual(minimum_distance_squared(points),
                                minimum_distance_squared_naive(points),
                                delta=1e-03)
コード例 #2
0
    def test_small(self):
        for points in (
            [Point(4, 4), Point(-2, -2), Point(-3, -4), Point(-1, 3), Point(2, 3), Point(-4, 0),
             Point(1, 1), Point(-1, -1), Point(3, -1), Point(-4, 2), Point(-2, 4)],

        ):
            self.assertAlmostEqual(minimum_distance_squared(points),
                                   minimum_distance_squared_naive(points),
                                   delta=1e-03)
コード例 #3
0
 def test_large(self):
     points = []
     for i in range(10):
         for j in range(i + 1, 10):
             x, y = i, j
             points.append(Point(x, y))
         self.assertAlmostEqual(minimum_distance_squared(points),
                                minimum_distance_squared_naive(points),
                                delta=1e-03)
コード例 #4
0
    def test_random(self, n=100, max_value=1000):
        points = []
        for _ in range(n):
            x = randint(-max_value, max_value)
            y = randint(-max_value, max_value)
            points.append(Point(x, y))

        self.assertAlmostEqual(minimum_distance_squared(points),
                               minimum_distance_squared_naive(points),
                               delta=1e-03)
コード例 #5
0
 def test_small(self):
     for points in ([Point(1, 0), Point(1, 1)], [
             Point(-1, -3),
             Point(0, 0),
             Point(-3, 1),
             Point(1, -3),
             Point(-1, 3)
     ]):
         actual_answer = minimum_distance_squared(points)
         expected_answer = minimum_distance_squared_naive(points)
         self.assertAlmostEqual(actual_answer, expected_answer, delta=1e-03)
コード例 #6
0
 def test_random(self):
     for n in [2, 5, 10, 100]:
         for max_value in [1, 2, 3, 1000]:
             points = []
             for _ in range(n):
                 x = randint(-max_value, max_value)
                 y = randint(-max_value, max_value)
                 points.append(Point(x, y))
             self.assertAlmostEqual(minimum_distance_squared(points),
                                    minimum_distance_squared_naive(points),
                                    delta=1e-03)
コード例 #7
0
    def test_random(self):
        test = 0
        for _ in range(10):
            for n in [3, 10, 25, 50, 500]:
                for max_value in [10**9]:
                    points = []
                    for _ in range(n):
                        x = randint(-max_value, max_value)
                        y = randint(-max_value, max_value)
                        points.append(Point(x, y))
                    test += 1

                    if minimum_distance_squared(points) != minimum_distance_squared_naive(points):
                        print(f"case {test}:right {minimum_distance_squared_naive(points)} wrong {minimum_distance_squared(points)}")
                        print(points)
                        return
                    elif test % 1000 == 0:
                        print(f"{test} have done")

                    self.assertAlmostEqual(minimum_distance_squared(points),
                                           minimum_distance_squared_naive(points),
                                           delta=1e-03)
コード例 #8
0
from test_helper import run_common_tests, failed, passed, check_tests_pass
from closest_points import minimum_distance_squared, minimum_distance_squared_naive, Point
from math import fabs
from random import randint

if __name__ == '__main__':
    run_common_tests()
    check_tests_pass("closest_points_unit_tests.py")

    all_tests_passed = True

    for points in ([Point(-10**9, -10**9),
                    Point(10**9,
                          10**9)], [Point(i, i + 1) for i in range(100)],
                   [Point(randint(1, 10), randint(1, 10)) for _ in range(5)],
                   [Point(randint(1, 10), randint(1, 10))
                    for _ in range(500)]):
        if fabs(
                minimum_distance_squared(points) -
                minimum_distance_squared_naive(points)) > 1e-03:
            all_tests_passed = False
            failed("Wrong answer for points={}".format(points))
            break

    if all_tests_passed:
        passed()