ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ListBash.java
Revision: 1.1
Committed: Mon May 2 19:19:38 2005 UTC (19 years ago) by dl
Branch: MAIN
Log Message:
Put misc performance tests into CVS

File Contents

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