Untitled

Anonymous
plain_text
03/01/2026 5:50 AM
19.3 KB
19
Indexable
import student.TestCase;
import student.testingsupport.annotations.ScoringWeight;

/**
 * Test class for the DNA String Database Project.
 * Tests the DNADB implementation through the DNA interface,
 * covering insert, remove, search, and print operations.
 *
 * @author Ishita Punna
 * @author Leguejou Awunganyi
 * @version Spring 2026
 */
public class DNAProjTest extends TestCase {
    private DNA it;


    /**
     * Sets up the tests that follow. In general, used for initialization
     */
    public void setUp() {
        it = new DNADB();
    }


    /**
     * Test output formatting
     */
    
    public void testSampleInput() {
        assertFuzzyEquals(
            "Sequence |ACGT| inserted",
            it.insert("ACGT"));
        assertFuzzyEquals(
            "Sequence |ACGT| already exists",
            it.insert("ACGT"));
        assertFuzzyEquals(
            "Sequence |ACGT| removed",
            it.remove("ACGT"));
        assertFuzzyEquals(
            "Sequence |AAAA| inserted",
            it.insert("AAAA"));
        assertFuzzyEquals(
            "Sequence |AA| inserted",
            it.insert("AA"));
        assertFuzzyEquals(
            "Sequence |ACG| does not exist",
            it.remove("ACG"));
        assertFuzzyEquals(
            "tree dump:\r\n"
            + "I\r\n"
            + "  I\r\n"
            + "    I\r\n"
            + "      AAAA\r\n"
            + "      E\r\n"
            + "      E\r\n"
            + "      E\r\n"
            + "      AA\r\n"
            + "    E\r\n"
            + "    E\r\n"
            + "    E\r\n"
            + "    E\r\n"
            + "  E\r\n"
            + "  E\r\n"
            + "  E\r\n"
            + "  E",
                it.print());
        assertFuzzyEquals(
            "tree dump with lengths:\r\n"
            + "I\r\n"
            + "  I\r\n"
            + "    I\r\n"
            + "      AAAA 4\r\n"
            + "      E\r\n"
            + "      E\r\n"
            + "      E\r\n"
            + "      AA 2\r\n"
            + "    E\r\n"
            + "    E\r\n"
            + "    E\r\n"
            + "    E\r\n"
            + "  E\r\n"
            + "  E\r\n"
            + "  E\r\n"
            + "  E",
                it.printLengths());
        assertFuzzyEquals(
            "tree dump with stats:\r\n"
            + "I\r\n"
            + "  I\r\n"
            + "    I\r\n"
            + "      AAAA A:100.00 C:0.00 G:0.00 T:0.00\r\n"
            + "      E\r\n"
            + "      E\r\n"
            + "      E\r\n"
            + "      AA A:100.00 C:0.00 G:0.00 T:0.00\r\n"
            + "    E\r\n"
            + "    E\r\n"
            + "    E\r\n"
            + "    E\r\n"
            + "  E\r\n"
            + "  E\r\n"
            + "  E\r\n"
            + "  E",
                it.printStats());
        assertFuzzyEquals(
            "AAAA\r\n"
            + "# of nodes visited: 4",
                it.search("AAAA$"));
        assertFuzzyEquals(
            "AAAA\r\n"
            + "AA\r\n"
            + "# of nodes visited: 8",
                it.search("AA"));
        assertFuzzyEquals(
            "No sequence found\r\n"
                + "# of nodes visited: 3",
                it.search("ACGT$"));
    }
    
    /**
     * Example tests for bad input error formatting
     */
    @ScoringWeight(10.0)
    public void testBadInput() {
        assertFuzzyEquals(
            "Bad input: Sequence may not be null\r\n",
            it.insert(null));
        assertFuzzyEquals(
            "Bad input: Sequence may not be empty\r\n",
            it.insert(""));
        assertFuzzyEquals(
            "Bad Input Sequence |AXA|\r\n",
            it.insert("AXA"));
    }
    
    
    /**
     * Tests that remove() correctly handles an empty string input.
     * Verifies that the proper bad input message is returned
     * when attempting to remove an empty DNA sequence.
     */
    public void testRemoveBad() {
        assertFuzzyEquals(
            "Bad input: Sequence may not be empty\r\n",
            it.remove(""));
    }


    /**
     * Tests that search() correctly handles invalid characters.
     * Ensures that a sequence containing non-DNA characters
     * returns the proper formatted bad input error message.
     */
    public void testSearchBad() {
        assertFuzzyEquals(
            "Bad Input Sequence |XYZ|\r\n",
            it.search("XYZ"));
    }


