Skip to content

Commit

Permalink
Add smoke test
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderploegsma committed Oct 27, 2023
1 parent bec29b1 commit 23a86de
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ build
.settings/
.project
.classpath

tests/**/*/analysis.json
tests/**/*/tags.json
49 changes: 49 additions & 0 deletions tests/hamming/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"authors": [
"wdjunaidi"
],
"contributors": [
"c-thornton",
"ChristianWilkie",
"FridaTveit",
"javaeeeee",
"jmrunkle",
"jonnynabors",
"jtigger",
"kytrinyx",
"lemoncurry",
"matthewmorgan",
"michael-berger-FR",
"michaelspets",
"mirkoperillo",
"msomji",
"muzimuzhi",
"odzeno",
"sjwarner-bp",
"SleeplessByte",
"Smarticles101",
"sshine",
"stkent",
"t0dd",
"Valkryst",
"vasouv",
"Zaldrick"
],
"files": {
"solution": [
"src/main/java/Hamming.java"
],
"test": [
"src/test/java/HammingTest.java"
],
"example": [
".meta/src/reference/java/Hamming.java"
],
"invalidator": [
"build.gradle"
]
},
"blurb": "Calculate the Hamming difference between two DNA strands.",
"source": "The Calculating Point Mutations problem at Rosalind",
"source_url": "https://rosalind.info/problems/hamm/"
}
26 changes: 26 additions & 0 deletions tests/hamming/.meta/src/reference/java/Hamming.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.function.IntPredicate;
import java.util.stream.IntStream;

class Hamming {
private final int hammingDistance;

Hamming(String leftStrand, String rightStrand) {
String exceptionMessage = "leftStrand and rightStrand must be of equal length.";
if (leftStrand.length() != rightStrand.length()) {
if (leftStrand.isEmpty()) {
exceptionMessage = "left strand must not be empty.";
}
if (rightStrand.isEmpty()) {
exceptionMessage = "right strand must not be empty.";
}
throw new IllegalArgumentException(exceptionMessage);
}

IntPredicate areNotEqual = index -> leftStrand.charAt(index) != rightStrand.charAt(index);
hammingDistance = (int) IntStream.range(0, leftStrand.length()).filter(areNotEqual).count();
}

int getHammingDistance() {
return hammingDistance;
}
}
30 changes: 30 additions & 0 deletions tests/hamming/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.

[f6dcb64f-03b0-4b60-81b1-3c9dbf47e887]
description = "empty strands"

[54681314-eee2-439a-9db0-b0636c656156]
description = "single letter identical strands"

[294479a3-a4c8-478f-8d63-6209815a827b]
description = "single letter different strands"

[9aed5f34-5693-4344-9b31-40c692fb5592]
description = "long identical strands"

[cd2273a5-c576-46c8-a52b-dee251c3e6e5]
description = "long different strands"

[919f8ef0-b767-4d1b-8516-6379d07fcb28]
description = "disallow first strand longer"

[8a2d4ed0-ead5-4fdd-924d-27c4cf56e60e]
description = "disallow second strand longer"

[5dce058b-28d4-4ca7-aa64-adfe4e17784c]
description = "disallow left empty strand"

[38826d4b-16fb-4639-ac3e-ba027dec8b5f]
description = "disallow right empty strand"
1 change: 1 addition & 0 deletions tests/hamming/.meta/version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.0
1 change: 1 addition & 0 deletions tests/hamming/expected_analysis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"comments":["java.hamming.calculate_distance_in_constructor","java.hamming.should_use_string_is_empty"]}
1 change: 1 addition & 0 deletions tests/hamming/expected_tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
53 changes: 53 additions & 0 deletions tests/hamming/src/main/java/Hamming.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class Hamming {

private String leftStrand;
private String rightStrand;

Hamming(String leftStrand, String rightStrand) {

validate(leftStrand, rightStrand);
this.leftStrand = leftStrand;
this.rightStrand = rightStrand;
}


private void validate(String leftStrand, String rightStrand) {
validateNotNull(leftStrand, rightStrand);
validateNotOneEmpty(leftStrand, rightStrand);
validateSameLength(leftStrand, rightStrand);
}

private void validateNotNull(String leftStrand, String rightStrand) {
if (leftStrand == null || rightStrand == null) {
throw new IllegalArgumentException("Either left or right stand is null");
}
}

private void validateNotOneEmpty(String leftStrand, String rightStrand) {
if (leftStrand.length() == 0 && rightStrand.length() > 0) {
throw new IllegalArgumentException("left strand must not be empty.");
} else if (rightStrand.length() == 0 && leftStrand.length() > 0) {
throw new IllegalArgumentException("right strand must not be empty.");
}
}

private void validateSameLength(String leftStrand, String rightStrand) {
if (leftStrand.length() != rightStrand.length()) {
throw new IllegalArgumentException("leftStrand and rightStrand must be of equal length.");
}
}

int getHammingDistance() {

int strandLen = this.leftStrand.length();
int diffCount = 0;
for (int i = 0; i < strandLen; i++) {
if (this.leftStrand.charAt(i) != this.rightStrand.charAt(i)) {
diffCount++;
}
}

return diffCount;
}

}

0 comments on commit 23a86de

Please sign in to comment.