JavaAlgorithms
Elementary and no so elementary Java algorithms
sort/MergeSortTest.java
Go to the documentation of this file.
00001 
00009 package sort;
00010 
00011 import static org.junit.Assert.*;
00012 
00013 import java.util.ArrayList;
00014 import java.util.Arrays;
00015 import java.util.Random;
00016 
00017 import org.junit.Test;
00018 
00030 public class MergeSortTest {
00031     final static int seedVal = 127;
00032 
00033     private Random rand = null;
00034     private MergeSort<Integer> mSort = null;
00035 
00036     public MergeSortTest() {
00037         rand = new Random(seedVal);
00038         mSort = new MergeSort<Integer>();
00039     }
00040 
00041     private void initArray(ArrayList<Integer> a, final int N) {
00042         final int randRange = N * 2;
00043         for (int i = 0; i < N; i++) {
00044             a.add(rand.nextInt(randRange));
00045         }
00046     }
00047 
00048     private <T extends Comparable<T>> boolean isSortedArray(ArrayList<T> a) {
00049         boolean isSorted = true;
00050         for (int i = 0; i < a.size() - 1; i++) {
00051             // a[i] should be <= a[i+1] of if a[i] is > a[i+1] then the sort
00052             // failed
00053             if (a.get(i).compareTo(a.get(i + 1)) > 0) {
00054                 isSorted = false;
00055                 break;
00056             }
00057         }
00058         return isSorted;
00059     }
00060 
00061     private boolean testSort(int N) {
00062         boolean passed = true;
00063 
00064         if (N > 0) {
00065             ArrayList<Integer> vals = new ArrayList<Integer>();
00066             initArray(vals, N);
00067             mSort.mergeSort(vals);
00068             passed = isSortedArray(vals);
00069         } else {
00070             mSort.mergeSort(null);
00071         }
00072         return passed;
00073     }
00074 
00075     @Test
00076     public void testNullArray() {
00077         assertTrue("MergeSort test failed for null array", testSort(0));
00078     }
00079 
00080     @Test
00081     public void testEvenElement() {
00082         // test sorting an even number of values
00083         assertTrue("MergeSort test failed for even numbers of values",
00084                 testSort(64));
00085     }
00086 
00087     @Test
00088     public void testOddElement() {
00089         assertTrue("MergeSort test failed for odd numbers of values",
00090                 testSort(127));
00091     }
00092 
00093     @Test
00094     public void testOneElement() {
00095         // test for 1 value
00096         assertTrue("MergeSort test failed for 1 value", testSort(1));
00097     }
00098 
00099     @Test
00100     public void testTwoElements() {
00101         // test for 2 value
00102         assertTrue("MergeSort test failed for 2 values", testSort(2));
00103     }
00104 
00105     @Test
00106     public void testStringSort() {
00107         ArrayList<String> words = new ArrayList<String>(Arrays.asList("T'was",
00108                 "brillig", "and", "Slithey", "Toves", "all", "mimsy", "were",
00109                 "the", "borogroves", "and", "moonraths", "outgrabe"));
00110         MergeSort<String> strSort = new MergeSort<String>();
00111         strSort.mergeSort(words);
00112         boolean sorted = isSortedArray(words);
00113         assertTrue("MergeSort test failed for String sort", sorted);
00114     }
00115 }
 All Classes Namespaces Files Functions Variables