    /**
     * Tests that print() returns output containing the expected
     * tree dump header. This verifies that the method does not
     * return null and includes the required formatting string.
     */
    public void testPrint() {
        assertTrue(it.print().contains("tree dump"));
    }
    
    
    /**
     * Tests that a valid DNA sequence can be inserted successfully.
     * Verifies the correct insertion confirmation message is returned.
     */
    public void testInsertValid() {
        assertFuzzyEquals(
            "Sequence |AAAA| inserted",
            it.insert("AAAA"));
    }

    /**
     * Tests that removing a sequence not present in the tree
     * returns the correct does-not-exist message.
     */
    public void testRemoveValidNotFound() {
        assertFuzzyEquals(
            "Sequence |TTT| does not exist",
            it.remove("TTT"));
    }

    /**
     * Tests that searching for a sequence not in the tree
     * returns the not-found message with the correct node visit count.
     */
    public void testSearchValidNotFound() {
        assertFuzzyEquals(
            "No sequence found\r\n# of nodes visited: 1",
            it.search("ACGT$"));
    }

    /**
     * Tests that a long DNA sequence can be inserted successfully.
     * Verifies the tree handles sequences longer than typical inputs.
     */
    public void testLongSequence() {
        assertFuzzyEquals(
            "Sequence |ACGTACGTACGT| inserted",
            it.insert("ACGTACGTACGT"));
    }

    /**
     * Tests that printLengths() returns output containing
     * the required tree dump with lengths header.
     */
    public void testPrintLengths() {
        assertTrue(it.printLengths().contains("tree dump with lengths"));
    }

    /**
     * Tests that printStats() returns output containing
     * the required tree dump with stats header.
     */
    public void testPrintStats() {
        assertTrue(it.printStats().contains("tree dump with stats"));
    }

    /**
     * Tests multiple bad input cases for insert and search.
     * Covers sequences with spaces, dollar signs not at end,
     * and dollar signs in invalid positions.
     */
    public void testMoreBadInput() {
        assertFuzzyEquals("Bad Input Sequence |A A|\r\n", it.insert("A A"));
        assertFuzzyEquals("Bad Input Sequence |A |\r\n", it.insert("A "));
        assertFuzzyEquals("Bad Input Sequence |A$|\r\n", it.insert("A$"));
        assertFuzzyEquals("Bad input sequence |A$A|\r\n", it.search("A$A"));
    }

    /**
     * Tests that searching with an empty prefix on an empty tree
     * returns the not-found message with a visit count of 1.
     */
    public void testSearchEmptyPrefixValid() {
        assertFuzzyEquals(
            "No sequence found\r\n# of nodes visited: 1",
            it.search(""));
    }

    /**
     * Tests that inserting a duplicate sequence returns
     * the already-exists message on the second insert.
     */
    public void testInsertingDuplicate() {
        assertFuzzyEquals(
            "Sequence |AGCT| inserted",
            it.insert("AGCT"));
        assertFuzzyEquals(
            "Sequence |AGCT| already exists",
            it.insert("AGCT"));
    }

    /**
     * Tests that two different valid sequences can both be inserted
     * and each returns the correct confirmation message.
     */
    public void testValidInsert() {
        assertFuzzyEquals(
            "Sequence |AAAA| inserted",
            it.insert("AAAA"));
        assertFuzzyEquals(
            "Sequence |TTTT| inserted",
            it.insert("TTTT"));
    }

    /**
     * Tests inserting many sequences of varying lengths to stress
     * the tree structure and verify duplicate detection still works
     * after multiple insertions.
     */
    public void testEnsureCapasity() {
        String[] cap = {
            "A", "C", "G", "T",
            "AA", "CC", "GG", "TT",
            "AAA", "CCC", "GGG"
        };
        for (int i = 0; i < cap.length; i++) {
            assertFuzzyEquals(
                "Sequence |" + cap[i] + "| inserted",
                it.insert(cap[i]));
        }
        // Verify duplicate detection still works after many inserts
        assertFuzzyEquals(
            "Sequence |A| already exists",
            it.insert("A"));
    }
    
    /**
     * Tests printStats contains required header.
     */
    public void testPrintStatsHeader() {
        assertTrue(it.printStats().contains("tree dump with stats"));
    }

    /**
     * Tests a long sequence inserts correctly.
     */
    public void testLongSequenceAgain() {
        assertFuzzyEquals(
            "Sequence |ACGTACGTACGT| inserted",
            it.insert("ACGTACGTACGT"));
    }

    /**
     * Tests all four single-character sequences can be inserted.
     */
    public void testAllSingleChars() {
        assertFuzzyEquals("Sequence |A| inserted", it.insert("A"));
        assertFuzzyEquals("Sequence |C| inserted", it.insert("C"));
        assertFuzzyEquals("Sequence |G| inserted", it.insert("G"));
        assertFuzzyEquals("Sequence |T| inserted", it.insert("T"));
        assertFuzzyEquals(
            "tree dump:\r\n"
            + "I\r\n"
            + "  A\r\n"
            + "  C\r\n"
            + "  G\r\n"
            + "  T\r\n"
            + "  E",
            it.print());
    }
    
