JavaAlgorithms
Elementary and no so elementary Java algorithms
listAlgorithms/TestListAlgorithms.java
Go to the documentation of this file.
00001 
00009 package listAlgorithms;
00010 
00011 import static org.junit.Assert.*;
00012 
00013 import org.junit.Test;
00014 
00024 public class TestListAlgorithms {
00025 
00029     @Test
00030     public void testReverse() {
00031         Character[] vals = new Character[] { 'A', 'B', 'C', 'D', 'E' };
00032         Character[] rvals = new Character[] { 'E', 'D', 'C', 'B', 'A' };
00033         ListNode<Character> list = Algorithms.buildList(vals);
00034         ListNode<Character> rlist = Algorithms.buildList(rvals);
00035         ListNode<Character> newList = Algorithms.reverse(list);
00036         assertTrue("List reverse failed", Algorithms.listEquals(rlist, newList));
00037     }
00038 
00039     @SuppressWarnings("javadoc")
00040     @Test
00041     public void testDedup() {
00042         Character[] vals = new Character[] { 'A', 'B', 'B', 'C', 'D', 'D', 'D',
00043                 'D', 'E', 'E', 'F', 'G', 'H', 'H', 'H' };
00044         Character[] dedupVals = new Character[] { 'A', 'B', 'C', 'D', 'E', 'F',
00045                 'G', 'H' };
00046         Character[] aVals = new Character[] { 'A', 'A', 'A', 'A', 'A', 'A', 'A' };
00047         ListNode<Character> check = Algorithms.buildList(dedupVals);
00048         ListNode<Character> list = Algorithms.buildList(vals);
00049         Algorithms.dedup(list);
00050         assertTrue("1. List dedup failed", Algorithms.listEquals(list, check));
00051         list = Algorithms.buildList(aVals);
00052         check = Algorithms.buildList(new Character[] { 'A' });
00053         Algorithms.dedup(list);
00054         assertTrue("2. List dedup failed", Algorithms.listEquals(list, check));
00055     }
00056 
00057     @SuppressWarnings("javadoc")
00058     @Test
00059     public void testKthElement() {
00060         Character[] vals = new Character[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G',
00061                 'H' };
00062         ListNode<Character> list = Algorithms.buildList(vals);
00063         Character elemVal = Algorithms.kthFromEnd(list, 2).getValue();
00064         assertTrue("1. should have returned F, but returned " + elemVal,
00065                 elemVal.compareTo('F') == 0);
00066         elemVal = Algorithms.kthFromEnd(list, 0).getValue();
00067         assertTrue("2. should have returned H, but returned " + elemVal,
00068                 elemVal.compareTo('H') == 0);
00069         elemVal = Algorithms.kthFromEnd(list, 7).getValue();
00070         assertTrue("3. should have returned A, but returned " + elemVal,
00071                 elemVal.compareTo('A') == 0);
00072     }
00073 }
 All Classes Namespaces Files Functions Variables