Skip to content

Commit c30e410

Browse files
committed
improve tests for correct preallocation
1 parent bfc517d commit c30e410

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

pandas/tests/libs/test_hashtable.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,18 @@ def test_get_state(self, table_type, dtype):
164164
assert "upper_bound" in state
165165

166166
def test_no_reallocation(self, table_type, dtype):
167-
N = 110
168-
keys = np.arange(N).astype(dtype)
169-
table = table_type(N)
170-
n_buckets_start = table.get_state()["n_buckets"]
171-
table.map_locations(keys)
172-
n_buckets_end = table.get_state()["n_buckets"]
173-
# orgininal number of buckets was enough:
174-
assert n_buckets_start == n_buckets_end
167+
for N in range(1, 110):
168+
keys = np.arange(N).astype(dtype)
169+
preallocated_table = table_type(N)
170+
n_buckets_start = preallocated_table.get_state()["n_buckets"]
171+
preallocated_table.map_locations(keys)
172+
n_buckets_end = preallocated_table.get_state()["n_buckets"]
173+
# orgininal number of buckets was enough:
174+
assert n_buckets_start == n_buckets_end
175+
# check with clean table (not too much preallocated)
176+
clean_table = table_type()
177+
clean_table.map_locations(keys)
178+
assert n_buckets_start == clean_table.get_state()["n_buckets"]
175179

176180

177181
def test_get_labels_groupby_for_Int64(writable):
@@ -209,14 +213,18 @@ def test_tracemalloc_for_empty_StringHashTable():
209213

210214

211215
def test_no_reallocation_StringHashTable():
212-
N = 110
213-
keys = np.arange(N).astype(np.compat.unicode).astype(np.object_)
214-
table = ht.StringHashTable(N)
215-
n_buckets_start = table.get_state()["n_buckets"]
216-
table.map_locations(keys)
217-
n_buckets_end = table.get_state()["n_buckets"]
218-
# orgininal number of buckets was enough:
219-
assert n_buckets_start == n_buckets_end
216+
for N in range(1, 110):
217+
keys = np.arange(N).astype(np.compat.unicode).astype(np.object_)
218+
preallocated_table = ht.StringHashTable(N)
219+
n_buckets_start = preallocated_table.get_state()["n_buckets"]
220+
preallocated_table.map_locations(keys)
221+
n_buckets_end = preallocated_table.get_state()["n_buckets"]
222+
# orgininal number of buckets was enough:
223+
assert n_buckets_start == n_buckets_end
224+
# check with clean table (not too much preallocated)
225+
clean_table = ht.StringHashTable()
226+
clean_table.map_locations(keys)
227+
assert n_buckets_start == clean_table.get_state()["n_buckets"]
220228

221229

222230
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)