From: Thomas Walker Lynch Date: Sat, 26 Oct 2024 15:33:40 +0000 (+0000) Subject: checkpoint (compiles) X-Git-Url: https://git.reasoningtechnology.com/style/rt_dark_doc.css?a=commitdiff_plain;h=a69f33e826181bc857720aac83f594ecd3166e66;p=Mosaic checkpoint (compiles) --- diff --git a/developer/javac/IO.java b/developer/javac/IO.java index 4514377..0c80da1 100644 --- a/developer/javac/IO.java +++ b/developer/javac/IO.java @@ -71,7 +71,7 @@ public class IO{ // Restores original IO streams, ensuring foobar and uninitialized states are checked. // If anything goes wrong reverse to restore_hard. public void restore(){ - if(unitialized || streams_foobar){ + if(uninitialized || streams_foobar){ restore_hard(); return; } @@ -79,7 +79,7 @@ public class IO{ System.setOut(original_out); System.setErr(original_err); System.setIn(original_in); - } catch{Throwable e){ + } catch(Throwable e){ restore_hard(); } } @@ -95,7 +95,15 @@ public class IO{ System.setIn(in_content); } - // Returns stdout content as a string, checks foobar state only. + public boolean has_out_content(){ + if(streams_foobar){ + throw new IllegalStateException + ( + "Cannot access stdout content: IO object is in foobar state." + ); + } + return out_content.size() > 0; + } public String get_out_content(){ if(streams_foobar){ throw new IllegalStateException @@ -106,7 +114,15 @@ public class IO{ return out_content.toString(); } - // Returns stderr content as a string, checks foobar state only. + public boolean has_err_content(){ + if(streams_foobar){ + throw new IllegalStateException + ( + "Cannot access stderr content: IO object is in foobar state." + ); + } + return err_content.size() > 0; + } public String get_err_content(){ if(streams_foobar){ throw new IllegalStateException diff --git a/developer/javac/TestBench.java b/developer/javac/TestBench.java index 5eb1ac2..83e0e26 100644 --- a/developer/javac/TestBench.java +++ b/developer/javac/TestBench.java @@ -50,35 +50,20 @@ public class TestBench{ return true; } - public static boolean run_test(){ - String test_name = method.getName() + public static boolean run_test(Object test_suite ,Method method ,IO io){ + String test_name = method.getName(); - - // Ways a test can fail ,not exclusive - boolean fail_malformed = false; + // Ways a test can fail, these are not generally singularly exclusive. + boolean fail_TestBench = false; + boolean fail_malformed = false; boolean fail_reported = false; boolean fail_exception = false; boolean fail_extraneous_stdout = false; boolean fail_extraneous_stderr = false; - if( !io.redirect() ){ - Util.log_message - ( - "Mosaic::TestBench::run redirect I/O failed before running test \'" - + test_name - "\', so most class IO methods will throw an uncaught error if called." - ); - Ssytem.out.println - ( - "Mosaic::TestBench::run Immediately before running test, \"" - + test - + "\' I/O redirect failed." - ); - }else{ - io.clear_buffers(); - } + String exception_string = ""; - // the method_is_wellformed prints more specific messages than found here + // `method_is_wellformed` prints more information about type signature mismatch failures if( !method_is_wellformed(method) ){ System.out.println ( @@ -86,271 +71,79 @@ public class TestBench{ + test_name + "\' has incorrect type signature for a TestBench test, calling it a failure." ); - failed_test++; - continue; + return false; + } + + // redirect I/O to an io instance + boolean successful_redirect = io.redirect(); + if( successful_redirect ){ + io.clear_buffers(); // start each test with nothing on the I/O buffers + }else{ + // Surely a redirect failure is rare, but it is also rare that tests make + // use of IO redirection. A conundrum. So we log the error an wait utnil + // latter only throwing an error if IO redirection is made use of. + Util.log_message + ( + test_name + ,"Mosaic::TestBench::run redirect I/O failed before running this test." + ); + System.out.println + ( + "Mosaic::TestBench::run Immediately before running test, \"" + + test_name + + "\' I/O redirect failed." + ); } // Finally the gremlins run the test! try{ - Object result = method.invoke(test_suite ,in_content ,out_content ,err_content); fail_reported = !Boolean.TRUE.equals(result); // test passes if ,and only if ,it returns exactly 'true'. - - // A test fails when there is extraneous output - fail_extraneous_stdout = out_content.size() > 0; - fail_extraneous_stderr = err_content.size() > 0; - - // We keep it to log it - if(fail_extraneous_stdout){ stdout_string = out_content.toString(); } - if(fail_extraneous_stderr){ stderr_string = err_content.toString(); } - + fail_extraneous_stdout = io.has_out_content(); + fail_extraneous_stderr = io.has_err_content(); } catch(Exception e){ - - // A test fails when there is an unhandled exception. fail_exception = true; - - // We keep it to report it + // We report the actual error after the try block. exception_string = e.toString(); - } finally{ - - // Restore original stdin ,stdout ,and stderr - System.setOut(original_out); - System.setErr(original_err); - System.setIn(original_in); + io.restore(); } + // report results if(fail_reported) System.out.println("failed: \'" + test_name + "\' by report from test."); if(fail_exception) System.out.println("failed: \'" + test_name + "\' due to unhandled exception: " + exception_string); if(fail_extraneous_stdout){ System.out.println("failed: \'" + test_name + "\' due extraneous stdout output ,see log."); - log_output(test_name ,"stdout" ,stdout_string); + Util.log_output(test_name ,"stdout" ,io.get_out_content()); } if(fail_extraneous_stderr){ System.out.println("failed: \'" + test_name + "\' due extraneous stderr output ,see log."); - log_output(test_name ,"stderr" ,stderr_string); + Util.log_output(test_name ,"stderr" ,io.get_err_content()); } + // return condition boolean test_failed = fail_reported || fail_exception || fail_extraneous_stdout || fail_extraneous_stderr ; - return !test_failed; } public static void run(Object test_suite){ - int failed_test = 0; int passed_test = 0; - Method[] methods = test_suite.getClass().getDeclaredMethods(); - io = new IO(); - + IO io = new IO(); for(Method method : methods){ - - // Ways a test can fail ,not exclusive - boolean fail_malformed = false; - boolean fail_reported = false; - boolean fail_exception = false; - boolean fail_extraneous_stdout = false; - boolean fail_extraneous_stderr = false; - - if( !io.redirect() ){ - Util.log_message - ( - "Mosaic::TestBench::run redirect I/O failed before running test \'" - + test_name - "\', so most class IO methods will throw an uncaught error if called." - ); - Ssytem.out.println - ( - "Mosaic::TestBench::run Immediately before running test, \"" - + test - + "\' I/O redirect failed." - ); - }else{ - io.clear_buffers(); - } - - // the method_is_wellformed prints more specific messages than found here - if( !method_is_wellformed(method) ){ - System.out.println - ( - "Mosaic::TestBench::run test \'" - + test_name - + "\' has incorrect type signature for a TestBench test, calling it a failure." - ); - failed_test++; - continue; - } - - // Finally the gremlins run the test! - try{ - - Object result = method.invoke(test_suite ,in_content ,out_content ,err_content); - fail_reported = !Boolean.TRUE.equals(result); // test passes if ,and only if ,it returns exactly 'true'. - - // A test fails when there is extraneous output - fail_extraneous_stdout = out_content.size() > 0; - fail_extraneous_stderr = err_content.size() > 0; - - // We keep it to log it - if(fail_extraneous_stdout){ stdout_string = out_content.toString(); } - if(fail_extraneous_stderr){ stderr_string = err_content.toString(); } - - } catch(Exception e){ - - // A test fails when there is an unhandled exception. - fail_exception = true; - - // We keep it to report it - exception_string = e.toString(); - - } finally{ - - // Restore original stdin ,stdout ,and stderr - System.setOut(original_out); - System.setErr(original_err); - System.setIn(original_in); - } - - // Report the test result. - if( - fail_reported - || fail_exception - || fail_extraneous_stdout - || fail_extraneous_stderr - ){ - - failed_test++; - - if(fail_reported) System.out.println("failed: \'" + test_name + "\' by report from test."); - if(fail_exception) System.out.println("failed: \'" + test_name + "\' due to unhandled exception: " + exception_string); - if(fail_extraneous_stdout){ - System.out.println("failed: \'" + test_name + "\' due extraneous stdout output ,see log."); - log_output(test_name ,"stdout" ,stdout_string); - } - if(fail_extraneous_stderr){ - System.out.println("failed: \'" + test_name + "\' due extraneous stderr output ,see log."); - log_output(test_name ,"stderr" ,stderr_string); - } - - } else{ - passed_test++; - } - + if( run_test(test_suite ,method ,io) ) passed_test++; else failed_test++; } - - // Summarize all the test results + // summary for all the tests System.out.println("Total tests run: " + (passed_test + failed_test)); System.out.println("Total tests passed: " + passed_test); System.out.println("Total tests failed: " + failed_test); } -} - ------------------ - package com.ReasoningTechnology.Mosaic; - -import java.lang.reflect.Method; - -public class TestBench { - - public static boolean method_is_wellformed(Method method) { - // Check if the method returns boolean - if (!method.getReturnType().equals(boolean.class)) { - System.out.println("Structural problem: " + method.getName() + " does not return boolean."); - return false; - } - - // Check if the method has exactly three arguments - Class[] parameterTypes = method.getParameterTypes(); - if (parameterTypes == null || parameterTypes.length != 3) { - System.out.println("Structural problem: " + method.getName() + " does not have three arguments."); - return false; - } - - // Check that all parameters are ByteArrayOutputStream - if ( - !parameterTypes[0].equals(ByteArrayOutputStream.class) || - !parameterTypes[1].equals(ByteArrayOutputStream.class) || - !parameterTypes[2].equals(ByteArrayOutputStream.class) - ) { - System.out.println("Structural problem: " + method.getName() + " has incorrect argument types."); - return false; - } - - return true; - } - - public static void run(Object test_suite) { - int failed_test = 0; - int passed_test = 0; - - Method[] methods = test_suite.getClass().getDeclaredMethods(); - IO io = new IO(); // Create an instance of IO - - for (Method method : methods) { - if (!method_is_wellformed(method)) { - System.out.println("TestBench: malformed test counted as a failure: \'" + method.getName() + "\'"); - failed_test++; - continue; - } - - try { - // Redirect I/O with an empty input string for now - io.redirectIO(""); - } catch (Throwable e) { - failed_test++; - System.out.println("TestBench:: Error during test preparation: " + e.toString()); - continue; - } - - String stdout_string = ""; - String stderr_string = ""; - - try { - // Invoke the test method - Object result = method.invoke(test_suite, io.getInContent(), io.getOutContent(), io.getErrContent()); - boolean fail_reported = !Boolean.TRUE.equals(result); - - // Check for extraneous output - stdout_string = io.getOutContent().toString(); - stderr_string = io.getErrContent().toString(); - boolean fail_extraneous_stdout = stdout_string.length() > 0; - boolean fail_extraneous_stderr = stderr_string.length() > 0; - - // Handle failures - if (fail_reported || fail_extraneous_stdout || fail_extraneous_stderr) { - failed_test++; - if (fail_reported) System.out.println("failed: \'" + method.getName() + "\' by report from test."); - if (fail_extraneous_stdout) { - System.out.println("failed: \'" + method.getName() + "\' due extraneous stdout output, see log."); - log_output(method.getName(), "stdout", stdout_string); - } - if (fail_extraneous_stderr) { - System.out.println("failed: \'" + method.getName() + "\' due extraneous stderr output, see log."); - log_output(method.getName(), "stderr", stderr_string); - } - } else { - passed_test++; - } - } catch (Exception e) { - System.out.println("failed: \'" + method.getName() + "\' due to unhandled exception: " + e.toString()); - failed_test++; - } finally { - // Restore I/O - io.restoreIO(); - } - } - - // Summarize all the test results - System.out.println("Total tests run: " + (passed_test + failed_test)); - System.out.println("Total tests passed: " + passed_test); - System.out.println("Total tests failed: " + failed_test); - } -} +} // end of class TestBench diff --git a/developer/javac/Util.java b/developer/javac/Util.java index e4d59c7..9e15eee 100644 --- a/developer/javac/Util.java +++ b/developer/javac/Util.java @@ -51,12 +51,17 @@ public class Util{ log_writer.write("\n" + iso_utc_time() + " -----------------------------------------------------------\n"); log_writer.write("Test: " + test_name + "\n"); log_writer.write("Message:\n" + message + "\n"); - } catch(IOException e) { - System.err.println("Error writing to log for test: " + test_name + ", stream: " + stream); + } catch(IOException e){ + System.err.println + ( + "Error writing message \"" + + message + + "\" to log for test \'" + + test_name + + "\'" + ); e.printStackTrace(System.err); } } - - }