In [1]:
def kepler_loc(p1, p2, dt, t):
...
return p3
In [2]:
def test_kepler_loc():
p1 = jupiter(two_days_ago)
p2 = jupiter(yesterday)
exp = jupiter(today)
obs = kepler_loc(p1, p2, 1, 1)
if exp != obs:
raise ValueError("Jupiter is not where it should be!")
In [3]:
def test_func():
exp = get_expected()
obs = func(*args, **kwargs)
assert exp == obs
In [4]:
def test_keppler_loc():
p1 = jupiter(two_days_ago)
p2 = jupiter(yesterday)
exp = jupiter(today)
obs = keppler_loc(p1, p2, 1, 1)
assert exp == obs
In [5]:
from nose.tools import assert_equal
def test_kepler_loc():
p1 = jupiter(two_days_ago)
p2 = jupiter(yesterday)
exp = jupiter(today)
obs = keppler_loc(p1, p2, 1, 1)
assert_equal(exp, obs)
In [6]:
def fib(n):
if n == 0 or n == 1:
return 1
else:
return fib(n - 1) + fib(n - 2)
In [7]:
from nose.tools import assert_equal
def test_fib0():
# test edge 0
obs = fib(0)
assert_equal(1, obs)
def test_fib1():
# test edge 1
obs = fib(1)
assert_equal(1, obs)
def test_fib6():
# test regular point
obs = fib(6)
assert_equal(13, obs)
In [8]:
# Running the test functions manually should produce no output if the tests pass
test_fib0()
test_fib1()
test_fib6()
In [9]:
import numpy as np
def sinc2d(x, y):
if x == 0.0 and y == 0.0:
return 1.0
elif x == 0.0:
return np.sin(y) / y
elif y == 0.0:
return np.sin(x) / x
else:
return (np.sin(x) / x) * (np.sin(y) / y)
In [10]:
import numpy as np
from nose.tools import assert_equal
def test_internal():
exp = (2.0 / np.pi) * (-2.0 / (3.0 * np.pi))
obs = sinc2d(np.pi / 2.0, 3.0 * np.pi / 2.0)
assert_equal(exp, obs)
def test_edge_x():
exp = (-2.0 / (3.0 * np.pi))
obs = sinc2d(0.0, 3.0 * np.pi / 2.0)
assert_equal(exp, obs)
def test_edge_y():
exp = (2.0 / np.pi)
obs = sinc2d(np.pi / 2.0, 0.0)
assert_equal(exp, obs)
def test_corner():
exp = 1.0
obs = sinc2d(0.0, 0.0)
assert_equal(exp, obs)
In [11]:
# run the tests for sinc2() manually here!
In [12]:
def a(x):
return x + 1
def b(x):
return 2 * x
def c(x):
return b(a(x))
In [13]:
from nose.tools import assert_equal
def test_c():
exp = 6
obs = c(2)
assert_equal(exp, obs)
test_c()
In [14]:
from nose.tools import assert_equal
def test_std1():
obs = std([0.0, 2.0])
exp = 1.0
assert_equal(obs, exp)
In [15]:
def std(vals):
# surely this is cheating...
return 1.0
# run the test
test_std1()
In [16]:
def test_std1():
obs = std([0.0, 2.0])
exp = 1.0
assert_equal(obs, exp)
def test_std2():
obs = std([])
exp = 0.0
assert_equal(obs, exp)
def test_std3():
obs = std([0.0, 4.0])
exp = 2.0
assert_equal(obs, exp)
# run the tests
test_std1()
test_std2()
test_std3()
In [17]:
def std(vals):
# a little better
if len(vals) == 0:
return 0.0
return vals[-1] / 2.0
In [18]:
def test_std1():
obs = std([0.0, 2.0])
exp = 1.0
assert_equal(obs, exp)
def test_std2():
obs = std([])
exp = 0.0
assert_equal(obs, exp)
def test_std3():
obs = std([0.0, 4.0])
exp = 2.0
assert_equal(obs, exp)
def test_std4():
obs = std([1.0, 3.0])
exp = 1.0
assert_equal(obs, exp)
def test_std5():
obs = std([1.0, 1.0, 1.0])
exp = 0.0
assert_equal(obs, exp)
# run the tests
test_std1()
test_std2()
test_std3()
test_std4()
test_std5()
In [19]:
def std(vals):
# finally, some math
n = len(vals)
if n == 0:
return 0.0
mu = sum(vals) / n
var = 0.0
for val in vals:
var = var + (val - mu)**2
return (var / n)**0.5
In [20]:
# run the tests
test_std1()
test_std2()
test_std3()
test_std4()
test_std5()