    /**
     * Tests that removing a sequence not present in the tree
     * leaves the existing leaf node unchanged.
     * Verifies the tree still contains the original sequence after
     * an unsuccessful remove attempt.
     */
    public void testLeafRemoveNoMatch() {
        it.insert("ACGT");
        // Remove something that doesn't exist - ACGT leaf stays
        it.remove("TTTT");
        assertFuzzyEquals("tree dump:\r\nACGT", it.print());
    }

    /**
     * Tests printLengths on a sequence of length 1.
     */
    public void testPrintLengthsOne() {
        it.insert("A");
        assertFuzzyEquals("tree dump with lengths:\r\nA 1", it.printLengths());
    }

    /**
     * Tests printStats on a single-char sequence.
     */
    public void testPrintStatsSingleChar() {
        it.insert("G");
        assertFuzzyEquals(
            "tree dump with stats:\r\nG A:0.00 C:0.00 G:100.00 T:0.00",
            it.printStats());
    }
    
    
    /**
     * Kills trimTrailingNewline return s path.
     * printLengths/printStats headers don't end with CRLF so the
     * non-trimming branch must be exercised via a string that won't match.
     * A single leaf print ends with the sequence, not CRLF.
     */
    public void testTrimNoNewline() {
        it.insert("ACGT");
        String result = it.print();
        assertFalse(result.endsWith("\r\n"));
    }

    /**
     * Kills validateInsertRemove null path for remove.
     */
    public void testRemoveNull() {
        assertFuzzyEquals(
            "Bad input: Sequence may not be null\r\n",
            it.remove(null));
    }

    /**
     * Kills validateInsertRemove invalid char path for insert.
     */
    public void testInsertInvalidChar() {
        assertFuzzyEquals(
            "Bad Input Sequence |AXA|\r\n",
            it.insert("AXA"));
    }
    
    
    /**
     * Kills validateSearch null check red line.
     * search(null) must return the null error message.
     */
    public void testSearchNull() {
        assertFuzzyEquals(
            "Bad input: Sequence may not be null\r\n",
            it.search(null));
    }
    
    /**
     * Kills validateSearch line 219 EQUAL_ELSE mutant.
     * Each individual DNA char must be accepted as valid in search.
     * The mutant flips one != to ==, rejecting a valid char.
     */
    public void testSearchEachValidChar() {
        it.insert("AAAA");
        it.insert("CCCC");
        it.insert("GGGG");
        it.insert("TTTT");
        // Each single char search must NOT return bad input
        assertFalse(it.search("A").contains("Bad"));
        assertFalse(it.search("C").contains("Bad"));
        assertFalse(it.search("G").contains("Bad"));
        assertFalse(it.search("T").contains("Bad"));
        // Each must actually find its sequence
        assertTrue(it.search("A").contains("AAAA"));
        assertTrue(it.search("C").contains("CCCC"));
        assertTrue(it.search("G").contains("GGGG"));
        assertTrue(it.search("T").contains("TTTT"));
    }

    /**
     * Kills validateSearch line 210 EQUAL_ELSE mutant.
     * Dollar at last position with a preceding valid char must be accepted.
     * The mutant flips the position check, rejecting valid trailing $.
     */
    public void testSearchDollarValidAtEnd() {
        it.insert("ACGT");
        // A$ is valid - $ is at last position (index 1, length-1=1)
        String result = it.search("A$");
        assertFalse(result.contains("Bad"));
        // ACGT$ is valid exact search
        assertFalse(it.search("ACGT$").contains("Bad"));
        assertTrue(it.search("ACGT$").contains("ACGT"));
    }
    
    
    /**
     * Kills validateSearch null-check EQUAL_ELSE mutation (line 210).
     * Mutant makes the null check always false, skipping it entirely.
     */
    public void testSearchNullInput() {
        String result = it.search(null);
        assertEquals("Bad input: Sequence may not be null\r\n", result);
    }

    /**
     * Kills validateSearch dollar-sign EQUAL_ELSE mutation (line 219).
     * Mutant treats '$' as not-dollar, routing it to the invalid-char branch.
     * A valid "AA$" search must succeed (return null from validateSearch).
     */
    public void testSearchValidDollarTerminator() {
        it.insert("AA");
        // If mutant is active, "AA$" fails validation instead of being valid
        String result = it.search("AA$");
        assertTrue(result.contains("AA"));
        assertFalse(result.contains("Bad input"));
    }
    
    
    

}

Editor is loading...
Leave a Comment