KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > util > TestParallelSorter


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.cglib.util;
17
18 import net.sf.cglib.CodeGenTestCase;
19 import java.io.*;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.*;
22 import junit.framework.*;
23
24 /**
25  * @author Chris Nokleberg <a HREF="mailto:chris@nokleberg.com">chris@nokleberg.com</a>
26  * @version $Id: TestParallelSorter.java,v 1.4 2004/06/24 21:15:13 herbyderby Exp $
27  */

28 public class TestParallelSorter extends CodeGenTestCase {
29     public void testSorts() throws Throwable JavaDoc {
30         Object JavaDoc[] data1 = getTestData();
31         Object JavaDoc[] data2 = copy(data1);
32         Object JavaDoc[] 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 JavaDoc[]{ data1, idx1 });
37         ParallelSorter p2 = ParallelSorter.create(new Object JavaDoc[]{ 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 JavaDoc[] data1, Object JavaDoc[] 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 JavaDoc[] 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 JavaDoc 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 JavaDoc[] copy(Object JavaDoc[] data) {
83         Object JavaDoc[] copy = new Object JavaDoc[data.length];
84         System.arraycopy(data, 0, copy, 0, data.length);
85         return copy;
86     }
87
88     public TestParallelSorter(String JavaDoc testName) {
89         super(testName);
90     }
91     
92     public static void main(String JavaDoc[] 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 JavaDoc loader) throws Throwable JavaDoc {
101     }
102     
103     public void testFailOnMemoryLeak() throws Throwable JavaDoc {
104     }
105     
106 }
107
Popular Tags