001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.core.util;
017:
018: import java.util.Collections;
019: import java.util.Iterator;
020: import java.util.LinkedList;
021: import java.util.Random;
022:
023: import junit.framework.TestCase;
024:
025: import org.columba.core.base.ListTools;
026:
027: /**
028: * @author timo
029: *
030: * To change this generated comment edit the template variable "typecomment":
031: * Window>Preferences>Java>Templates.
032: * To enable and disable the creation of type comments go to
033: * Window>Preferences>Java>Code Generation.
034: */
035: public class ListToolsTest extends TestCase {
036: LinkedList listFull_Integer;
037: LinkedList listFull_String;
038: Random random;
039:
040: /**
041: * Constructor for ListToolsTest.
042: * @param arg0
043: */
044: public ListToolsTest(String arg0) {
045: super (arg0);
046: }
047:
048: public void testIntersect() {
049: /*
050: LinkedList testList = new LinkedList(listFull_I);
051:
052: ListTools.intersect(testList,listFull_I);
053: assertTrue(testList.equals(listFull_I));
054:
055: testList = new LinkedList(listFull_I);
056:
057: ListTools.intersect(testList,new LinkedList());
058: assertTrue(testList.size()==0);
059:
060: testList = new LinkedList(listFull_I);
061:
062: ListTools.intersect(testList,listPart1_I);
063: assertTrue(testList.equals(listPart1_I));
064:
065: testList = new LinkedList(listFull_I);
066:
067: ListTools.intersect(testList,listPart2_I);
068: assertTrue(testList.equals(listPart2_I));
069:
070: testList = new LinkedList(listPart1_I);
071:
072: ListTools.intersect(testList,listPart2_I);
073: assertTrue(testList.size()==0);
074: */
075: }
076:
077: public void testSubstract() {
078: testSubstractOnLists(listFull_Integer);
079: }
080:
081: private void testSubstractOnLists(LinkedList listFull) {
082: LinkedList testList = new LinkedList(listFull);
083: LinkedList listPart1 = new LinkedList();
084: LinkedList listPart2 = new LinkedList();
085:
086: Iterator it = listFull.iterator();
087:
088: while (it.hasNext()) {
089: if (random.nextBoolean()) {
090: listPart1.add(it.next());
091: } else {
092: listPart2.add(it.next());
093: }
094: }
095:
096: // test if the size of self substracted list is 0
097: ListTools.substract(testList, listFull);
098: assertTrue(testList.size() == 0);
099:
100: testList = new LinkedList(listFull);
101:
102: // tests, if the list substracted with an empty list is equal to itself
103: ListTools.substract(testList, new LinkedList());
104: assertTrue(testList.equals(listFull));
105:
106: testList = new LinkedList(listFull);
107:
108: // tests, if the listPart substracted
109: ListTools.substract(testList, listPart1);
110: Collections.sort(listPart2);
111: assertTrue(testList.equals(listPart2));
112:
113: testList = new LinkedList(listFull);
114:
115: ListTools.substract(testList, listPart2);
116: assertTrue(testList.equals(listPart1));
117:
118: testList = new LinkedList(listPart1);
119:
120: ListTools.substract(testList, listPart2);
121: assertTrue(testList.equals(listPart1));
122:
123: testList = new LinkedList(listFull);
124:
125: ListTools.substract(testList, listPart1);
126: ListTools.substract(testList, listPart2);
127: assertTrue(testList.size() == 0);
128: }
129:
130: /**
131: * @see junit.framework.TestCase#setUp()
132: */
133: protected void setUp() throws Exception {
134: random = new Random();
135:
136: listFull_Integer = new LinkedList();
137:
138: for (int i = 0; i < 10; i++) {
139: listFull_Integer.add(new Integer(random.nextInt()));
140: }
141:
142: listFull_String = new LinkedList();
143: listFull_String.add("Hello");
144: listFull_String.add("World");
145: listFull_String.add("it is");
146: listFull_String.add("a nice");
147: listFull_String.add("day");
148: }
149: }
|