ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ListBash.java
Revision: 1.2
Committed: Mon May 9 19:33:30 2005 UTC (19 years ago) by dl
Branch: MAIN
Changes since 1.1: +6 -0 lines
Log Message:
Add headers

File Contents

# User Rev Content
1 dl 1.2 /*
2     * Written by Josh Bloch and Doug Lea with assistance from members of
3     * JCP JSR-166 Expert Group and released to the public domain, as
4     * explained at http://creativecommons.org/licenses/publicdomain
5     */
6    
7 dl 1.1 import java.util.*;
8    
9     public class ListBash {
10     static Random rnd = new Random();
11    
12     public static void main(String[] args) {
13     int numItr = Integer.parseInt(args[1]);
14     int listSize = Integer.parseInt(args[2]);
15     Class cl = null;
16    
17     try {
18     cl = Class.forName(args[0]);
19     } catch(ClassNotFoundException e) {
20     fail("Class " + args[0] + " not found.");
21     }
22    
23     boolean synch = (args.length>3);
24    
25     for (int i=0; i<numItr; i++) {
26     List s1 = newList(cl, synch);
27     AddRandoms(s1, listSize);
28    
29     List s2 = newList(cl, synch);
30     AddRandoms(s2, listSize);
31    
32     List intersection = clone(s1, cl,synch);intersection.retainAll(s2);
33     List diff1 = clone(s1, cl, synch); diff1.removeAll(s2);
34     List diff2 = clone(s2, cl, synch); diff2.removeAll(s1);
35     List union = clone(s1, cl, synch); union.addAll(s2);
36    
37     if (diff1.removeAll(diff2))
38     fail("List algebra identity 2 failed");
39     if (diff1.removeAll(intersection))
40     fail("List algebra identity 3 failed");
41     if (diff2.removeAll(diff1))
42     fail("List algebra identity 4 failed");
43     if (diff2.removeAll(intersection))
44     fail("List algebra identity 5 failed");
45     if (intersection.removeAll(diff1))
46     fail("List algebra identity 6 failed");
47     if (intersection.removeAll(diff1))
48     fail("List algebra identity 7 failed");
49    
50     intersection.addAll(diff1); intersection.addAll(diff2);
51     if (!(intersection.containsAll(union) &&
52     union.containsAll(intersection)))
53     fail("List algebra identity 1 failed");
54    
55     Iterator e = union.iterator();
56     while (e.hasNext())
57     intersection.remove(e.next());
58     if (!intersection.isEmpty())
59     fail("Copy nonempty after deleting all elements.");
60    
61     e = union.iterator();
62     while (e.hasNext()) {
63     Object o = e.next();
64     if (!union.contains(o))
65     fail("List doesn't contain one of its elements.");
66     e.remove();
67     }
68     if (!union.isEmpty())
69     fail("List nonempty after deleting all elements.");
70    
71     s1.clear();
72     if (s1.size() != 0)
73     fail("Clear didn't reduce size to zero.");
74    
75     s1.addAll(0, s2);
76     if (!(s1.equals(s2) && s2.equals(s1)))
77     fail("addAll(int, Collection) doesn't work.");
78     // Reverse List
79     for (int j=0, n=s1.size(); j<n; j++)
80     s1.set(j, s1.set(n-j-1, s1.get(j)));
81     // Reverse it again
82     for (int j=0, n=s1.size(); j<n; j++)
83     s1.set(j, s1.set(n-j-1, s1.get(j)));
84     if (!(s1.equals(s2) && s2.equals(s1)))
85     fail("set(int, Object) doesn't work");
86     }
87    
88     List s = newList(cl, synch);
89     for (int i=0; i<listSize; i++)
90     s.add(new Integer(i));
91     if (s.size() != listSize)
92     fail("Size of [0..n-1] != n");
93    
94     List even = clone(s, cl, synch);
95     Iterator it = even.iterator();
96     while(it.hasNext())
97     if(((Integer)it.next()).intValue() % 2 == 1)
98     it.remove();
99     it = even.iterator();
100     while(it.hasNext())
101     if(((Integer)it.next()).intValue() % 2 == 1)
102     fail("Failed to remove all odd nubmers.");
103    
104     List odd = clone(s, cl, synch);
105     for (int i=0; i<(listSize/2); i++)
106     odd.remove(i);
107     for (int i=0; i<(listSize/2); i++) {
108     int ii = ((Integer)odd.get(i)).intValue();
109     if(ii % 2 != 1)
110     fail("Failed to remove all even nubmers. " + ii);
111     }
112    
113     List all = clone(odd, cl, synch);
114     for (int i=0; i<(listSize/2); i++)
115     all.add(2*i, even.get(i));
116     if (!all.equals(s))
117     fail("Failed to reconstruct ints from odds and evens.");
118    
119     all = clone(odd, cl, synch);
120     ListIterator itAll = all.listIterator(all.size());
121     ListIterator itEven = even.listIterator(even.size());
122     while (itEven.hasPrevious()) {
123     itAll.previous();
124     itAll.add(itEven.previous());
125     itAll.previous(); // ???
126     }
127     itAll = all.listIterator();
128     while (itAll.hasNext()) {
129     Integer i = (Integer)itAll.next();
130     itAll.set(new Integer(i.intValue()));
131     }
132     itAll = all.listIterator();
133     it = s.iterator();
134     while(it.hasNext())
135     if(it.next()==itAll.next())
136     fail("Iterator.set failed to change value.");
137    
138     if (!all.equals(s))
139     fail("Failed to reconstruct ints with ListIterator.");
140    
141     it = all.listIterator();
142     int i=0;
143     while (it.hasNext()) {
144     Object o = it.next();
145     if (all.indexOf(o) != all.lastIndexOf(o))
146     fail("Apparent duplicate detected.");
147     if (all.subList(i, all.size()).indexOf(o) != 0) {
148     System.out.println("s0: " + all.subList(i, all.size()).indexOf(o));
149     fail("subList/indexOf is screwy.");
150     }
151     if (all.subList(i+1, all.size()).indexOf(o) != -1) {
152     System.out.println("s-1: " + all.subList(i+1, all.size()).indexOf(o));
153     fail("subList/indexOf is screwy.");
154     }
155     if (all.subList(0,i+1).lastIndexOf(o) != i) {
156     System.out.println("si" + all.subList(0,i+1).lastIndexOf(o));
157     fail("subList/lastIndexOf is screwy.");
158     }
159     i++;
160     }
161    
162     List l = newList(cl, synch);
163     AddRandoms(l, listSize);
164     Integer[] ia = (Integer[]) l.toArray(new Integer[0]);
165     if (!l.equals(Arrays.asList(ia)))
166     fail("toArray(Object[]) is hosed (1)");
167     ia = new Integer[listSize];
168     Integer[] ib = (Integer[]) l.toArray(ia);
169     if (ia != ib || !l.equals(Arrays.asList(ia)))
170     fail("toArray(Object[]) is hosed (2)");
171     ia = new Integer[listSize+1];
172     ia[listSize] = new Integer(69);
173     ib = (Integer[]) l.toArray(ia);
174     if (ia != ib || ia[listSize] != null
175     || !l.equals(Arrays.asList(ia).subList(0, listSize)))
176     fail("toArray(Object[]) is hosed (3)");
177    
178     System.out.println("Success.");
179     }
180    
181     // Done inefficiently so as to exercise toArray
182     static List clone(List s, Class cl, boolean synch) {
183     List a = Arrays.asList(s.toArray());
184     if (s.hashCode() != a.hashCode())
185     fail("Incorrect hashCode computation.");
186    
187     List clone = newList(cl, synch);
188     clone.addAll(a);
189     if (!s.equals(clone))
190     fail("List not equal to copy.");
191     if (!s.containsAll(clone))
192     fail("List does not contain copy.");
193     if (!clone.containsAll(s))
194     fail("Copy does not contain list.");
195    
196     return clone;
197     }
198    
199     static List newList(Class cl, boolean synch) {
200     try {
201     List s = (List)cl.newInstance();
202     if (synch)
203     s = Collections.synchronizedList(s);
204     if (!s.isEmpty())
205     fail("New instance non empty.");
206     return s;
207     } catch(Throwable t) {
208     fail("Can't instantiate " + cl + ": " + t);
209     }
210     return null; //Shut up compiler.
211     }
212    
213     static void AddRandoms(List s, int n) {
214     for (int i=0; i<n; i++) {
215     int r = rnd.nextInt() % n;
216     Integer e = new Integer(r < 0 ? -r : r);
217    
218     int preSize = s.size();
219     if (!s.add(e))
220     fail ("Add failed.");
221     int postSize = s.size();
222     if (postSize-preSize != 1)
223     fail ("Add didn't increase size by 1.");
224     }
225     }
226    
227     static void fail(String s) {
228     System.out.println(s);
229     System.exit(1);
230     }
231     }