function test($name, $data, $datatype, $plottype, $expect_fail, $errmatch = '') { global $test_verbose, $php_warning, $n_tests, $n_pass, $n_fail; global $test_debug_images; $n_tests++; $php_warning = ''; // Initialize empty. Error handler will append. $details = ''; // Why it passed or failed. if ($test_verbose) { echo "Test case {$n_tests}: {$name}\n"; } $plot = new PHPlot_noerr(); if (empty($test_debug_images)) { $plot->SetPrintImage(False); // Don't produce an image if OK } else { $plot->SetTitle("Case {$n_tests}: {$name}"); $plot->SetOutputFile($test_debug_images . sprintf("%03d", $n_tests) . ".png"); $plot->SetIsInline(True); } $plot->SetDataType($datatype); $plot->SetPlotType($plottype); $failed = !$plot->SetDataValues($data) || !$plot->DrawGraph() || !empty($plot->test_error_text); $fail_message = $plot->test_error_text; if ($test_verbose) { if ($failed) { echo "Result: PHPlot error: '{$fail_message}'\n"; } else { echo "Result: PHPlot graph, no error message\n"; } } # Evaluate pass/fail status of the test case: if ($expect_fail) { if ($failed) { if (preg_match("{$errmatch}i", $fail_message)) { $details = "(produced error as expected)\n"; $test_passed = True; } else { $details = " Expecting an error, got a different error\n" . " Actual message: {$fail_message}\n" . " Expected to match: {$errmatch}\n"; $test_passed = False; } } else { $details = " Expecting an error, but produced a graph.\n"; $test_passed = False; } } else { // Not expected to fail if ($failed) { $details = " Expected a graph, but got this error instead:\n" . " {$fail_message}\n"; $test_passed = False; } else { $details = "(produced graph as expected)\n"; $test_passed = True; } } // Fail the test if any notice or warnings were produced: if (!empty($php_warning)) { if ($test_passed) { // Replace the "test passed" message. $details = "Test would have passed except for PHP warnings or notices:\n" . $php_warning . "\n"; $test_passed = False; } else { $details .= "Test also failed due to PHP warnings or notices:\n" . $php_warning . "\n"; } } if ($test_passed) { $n_pass++; if ($test_verbose) { echo "Pass {$details}"; } } else { $n_fail++; echo "Failed test case {$n_tests}: {$name}\n{$details}"; } if ($test_verbose) { echo "\n"; } }