2121 * questions.
2222 */
2323
24+ import java .io .*;
25+ import java .util .Formatter ;
26+ import java .util .Locale ;
27+
2428public class Basic {
2529
2630 private static int fail = 0 ;
31+
2732 private static int pass = 0 ;
2833
2934 private static Throwable first ;
3035
31- static void pass () {
36+ protected static void test (String fs , String exp , Object ... args ) {
37+ Formatter f = new Formatter (new StringBuilder (), Locale .US );
38+ f .format (fs , args );
39+ ck (fs , exp , f .toString ());
40+
41+ f = new Formatter (new StringBuilder (), Locale .US );
42+ f .format ("foo " + fs + " bar" , args );
43+ ck (fs , "foo " + exp + " bar" , f .toString ());
44+ }
45+
46+ protected static void test (Locale l , String fs , String exp , Object ... args )
47+ {
48+ Formatter f = new Formatter (new StringBuilder (), l );
49+ f .format (fs , args );
50+ ck (fs , exp , f .toString ());
51+
52+ f = new Formatter (new StringBuilder (), l );
53+ f .format ("foo " + fs + " bar" , args );
54+ ck (fs , "foo " + exp + " bar" , f .toString ());
55+ }
56+
57+ protected static void test (String fs , Object ... args ) {
58+ Formatter f = new Formatter (new StringBuilder (), Locale .US );
59+ f .format (fs , args );
60+ ck (fs , "fail" , f .toString ());
61+ }
62+
63+ protected static void test (String fs ) {
64+ Formatter f = new Formatter (new StringBuilder (), Locale .US );
65+ f .format (fs , "fail" );
66+ ck (fs , "fail" , f .toString ());
67+ }
68+
69+ protected static void testSysOut (String fs , String exp , Object ... args ) {
70+ FileOutputStream fos = null ;
71+ FileInputStream fis = null ;
72+ try {
73+ PrintStream saveOut = System .out ;
74+ fos = new FileOutputStream ("testSysOut" );
75+ System .setOut (new PrintStream (fos ));
76+ System .out .format (Locale .US , fs , args );
77+ fos .close ();
78+
79+ fis = new FileInputStream ("testSysOut" );
80+ byte [] ba = new byte [exp .length ()];
81+ int len = fis .read (ba );
82+ String got = new String (ba );
83+ if (len != ba .length )
84+ fail (fs , exp , got );
85+ ck (fs , exp , got );
86+
87+ System .setOut (saveOut );
88+ } catch (FileNotFoundException ex ) {
89+ fail (fs , ex .getClass ());
90+ } catch (IOException ex ) {
91+ fail (fs , ex .getClass ());
92+ } finally {
93+ try {
94+ if (fos != null )
95+ fos .close ();
96+ if (fis != null )
97+ fis .close ();
98+ } catch (IOException ex ) {
99+ fail (fs , ex .getClass ());
100+ }
101+ }
102+ }
103+
104+ protected static void tryCatch (String fs , Class <?> ex ) {
105+ boolean caught = false ;
106+ try {
107+ test (fs );
108+ } catch (Throwable x ) {
109+ if (ex .isAssignableFrom (x .getClass ()))
110+ caught = true ;
111+ }
112+ if (!caught )
113+ fail (fs , ex );
114+ else
115+ pass ();
116+ }
117+
118+ protected static void tryCatch (String fs , Class <?> ex , Object ... args ) {
119+ boolean caught = false ;
120+ try {
121+ test (fs , args );
122+ } catch (Throwable x ) {
123+ if (ex .isAssignableFrom (x .getClass ()))
124+ caught = true ;
125+ }
126+ if (!caught )
127+ fail (fs , ex );
128+ else
129+ pass ();
130+ }
131+
132+ private static void pass () {
32133 pass ++;
33134 }
34135
35- static void fail (String fs , Class ex ) {
136+ private static void fail (String fs , Class ex ) {
36137 String message = "'%s': %s not thrown" .formatted (fs , ex .getName ());
37138 if (first == null ) {
38139 setFirst (message );
@@ -41,7 +142,7 @@ static void fail(String fs, Class ex) {
41142 fail ++;
42143 }
43144
44- static void fail (String fs , String exp , String got ) {
145+ private static void fail (String fs , String exp , String got ) {
45146 String message = "'%s': Expected '%s', got '%s'" .formatted (fs , exp , got );
46147 if (first == null ) {
47148 setFirst (message );
@@ -58,7 +159,7 @@ private static void setFirst(String s) {
58159 }
59160 }
60161
61- static void ck (String fs , String exp , String got ) {
162+ private static void ck (String fs , String exp , String got ) {
62163 if (!exp .equals (got )) {
63164 fail (fs , exp , got );
64165 } else {
0 commit comments