X-Git-Url: http://gitweb.michael.orlitzky.com/?p=octave.git;a=blobdiff_plain;f=unit_test%2Funit_test.m;fp=unit_test%2Funit_test.m;h=e21739dfee0a659ec8daa2c7af9b6e627603cbc7;hp=0000000000000000000000000000000000000000;hb=ef3085ef3c40eab925219bad1c6dbb2c6e106727;hpb=33e4dd7a64f35fb22774d59af11f5670ab566fbc diff --git a/unit_test/unit_test.m b/unit_test/unit_test.m new file mode 100644 index 0000000..e21739d --- /dev/null +++ b/unit_test/unit_test.m @@ -0,0 +1,67 @@ +function result = unit_test(test_title, expect_pass, actual_result) + ## Function unittest compares the ACTUAL_RESULT of running + ## a test (either 0 for failure, or 1 for success) with the + ## expected outcome of the test EXPECT_PASS (either 0 for expecting + ## a failure, or 1 for expecting pass). TEST_TITLE is the name of + ## the test. All test results will be accompanied by the test's + ## title. + ## + ## The result of unit_test is on of the following: UNRESOLVED: The + ## test did neither return 0 nor 1. PASS: expected pass, got pass. + ## FAIL: expected pass, got fail. UPASS: expected fail, got pass. + ## XFAIL: expected fail, got fail. + ## + ## A call to unit_test typically looks like this: + ## + ## unit_test("scalar integer addition", 1, eval("1 + 1 == 2;")); + + global unittest_results; + + ## Sanity check input parameters + if ( nargin < 3 || nargin > 4 ) + error("Function run_rest expects 3 or 4 parameters."); + endif + + if (!ischar(test_title)) + error("Expecting TEST_TITLE (arg 1) to be a string."); + endif + + if (expect_pass != 0 && expect_pass != 1) + error("Expecting EXPECT_PASS (arg 2) to be 0 or 1."); + endif + + unittest_results.total++; + + ## Take actions depending on what test result we expect + ## (expect_pass), and what we actually got (actual_result). + if (actual_result != 0 && actual_result != 1) + result = "UNRESOLVED"; + unittest_results.unresolved++; + if (actual_result == 2) + printf("SYNTAX ERROR: %s\n", test_title); + endif + return; + endif + + if (expect_pass == 1 && actual_result == 1) + result = "PASS"; + if (unittest_results.verbose != 0) + printf("PASS: %s\n", test_title); + else + printf('.'); + endif + unittest_results.pass++; + elseif (expect_pass == 1 && actual_result == 0) + result = "FAIL"; + printf("FAIL: %s\n\n", test_title); + unittest_results.fail++; + elseif (expect_pass == 0 && actual_result == 0) + result = "XFAIL"; + printf("XFAIL: %s\n", test_title); + unittest_results.xfail++; + elseif (expect_pass == 0 && actual_result == 1) + result = "UPASS"; + printf("UPASS: %s\n", test_title); + unittest_results.upass++; + endif +endfunction