ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ListBash.java
(Generate patch)

Comparing jsr166/src/test/loops/ListBash.java (file contents):
Revision 1.1 by dl, Mon May 2 19:19:38 2005 UTC vs.
Revision 1.4 by dl, Sun Sep 25 13:10:45 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines