From: Michael Orlitzky Date: Wed, 6 Feb 2013 17:00:06 +0000 (-0500) Subject: Move all of the tests into a subdirectory. X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=ef3085ef3c40eab925219bad1c6dbb2c6e106727;p=octave.git Move all of the tests into a subdirectory. Move the unit_test code into a subdirectory. --- diff --git a/run-tests.m b/run-tests.m index 2ad4c24..a5373fc 100755 --- a/run-tests.m +++ b/run-tests.m @@ -1,117 +1,10 @@ #!/usr/bin/octave --silent -unit_init(1, {}); - -unit_test_equals("sin[0] == 0", ... - 0, ... - divided_difference(@sin, 0)); - -unit_test_equals("sin[0, pi] == 0", ... - 0, ... - divided_difference(@sin, [0,pi])); - -unit_test_equals("sin[0, pi, 2*pi] == 0", ... - 0, ... - divided_difference(@sin, [0,pi,2*pi])); - -unit_test_equals("zero order divided_difference_coefficients", ... - [1], ... - divided_difference_coefficients([0])); - -unit_test_equals("first order divided_difference_coefficients", ... - [-1, 1] / pi, ... - divided_difference_coefficients([0, pi])); - -unit_test_equals("second order divided_difference_coefficients", ... - [1, -2, 1] / (2*pi^2), ... - divided_difference_coefficients([0, pi, 2*pi])); - - -unit_test_equals("1 is odd", ... - true, ... - odd(1)); - -unit_test_equals("1 is not even", ... - false, ... - even(1)); - -unit_test_equals("2 is not odd", ... - false, ... - odd(2)); - -unit_test_equals("2 is even", ... - true, ... - even(2)); - -expected_A = [-1, 0, 0, 0, 0; ... - -16, 32, -16, 0, 0; ... - 0, -16, 32, -16, 0; ... - 0, 0, -16, 32, -16; ... - 0, 0, 0, 0, -1]; -unit_test_equals("Homework #1 problem #1 Poisson matrix is correct", ... - true, ... - expected_A == poisson_matrix(4, 0, 1)); - - -g = @(x) 1 + atan(x); -expected_fp = 2.1323; -tol = 1 / 10^10; -x0 = 2.4; -unit_test_equals("Homework #2 problem #5 fixed point is correct", ... - expected_fp, ... - fixed_point_method(g, tol, x0)); - - -h = 0.5; -g1 = @(u) 1 + h*exp(-u(1)^2)/(1+u(2)^2); -g2 = @(u) 0.5 + h*atan(u(1)^2 + u(2)^2); -my_g = @(u) [g1(u), g2(u)]; -tol = 1 / 10^9; -u0 = [1,1]; -expected_fp = [1.0729, 1.0821]; -unit_test_equals("Homework #3 problem #3i fixed point is correct", ... - expected_fp, ... - fixed_point_method(my_g, tol, u0)); - - -f = @(x) x^6 - x - 1; -f_prime = @(x) 6*x^5 - 1; -tol = 1/1000000; -x0 = 2; -expected_root = 1.1347; -unit_test_equals("Newton's method agrees with Haskell", ... - expected_root, ... - newtons_method(f, f_prime, tol, x0)); - - - -f1 = @(u) u(1)^2 + u(1)*u(2)^3 - 9; -f2 = @(u) 3*u(1)^2*u(2) - u(2)^3 - 4; -f = @(u) [f1(u); f2(u)]; -## The partials for the Jacobian. -f1x = @(u) 2*u(1) + u(2)^3; -f1y = @(u) 3*u(1)*u(2)^2; -f2x = @(u) 6*u(1)*u(2); -f2y = @(u) 3*u(1)^2 - 3*u(2)^2; -## f_prime == Jacobian. -f_prime = @(u) [ f1x(u), f1y(u); f2x(u), f2y(u) ]; -tol = 1 / 10^12; -u0 = [1.2; 2.5]; -expected_root = [1.33635; 1.75424]; -[actual_root, iterations] = newtons_method(f, f_prime, tol, u0); -unit_test_equals("Homework #3 problem #4 root is correct", ... - expected_root, ... - actual_root); - +addpath('./unit_test'); +unit_init(1, {}); -f = @(x,y) y; -x0 = 0; -y0 = 1; -h = 1; -actual_y = forward_euler1(x0, y0, f, h); -expected_y = 2; +test_files = glob('tests/*.m'); -unit_test_equals("Forward Euler works for one step", ... - expected_y, ... - actual_y); +## Source every file that matches the glob above. +cellfun(@source, test_files); diff --git a/tests/divided_difference_coefficients_tests.m b/tests/divided_difference_coefficients_tests.m new file mode 100644 index 0000000..301e2fb --- /dev/null +++ b/tests/divided_difference_coefficients_tests.m @@ -0,0 +1,11 @@ +unit_test_equals("zero order divided_difference_coefficients", ... + [1], ... + divided_difference_coefficients([0])); + +unit_test_equals("first order divided_difference_coefficients", ... + [-1, 1] / pi, ... + divided_difference_coefficients([0, pi])); + +unit_test_equals("second order divided_difference_coefficients", ... + [1, -2, 1] / (2*pi^2), ... + divided_difference_coefficients([0, pi, 2*pi])); diff --git a/tests/divided_difference_tests.m b/tests/divided_difference_tests.m new file mode 100644 index 0000000..38e47bb --- /dev/null +++ b/tests/divided_difference_tests.m @@ -0,0 +1,11 @@ +unit_test_equals("sin[0] == 0", ... + 0, ... + divided_difference(@sin, 0)); + +unit_test_equals("sin[0, pi] == 0", ... + 0, ... + divided_difference(@sin, [0,pi])); + +unit_test_equals("sin[0, pi, 2*pi] == 0", ... + 0, ... + divided_difference(@sin, [0,pi,2*pi])); diff --git a/tests/even_tests.m b/tests/even_tests.m new file mode 100644 index 0000000..09c57f2 --- /dev/null +++ b/tests/even_tests.m @@ -0,0 +1,7 @@ +unit_test_equals("1 is not even", ... + false, ... + even(1)); + +unit_test_equals("2 is even", ... + true, ... + even(2)); diff --git a/tests/fixed_point_method_tests.m b/tests/fixed_point_method_tests.m new file mode 100644 index 0000000..df3b05e --- /dev/null +++ b/tests/fixed_point_method_tests.m @@ -0,0 +1,19 @@ +g = @(x) 1 + atan(x); +expected_fp = 2.1323; +tol = 1 / 10^10; +x0 = 2.4; +unit_test_equals("Homework #2 problem #5 fixed point is correct", ... + expected_fp, ... + fixed_point_method(g, tol, x0)); + + +h = 0.5; +g1 = @(u) 1 + h*exp(-u(1)^2)/(1+u(2)^2); +g2 = @(u) 0.5 + h*atan(u(1)^2 + u(2)^2); +my_g = @(u) [g1(u), g2(u)]; +tol = 1 / 10^9; +u0 = [1,1]; +expected_fp = [1.0729, 1.0821]; +unit_test_equals("Homework #3 problem #3i fixed point is correct", ... + expected_fp, ... + fixed_point_method(my_g, tol, u0)); diff --git a/tests/forward_euler1_tests.m b/tests/forward_euler1_tests.m new file mode 100644 index 0000000..8bc337e --- /dev/null +++ b/tests/forward_euler1_tests.m @@ -0,0 +1,10 @@ +f = @(x,y) y; +x0 = 0; +y0 = 1; +h = 1; +actual_y = forward_euler1(x0, y0, f, h); +expected_y = 2; + +unit_test_equals("Forward Euler works for one step", ... + expected_y, ... + actual_y); diff --git a/tests/newtons_method_tests.m b/tests/newtons_method_tests.m new file mode 100644 index 0000000..266b0b4 --- /dev/null +++ b/tests/newtons_method_tests.m @@ -0,0 +1,27 @@ +f = @(x) x^6 - x - 1; +f_prime = @(x) 6*x^5 - 1; +tol = 1/1000000; +x0 = 2; +expected_root = 1.1347; +unit_test_equals("Newton's method agrees with Haskell", ... + expected_root, ... + newtons_method(f, f_prime, tol, x0)); + + +f1 = @(u) u(1)^2 + u(1)*u(2)^3 - 9; +f2 = @(u) 3*u(1)^2*u(2) - u(2)^3 - 4; +f = @(u) [f1(u); f2(u)]; +## The partials for the Jacobian. +f1x = @(u) 2*u(1) + u(2)^3; +f1y = @(u) 3*u(1)*u(2)^2; +f2x = @(u) 6*u(1)*u(2); +f2y = @(u) 3*u(1)^2 - 3*u(2)^2; +## f_prime == Jacobian. +f_prime = @(u) [ f1x(u), f1y(u); f2x(u), f2y(u) ]; +tol = 1 / 10^12; +u0 = [1.2; 2.5]; +expected_root = [1.33635; 1.75424]; +[actual_root, iterations] = newtons_method(f, f_prime, tol, u0); +unit_test_equals("Homework #3 problem #4 root is correct", ... + expected_root, ... + actual_root); diff --git a/tests/odd_tests.m b/tests/odd_tests.m new file mode 100644 index 0000000..d2ab7ba --- /dev/null +++ b/tests/odd_tests.m @@ -0,0 +1,7 @@ +unit_test_equals("1 is odd", ... + true, ... + odd(1)); + +unit_test_equals("2 is not odd", ... + false, ... + odd(2)); diff --git a/tests/poisson_matrix_tests.m b/tests/poisson_matrix_tests.m new file mode 100644 index 0000000..4b50b2e --- /dev/null +++ b/tests/poisson_matrix_tests.m @@ -0,0 +1,9 @@ +expected_A = [-1, 0, 0, 0, 0; ... + -16, 32, -16, 0, 0; ... + 0, -16, 32, -16, 0; ... + 0, 0, -16, 32, -16; ... + 0, 0, 0, 0, -1]; + +unit_test_equals("Homework #1 problem #1 Poisson matrix is correct", ... + true, ... + expected_A == poisson_matrix(4, 0, 1)); diff --git a/errcheck.m b/unit_test/errcheck.m similarity index 100% rename from errcheck.m rename to unit_test/errcheck.m diff --git a/erreval.m b/unit_test/erreval.m similarity index 100% rename from erreval.m rename to unit_test/erreval.m diff --git a/unit_init.m b/unit_test/unit_init.m similarity index 100% rename from unit_init.m rename to unit_test/unit_init.m diff --git a/unit_results.m b/unit_test/unit_results.m similarity index 100% rename from unit_results.m rename to unit_test/unit_results.m diff --git a/unit_test.m b/unit_test/unit_test.m similarity index 100% rename from unit_test.m rename to unit_test/unit_test.m diff --git a/unit_test_equals.m b/unit_test/unit_test_equals.m similarity index 100% rename from unit_test_equals.m rename to unit_test/unit_test_equals.m diff --git a/unit_test_err.m b/unit_test/unit_test_err.m similarity index 100% rename from unit_test_err.m rename to unit_test/unit_test_err.m