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 L = new List(); L.addToFront( "d" ); L.addToFront( "c" ); L.addToFront( "b" ); List SafeL = L.addToFrontSafe( "a" ); L.addToFront( "a" ); // now, we check that the lists are the same structurally, assertEquals( L, SafeL ); // next, we change one of them... L.add( "e" ); // now, we make sure they're different: assertFalse( L.equals(SafeL) ); // change the other one... SafeL.add( "e" ); // and make sure they're the same (structurally) assertEquals( L, SafeL ); } /** * 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 L = new List(); L.addToFront( "d" ); L.addToFront( "c" ); L.addToFront( "b" ); List L2 = new List(); L2.addToFront( "f" ); L2.addToFront( "e" ); List SafeL = L.appendSafe( L2 ); // make sure it's a different memory address assertTrue( SafeL != L ); // go ahead and append L and L2 in place... L.appendInPlace(L2); // now, we check that the lists are the same structurally, assertEquals( L, SafeL ); // next, we change one of them... L.add( "g" ); // now, we make sure they're different structurally assertFalse( L.equals(SafeL) ); // change the other one... SafeL.add( "g" ); // and make sure they're the same (structurally) assertEquals( L, SafeL ); } /** * test for the safe version (returning new data) of * the reverse method (this one is named reverseSafe) */ @Test public void test_reverseSafe() { List L = new List(); L.addToFront( "d" ); L.addToFront( "c" ); L.addToFront( "b" ); L.addToFront( "a" ); List SafeL = L.reverseSafe(); // make sure it's a different memory address assertTrue( SafeL != L ); // go ahead and reverse L in place: L.reverse(); // now, we check that the lists are the same structurally, assertEquals( L, SafeL ); // next, we change one of them... L.add( "e" ); // now, we make sure they're different structurally assertFalse( L.equals(SafeL) ); // change the other one... SafeL.add( "e" ); // and make sure they're the same (structurally) assertEquals( L, SafeL ); } /** * 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 L = new List(); L.addToFront( "e" ); L.addToFront( "d" ); L.addToFront( "c" ); L.addToFront( "b" ); L.addToFront( "a" ); List[] LA = L.splitSafe(); // make sure it's a different memory address assertTrue( LA.length == 2 ); assertTrue( LA[0] != null ); assertTrue( LA[1] != null ); // now, we split L List L2 = L.split(); // make sure that the result of splitSafe contains different lists... assertTrue( LA[0] != L ); assertTrue( LA[1] != L2 ); // now, we check that the lists are the same structurally, assertEquals( L, LA[0] ); assertEquals( L2, LA[1] ); // next, we change one of them... L.add( "d2" ); // now, we make sure they're different structurally assertFalse( LA[0].equals(L) ); // change the other one... LA[0].add( "d2" ); // and make sure they're the same (structurally) assertEquals( L, LA[0] ); } /** * test for the safe version (returning new data) of * the merge method (this one is named mergeSafe) */ @Test public void test_mergeSafe() { List L = new List(); L.addToFront( "e" ); L.addToFront( "d" ); L.addToFront( "a" ); List L2 = new List(); L2.addToFront("f"); L2.addToFront("c"); L2.addToFront("b"); List Lmerge = L.mergeSafe( L2 ); // make sure it's a different memory address from both... assertTrue( Lmerge.size() == 6 ); assertTrue( Lmerge != L ); assertTrue( Lmerge != L2 ); // now, we merge L2 into L L.merge(L2); // now, we check that the lists are the same structurally, assertEquals( L, Lmerge ); // next, we change one of them... L.add( "g" ); // now, we make sure they're different structurally assertFalse( Lmerge.equals(L) ); // change the other one... Lmerge.add( "g" ); // and make sure they're the same (structurally) assertEquals( L, Lmerge ); } /** * test for the safe version (returning new data) of * the mergesort method (this one is named mergesortSafe) */ @Test public void test_mergesortSafe() { List L = new List(); L.addToFront( "e" ); L.addToFront( "d" ); L.addToFront( "a" ); L.addToFront("f"); L.addToFront("c"); L.addToFront("b"); List Lsorted = L.mergesortSafe( ); // actually sort L in place, as well... L.mergeSort(); // make sure it's a different memory address from both... assertTrue( Lsorted.size() == 6 ); assertTrue( Lsorted != L ); // now, we check that the lists are the same structurally, assertEquals( L, Lsorted ); // next, we change one of them... L.add( "g" ); // now, we make sure they're different structurally assertFalse( Lsorted.equals(L) ); // change the other one... Lsorted.add( "g" ); // and make sure they're the same (structurally) assertEquals( L, Lsorted ); } }