From 675e5b47b6c7164b60c7905aad97e01a262244f7 Mon Sep 17 00:00:00 2001 From: Alex Klimenko Date: Fri, 25 Jul 2025 22:46:05 +0200 Subject: [PATCH 1/2] testing: improve testing CRCAlgorithmTest --- .../others/CRCAlgorithmTest.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/src/test/java/com/thealgorithms/others/CRCAlgorithmTest.java b/src/test/java/com/thealgorithms/others/CRCAlgorithmTest.java index 542c256a3e88..c6abd575a982 100644 --- a/src/test/java/com/thealgorithms/others/CRCAlgorithmTest.java +++ b/src/test/java/com/thealgorithms/others/CRCAlgorithmTest.java @@ -79,4 +79,86 @@ void testLargeMessageSize() { assertTrue(c.getWrongMess() >= 0); assertTrue(c.getCorrectMess() >= 0); } + + @Test + void testSingleBitMessage() { + CRCAlgorithm c = new CRCAlgorithm("11", 1, 0.0); + c.generateRandomMess(); + c.divideMessageWithP(false); + c.changeMess(); + c.divideMessageWithP(true); + + assertTrue(c.getCorrectMess() >= 0, "Single bit message should be handled"); + } + + @Test + void testPolynomialLongerThanMessage() { + CRCAlgorithm c = new CRCAlgorithm("11010101", 3, 0.0); + c.generateRandomMess(); + c.divideMessageWithP(false); + c.changeMess(); + c.divideMessageWithP(true); + + // Should not crash, counters should be valid + assertTrue(c.getCorrectMess() + c.getWrongMess() >= 0); + } + + @Test + void testPolynomialWithOnlyOnes() { + CRCAlgorithm c = new CRCAlgorithm("1111", 5, 0.1); + c.generateRandomMess(); + c.divideMessageWithP(false); + c.changeMess(); + c.divideMessageWithP(true); + + assertTrue(c.getCorrectMess() + c.getWrongMess() >= 0); + } + + @Test + void testMultipleRefactorCalls() { + CRCAlgorithm c = new CRCAlgorithm("1101", 5, 0.2); + + for (int i = 0; i < 5; i++) { + c.refactor(); + c.generateRandomMess(); + c.divideMessageWithP(false); + c.changeMess(); + c.divideMessageWithP(true); + } + + // Counters should accumulate across multiple runs + assertTrue(c.getCorrectMess() + c.getWrongMess() > 0); + } + + @Test + void testCounterConsistency() { + CRCAlgorithm c = new CRCAlgorithm("1101", 10, 0.3); + + for (int i = 0; i < 100; i++) { + c.refactor(); + c.generateRandomMess(); + c.divideMessageWithP(false); + c.changeMess(); + c.divideMessageWithP(true); + } + + // Total messages processed should equal correct + wrong + int totalProcessed = c.getCorrectMess() + c.getWrongMess(); + assertEquals(100, totalProcessed, "Total processed messages should equal iterations"); + + // Wrong messages should equal caught + not caught + assertEquals(c.getWrongMess(), c.getWrongMessCaught() + c.getWrongMessNotCaught(), "Wrong messages should equal sum of caught and not caught"); + } + + @Test + void testGetterMethodsInitialState() { + CRCAlgorithm c = new CRCAlgorithm("1101", 10, 0.1); + + // Check initial state + assertEquals(0, c.getCorrectMess(), "Initial correct messages should be 0"); + assertEquals(0, c.getWrongMess(), "Initial wrong messages should be 0"); + assertEquals(0, c.getWrongMessCaught(), "Initial caught wrong messages should be 0"); + assertEquals(0, c.getWrongMessNotCaught(), "Initial not caught wrong messages should be 0"); + } + } From 97925cf3ff58de04965573882439447ac18dd3ec Mon Sep 17 00:00:00 2001 From: Alex Klimenko Date: Fri, 25 Jul 2025 22:49:04 +0200 Subject: [PATCH 2/2] style: formatting fix --- src/test/java/com/thealgorithms/others/CRCAlgorithmTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/others/CRCAlgorithmTest.java b/src/test/java/com/thealgorithms/others/CRCAlgorithmTest.java index c6abd575a982..3dc61f2c6569 100644 --- a/src/test/java/com/thealgorithms/others/CRCAlgorithmTest.java +++ b/src/test/java/com/thealgorithms/others/CRCAlgorithmTest.java @@ -160,5 +160,4 @@ void testGetterMethodsInitialState() { assertEquals(0, c.getWrongMessCaught(), "Initial caught wrong messages should be 0"); assertEquals(0, c.getWrongMessNotCaught(), "Initial not caught wrong messages should be 0"); } - } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy