jdk/test/java/util/NavigableMap/LockStep.java
changeset 24692 268fbc344d53
parent 23010 6dadb192ad81
child 30046 cf2c86e1819e
equal deleted inserted replaced
24691:f96e172a6ce8 24692:268fbc344d53
     1 /*
     1 /*
     2  * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
   234             }
   234             }
   235         }
   235         }
   236         Comparator cmp = comparator(s);
   236         Comparator cmp = comparator(s);
   237         if (s.isEmpty()) {
   237         if (s.isEmpty()) {
   238             THROWS(NoSuchElementException.class,
   238             THROWS(NoSuchElementException.class,
   239                    new Fun(){void f(){ s.first(); }},
   239                    () -> s.first(),
   240                    new Fun(){void f(){ s.last();  }});
   240                    () -> s.last());
   241             equal(null, s.lower(1));
   241             equal(null, s.lower(1));
   242             equal(null, s.floor(1));
   242             equal(null, s.floor(1));
   243             equal(null, s.ceiling(1));
   243             equal(null, s.ceiling(1));
   244             equal(null, s.higher(1));
   244             equal(null, s.higher(1));
   245         } else {
   245         } else {
   263             s.iterator(),
   263             s.iterator(),
   264             s.descendingSet().descendingSet().iterator(),
   264             s.descendingSet().descendingSet().iterator(),
   265         };
   265         };
   266         for (final Iterator it : its)
   266         for (final Iterator it : its)
   267             if (maybe(4))
   267             if (maybe(4))
   268                 THROWS(IllegalStateException.class,
   268                 THROWS(IllegalStateException.class, () -> it.remove());
   269                        new Fun(){void f(){ it.remove(); }});
       
   270         Object prev = null;
   269         Object prev = null;
   271         for (Object e : s) {
   270         for (Object e : s) {
   272             check(s.contains(e));
   271             check(s.contains(e));
   273             for (Iterator it : its) equalNext(it, e);
   272             for (Iterator it : its) equalNext(it, e);
   274             equal(e, s.ceiling(e));
   273             equal(e, s.ceiling(e));
   282             prev = e;
   281             prev = e;
   283         }
   282         }
   284         for (final Iterator it : its) {
   283         for (final Iterator it : its) {
   285             if (maybe(2))
   284             if (maybe(2))
   286                 check(! it.hasNext());
   285                 check(! it.hasNext());
   287             Fun fun = new Fun(){void f(){ it.next(); }};
   286             Fun fun = () -> it.next();
   288             THROWS(NoSuchElementException.class, fun, fun, fun);
   287             THROWS(NoSuchElementException.class, fun, fun, fun);
   289         }
   288         }
   290     }
   289     }
   291 
   290 
   292     static void equalIterators(final Iterator<?> it1,
   291     static void equalIterators(final Iterator<?> it1,
   378             equal2(m, serialClone(m));
   377             equal2(m, serialClone(m));
   379         equal2(m.keySet(), m.descendingKeySet());
   378         equal2(m.keySet(), m.descendingKeySet());
   380         Comparator cmp = comparator(m);
   379         Comparator cmp = comparator(m);
   381         if (m.isEmpty()) {
   380         if (m.isEmpty()) {
   382             THROWS(NoSuchElementException.class,
   381             THROWS(NoSuchElementException.class,
   383                    new Fun(){void f(){ m.firstKey(); }},
   382                    () -> m.firstKey(),
   384                    new Fun(){void f(){ m.lastKey();  }});
   383                    () -> m.lastKey());
   385             equal(null, m.firstEntry());
   384             equal(null, m.firstEntry());
   386             equal(null, m.lastEntry());
   385             equal(null, m.lastEntry());
   387             equal(null, m.pollFirstEntry());
   386             equal(null, m.pollFirstEntry());
   388             equal(null, m.pollLastEntry());
   387             equal(null, m.pollLastEntry());
   389             equal(null, m.lowerKey(1));
   388             equal(null, m.lowerKey(1));
   428             m.descendingMap().descendingMap().entrySet().iterator(),
   427             m.descendingMap().descendingMap().entrySet().iterator(),
   429         };
   428         };
   430         Iterator[] its = concat(kits, vits, eits);
   429         Iterator[] its = concat(kits, vits, eits);
   431         for (final Iterator it : its)
   430         for (final Iterator it : its)
   432             if (maybe(4))
   431             if (maybe(4))
   433                 THROWS(IllegalStateException.class,
   432                 THROWS(IllegalStateException.class, () -> it.remove());
   434                        new Fun(){void f(){ it.remove(); }});
       
   435         Map.Entry prev = null;
   433         Map.Entry prev = null;
   436         for (Map.Entry e : (Set<Map.Entry>) m.entrySet()) {
   434         for (Map.Entry e : (Set<Map.Entry>) m.entrySet()) {
   437             Object k = e.getKey();
   435             Object k = e.getKey();
   438             Object v = e.getValue();
   436             Object v = e.getValue();
   439             check(m.containsKey(k));
   437             check(m.containsKey(k));
   457             prev = e;
   455             prev = e;
   458         }
   456         }
   459         for (final Iterator it : its) {
   457         for (final Iterator it : its) {
   460             if (maybe(2))
   458             if (maybe(2))
   461                 check(! it.hasNext());
   459                 check(! it.hasNext());
   462             Fun fun = new Fun(){void f(){ it.next(); }};
   460             Fun fun = () -> it.next();
   463             THROWS(NoSuchElementException.class, fun, fun, fun);
   461             THROWS(NoSuchElementException.class, fun, fun, fun);
   464         }
   462         }
   465     }
   463     }
   466 
   464 
   467     static void equalNavigableMapsLeaf(final NavigableMap m1,
   465     static void equalNavigableMapsLeaf(final NavigableMap m1,
   631         if (maybe(2))
   629         if (maybe(2))
   632             check(! s.remove(e));
   630             check(! s.remove(e));
   633     }
   631     }
   634 
   632 
   635     static Fun remover(final Iterator it) {
   633     static Fun remover(final Iterator it) {
   636         return new Fun(){void f(){ it.remove(); }};
   634         return () -> it.remove();
   637     }
   635     }
   638 
   636 
   639     static MapFrobber randomRemover(NavigableMap m) {
   637     static MapFrobber randomRemover(NavigableMap m) {
   640         final Integer k = usedKey(m);
   638         final Integer k = usedKey(m);
   641         final MapFrobber[] randomRemovers = {
   639         final MapFrobber[] randomRemovers = {
   661                 while (it.hasNext())
   659                 while (it.hasNext())
   662                     if (it.next().equals(k)) {
   660                     if (it.next().equals(k)) {
   663                         it.remove();
   661                         it.remove();
   664                         if (maybe(2))
   662                         if (maybe(2))
   665                             THROWS(IllegalStateException.class,
   663                             THROWS(IllegalStateException.class,
   666                                    new Fun(){void f(){ it.remove(); }});
   664                                    () -> it.remove());
   667                     }
   665                     }
   668                 checkUnusedKey(m, k);}},
   666                 checkUnusedKey(m, k);}},
   669             new MapFrobber() {void frob(NavigableMap m) {
   667             new MapFrobber() {void frob(NavigableMap m) {
   670                 final Iterator it = m.navigableKeySet().descendingIterator();
   668                 final Iterator it = m.navigableKeySet().descendingIterator();
   671                 while (it.hasNext())
   669                 while (it.hasNext())
   672                     if (it.next().equals(k)) {
   670                     if (it.next().equals(k)) {
   673                         it.remove();
   671                         it.remove();
   674                         if (maybe(2))
   672                         if (maybe(2))
   675                             THROWS(IllegalStateException.class,
   673                             THROWS(IllegalStateException.class,
   676                                    new Fun(){void f(){ it.remove(); }});
   674                                    () -> it.remove());
   677                     }
   675                     }
   678                 checkUnusedKey(m, k);}},
   676                 checkUnusedKey(m, k);}},
   679             new MapFrobber() {void frob(NavigableMap m) {
   677             new MapFrobber() {void frob(NavigableMap m) {
   680                 final Iterator<Map.Entry> it = m.entrySet().iterator();
   678                 final Iterator<Map.Entry> it = m.entrySet().iterator();
   681                 while (it.hasNext())
   679                 while (it.hasNext())
   716                 while (it.hasNext())
   714                 while (it.hasNext())
   717                     if (it.next().equals(e)) {
   715                     if (it.next().equals(e)) {
   718                         it.remove();
   716                         it.remove();
   719                         if (maybe(2))
   717                         if (maybe(2))
   720                             THROWS(IllegalStateException.class,
   718                             THROWS(IllegalStateException.class,
   721                                    new Fun(){void f(){ it.remove(); }});
   719                                    () -> it.remove());
   722                     }
   720                     }
   723                 checkUnusedElt(s, e);}},
   721                 checkUnusedElt(s, e);}},
   724             new SetFrobber() {void frob(NavigableSet s) {
   722             new SetFrobber() {void frob(NavigableSet s) {
   725                 final Iterator it = s.descendingSet().iterator();
   723                 final Iterator it = s.descendingSet().iterator();
   726                 while (it.hasNext())
   724                 while (it.hasNext())
   727                     if (it.next().equals(e)) {
   725                     if (it.next().equals(e)) {
   728                         it.remove();
   726                         it.remove();
   729                         if (maybe(2))
   727                         if (maybe(2))
   730                             THROWS(IllegalStateException.class,
   728                             THROWS(IllegalStateException.class,
   731                                    new Fun(){void f(){ it.remove(); }});
   729                                    () -> it.remove());
   732                     }
   730                     }
   733                 checkUnusedElt(s, e);}},
   731                 checkUnusedElt(s, e);}},
   734             new SetFrobber() {void frob(NavigableSet s) {
   732             new SetFrobber() {void frob(NavigableSet s) {
   735                 final Iterator it = s.descendingIterator();
   733                 final Iterator it = s.descendingIterator();
   736                 while (it.hasNext())
   734                 while (it.hasNext())
   737                     if (it.next().equals(e)) {
   735                     if (it.next().equals(e)) {
   738                         it.remove();
   736                         it.remove();
   739                         if (maybe(2))
   737                         if (maybe(2))
   740                             THROWS(IllegalStateException.class,
   738                             THROWS(IllegalStateException.class,
   741                                    new Fun(){void f(){ it.remove(); }});
   739                                    () -> it.remove());
   742                     }
   740                     }
   743                 checkUnusedElt(s, e);}}
   741                 checkUnusedElt(s, e);}}
   744         };
   742         };
   745 
   743 
   746         return randomRemovers[rnd.nextInt(randomRemovers.length)];
   744         return randomRemovers[rnd.nextInt(randomRemovers.length)];
   767             equalNavigableMaps(m1, m2);
   765             equalNavigableMaps(m1, m2);
   768         }
   766         }
   769         for (final NavigableMap m : maps) {
   767         for (final NavigableMap m : maps) {
   770             final Object e = usedKey(m);
   768             final Object e = usedKey(m);
   771             THROWS(IllegalArgumentException.class,
   769             THROWS(IllegalArgumentException.class,
   772                    new Fun(){void f(){m.subMap(e,true,e,false)
   770                    () -> {m.subMap(e,true,e,false)
   773                                        .subMap(e,true,e,true);}},
   771                            .subMap(e,true,e,true);},
   774                    new Fun(){void f(){m.subMap(e,false,e,true)
   772                    () -> {m.subMap(e,false,e,true)
   775                                        .subMap(e,true,e,true);}},
   773                            .subMap(e,true,e,true);},
   776                    new Fun(){void f(){m.tailMap(e,false).tailMap(e,true);}},
   774                    () -> m.tailMap(e,false).tailMap(e,true),
   777                    new Fun(){void f(){m.headMap(e,false).headMap(e,true);}});
   775                    () -> m.headMap(e,false).headMap(e,true));
   778         }
   776         }
   779         //System.out.printf("%s%n", m1);
   777         //System.out.printf("%s%n", m1);
   780         for (int i = size; i > 0; i--) {
   778         for (int i = size; i > 0; i--) {
   781             MapFrobber remover = randomRemover(m1);
   779             MapFrobber remover = randomRemover(m1);
   782             for (final NavigableMap m : maps) {
   780             for (final NavigableMap m : maps) {
   809             equalNavigableSets(s1, s2);
   807             equalNavigableSets(s1, s2);
   810         }
   808         }
   811         for (final NavigableSet s : sets) {
   809         for (final NavigableSet s : sets) {
   812             final Object e = usedElt(s);
   810             final Object e = usedElt(s);
   813             THROWS(IllegalArgumentException.class,
   811             THROWS(IllegalArgumentException.class,
   814                    new Fun(){void f(){s.subSet(e,true,e,false)
   812                    () -> {s.subSet(e,true,e,false)
   815                                        .subSet(e,true,e,true);}},
   813                            .subSet(e,true,e,true);},
   816                    new Fun(){void f(){s.subSet(e,false,e,true)
   814                    () -> {s.subSet(e,false,e,true)
   817                                        .subSet(e,true,e,true);}},
   815                            .subSet(e,true,e,true);},
   818                    new Fun(){void f(){s.tailSet(e,false).tailSet(e,true);}},
   816                    () -> s.tailSet(e,false).tailSet(e,true),
   819                    new Fun(){void f(){s.headSet(e,false).headSet(e,true);}});
   817                    () -> s.headSet(e,false).headSet(e,true));
   820         }
   818         }
   821         //System.out.printf("%s%n", s1);
   819         //System.out.printf("%s%n", s1);
   822         for (int i = size; i > 0; i--) {
   820         for (int i = size; i > 0; i--) {
   823             SetFrobber remover = randomRemover(s1);
   821             SetFrobber remover = randomRemover(s1);
   824             for (final NavigableSet s : sets) {
   822             for (final NavigableSet s : sets) {
   845         try { realMain(args); } catch (Throwable t) { unexpected(t); }
   843         try { realMain(args); } catch (Throwable t) { unexpected(t); }
   846 
   844 
   847         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
   845         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
   848         if (failed > 0) throw new Exception("Some tests failed");
   846         if (failed > 0) throw new Exception("Some tests failed");
   849     }
   847     }
   850     static abstract class Fun {abstract void f() throws Throwable;}
   848     interface Fun {void f() throws Throwable;}
   851     static void THROWS(Class<? extends Throwable> k, Fun... fs) {
   849     static void THROWS(Class<? extends Throwable> k, Fun... fs) {
   852           for (Fun f : fs)
   850           for (Fun f : fs)
   853               try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
   851               try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
   854               catch (Throwable t) {
   852               catch (Throwable t) {
   855                   if (k.isAssignableFrom(t.getClass())) pass();
   853                   if (k.isAssignableFrom(t.getClass())) pass();