]> gitweb.michael.orlitzky.com - octave.git/blobdiff - unit_test/unit_test.m
Move all of the tests into a subdirectory.
[octave.git] / unit_test / unit_test.m
diff --git a/unit_test/unit_test.m b/unit_test/unit_test.m
new file mode 100644 (file)
index 0000000..e21739d
--- /dev/null
@@ -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