11package algorithms .sprint0 ;
22
3- import lombok .experimental .UtilityClass ;
3+ import lombok .AccessLevel ;
4+ import lombok .NoArgsConstructor ;
5+ import org .junit .jupiter .api .Test ;
46
5- import java .io .BufferedReader ;
6- import java .io .IOException ;
7- import java .io .Writer ;
7+ import java .io .*;
88import java .util .Arrays ;
99import java .util .List ;
1010import java .util .stream .Collectors ;
1111
12- @ UtilityClass
12+ import static org .junit .jupiter .api .Assertions .*;
13+
14+ @ NoArgsConstructor (access = AccessLevel .PUBLIC )
1315public class Utils {
1416
1517 public static List <Integer > readList (BufferedReader reader ) throws IOException {
16- return Arrays .stream (reader .readLine ().trim ().split ("\\ s+" ))
18+ String line = reader .readLine ();
19+ if (line == null ) throw new NumberFormatException ("EOF" );
20+ return Arrays .stream (line .trim ().split ("\\ s+" ))
1721 .map (Integer ::parseInt )
1822 .collect (Collectors .toList ());
1923 }
@@ -29,6 +33,118 @@ public static <T> void printList(List<T> list, Writer writer) {
2933 }
3034
3135 public static int readInt (BufferedReader reader ) throws IOException {
32- return Integer .parseInt (reader .readLine ());
36+ String line = reader .readLine ();
37+ if (line == null ) throw new NumberFormatException ("EOF" );
38+ return Integer .parseInt (line );
39+ }
40+
41+ @ Test
42+ public void readListbasic () throws Exception {
43+ BufferedReader br = new BufferedReader (new StringReader ("1 2 3" ));
44+ List <Integer > got = readList (br );
45+ assertEquals (Arrays .asList (1 , 2 , 3 ), got );
46+ }
47+
48+ @ Test
49+ void readList_mixedWhitespace_and_signs () throws Exception {
50+ BufferedReader br = new BufferedReader (new StringReader (" -1\t 0 5 " ));
51+ List <Integer > got = readList (br );
52+ assertEquals (Arrays .asList (-1 , 0 , 5 ), got );
53+ }
54+
55+ @ Test
56+ void readList_emptyLine_throwsNumberFormat () {
57+ BufferedReader br = new BufferedReader (new StringReader ("\\ n" ));
58+ assertThrows (NumberFormatException .class , () -> readList (br ));
59+ }
60+
61+ @ Test
62+ void readList_EOF_throwsNumberFormat () {
63+ BufferedReader br = new BufferedReader (new StringReader ("" ));
64+ assertThrows (NumberFormatException .class , () -> readList (br ));
65+ }
66+
67+ @ Test
68+ void readList_nonIntegerToken_throwsNumberFormat () {
69+ BufferedReader br = new BufferedReader (new StringReader ("1 a 3" ));
70+ assertThrows (NumberFormatException .class , () -> readList (br ));
71+ }
72+
73+ @ Test
74+ void readList_overflow_throwsNumberFormat () {
75+ BufferedReader br = new BufferedReader (new StringReader ("2147483648" ));
76+ assertThrows (NumberFormatException .class , () -> readList (br ));
77+ }
78+
79+ // --- readInt ---
80+
81+ @ Test
82+ void readInt_basic () throws Exception {
83+ BufferedReader br = new BufferedReader (new StringReader ("42" ));
84+ assertEquals (42 , readInt (br ));
85+ }
86+
87+ @ Test
88+ void readInt_negative () throws Exception {
89+ BufferedReader br = new BufferedReader (new StringReader ("-7" ));
90+ assertEquals (-7 , readInt (br ));
91+ }
92+
93+ @ Test
94+ void readInt_withSpaces_throwsNumberFormat () {
95+ BufferedReader br = new BufferedReader (new StringReader (" 42 " ));
96+ assertThrows (NumberFormatException .class , () -> readInt (br ));
97+ }
98+
99+ // --- printList ---
100+
101+ @ Test
102+ void printList_integers_trailingSpace_noNewline () throws Exception {
103+ StringWriter sw = new StringWriter ();
104+ printList (Arrays .asList (1 , 2 , 3 ), sw );
105+ assertEquals ("1 2 3 " , sw .toString ());
106+ }
107+
108+ @ Test
109+ void printList_strings_usesToString () throws Exception {
110+ StringWriter sw = new StringWriter ();
111+ printList (Arrays .asList ("a" , "b" ), sw );
112+ assertEquals ("a b " , sw .toString ());
113+ }
114+
115+ @ Test
116+ void printList_empty_writesNothing () throws Exception {
117+ StringWriter sw = new StringWriter ();
118+ printList (List .of (), sw );
119+ assertEquals ("" , sw .toString ());
120+ }
121+
122+ @ Test
123+ void printList_ioExceptions_areSwallowed () {
124+ Writer throwing = new Writer () {
125+ @ Override
126+ public void write (char [] cbuf , int off , int len ) throws IOException {
127+ throw new IOException ("boom" );
128+ }
129+
130+ @ Override
131+ public void flush () {
132+ }
133+
134+ @ Override
135+ public void close () {
136+ }
137+
138+ @ Override
139+ public void write (String str ) throws IOException {
140+ throw new IOException ("boom" );
141+ }
142+
143+ @ Override
144+ public void write (int c ) throws IOException {
145+ throw new IOException ("boom" );
146+ }
147+ };
148+ assertDoesNotThrow (() -> printList (Arrays .asList (1 , 2 , 3 ), throwing ));
33149 }
34150}
0 commit comments