]> gitweb.michael.orlitzky.com - octave.git/blob - unit_test/unit_test.m
Add the Rosenbrock functions and their tests.
[octave.git] / unit_test / unit_test.m
1 function result = unit_test(test_title, expect_pass, actual_result)
2 ## Function unittest compares the ACTUAL_RESULT of running
3 ## a test (either 0 for failure, or 1 for success) with the
4 ## expected outcome of the test EXPECT_PASS (either 0 for expecting
5 ## a failure, or 1 for expecting pass). TEST_TITLE is the name of
6 ## the test. All test results will be accompanied by the test's
7 ## title.
8 ##
9 ## The result of unit_test is on of the following: UNRESOLVED: The
10 ## test did neither return 0 nor 1. PASS: expected pass, got pass.
11 ## FAIL: expected pass, got fail. UPASS: expected fail, got pass.
12 ## XFAIL: expected fail, got fail.
13 ##
14 ## A call to unit_test typically looks like this:
15 ##
16 ## unit_test("scalar integer addition", 1, eval("1 + 1 == 2;"));
17
18 global unittest_results;
19
20 ## Sanity check input parameters
21 if ( nargin < 3 || nargin > 4 )
22 error("Function run_rest expects 3 or 4 parameters.");
23 endif
24
25 if (!ischar(test_title))
26 error("Expecting TEST_TITLE (arg 1) to be a string.");
27 endif
28
29 if (expect_pass != 0 && expect_pass != 1)
30 error("Expecting EXPECT_PASS (arg 2) to be 0 or 1.");
31 endif
32
33 unittest_results.total++;
34
35 ## Take actions depending on what test result we expect
36 ## (expect_pass), and what we actually got (actual_result).
37 if (actual_result != 0 && actual_result != 1)
38 result = "UNRESOLVED";
39 unittest_results.unresolved++;
40 if (actual_result == 2)
41 printf("SYNTAX ERROR: %s\n", test_title);
42 endif
43 return;
44 endif
45
46 if (expect_pass == 1 && actual_result == 1)
47 result = "PASS";
48 if (unittest_results.verbose != 0)
49 printf("PASS: %s\n", test_title);
50 else
51 printf('.');
52 endif
53 unittest_results.pass++;
54 elseif (expect_pass == 1 && actual_result == 0)
55 result = "FAIL";
56 printf("FAIL: %s\n\n", test_title);
57 unittest_results.fail++;
58 elseif (expect_pass == 0 && actual_result == 0)
59 result = "XFAIL";
60 printf("XFAIL: %s\n", test_title);
61 unittest_results.xfail++;
62 elseif (expect_pass == 0 && actual_result == 1)
63 result = "UPASS";
64 printf("UPASS: %s\n", test_title);
65 unittest_results.upass++;
66 endif
67 endfunction