Skip to content

testing: improving CRCAlgorithmTest #6403

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
71 changes: 62 additions & 9 deletions src/test/java/com/thealgorithms/others/CRCAlgorithmTest.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,82 @@

package com.thealgorithms.others;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class CRCAlgorithmTest {

@Test
void test1() {
void testNoErrorsWithZeroBER() {
CRCAlgorithm c = new CRCAlgorithm("10010101010100101010010000001010010101010", 10, 0.0);

// A bit-error rate of 0.0 should not provide any wrong messages
c.changeMess();
c.generateRandomMess();
c.divideMessageWithP(false);
assertEquals(c.getWrongMess(), 0);
c.changeMess();
c.divideMessageWithP(true);
assertEquals(0, c.getWrongMess(), "BER=0 should produce no wrong messages");
assertEquals(0, c.getWrongMessCaught(), "No errors, so no caught wrong messages");
assertEquals(0, c.getWrongMessNotCaught(), "No errors, so no uncaught wrong messages");
assertTrue(c.getCorrectMess() > 0, "Should have some correct messages");
}

@Test
void test2() {
void testAllErrorsWithBEROne() {
CRCAlgorithm c = new CRCAlgorithm("10010101010100101010010000001010010101010", 10, 1.0);
c.generateRandomMess();
c.divideMessageWithP(false);
c.changeMess();
c.divideMessageWithP(true);
assertTrue(c.getWrongMess() > 0, "BER=1 should produce wrong messages");
assertEquals(0, c.getCorrectMess(), "BER=1 should produce no correct messages");
}

// A bit error rate of 1.0 should not provide any correct messages
@Test
void testIntermediateBER() {
CRCAlgorithm c = new CRCAlgorithm("1101", 4, 0.5);
c.generateRandomMess();
for (int i = 0; i < 1000; i++) {
c.refactor();
c.generateRandomMess();
c.divideMessageWithP(false);
c.changeMess();
c.divideMessageWithP(true);
}
assertTrue(c.getWrongMess() > 0, "Some wrong messages expected with BER=0.5");
assertTrue(c.getWrongMessCaught() >= 0, "Wrong messages caught counter >= 0");
assertTrue(c.getWrongMessNotCaught() >= 0, "Wrong messages not caught counter >= 0");
assertTrue(c.getCorrectMess() >= 0, "Correct messages counter >= 0");
assertEquals(c.getWrongMess(), c.getWrongMessCaught() + c.getWrongMessNotCaught(), "Sum of caught and not caught wrong messages should equal total wrong messages");
}

@Test
void testMessageChangedFlag() {
CRCAlgorithm c = new CRCAlgorithm("1010", 4, 1.0);
c.generateRandomMess();
c.divideMessageWithP(false);
c.changeMess();
assertTrue(c.getWrongMess() > 0, "Message should be marked as changed with BER=1");
}

@Test
void testSmallMessageSize() {
CRCAlgorithm c = new CRCAlgorithm("11", 2, 0.0);
c.generateRandomMess();
c.divideMessageWithP(false);
c.changeMess();
c.divideMessageWithP(true);
assertEquals(0, c.getWrongMess(), "No errors expected for BER=0 with small message");
}

@Test
void testLargeMessageSize() {
CRCAlgorithm c = new CRCAlgorithm("1101", 1000, 0.01);
c.generateRandomMess();
c.divideMessageWithP(false);
assertEquals(c.getCorrectMess(), 0);
c.changeMess();
c.divideMessageWithP(true);
// Just ensure counters are updated, no exceptions
assertTrue(c.getWrongMess() >= 0);
assertTrue(c.getCorrectMess() >= 0);
}
}
Loading
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