Skip to content

Commit

Permalink
refactor holes test
Browse files Browse the repository at this point in the history
  • Loading branch information
erikbern committed May 7, 2018
1 parent c9d4dd1 commit 8729dcc
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions test/holes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,7 @@


class HolesTest(TestCase):
def test_root_one_child(self):
# See https://github.com/spotify/annoy/issues/223
f = 10
index = AnnoyIndex(f)
index.add_item(1000, numpy.random.normal(size=(f,)))
index.build(10)
js = index.get_nns_by_vector(numpy.random.normal(size=(f,)), 100)
self.assertEquals(js, [1000])

def test_many_holes(self):
def test_random_holes(self):
f = 10
index = AnnoyIndex(f)
valid_indices = random.sample(range(2000), 1000) # leave holes
Expand All @@ -46,22 +37,24 @@ def test_many_holes(self):
for j in js:
self.assertTrue(j in valid_indices)

def test_holes_many_children(self):
# See https://github.com/spotify/annoy/issues/295
f, base_i, n = 100, 100000, 10
def _test_holes_base(self, n, f=100, base_i=100000):
annoy = AnnoyIndex(f)
for i in range(n):
annoy.add_item(base_i + i, numpy.random.normal(size=(f,)))
annoy.build(100)
res = annoy.get_nns_by_item(base_i, n)
self.assertEquals(set(res), set([base_i + i for i in range(n)]))

def test_root_two_children(self):
def test_root_one_child(self):
# See https://github.com/spotify/annoy/issues/223
f = 10
index = AnnoyIndex(f)
index.add_item(1000, numpy.random.normal(size=(f,)))
index.add_item(1001, numpy.random.normal(size=(f,)))
index.build(10)
js = index.get_nns_by_vector(numpy.random.normal(size=(f,)), 100)
self.assertEquals(set(js), set([1000, 1001]))
self._test_holes_base(1)

def test_root_two_children(self):
self._test_holes_base(2)

def test_root_some_children(self):
# See https://github.com/spotify/annoy/issues/295
self._test_holes_base(10)

def test_root_many_children(self):
self._test_holes_base(1000)

0 comments on commit 8729dcc

Please sign in to comment.