001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.util;
006:
007: import com.tc.object.ObjectID;
008: import com.tc.test.TCTestCase;
009:
010: import java.io.ByteArrayInputStream;
011: import java.io.ByteArrayOutputStream;
012: import java.io.ObjectInput;
013: import java.io.ObjectInputStream;
014: import java.io.ObjectOutput;
015: import java.io.ObjectOutputStream;
016: import java.security.SecureRandom;
017: import java.util.ArrayList;
018: import java.util.Collection;
019: import java.util.Collections;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Random;
024: import java.util.Set;
025:
026: public class ObjectIDSetTest extends TCTestCase {
027:
028: interface SetCreator {
029: public Set create();
030:
031: public Set create(Collection c);
032: }
033:
034: /**
035: * This test is disabled since it is too slow and times out in slow boxes.
036: */
037: public void DISABLEDtestObjectIDSet() {
038: SetCreator creator = new SetCreator() {
039: public Set create() {
040: return new ObjectIDSet();
041: }
042:
043: public Set create(Collection c) {
044: return new ObjectIDSet(c);
045: }
046:
047: };
048: basicTest(creator);
049: iteratorRemoveTest(creator);
050: }
051:
052: public void basicTest(SetCreator creator) {
053: basicTest(creator, 100000, 100000);
054: basicTest(creator, 500000, 100000);
055: basicTest(creator, 100000, 1000000);
056: }
057:
058: public void basicTest(SetCreator creator, int distRange,
059: int iterationCount) {
060: long test_start = System.currentTimeMillis();
061: Set s = new HashSet();
062: Set small = creator.create();
063: String cname = small.getClass().getName();
064: System.err.println("Running tests for " + cname
065: + " distRange = " + distRange + " iterationCount = "
066: + iterationCount);
067: assertTrue(small.isEmpty());
068: assertTrue(small.size() == 0);
069: SecureRandom sr = new SecureRandom();
070: long seed = sr.nextLong();
071: System.err.println("Seed for Random is " + seed);
072: Random r = new Random(seed);
073: for (int i = 0; i < iterationCount; i++) {
074: long l = r.nextInt(distRange);
075: ObjectID id = new ObjectID(l);
076: s.add(id);
077: small.add(id);
078: assertEquals(s.size(), small.size());
079: }
080: Iterator sit = small.iterator();
081: List all = new ArrayList();
082: all.addAll(s);
083: while (sit.hasNext()) {
084: ObjectID i = (ObjectID) sit.next();
085: Assert.eval("FAILED:" + i.toString(), s.remove(i));
086: }
087: Assert.eval(s.size() == 0);
088:
089: // test retain all
090: Set odds = new HashSet();
091: Set evens = new HashSet();
092: for (int i = 0; i < all.size(); i++) {
093: if (i % 2 == 0) {
094: evens.add(all.get(i));
095: } else {
096: odds.add(all.get(i));
097: }
098: }
099: boolean b = small.retainAll(odds);
100: assertTrue(b);
101: assertEquals(odds, small);
102: b = small.retainAll(evens);
103: assertTrue(b);
104: assertEquals(0, small.size());
105: small.addAll(all); // back to original state
106:
107: // test new set creation (which uses cloning
108: long start = System.currentTimeMillis();
109: Set copy = creator.create(all);
110: System.err
111: .println("Time to add all IDs from a collection to a new "
112: + cname
113: + " = "
114: + (System.currentTimeMillis() - start) + " ms");
115: start = System.currentTimeMillis();
116: Set clone = creator.create(small);
117: System.err
118: .println("Time to add all IDs from an ObjectIDSet to a new "
119: + cname
120: + " = "
121: + (System.currentTimeMillis() - start) + " ms");
122:
123: Collections.shuffle(all);
124: for (Iterator i = all.iterator(); i.hasNext();) {
125: ObjectID rid = (ObjectID) i.next();
126: Assert.eval(small.contains(rid));
127: Assert.eval(clone.contains(rid));
128: Assert.eval(copy.contains(rid));
129: if (!small.remove(rid)) {
130: throw new AssertionError("couldn't remove:" + rid);
131: }
132: if (small.contains(rid)) {
133: throw new AssertionError(rid);
134: }
135: if (!clone.remove(rid)) {
136: throw new AssertionError("couldn't remove:" + rid);
137: }
138: if (clone.contains(rid)) {
139: throw new AssertionError(rid);
140: }
141: if (!copy.remove(rid)) {
142: throw new AssertionError("couldn't remove:" + rid);
143: }
144: if (copy.contains(rid)) {
145: throw new AssertionError(rid);
146: }
147: }
148: for (Iterator i = all.iterator(); i.hasNext();) {
149: ObjectID rid = (ObjectID) i.next();
150: Assert.eval(!small.contains(rid));
151: if (small.remove(rid)) {
152: throw new AssertionError("shouldn't have removed:"
153: + rid);
154: }
155: if (small.contains(rid)) {
156: throw new AssertionError(rid);
157: }
158: if (clone.remove(rid)) {
159: throw new AssertionError("shouldn't have removed:"
160: + rid);
161: }
162: if (clone.contains(rid)) {
163: throw new AssertionError(rid);
164: }
165: if (copy.remove(rid)) {
166: throw new AssertionError("shouldn't have removed:"
167: + rid);
168: }
169: if (copy.contains(rid)) {
170: throw new AssertionError(rid);
171: }
172: }
173: Assert.eval(s.size() == 0);
174: Assert.eval(small.size() == 0);
175: Assert.eval(copy.size() == 0);
176: Assert.eval(clone.size() == 0);
177: System.err.println("Time taken to run basic Test for "
178: + small.getClass().getName() + " is "
179: + (System.currentTimeMillis() - test_start) + " ms");
180: }
181:
182: public void testSerializationObjectIDSet2() throws Exception {
183: for (int i = 0; i < 20; i++) {
184: Set s = createRandomSetOfObjectIDs();
185: serializeAndVerify(s);
186: }
187: }
188:
189: private void serializeAndVerify(Set s) throws Exception {
190: ObjectIDSet2 org = new ObjectIDSet2(s);
191: assertEquals(s, org);
192:
193: ObjectIDSet2 ser = serializeAndRead(org);
194: assertEquals(s, ser);
195: assertEquals(org, ser);
196: }
197:
198: private ObjectIDSet2 serializeAndRead(ObjectIDSet2 org)
199: throws Exception {
200: ByteArrayOutputStream bo = new ByteArrayOutputStream();
201: ObjectOutput oo = new ObjectOutputStream(bo);
202: oo.writeObject(org);
203: System.err.println("Written ObjectIDSet2 size : " + org.size());
204: ByteArrayInputStream bi = new ByteArrayInputStream(bo
205: .toByteArray());
206: ObjectInput oi = new ObjectInputStream(bi);
207: ObjectIDSet2 oids = (ObjectIDSet2) oi.readObject();
208: System.err.println("Read ObjectIDSet2 size : " + oids.size());
209: return oids;
210: }
211:
212: private Set createRandomSetOfObjectIDs() {
213: Set s = new HashSet();
214: SecureRandom sr = new SecureRandom();
215: long seed = sr.nextLong();
216: System.err.println("Random Set creation : Seed for Random is "
217: + seed);
218: Random r = new Random(seed);
219: for (int i = 0; i < r.nextInt(100000); i++) {
220: s.add(new ObjectID(r.nextLong()));
221: }
222: System.err.println("Created a set of size : " + s.size());
223: return s;
224: }
225:
226: public void testObjectIDSet2() {
227: SetCreator creator = new SetCreator() {
228: public Set create() {
229: return new ObjectIDSet2();
230: }
231:
232: public Set create(Collection c) {
233: return new ObjectIDSet2(c);
234: }
235:
236: };
237: basicTest(creator);
238: }
239:
240: public void iteratorRemoveTest(SetCreator creator) {
241: SecureRandom sr = new SecureRandom();
242: long seed = sr.nextLong();
243: iteratorRemoveTest(creator, seed);
244: }
245:
246: private void iteratorRemoveTest(SetCreator creator, long seed) {
247: Set all = new HashSet();
248: Set oidSet = creator.create();
249: System.err.println("Running iteratorRemoveTest for "
250: + oidSet.getClass().getName() + " and seed is " + seed);
251: Random r = new Random(seed);
252: for (int i = 0; i < 50000; i++) {
253: long l = r.nextInt(100000);
254: ObjectID id = new ObjectID(l);
255: all.add(id);
256: oidSet.add(id);
257: }
258: for (Iterator i = all.iterator(); i.hasNext();) {
259: ObjectID rid = (ObjectID) i.next();
260: Assert.eval(oidSet.contains(rid));
261: for (Iterator j = oidSet.iterator(); j.hasNext();) {
262: ObjectID crid = (ObjectID) j.next();
263: if (crid.equals(rid)) {
264: j.remove();
265: break;
266: }
267: }
268: }
269: Assert.eval(oidSet.size() == 0);
270: }
271:
272: // See the comment above
273: public void DISABLEDtestFailedCase() {
274: System.err.println("\nRunning testFailedCase()... ");
275: SetCreator creator = new SetCreator() {
276: public Set create() {
277: return new ObjectIDSet();
278: }
279:
280: public Set create(Collection c) {
281: return new ObjectIDSet(c);
282: }
283:
284: };
285: long seed = 1576555335886137186L;
286: iteratorRemoveTest(creator, seed);
287: }
288:
289: }
|