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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines