1 16 package net.sf.cglib.util; 17 18 import net.sf.cglib.CodeGenTestCase; 19 import java.io.*; 20 import java.lang.reflect.Method ; 21 import java.util.*; 22 import junit.framework.*; 23 24 28 public class TestParallelSorter extends CodeGenTestCase { 29 public void testSorts() throws Throwable { 30 Object [] data1 = getTestData(); 31 Object [] data2 = copy(data1); 32 Object [] data3 = copy(data1); 33 int[] idx1 = getIndexes(data1.length); 34 int[] idx2 = getIndexes(data1.length); 35 int[] idx3 = getIndexes(data1.length); 36 ParallelSorter p1 = ParallelSorter.create(new Object []{ data1, idx1 }); 37 ParallelSorter p2 = ParallelSorter.create(new Object []{ data2, idx2 }); 38 p1.quickSort(0); 39 p2.mergeSort(0); 40 compare(data1, data2); 41 compare(idx1, idx2); 42 p1.quickSort(1); 43 compare(idx1, idx3); 44 compare(data1, data3); 45 } 46 47 private void compare(Object [] data1, Object [] data2) { 48 assertTrue(data1.length == data2.length); 49 for (int i = 0; i < data1.length; i++) { 50 assertTrue(data1[i].equals(data2[i])); 51 } 52 } 53 54 private void compare(int[] data1, int[] data2) { 55 assertTrue(data1.length == data2.length); 56 for (int i = 0; i < data1.length; i++) { 57 assertTrue(data1[i] == data2[i]); 58 } 59 } 60 61 private int[] getIndexes(int len) { 62 int[] idx = new int[len]; 63 for (int i = 0; i < len; i++) { 64 idx[i] = i; 65 } 66 return idx; 67 } 68 69 private Object [] getTestData() throws IOException { 70 InputStream in = getClass().getResourceAsStream("words.txt"); 71 BufferedReader r = new BufferedReader(new InputStreamReader(in)); 72 List list = new ArrayList(); 73 String line; 74 int c = 0; 75 while ((line = r.readLine()) != null) { 76 list.add(line); 77 if (c++ == 20) break; 78 } 79 return list.toArray(); 80 } 81 82 private Object [] copy(Object [] data) { 83 Object [] copy = new Object [data.length]; 84 System.arraycopy(data, 0, copy, 0, data.length); 85 return copy; 86 } 87 88 public TestParallelSorter(String testName) { 89 super(testName); 90 } 91 92 public static void main(String [] args) { 93 junit.textui.TestRunner.run(suite()); 94 } 95 96 public static Test suite() { 97 return new TestSuite(TestParallelSorter.class); 98 } 99 100 public void perform(ClassLoader loader) throws Throwable { 101 } 102 103 public void testFailOnMemoryLeak() throws Throwable { 104 } 105 106 } 107 | Popular Tags |