ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ListBash.java
Revision: 1.3
Committed: Tue May 31 15:08:32 2005 UTC (18 years, 11 months ago) by dl
Branch: MAIN
Changes since 1.2: +71 -59 lines
Log Message:
Add tests

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