Просмотр исходного кода

Added WordCountTest.java and a sample example.java for word-count assignment

Sagar Sane 13 лет назад
Родитель
Сommit
253fd3df57
2 измененных файлов: 118 добавлений и 0 удалений
  1. 99
    0
      assignments/java/word-count/WordCountTest.java
  2. 19
    0
      assignments/java/word-count/example.java

+ 99
- 0
assignments/java/word-count/WordCountTest.java Просмотреть файл

1
+import org.junit.Test;
2
+
3
+import java.lang.Integer;
4
+import java.lang.String;
5
+import java.util.HashMap;
6
+import java.util.Map;
7
+
8
+import static org.junit.Assert.*;
9
+
10
+public class WordCountTest {
11
+
12
+    private final WordCount wordCount = new WordCount();
13
+
14
+    @Test
15
+    public void countOneWord() {
16
+        Map<String, Integer> actualWordCount = new HashMap<String, Integer>();
17
+        final Map<String, Integer> expectedWordCount = new HashMap<String, Integer>();
18
+        expectedWordCount.put("word", 1);
19
+
20
+        actualWordCount = wordCount.Phrase("word");
21
+        assertEquals(
22
+            expectedWordCount, actualWordCount
23
+        );
24
+    }
25
+
26
+    @Test
27
+    public void countOneOfEach() {
28
+        Map<String, Integer> actualWordCount = new HashMap<String, Integer>();
29
+        final Map<String, Integer> expectedWordCount = new HashMap<String, Integer>();
30
+        expectedWordCount.put("one", 1);
31
+        expectedWordCount.put("of", 1);
32
+        expectedWordCount.put("each", 1);
33
+
34
+        actualWordCount = wordCount.Phrase("one of each");
35
+        assertEquals(
36
+            expectedWordCount, actualWordCount
37
+        );
38
+    }
39
+
40
+    @Test
41
+    public void countMultipleOccurences() {
42
+        Map<String, Integer> actualWordCount = new HashMap<String, Integer>();
43
+        final Map<String, Integer> expectedWordCount = new HashMap<String, Integer>();
44
+        expectedWordCount.put("one", 1);
45
+        expectedWordCount.put("fish", 4);
46
+        expectedWordCount.put("two", 1);
47
+        expectedWordCount.put("red", 1);
48
+        expectedWordCount.put("blue", 1);
49
+
50
+        actualWordCount = wordCount.Phrase("one fish two fish red fish blue fish");
51
+        assertEquals(
52
+            expectedWordCount, actualWordCount
53
+        );
54
+    }
55
+
56
+    @Test
57
+    public void ignorePunctuation() {
58
+        Map<String, Integer> actualWordCount = new HashMap<String, Integer>();
59
+        final Map<String, Integer> expectedWordCount = new HashMap<String, Integer>();
60
+        expectedWordCount.put("car", 1);
61
+        expectedWordCount.put("carpet", 1);
62
+        expectedWordCount.put("as", 1);
63
+        expectedWordCount.put("java", 1);
64
+        expectedWordCount.put("javascript", 1);
65
+
66
+        actualWordCount = wordCount.Phrase("car : carpet as java : javascript!!&@$%^&");
67
+        assertEquals(
68
+            expectedWordCount, actualWordCount
69
+        );
70
+
71
+    }
72
+
73
+    @Test
74
+    public void includeNumbers() {
75
+        Map<String, Integer> actualWordCount = new HashMap<String, Integer>();
76
+        final Map<String, Integer> expectedWordCount = new HashMap<String, Integer>();
77
+        expectedWordCount.put("testing", 2);
78
+        expectedWordCount.put("1", 1);
79
+        expectedWordCount.put("2", 1);
80
+
81
+        actualWordCount = wordCount.Phrase("testing, 1, 2 testing");
82
+        assertEquals(
83
+            expectedWordCount, actualWordCount
84
+        );
85
+    }
86
+
87
+    @Test
88
+    public void normalizeCase() {
89
+        Map<String, Integer> actualWordCount = new HashMap<String, Integer>();
90
+        final Map<String, Integer> expectedWordCount = new HashMap<String, Integer>();
91
+        expectedWordCount.put("go", 3);
92
+
93
+        actualWordCount = wordCount.Phrase("go Go GO");
94
+        assertEquals(
95
+            expectedWordCount, actualWordCount
96
+        );
97
+    }
98
+
99
+}

+ 19
- 0
assignments/java/word-count/example.java Просмотреть файл

1
+import java.lang.Integer;
2
+import java.lang.String;
3
+import java.util.HashMap;
4
+import java.util.Map;
5
+
6
+public class WordCount {
7
+
8
+    public Map<String, Integer> Phrase( String input ) {
9
+        Map<String, Integer> countMap = new HashMap<String, Integer>();
10
+        input = input.trim().toLowerCase().replaceAll("[\\W]", " ");
11
+        final String[] tokenizedInput = input.split("\\s+");
12
+        for( String aWord : tokenizedInput ) {
13
+            Integer count = countMap.get(aWord);
14
+            countMap.put(aWord, count == null ? 1 : count + 1 );
15
+        }
16
+        return countMap;
17
+    }
18
+
19
+}