Skip to content

Commit cf6ea2f

Browse files
Merge pull request exercism#1629 from lemoncurry/hamming-updatetest
hamming: update to v2.3.0, change reference implementation
2 parents 9e5914a + 3e8ccf2 commit cf6ea2f

3 files changed

Lines changed: 30 additions & 53 deletions

File tree

‎exercises/hamming/.meta/src/reference/java/Hamming.java‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ int getHammingDistance() {
1010
}
1111

1212
private int computeHammingDistance(String leftStrand, String rightStrand) {
13-
if (leftStrand.length() != rightStrand.length()) {
13+
if (leftStrand.length() == 0 && rightStrand.length() == 0) {
14+
return 0;
15+
} else if (leftStrand.length() == 0) {
16+
throw new IllegalArgumentException("left strand must not be empty.");
17+
} else if (rightStrand.length() == 0) {
18+
throw new IllegalArgumentException("right strand must not be empty.");
19+
} else if (leftStrand.length() != rightStrand.length()) {
1420
throw new IllegalArgumentException("leftStrand and rightStrand must be of equal length.");
1521
}
16-
final int length = Math.min(leftStrand.length(), rightStrand.length());
22+
23+
int length = leftStrand.length();
1724
int distance = 0;
1825
for (int i = 0; i < length; i++) {
1926
distance += hammingContributionAt(i, leftStrand, rightStrand);

‎exercises/hamming/.meta/version‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.0
1+
2.3.0

‎exercises/hamming/src/test/java/HammingTest.java‎

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,86 +24,56 @@ public void testNoDistanceBetweenShortIdenticalStrands() {
2424

2525
@Ignore("Remove to run test")
2626
@Test
27-
public void testNoDistanceBetweenLongIdenticalStrands() {
28-
assertEquals(0, new Hamming("GGACTGA", "GGACTGA").getHammingDistance());
27+
public void testCompleteDistanceInSingleLetterDifferentStrands() {
28+
assertEquals(1, new Hamming("G", "T").getHammingDistance());
2929
}
3030

3131
@Ignore("Remove to run test")
3232
@Test
33-
public void testCompleteDistanceInSingleNucleotideStrand() {
34-
assertEquals(1, new Hamming("A", "G").getHammingDistance());
33+
public void testDistanceInLongIdenticalStrands() {
34+
assertEquals(0, new Hamming("GGACTGAAATCTG", "GGACTGAAATCTG").getHammingDistance());
3535
}
3636

3737
@Ignore("Remove to run test")
3838
@Test
39-
public void testCompleteDistanceInSmallStrand() {
40-
assertEquals(2, new Hamming("AG", "CT").getHammingDistance());
41-
}
42-
43-
@Ignore("Remove to run test")
44-
@Test
45-
public void testSmallDistanceInSmallStrand() {
46-
assertEquals(1, new Hamming("AT", "CT").getHammingDistance());
47-
}
48-
49-
@Ignore("Remove to run test")
50-
@Test
51-
public void testSmallDistanceInMediumStrand() {
52-
assertEquals(1, new Hamming("GGACG", "GGTCG").getHammingDistance());
53-
}
54-
55-
@Ignore("Remove to run test")
56-
@Test
57-
public void testSmallDistanceInLongStrand() {
58-
assertEquals(2, new Hamming("ACCAGGG", "ACTATGG").getHammingDistance());
59-
}
60-
61-
@Ignore("Remove to run test")
62-
@Test
63-
public void testNonUniqueCharacterInFirstStrand() {
64-
assertEquals(1, new Hamming("AAG", "AAA").getHammingDistance());
39+
public void testDistanceInLongDifferentStrands() {
40+
assertEquals(9, new Hamming("GGACGGATTCTG", "AGGACGGATTCT").getHammingDistance());
6541
}
6642

6743
@Ignore("Remove to run test")
6844
@Test
69-
public void testNonUniqueCharacterInSecondStrand() {
70-
assertEquals(1, new Hamming("AAA", "AAG").getHammingDistance());
71-
}
45+
public void testValidatesFirstStrandNotLonger() {
46+
expectedException.expect(IllegalArgumentException.class);
47+
expectedException.expectMessage("leftStrand and rightStrand must be of equal length.");
7248

73-
@Ignore("Remove to run test")
74-
@Test
75-
public void testSameNucleotidesInDifferentPositions() {
76-
assertEquals(2, new Hamming("TAG", "GAT").getHammingDistance());
49+
new Hamming("AATG", "AAA");
7750
}
7851

7952
@Ignore("Remove to run test")
8053
@Test
81-
public void testLargeDistanceInPermutedStrand() {
82-
assertEquals(4, new Hamming("GATACA", "GCATAA").getHammingDistance());
83-
}
54+
public void testValidatesSecondStrandNotLonger() {
55+
expectedException.expect(IllegalArgumentException.class);
56+
expectedException.expectMessage("leftStrand and rightStrand must be of equal length.");
8457

85-
@Ignore("Remove to run test")
86-
@Test
87-
public void testLargeDistanceInOffByOneStrand() {
88-
assertEquals(9, new Hamming("GGACGGATTCTG", "AGGACGGATTCT").getHammingDistance());
58+
new Hamming("ATA", "AGTG");
8959
}
9060

9161
@Ignore("Remove to run test")
9262
@Test
93-
public void testValidatesFirstStrandNotLonger() {
63+
public void testDisallowLeftEmptyStrand() {
9464
expectedException.expect(IllegalArgumentException.class);
95-
expectedException.expectMessage("leftStrand and rightStrand must be of equal length.");
65+
expectedException.expectMessage("left strand must not be empty");
9666

97-
new Hamming("AATG", "AAA");
67+
new Hamming("", "G");
9868
}
9969

10070
@Ignore("Remove to run test")
10171
@Test
102-
public void testValidatesSecondStrandNotLonger() {
72+
public void testDisallowRightEmptyStrand() {
10373
expectedException.expect(IllegalArgumentException.class);
104-
expectedException.expectMessage("leftStrand and rightStrand must be of equal length.");
74+
expectedException.expectMessage("right strand must not be empty.");
10575

106-
new Hamming("ATA", "AGTG");
76+
new Hamming("G", "");
10777
}
10878

10979
}

0 commit comments

Comments
 (0)