From 23a86debdaa571099718c48610b8c47038d50f04 Mon Sep 17 00:00:00 2001 From: Sander Ploegsma Date: Fri, 27 Oct 2023 16:33:44 +0200 Subject: [PATCH] Add smoke test --- .gitignore | 3 ++ tests/hamming/.meta/config.json | 49 +++++++++++++++++ .../.meta/src/reference/java/Hamming.java | 26 +++++++++ tests/hamming/.meta/tests.toml | 30 +++++++++++ tests/hamming/.meta/version | 1 + tests/hamming/expected_analysis.json | 1 + tests/hamming/expected_tags.json | 1 + tests/hamming/src/main/java/Hamming.java | 53 +++++++++++++++++++ 8 files changed, 164 insertions(+) create mode 100644 tests/hamming/.meta/config.json create mode 100644 tests/hamming/.meta/src/reference/java/Hamming.java create mode 100644 tests/hamming/.meta/tests.toml create mode 100644 tests/hamming/.meta/version create mode 100644 tests/hamming/expected_analysis.json create mode 100644 tests/hamming/expected_tags.json create mode 100644 tests/hamming/src/main/java/Hamming.java diff --git a/.gitignore b/.gitignore index f6ae40e4..48f830ba 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ build .settings/ .project .classpath + +tests/**/*/analysis.json +tests/**/*/tags.json diff --git a/tests/hamming/.meta/config.json b/tests/hamming/.meta/config.json new file mode 100644 index 00000000..5e2dba3a --- /dev/null +++ b/tests/hamming/.meta/config.json @@ -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/" +} diff --git a/tests/hamming/.meta/src/reference/java/Hamming.java b/tests/hamming/.meta/src/reference/java/Hamming.java new file mode 100644 index 00000000..7db3984d --- /dev/null +++ b/tests/hamming/.meta/src/reference/java/Hamming.java @@ -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; + } +} diff --git a/tests/hamming/.meta/tests.toml b/tests/hamming/.meta/tests.toml new file mode 100644 index 00000000..b2f80f48 --- /dev/null +++ b/tests/hamming/.meta/tests.toml @@ -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" diff --git a/tests/hamming/.meta/version b/tests/hamming/.meta/version new file mode 100644 index 00000000..276cbf9e --- /dev/null +++ b/tests/hamming/.meta/version @@ -0,0 +1 @@ +2.3.0 diff --git a/tests/hamming/expected_analysis.json b/tests/hamming/expected_analysis.json new file mode 100644 index 00000000..f4698719 --- /dev/null +++ b/tests/hamming/expected_analysis.json @@ -0,0 +1 @@ +{"comments":["java.hamming.calculate_distance_in_constructor","java.hamming.should_use_string_is_empty"]} \ No newline at end of file diff --git a/tests/hamming/expected_tags.json b/tests/hamming/expected_tags.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/tests/hamming/expected_tags.json @@ -0,0 +1 @@ +{} diff --git a/tests/hamming/src/main/java/Hamming.java b/tests/hamming/src/main/java/Hamming.java new file mode 100644 index 00000000..a3998156 --- /dev/null +++ b/tests/hamming/src/main/java/Hamming.java @@ -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; + } + +}