#!/usr/bin/env python3 # Test Lagrange Polynomial Interpolation Implementation #------------------------------------------------------ from ex2 import my_lagrange, my_lagrange_polynomials import numpy as np def test_import(): """ Test the import """ assert my_lagrange is not None assert my_lagrange_polynomials is not None def test_my_lagrange_polynomials(): """ Test the my_lagrange_polynomials function """ # Take Numpy Array assert (my_lagrange_polynomials(np.array([0,1]), 1) == np.array([0, 1])).all() # Take Python List assert (my_lagrange_polynomials([0,1], 1) == np.array([0, 1])).all() # Changing the order of x_values changes the order of the returned values assert (my_lagrange_polynomials([2,4,6,8], 5) == np.array([-0.0625, 0.5625, 0.5625, -0.0625])).all() assert (my_lagrange_polynomials([6,8,2,4], 5) == np.array([0.5625, -0.0625, -0.0625, 0.5625])).all() def test_my_lagrange(): """ Test the my_lagrange function """ # Take x_i as a single number assert (my_lagrange([1, 2], [1, 2], 1) == np.array([1])).all() assert (my_lagrange([1, 2], [1, 2], [1, 1.5, 2]) == np.array([1, 1.5, 2])).all() if __name__ == "__main__": import pytest print("pyTesting ", __file__) pytest.main([__file__])