001: /*
002: * Copyright 2003,2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package net.sf.cglib.util;
017:
018: import net.sf.cglib.CodeGenTestCase;
019: import java.io.*;
020: import java.lang.reflect.Method;
021: import java.util.*;
022: import junit.framework.*;
023:
024: /**
025: * @author Chris Nokleberg <a href="mailto:chris@nokleberg.com">chris@nokleberg.com</a>
026: * @version $Id: TestParallelSorter.java,v 1.4 2004/06/24 21:15:13 herbyderby Exp $
027: */
028: public class TestParallelSorter extends CodeGenTestCase {
029: public void testSorts() throws Throwable {
030: Object[] data1 = getTestData();
031: Object[] data2 = copy(data1);
032: Object[] data3 = copy(data1);
033: int[] idx1 = getIndexes(data1.length);
034: int[] idx2 = getIndexes(data1.length);
035: int[] idx3 = getIndexes(data1.length);
036: ParallelSorter p1 = ParallelSorter.create(new Object[] { data1,
037: idx1 });
038: ParallelSorter p2 = ParallelSorter.create(new Object[] { data2,
039: idx2 });
040: p1.quickSort(0);
041: p2.mergeSort(0);
042: compare(data1, data2);
043: compare(idx1, idx2);
044: p1.quickSort(1);
045: compare(idx1, idx3);
046: compare(data1, data3);
047: }
048:
049: private void compare(Object[] data1, Object[] data2) {
050: assertTrue(data1.length == data2.length);
051: for (int i = 0; i < data1.length; i++) {
052: assertTrue(data1[i].equals(data2[i]));
053: }
054: }
055:
056: private void compare(int[] data1, int[] data2) {
057: assertTrue(data1.length == data2.length);
058: for (int i = 0; i < data1.length; i++) {
059: assertTrue(data1[i] == data2[i]);
060: }
061: }
062:
063: private int[] getIndexes(int len) {
064: int[] idx = new int[len];
065: for (int i = 0; i < len; i++) {
066: idx[i] = i;
067: }
068: return idx;
069: }
070:
071: private Object[] getTestData() throws IOException {
072: InputStream in = getClass().getResourceAsStream("words.txt");
073: BufferedReader r = new BufferedReader(new InputStreamReader(in));
074: List list = new ArrayList();
075: String line;
076: int c = 0;
077: while ((line = r.readLine()) != null) {
078: list.add(line);
079: if (c++ == 20)
080: break;
081: }
082: return list.toArray();
083: }
084:
085: private Object[] copy(Object[] data) {
086: Object[] copy = new Object[data.length];
087: System.arraycopy(data, 0, copy, 0, data.length);
088: return copy;
089: }
090:
091: public TestParallelSorter(String testName) {
092: super (testName);
093: }
094:
095: public static void main(String[] args) {
096: junit.textui.TestRunner.run(suite());
097: }
098:
099: public static Test suite() {
100: return new TestSuite(TestParallelSorter.class);
101: }
102:
103: public void perform(ClassLoader loader) throws Throwable {
104: }
105:
106: public void testFailOnMemoryLeak() throws Throwable {
107: }
108:
109: }
|