1+ import java .io .BufferedReader ;
2+ import java .io .File ;
3+ import java .io .FileReader ;
4+ import java .io .IOException ;
5+
6+ import org .junit .Assert ;
7+ import org .junit .Test ;
8+
9+ /**
10+ * @author medany
11+ */
12+
13+ public class ContactsTest {
14+
15+ private Contacts alg = new Contacts ();
16+ Integer [] actual = null , expected = null ;
17+
18+ @ Test
19+ public void Test_1 () {
20+
21+ actual = alg .solve (new String [] { "add hack" , "add hackerrank" , "find hac" , "find hak" }, 2 );
22+ expected = new Integer [] { 2 , 0 };
23+
24+ Assert .assertArrayEquals (expected , actual );
25+
26+ }
27+
28+ @ Test
29+ public void Test_2 () {
30+
31+ BufferedReader ir = null , or = null ;
32+ FileReader i = null , o = null ;
33+
34+ try {
35+ i = new FileReader (new File (this .getClass ().getClassLoader ().getResource ("contacts_input" ).getPath ()));
36+ o = new FileReader (new File (this .getClass ().getClassLoader ().getResource ("contacts_output" ).getPath ()));
37+ ir = new BufferedReader (i );
38+ or = new BufferedReader (o );
39+ int n = 0 ;
40+ String [] lines ;
41+ String line = ir .readLine ();
42+
43+ n = Integer .parseInt (line );
44+ lines = new String [n ];
45+
46+ int l = 0 , f = 0 ;
47+ while ((line = ir .readLine ()) != null ) {
48+ lines [l ] = line ;
49+ if (line .split (" " )[0 ].equals ("find" ))
50+ f ++;
51+ l ++;
52+ }
53+
54+ expected = new Integer [f ];
55+ l = 0 ;
56+ while ((line = or .readLine ()) != null ) {
57+ expected [l ] = Integer .parseInt (line );
58+ l ++;
59+ }
60+
61+ actual = alg .solve (lines , f );
62+ Assert .assertArrayEquals (expected , actual );
63+
64+ } catch (IOException e ) {
65+ e .printStackTrace ();
66+ } finally {
67+ try {
68+ if (ir != null )
69+ ir .close ();
70+ if (or != null )
71+ or .close ();
72+ if (i != null )
73+ i .close ();
74+ if (o != null )
75+ o .close ();
76+ } catch (IOException ex ) {
77+ ex .printStackTrace ();
78+ }
79+ }
80+
81+ }
82+
83+ @ Test
84+ public void Test_3 () {
85+
86+ actual = alg .solve (new String [] { "add s" , "add ss" , "add sss" , "add ssss" , "add sssss" , "find s" , "find ss" ,
87+ "find sss" , "find ssss" , "find sssss" , "find ssssss" }, 6 );
88+ expected = new Integer [] { 5 , 4 , 3 , 2 , 1 , 0 };
89+
90+ Assert .assertArrayEquals (expected , actual );
91+
92+ }
93+
94+ }
0 commit comments