Skip to content

testing: improve testing CRCAlgorithmTest #6444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/test/java/com/thealgorithms/others/CRCAlgorithmTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,85 @@ 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");
}
}
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