J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6312056 4155650 4294891 4904074 |
| 27 | * @summary Reasonable things should happen if mutating while iterating. |
| 28 | */ |
| 29 | |
| 30 | import java.util.*; |
| 31 | import java.util.concurrent.*; |
| 32 | |
| 33 | public class ConcurrentModification { |
| 34 | static volatile int passed = 0, failed = 0; |
| 35 | |
| 36 | static void fail(String msg) { |
| 37 | failed++; |
| 38 | new AssertionError(msg).printStackTrace(); |
| 39 | } |
| 40 | |
| 41 | static void unexpected(Throwable t) { |
| 42 | failed++; |
| 43 | t.printStackTrace(); |
| 44 | } |
| 45 | |
| 46 | static void check(boolean condition, String msg) { |
| 47 | if (condition) |
| 48 | passed++; |
| 49 | else |
| 50 | fail(msg); |
| 51 | } |
| 52 | |
| 53 | static void check(boolean condition) { |
| 54 | check(condition, "Assertion failed"); |
| 55 | } |
| 56 | |
| 57 | private static void test(ConcurrentMap<Integer, Integer> m) |
| 58 | { |
| 59 | try { |
| 60 | m.clear(); |
| 61 | check(m.isEmpty()); |
| 62 | m.put(1,2); |
| 63 | Iterator<Map.Entry<Integer,Integer>> it = m.entrySet().iterator(); |
| 64 | if (it.hasNext()) { |
| 65 | m.remove(1); // sneaky |
| 66 | Map.Entry<Integer, Integer> e = it.next(); |
| 67 | check(m.isEmpty()); |
| 68 | check(e.getKey() == 1); |
| 69 | check(e.getValue() == 2); |
| 70 | } |
| 71 | } catch (Throwable t) {unexpected(t);} |
| 72 | |
| 73 | try { |
| 74 | m.clear(); |
| 75 | check(m.isEmpty()); |
| 76 | m.put(1,2); |
| 77 | Iterator<Map.Entry<Integer,Integer>> it = m.entrySet().iterator(); |
| 78 | if (it.hasNext()) { |
| 79 | m.put(1,3); // sneaky |
| 80 | Map.Entry<Integer, Integer> e = it.next(); |
| 81 | check(e.getKey() == 1); |
| 82 | check(e.getValue() == 2 || e.getValue() == 3); |
| 83 | if (m instanceof ConcurrentHashMap) { |
| 84 | e.setValue(4); |
| 85 | check(m.get(1) == 4); |
| 86 | } |
| 87 | } |
| 88 | } catch (Throwable t) {unexpected(t);} |
| 89 | } |
| 90 | |
| 91 | public static void main(String[] args) { |
| 92 | test(new ConcurrentHashMap<Integer,Integer>()); |
| 93 | test(new ConcurrentSkipListMap<Integer,Integer>()); |
| 94 | |
| 95 | System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
| 96 | if (failed > 0) throw new Error("Some tests failed"); |
| 97 | } |
| 98 | } |