From 862775ef5adc0090008104eebcc450a77cab8777 Mon Sep 17 00:00:00 2001 From: Gerard Gorman Date: Sat, 14 Nov 2020 10:49:23 +0000 Subject: [PATCH] Updated test to use an array of random integers with a fixed seed rather than random floats with random seed to that the test: * is deterministic between runs * duplicate keys are in fact created so unique actually does something. Additionally, the tic/toc timings are now over multiple calls to the function so that the variance is reduced. --- tests/test_tools.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/test_tools.py b/tests/test_tools.py index 2708dcf16e..8d9c9520cd 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -23,12 +23,19 @@ def test_toposort(elements, expected): def test_sorting(): key = lambda x: x - array = np.random.rand(10000) + + # Need predictable random sequence or test will + # have inconsistent behaviour results between tests. + np.random.seed(0) + array = np.random.randint(-1000, 1000, 10000) + t0 = time.time() - sort_key = filter_ordered(array, key=key) + for _ in range(100): + sort_key = filter_ordered(array, key=key) t1 = time.time() - sort_nokey = filter_ordered(array) + for _ in range(100): + sort_nokey = filter_ordered(array) t2 = time.time() - # This one is slightly faster - assert t2 - t1 < .5 * (t1 - t0) + + assert t2 - t1 < 0.5 * (t1 - t0) assert sort_key == sort_nokey