def testinit_with_center(self): M = MetricSpace([Point([2 * i, 2 * i]) for i in range(100)]) MetricCell = Cell(MetricSpace()) C = MetricCell(Point([99, 99])) for p in M: C.addpoint(p) self.assertEqual(len(C), 101)
def testaddpoint(self): a, b, c = Point([1, 2]), Point([2, 3]), Point([3, 4]) MetricCell = Cell(MetricSpace()) C = MetricCell(a) self.assertEqual(len(C), 1) C.addpoint(b) self.assertEqual(len(C), 2) C.addpoint(c) self.assertEqual(len(C), 3) self.assertEqual(C.points, {a, b, c})
def testpop(self): a, b, c, d = Point([0, 0]), Point([100, 0]), Point([0, 50]), Point([25, 25]) MetricCell = Cell(MetricSpace()) C = MetricCell(a) C.addpoint(b) C.addpoint(c) C.addpoint(d) self.assertEqual(C.pop(), b) self.assertEqual(C.pop(), c) self.assertEqual(C.pop(), d) self.assertEqual(C.pop(), None)
def testrebalance(self): a, b = Point([-1]), Point([200]) G = NeighborGraph(MetricSpace([a, b])) MetricCell = Cell(MetricSpace()) A = MetricCell(a) B = MetricCell(b) for i in range(200): B.addpoint(Point([i])) self.assertEqual(len(A), 1) self.assertEqual(len(B), 201) G.rebalance(A, B) self.assertEqual(len(B), 101) self.assertEqual(len(A), 101)
def testaddpoint_duplicatepoint(self): a, b = Point([1, 2]), Point([2, 3]) MetricCell = Cell(MetricSpace()) C = MetricCell(a) self.assertEqual(len(C), 1) C.addpoint(b) self.assertEqual(len(C), 2) # Add b a second time. C.addpoint(b) self.assertEqual(len(C), 2) self.assertEqual(C.points, {a, b}) # Add the center again. C.addpoint(a) self.assertEqual(len(C), 2) self.assertEqual(C.points, {a, b})
def testdist(self): MetricCell = Cell(MetricSpace()) A = MetricCell(Point([2, 3])) self.assertEqual(A.dist(Point([7, 3])), 5) self.assertEqual(A.dist(A.center), 0) self.assertEqual(A.dist(Point([7, 15])), 13)
def testupdateradius_empty_cell(self): MetricCell = Cell(MetricSpace()) C = MetricCell(Point([1, 2, 3])) C.updateradius() self.assertEqual(C.radius, 0)