From 7abb60e0fae806a2dc02f40cea1b60ed9bfeffe2 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 3 Sep 2019 15:04:17 +0200 Subject: [PATCH 1/2] added tests for numpy var --- .../numerical_fuzzing/test_var.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 hands_on/numerical_fuzzing/numerical_fuzzing/test_var.py diff --git a/hands_on/numerical_fuzzing/numerical_fuzzing/test_var.py b/hands_on/numerical_fuzzing/numerical_fuzzing/test_var.py new file mode 100644 index 0000000..4c6bf25 --- /dev/null +++ b/hands_on/numerical_fuzzing/numerical_fuzzing/test_var.py @@ -0,0 +1,21 @@ +import numpy as np +from math import isclose + +def test_var_deterministic(): + x = np.array([0.0, 1.0, 2.0, 3.0]) + print(np.var(x)) + expected = 1.25 + assert isclose(np.var(x), expected) + + +def test_var_fuzzing(): + rand_state = np.random.RandomState(1333) + + N, D = 100000, 5 + # Goal means: [0.1 , 0.45, 0.8 , 1.15, 1.5] + testdata = np.linspace(0.1, 1.5, D) + expected = np.ones(D) + # Generate random, D-dimensional data with the desired mean + x = rand_state.randn(N, D) + testdata + vars = np.var(x, axis=0) + np.testing.assert_allclose(vars, expected, rtol=1e-2) From e7e8b33c5d5b0b538df36caed4558108e9bef6d1 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 3 Sep 2019 15:10:16 +0200 Subject: [PATCH 2/2] deleted superfluous code --- hands_on/numerical_fuzzing/numerical_fuzzing/test_var.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hands_on/numerical_fuzzing/numerical_fuzzing/test_var.py b/hands_on/numerical_fuzzing/numerical_fuzzing/test_var.py index 4c6bf25..5354b8f 100644 --- a/hands_on/numerical_fuzzing/numerical_fuzzing/test_var.py +++ b/hands_on/numerical_fuzzing/numerical_fuzzing/test_var.py @@ -3,7 +3,7 @@ def test_var_deterministic(): x = np.array([0.0, 1.0, 2.0, 3.0]) - print(np.var(x)) + expected = 1.25 assert isclose(np.var(x), expected) @@ -13,9 +13,9 @@ def test_var_fuzzing(): N, D = 100000, 5 # Goal means: [0.1 , 0.45, 0.8 , 1.15, 1.5] - testdata = np.linspace(0.1, 1.5, D) + expected = np.ones(D) # Generate random, D-dimensional data with the desired mean - x = rand_state.randn(N, D) + testdata + x = rand_state.randn(N, D) vars = np.var(x, axis=0) np.testing.assert_allclose(vars, expected, rtol=1e-2)