Skip to content

Commit 6f48b39

Browse files
committed
Got the basic Java histogram builder working, with some simple tests.
1 parent 3d9fedd commit 6f48b39

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

.classpath

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="src" path="test"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
7+
<classpathentry kind="output" path="bin"/>
8+
</classpath>

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>parallel_histogram</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

src/histogram/HistogramBuilder.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package histogram;
2+
3+
public class HistogramBuilder {
4+
5+
public int[] buildHistogram(String string) {
6+
int[] result = new int[128];
7+
8+
for (char c : string.toCharArray()) {
9+
++result[c];
10+
}
11+
12+
return result;
13+
}
14+
15+
}

test/histogram/HistogramTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package histogram;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class HistogramTest {
8+
@Test
9+
public void testSmallString() {
10+
HistogramBuilder histogramBuilder = new HistogramBuilder();
11+
int[] histogram = histogramBuilder.buildHistogram("Morris");
12+
assertEquals(1, histogram['M']);
13+
assertEquals(1, histogram['o']);
14+
assertEquals(2, histogram['r']);
15+
assertEquals(1, histogram['i']);
16+
assertEquals(1, histogram['s']);
17+
for (int i=0; i<128; ++i) {
18+
if (i != 'M' && i != 'o' && i != 'r' && i != 'i' && i != 's') {
19+
assertEquals("Count for '" + ((char) i) + "' wasn't zero as expected.", 0, histogram[i]);
20+
}
21+
}
22+
}
23+
24+
@Test
25+
public void testAlphabet() {
26+
HistogramBuilder histogramBuilder = new HistogramBuilder();
27+
String charsToCount = "";
28+
for (char c = 'a'; c <= 'z'; ++c) {
29+
charsToCount += c;
30+
}
31+
for (char c = 'A'; c <= 'Z'; ++c) {
32+
charsToCount += c;
33+
}
34+
int[] histogram = histogramBuilder.buildHistogram(charsToCount);
35+
for (char c = 'a'; c <= 'z'; ++c) {
36+
assertEquals(1, histogram[c]);
37+
}
38+
for (char c = 'A'; c <= 'Z'; ++c) {
39+
assertEquals(1, histogram[c]);
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)