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
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.
14 ## A call to unit_test typically looks like this:
16 ## unit_test("scalar integer addition", 1, eval("1 + 1 == 2;"));
18 global unittest_results;
20 ## Sanity check input parameters
21 if ( nargin < 3 || nargin > 4 )
22 error("Function run_rest expects 3 or 4 parameters.");
25 if (!ischar(test_title))
26 error("Expecting TEST_TITLE (arg 1) to be a string.");
29 if (expect_pass != 0 && expect_pass != 1)
30 error("Expecting EXPECT_PASS (arg 2) to be 0 or 1.");
33 unittest_results.total++;
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);
46 if (expect_pass == 1 && actual_result == 1)
48 if (unittest_results.verbose != 0)
49 printf("PASS: %s\n", test_title);
53 unittest_results.pass++;
54 elseif (expect_pass == 1 && actual_result == 0)
56 printf("FAIL: %s\n\n", test_title);
57 unittest_results.fail++;
58 elseif (expect_pass == 0 && actual_result == 0)
60 printf("XFAIL: %s\n", test_title);
61 unittest_results.xfail++;
62 elseif (expect_pass == 0 && actual_result == 1)
64 printf("UPASS: %s\n", test_title);
65 unittest_results.upass++;