def test_energy(): """ Optional description for nose reporting """ density = [2, 1] assert_equal(energy(density), 1.) assert_equal(energy(density, coeff=2), 2.) assert_raises(energy([])) assert_equal(energy([3, 4, 5]), 19.) assert_equal(type(energy([1])), type(1.))
def test_zero_energy_cases(): density = [0,0,0] coeff = 1.0 assert_almost_equal(energy(density, coeff), 0) density = [1,1,1] coeff = 0.0 assert_almost_equal(energy(density, coeff), 0)
def test_coefficient_is_linear(): from numpy import array density = array([1, 0, 1, 10, 15, 0]) value = energy(density, coefficient=1) twice = energy(density, coefficient=2e0) assert_almost_equal(value + value, twice)
def test_zero_energy_cases(): # Zero energy at zero density densities = [[], [0], [0, 0, 0]] for density in densities: assert_almost_equal(energy(density), 0) # Zero energy for coefficient == 0 assert_almost_equal(energy([1, 1, 1], coefficient=0), 0)
def test_monte_carlo_one_particle(): density = [0, 0, 1, 0] T = 2.0 iterations = 20 densities = monte_carlo(density, T, iterations, energy) constant_energy = energy(density) print constant_energy for i in range(len(density)): print "energies:", [energy(densities[i]), constant_energy] assert_equal(energy(densities[i]), constant_energy)
def test_derivative_no_self_energy(): """ If particle is alone, then its participation to energy is zero """ from numpy import array density = array([1, 0, 1, 10, 15, 0]) density_plus_one = density.copy() density[1] += 1 expected = 0 actual = energy(density_plus_one) - energy(density) assert_almost_equal(expected, actual)
def test_derivative(): from numpy.random import randint # Loop over vectors of different sizes (but not empty) for vector_size in randint(1, 1000, size=30): # Create random density of size N density = randint(50, size=vector_size) # will do derivative at this index element_index = randint(vector_size) # modified densities density_plus_one = density.copy() density_plus_one[element_index] += 1 # Compute and check result expected = density[element_index] if density[element_index] > 0 else 0 actual = energy(density_plus_one) - energy(density) assert_almost_equal(expected, actual)
def test_energy(): # Test if the output is as expected assert_equal(energy([1, 2, 3, 4]), 10)
def test_contains_rubbish_input(): # Test if the input is rubbish (e.g string) with assert_raises(TypeError): energy([1, 2, 'abc', 4])
def test_contains_negative_number(): # Test if the input contains a negative number with assert_raises(ValueError): energy([1, 2, -3, 4])
def test_contains_float(): # Test if the input contains a float number with assert_raises(TypeError): energy([1, 2.0, 3, 4])