+# call this to declare that a unit test has passed
+function unit_test_passed($msg) {
+ if(isset($GLOBALS['unit_tests_verbose']) && $GLOBALS['unit_tests_verbose'] > 1) {
+ message("PASSED: $msg");
+ }
+ $GLOBALS['unit_tests_passed'] += 1;
+}
+
+# call this to declare that a unit test has failed
+function unit_test_failed($msg) {
+ message("FAILED: $msg");
+ $GLOBALS['unit_tests_failed'] += 1;
+}
+
+# use this function to unit-test a function
+# real prototype: ($name, $args..., $correct)
+function unit_test_func() {
+ $args = func_get_args();
+ $function = array_shift($args);
+ $correct = array_pop($args);
+ $result = call_user_func_array($function, $args);
+ $message = "$function(" . join(', ', $args) . ") returned $result";
+ if($result == $correct) {
+ unit_test_passed($message);
+ } else {
+ unit_test_failed($message . " instead of $correct");
+ }
+}
+
+