From 24ad514c49cc99a6fb235df26e726902efe942c1 Mon Sep 17 00:00:00 2001 From: Maneeshka Thakur Date: Sun, 15 Nov 2026 07:27:46 -0700 Subject: [PATCH 1/2] added Checksum algo and unit tests --- .../com/thealgorithms/maths/Checksum.java | 29 +++++++++++ .../java/com/thealgorithms/ChecksumTest.java | 48 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/main/java/com/thealgorithms/maths/Checksum.java create mode 100644 src/test/java/com/thealgorithms/ChecksumTest.java diff --git a/src/main/java/com/thealgorithms/maths/Checksum.java b/src/main/java/com/thealgorithms/maths/Checksum.java new file mode 100644 index 000000000000..358065608efc --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/Checksum.java @@ -0,0 +1,29 @@ +package com.thealgorithms.maths; + +/** + * A simple Checksum implementation for data validation. + * Calculates an 8-bit checksum of a given byte array. + */ +public final class Checksum { + + private Checksum() { + // Private constructor to prevent instantiation + } + + /** + * Calculates an 8-bit checksum for the specified input data. + * + * @param data the byte array to checksum + * @return the 8-bit checksum value as an integer + */ + public static int calculateChecksum(byte[] data) { + if (data == null) { + throw new IllegalArgumentException("Input data cannot be null"); + } + int checksum = 0; + for (byte b : data) { + checksum = (checksum + b) & 0xFF; + } + return checksum; + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/ChecksumTest.java b/src/test/java/com/thealgorithms/ChecksumTest.java new file mode 100644 index 000000000000..e2902be3419e --- /dev/null +++ b/src/test/java/com/thealgorithms/ChecksumTest.java @@ -0,0 +1,48 @@ +package com.thealgorithms; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.Test; + +import com.thealgorithms.maths.Checksum; + +public class ChecksumTest { + + @Test + public void testCalculateChecksumNormal() { + byte[] data = {1, 2, 3, 4, 5}; + int result = Checksum.calculateChecksum(data); + assertThat(result).isEqualTo(15); + } + + @Test + public void testCalculateChecksumEmpty() { + byte[] data = {}; + int result = Checksum.calculateChecksum(data); + assertThat(result).isEqualTo(0); + } + + @Test + public void testCalculateChecksumNull() { + assertThatThrownBy(() -> Checksum.calculateChecksum(null)) + .isInstanceOf(IllegalArgumentException.class); + } + + + + @Test + public void testCalculateChecksumSingleByte() { + byte[] data = {100}; + int result = Checksum.calculateChecksum(data); + assertThat(result).isEqualTo(100); + } + + @Test + public void testCalculateChecksumMaxByteValue() { + // Tests handling of high byte values close to 255 + byte[] data = {(byte) 255}; + int result = Checksum.calculateChecksum(data); + assertThat(result).isEqualTo(255); + } +} \ No newline at end of file From 1cc4b66be831cdddb697c13811339fcde0366a90 Mon Sep 17 00:00:00 2001 From: Maneeshka Thakur Date: Sun, 15 Nov 2026 07:38:31 -0700 Subject: [PATCH 2/2] added Checksum algo and unit tests --- .../java/com/thealgorithms/maths/Checksum.java | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Checksum.java b/src/main/java/com/thealgorithms/maths/Checksum.java index 358065608efc..9964aa9caec7 100644 --- a/src/main/java/com/thealgorithms/maths/Checksum.java +++ b/src/main/java/com/thealgorithms/maths/Checksum.java @@ -1,21 +1,15 @@ package com.thealgorithms.maths; /** - * A simple Checksum implementation for data validation. - * Calculates an 8-bit checksum of a given byte array. + * Checksum implementation for data validation. + * Should calculate an 8 bit checksum of a given byte array */ public final class Checksum { - private Checksum() { - // Private constructor to prevent instantiation + private Checksum() { //priv constructor } - /** - * Calculates an 8-bit checksum for the specified input data. - * - * @param data the byte array to checksum - * @return the 8-bit checksum value as an integer - */ +//Calc an 8 bit checksum for the specified input data. public static int calculateChecksum(byte[] data) { if (data == null) { throw new IllegalArgumentException("Input data cannot be null");