import static org.junit.Assert.*; import org.junit.Test; public class ListTestSafe { /** * * test for the safe version (returning new data) of * * the addToFront method (this one is named addtoFrontSafe) */ @Test public void test_addToFrontSafe() { List regularList = new List(); regularList.addToFront("d"); regularList.addToFront("c"); regularList.addToFront("b"); List safeList = regularList.addToFrontSafe("a"); regularList.addToFront("a"); // now, we check that the lists are the same structurally, assertEquals(regularList, safeList); // next, we change one of them... regularList.add("e"); // now, we make sure they're different: assertFalse(regularList.equals(safeList)); // change the other one... safeList.add("e"); // and make sure they're the same (structurally) assertEquals(regularList, safeList); } /** * * test for the safe version (returning new data) of * * the add method (this one is named addSafe) * * * * Note - we won't test the version that takes in a ListNode, * * though you may want to do so... */ @Test public void test_addSafe() { List L = new List(); L.addToFront("d"); L.addToFront("c"); L.addToFront("b"); List SafeL = L.addSafe("e"); L.add("e"); // now, we check that the lists are the same structurally, assertEquals(L, SafeL); // next, we change one of them... L.add("f"); // now, we make sure they're different: assertFalse(L.equals(SafeL)); // change the other one... SafeL.add("f"); // and make sure they're the same (structurally) assertEquals(L, SafeL); } /** * * test for the safe version (returning new data) of * * the appendInPlace method (this one is named appendSafe) */ @Test public void test_appendSafe() { List myList1 = new List(); myList1.addToFront("d"); myList1.addToFront("c"); myList1.addToFront("b"); List myList2 = new List(); myList2.addToFront("f"); myList2.addToFront("e"); List safeList = myList1.appendSafe(myList2); // make sure it's a different memory address assertTrue(safeList != myList1); // go ahead and append L and L2 in place... myList1.appendInPlace(myList2); // now, we check that the lists are the same structurally, assertEquals(myList1, safeList); // next, we change one of them... myList1.add("g"); // now, we make sure they're different structurally assertFalse(myList1.equals(safeList)); // change the other one... safeList.add("g"); // and make sure they're the same (structurally) assertEquals(myList1, safeList); } /** * * test for the safe version (returning new data) of * * the reverse method (this one is named reverseSafe) */ @Test public void test_reverseSafe() { List regularList = new List(); regularList.addToFront("d"); regularList.addToFront("c"); regularList.addToFront("b"); regularList.addToFront("a"); List safeList = regularList.reverseSafe(); // make sure it's a different memory address assertTrue(safeList != regularList); // go ahead and reverse L in place: regularList.reverse(); // now, we check that the lists are the same structurally, assertEquals(regularList, safeList); // next, we change one of them... regularList.add("e"); // now, we make sure they're different structurally assertFalse(regularList.equals(safeList)); // change the other one... safeList.add("e"); // and make sure they're the same (structurally) assertEquals(regularList, safeList); } /** * * test for the safe version (returning new data) of * * the split method (this one is named splitSafe) * * This one returns an array, LA, of Lists: * * LA[0] is the first half * * LA[1] is the second half */ @Test public void test_splitSafe() { List myList1 = new List(); myList1.addToFront("e"); myList1.addToFront("d"); myList1.addToFront("c"); myList1.addToFront("b"); myList1.addToFront("a"); List[] myListA = myList1.splitSafe(); // make sure it's a different memory address assertTrue(myListA.length == 2); assertTrue(myListA[0] != null); assertTrue(myListA[1] != null); // now, we split L List myList2 = myList1.split(); // make sure that the result of splitSafe contains different lists... assertTrue(myListA[0] != myList1); assertTrue(myListA[1] != myList2); // now, we check that the lists are the same structurally, assertEquals(myList1, myListA[0]); assertEquals(myList2, myListA[1]); // next, we change one of them... myList1.add("d2"); // now, we make sure they're different structurally assertFalse(myListA[0].equals(myList1)); // change the other one... myListA[0].add("d2"); // and make sure they're the same (structurally) assertEquals(myList1, myListA[0]); } /** * * test for the safe version (returning new data) of * * the merge method (this one is named mergeSafe) */ @Test public void test_mergeSafe() { List myList1 = new List(); myList1.addToFront("e"); myList1.addToFront("d"); myList1.addToFront("a"); List myList2 = new List(); myList2.addToFront("f"); myList2.addToFront("c"); myList2.addToFront("b"); List Lmerge = myList1.mergeSafe(myList2); // make sure it's a different memory address from both... assertTrue(Lmerge.size() == 6); assertTrue(Lmerge != myList1); assertTrue(Lmerge != myList2); // now, we merge L2 into L myList1.merge(myList2); // now, we check that the lists are the same structurally, assertEquals(myList1, Lmerge); // next, we change one of them... myList1.add("g"); // now, we make sure they're different structurally assertFalse(Lmerge.equals(myList1)); // change the other one... Lmerge.add("g"); // and make sure they're the same (structurally) assertEquals(myList1, Lmerge); } /** * * test for the safe version (returning new data) of * * the mergesort method (this one is named mergesortSafe) */ @Test public void test_mergesortSafe() { List myList = new List(); myList.addToFront("e"); myList.addToFront("d"); myList.addToFront("a"); myList.addToFront("f"); myList.addToFront("c"); myList.addToFront("b"); List myListSorted = myList.mergesortSafe(); // actually sort L in place, as well... myList.mergeSort(); // make sure it's a different memory address from both... assertTrue(myListSorted.size() == 6); assertTrue(myListSorted != myList); // now, we check that the lists are the same structurally, assertEquals(myList, myListSorted); // next, we change one of them... myList.add("g"); // now, we make sure they're different structurally assertFalse(myListSorted.equals(myList)); // change the other one... myListSorted.add("g"); // and make sure they're the same (structurally) assertEquals(myList, myListSorted); } }