From: John Blischak Date: Mon, 17 Jun 2013 02:11:48 +0000 (-0500) Subject: Removed Fibonacci and close_line testing examples. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=d50b61b699c72d74d4fac595459ccefe94cd7762;p=swc-testing-nose.git Removed Fibonacci and close_line testing examples. --- diff --git a/09-testing/Readme.md b/09-testing/Readme.md index 1026ac5..4aa3da6 100644 --- a/09-testing/Readme.md +++ b/09-testing/Readme.md @@ -337,7 +337,7 @@ list of strings? **Example**: - nosetests test_mean.py + nosetests -v test_mean.py # Test Driven Development @@ -357,192 +357,4 @@ style was put forth most strongly by [Kent Beck in ## A TDD Example -Say you want to write a fib() function which generates values of the -Fibonacci sequence of given indexes. You would - of course - start by -writing the test, possibly testing a single value: - -```python -from nose.tools import assert_equal - -from pisa import fib - -def test_fib1(): - obs = fib(2) - exp = 1 - assert_equal(obs, exp) -``` - -You would *then* go ahead and write the actual function: - -```python -def fib(n): - # you snarky so-and-so - return 1 -``` - -And that is it right?! Well, not quite. This implementation fails for -most other values. Adding tests we see that: - -```python -def test_fib1(): - obs = fib(2) - exp = 1 - assert_equal(obs, exp) - - -def test_fib2(): - obs = fib(0) - exp = 0 - assert_equal(obs, exp) - - obs = fib(1) - exp = 1 - assert_equal(obs, exp) -``` - -This extra test now requires that we bother to implement at least the -initial values: - -```python -def fib(n): - # a little better - if n == 0 or n == 1: - return n - return 1 -``` - -However, this function still falls over for `2 < n`. Time for more -tests! - -```python -def test_fib1(): - obs = fib(2) - exp = 1 - assert_equal(obs, exp) - - -def test_fib2(): - obs = fib(0) - exp = 0 - assert_equal(obs, exp) - - obs = fib(1) - exp = 1 - assert_equal(obs, exp) - - -def test_fib3(): - obs = fib(3) - exp = 2 - assert_equal(obs, exp) - - obs = fib(6) - exp = 8 - assert_equal(obs, exp) -``` - -At this point, we had better go ahead and try do the right thing... - -```python -def fib(n): - # finally, some math - if n == 0 or n == 1: - return n - else: - return fib(n - 1) + fib(n - 2) -``` - -Here it becomes very tempting to take an extended coffee break or -possibly a power lunch. But then you remember those pesky negative -numbers and floats. Perhaps the right thing to do here is to just be -undefined. - -```python -def test_fib1(): - obs = fib(2) - exp = 1 - assert_equal(obs, exp) - - -def test_fib2(): - obs = fib(0) - exp = 0 - assert_equal(obs, exp) - - obs = fib(1) - exp = 1 - assert_equal(obs, exp) - - -def test_fib3(): - obs = fib(3) - exp = 2 - assert_equal(obs, exp) - - obs = fib(6) - exp = 8 - assert_equal(obs, exp) - - -def test_fib3(): - obs = fib(13.37) - exp = NotImplemented - assert_equal(obs, exp) - - obs = fib(-9000) - exp = NotImplemented - assert_equal(obs, exp) -``` - -This means that it is time to add the appropriate case to the function -itself: - -```python -def fib(n): - # sequence and you shall find - if n < 0 or int(n) != n: - return NotImplemented - elif n == 0 or n == 1: - return n - else: - return fib(n - 1) + fib(n - 2) -``` - -# Quality Assurance Exercise - -Can you think of other tests to make for the fibonacci function? I promise there -are at least two. - -Implement one new test in test_fib.py, run nosetests, and if it fails, implement -a more robust function for that case. - -And thus - finally - we have a robust function together with working -tests! - -# Exercise - -**The Problem:** In 2D or 3D, we have two points (p1 and p2) which -define a line segment. Additionally there exists experimental data which -can be anywhere in the domain. Find the data point which is closest to -the line segment. - -In the `close_line.py` file there are four different implementations -which all solve this problem. [You can read more about them -here.](http://inscight.org/2012/03/31/evolution_of_a_solution/) However, -there are no tests! Please write from scratch a `test_close_line.py` -file which tests the closest\_data\_to\_line() functions. - -*Hint:* you can use one implementation function to test another. Below -is some sample data to help you get started. - -![image](https://github.com/thehackerwithin/UofCSCBC2012/raw/scopz/5-Testing/evo_sol1.png) -> - - -```python -import numpy as np - -p1 = np.array([0.0, 0.0]) -p2 = np.array([1.0, 1.0]) -data = np.array([[0.3, 0.6], [0.25, 0.5], [1.0, 0.75]]) -``` diff --git a/09-testing/close_line.py b/09-testing/close_line.py deleted file mode 100644 index a6a5c5e..0000000 --- a/09-testing/close_line.py +++ /dev/null @@ -1,69 +0,0 @@ -import numpy as np -from scipy.optimize import fmin - -# -# Attempt 1 -# - -def point_on_line1(x, p1, p2): - y = p1[1] + (x - p1[0])*(p2[1] - p1[1]) / (p2[0] - p1[0]) - return np.array([x, y]) - - -def dist_from_line1(x, pdata, p1, p2): - pline = point_on_line1(x, p1, p2) - return np.sqrt(np.sum((pline - pdata)**2)) - - -def closest_data_to_line1(data, p1, p2): - dists = np.empty(len(data), dtype=float) - for i, pdata in enumerate(data): - x = fmin(dist_from_line1, p1[0], (pdata, p1, p2), disp=False)[0] - dists[i] = dist_from_line1(x, pdata, p1, p2) - imin = np.argmin(dists) - return imin, data[imin] - - -# -# Attempt 2 -# - -def dist_from_line2(pdata, p1, p2): - a = np.sqrt(np.sum((p1 - pdata)**2)) - b = np.sqrt(np.sum((p2 - pdata)**2)) - c = np.sqrt(np.sum((p2 - p1)**2)) - h = a * np.sqrt(1.0 - ((a**2 + c**2 - b**2) / (2.0 * a * c))**2) - return h - -def closest_data_to_line2(data, p1, p2): - dists = np.empty(len(data), dtype=float) - for i, pdata in enumerate(data): - dists[i] = dist_from_line2(pdata, p1, p2) - imin = np.argmin(dists) - return imin, data[imin] - -# -# Attempt 3 -# - -def perimeter3(pdata, p1, p2): - a = np.sqrt(np.sum((p1 - pdata)**2)) - b = np.sqrt(np.sum((p2 - pdata)**2)) - c = np.sqrt(np.sum((p2 - p1)**2)) - return (a + b + c) - -def closest_data_to_line3(data, p1, p2): - peris = np.empty(len(data), dtype=float) - for i, pdata in enumerate(data): - peris[i] = perimeter3(pdata, p1, p2) - imin = np.argmin(peris) - return imin, data[imin] - -# -# Attempt 4 -# - -def closest_data_to_line4(data, p1, p2): - return data[np.argmin(np.sqrt(np.sum((p1 - data)**2, axis=1)) + \ - np.sqrt(np.sum((p2 - data)**2, axis=1)))] - diff --git a/09-testing/evo_sol1.png b/09-testing/evo_sol1.png deleted file mode 100644 index cb131bf..0000000 Binary files a/09-testing/evo_sol1.png and /dev/null differ