test/java/util/Collection/MOAT.java

Print this page




   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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     6207984 6272521 6192552 6269713 6197726 6260652 5073546 4137464
  27  *          4155650 4216399 4294891 6282555 6318622 6355327 6383475 6420753
  28  *          6431845 4802633 6570566 6570575 6570631 6570924 6691185 6691215

  29  * @summary Run many tests on many Collection and Map implementations
  30  * @author  Martin Buchholz
  31  * @run main MOAT
  32  * @run main/othervm -XX:+AggressiveOpts MOAT
  33  */
  34 
  35 /* Mother Of All (Collection) Tests
  36  *
  37  * Testing of collection classes is often spotty, because many tests
  38  * need to be performed on many implementations, but the onus on
  39  * writing the tests falls on the engineer introducing the new
  40  * implementation.
  41  *
  42  * The idea of this mega-test is that:
  43  *
  44  * An engineer adding a new collection implementation could simply add
  45  * their new implementation to a list of implementations in this
  46  * test's main method.  Any general purpose Collection<Integer> or
  47  * Map<Integer,Integer> class is appropriate.
  48  *
  49  * An engineer fixing a regression could add their regression test here and
  50  * simultaneously test all other implementations.
  51  */
  52 
  53 import java.io.*;
  54 import java.util.*;
  55 import java.util.concurrent.*;
  56 import static java.util.Collections.*;
  57 
  58 public class MOAT {
  59     public static void realMain(String[] args) {
  60 


  61         testCollection(new LinkedHashSet<Integer>());
  62         testCollection(new HashSet<Integer>());
  63         testCollection(new Vector<Integer>());
  64         testCollection(new Vector<Integer>().subList(0,0));
  65         testCollection(new ArrayDeque<Integer>());
  66         testCollection(new ArrayList<Integer>());
  67         testCollection(new ArrayList<Integer>().subList(0,0));
  68         testCollection(new LinkedList<Integer>());
  69         testCollection(new LinkedList<Integer>().subList(0,0));
  70         testCollection(new TreeSet<Integer>());
  71 
  72         testCollection(new CopyOnWriteArrayList<Integer>());
  73         testCollection(new CopyOnWriteArrayList<Integer>().subList(0,0));
  74         testCollection(new CopyOnWriteArraySet<Integer>());
  75         testCollection(new PriorityQueue<Integer>());
  76         testCollection(new PriorityBlockingQueue<Integer>());
  77         testCollection(new ArrayBlockingQueue<Integer>(20));
  78         testCollection(new LinkedBlockingQueue<Integer>(20));
  79         testCollection(new LinkedBlockingDeque<Integer>(20));
  80         testCollection(new ConcurrentLinkedDeque<Integer>());


 736 
 737         try {
 738             oneElement(c);
 739             checkFunctionalInvariants(c);
 740         }
 741         catch (Throwable t) { unexpected(t); }
 742 
 743         clear(c);      testNullElement(c);
 744         oneElement(c); testNullElement(c);
 745 
 746         clear(c);      testStringElement(c);
 747         oneElement(c); testStringElement(c);
 748 
 749         if (c.getClass().getName().matches(".*concurrent.*"))
 750             testConcurrentCollection(c);
 751 
 752         //----------------------------------------------------------------
 753         // The "all" operations should throw NPE when passed null
 754         //----------------------------------------------------------------
 755         {








 756             oneElement(c);
 757             try {
 758                 c.removeAll(null);
 759                 fail("Expected NullPointerException");
 760             }
 761             catch (NullPointerException e) { pass(); }
 762             catch (Throwable t) { unexpected(t); }
 763 








 764             oneElement(c);
 765             try {
 766                 c.retainAll(null);
 767                 fail("Expected NullPointerException");
 768             }
 769             catch (NullPointerException e) { pass(); }
 770             catch (Throwable t) { unexpected(t); }
 771 
 772             oneElement(c);
 773             try {
 774                 c.addAll(null);
 775                 fail("Expected NullPointerException");
 776             }
 777             catch (NullPointerException e) { pass(); }
 778             catch (Throwable t) { unexpected(t); }
 779 
 780             oneElement(c);
 781             try {
 782                 c.containsAll(null);
 783                 fail("Expected NullPointerException");


1188     private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
1189           for (Fun f : fs)
1190               try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
1191               catch (Throwable t) {
1192                   if (k.isAssignableFrom(t.getClass())) pass();
1193                   else unexpected(t);}}
1194     static byte[] serializedForm(Object obj) {
1195         try {
1196             ByteArrayOutputStream baos = new ByteArrayOutputStream();
1197             new ObjectOutputStream(baos).writeObject(obj);
1198             return baos.toByteArray();
1199         } catch (IOException e) { throw new Error(e); }}
1200     static Object readObject(byte[] bytes)
1201         throws IOException, ClassNotFoundException {
1202         InputStream is = new ByteArrayInputStream(bytes);
1203         return new ObjectInputStream(is).readObject();}
1204     @SuppressWarnings("unchecked")
1205     static <T> T serialClone(T obj) {
1206         try { return (T) readObject(serializedForm(obj)); }
1207         catch (Exception e) { throw new Error(e); }}


















1208 }


   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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     6207984 6272521 6192552 6269713 6197726 6260652 5073546 4137464
  27  *          4155650 4216399 4294891 6282555 6318622 6355327 6383475 6420753
  28  *          6431845 4802633 6570566 6570575 6570631 6570924 6691185 6691215
  29  *          4802647
  30  * @summary Run many tests on many Collection and Map implementations
  31  * @author  Martin Buchholz
  32  * @run main MOAT
  33  * @run main/othervm -XX:+AggressiveOpts MOAT
  34  */
  35 
  36 /* Mother Of All (Collection) Tests
  37  *
  38  * Testing of collection classes is often spotty, because many tests
  39  * need to be performed on many implementations, but the onus on
  40  * writing the tests falls on the engineer introducing the new
  41  * implementation.
  42  *
  43  * The idea of this mega-test is that:
  44  *
  45  * An engineer adding a new collection implementation could simply add
  46  * their new implementation to a list of implementations in this
  47  * test's main method.  Any general purpose Collection<Integer> or
  48  * Map<Integer,Integer> class is appropriate.
  49  *
  50  * An engineer fixing a regression could add their regression test here and
  51  * simultaneously test all other implementations.
  52  */
  53 
  54 import java.io.*;
  55 import java.util.*;
  56 import java.util.concurrent.*;
  57 import static java.util.Collections.*;
  58 
  59 public class MOAT {
  60     public static void realMain(String[] args) {
  61 
  62         testCollection(new NewAbstractCollection<Integer>());
  63         testCollection(new NewAbstractSet<Integer>());
  64         testCollection(new LinkedHashSet<Integer>());
  65         testCollection(new HashSet<Integer>());
  66         testCollection(new Vector<Integer>());
  67         testCollection(new Vector<Integer>().subList(0,0));
  68         testCollection(new ArrayDeque<Integer>());
  69         testCollection(new ArrayList<Integer>());
  70         testCollection(new ArrayList<Integer>().subList(0,0));
  71         testCollection(new LinkedList<Integer>());
  72         testCollection(new LinkedList<Integer>().subList(0,0));
  73         testCollection(new TreeSet<Integer>());
  74 
  75         testCollection(new CopyOnWriteArrayList<Integer>());
  76         testCollection(new CopyOnWriteArrayList<Integer>().subList(0,0));
  77         testCollection(new CopyOnWriteArraySet<Integer>());
  78         testCollection(new PriorityQueue<Integer>());
  79         testCollection(new PriorityBlockingQueue<Integer>());
  80         testCollection(new ArrayBlockingQueue<Integer>(20));
  81         testCollection(new LinkedBlockingQueue<Integer>(20));
  82         testCollection(new LinkedBlockingDeque<Integer>(20));
  83         testCollection(new ConcurrentLinkedDeque<Integer>());


 739 
 740         try {
 741             oneElement(c);
 742             checkFunctionalInvariants(c);
 743         }
 744         catch (Throwable t) { unexpected(t); }
 745 
 746         clear(c);      testNullElement(c);
 747         oneElement(c); testNullElement(c);
 748 
 749         clear(c);      testStringElement(c);
 750         oneElement(c); testStringElement(c);
 751 
 752         if (c.getClass().getName().matches(".*concurrent.*"))
 753             testConcurrentCollection(c);
 754 
 755         //----------------------------------------------------------------
 756         // The "all" operations should throw NPE when passed null
 757         //----------------------------------------------------------------
 758         {
 759             clear(c);
 760             try {
 761                 c.removeAll(null);
 762                 fail("Expected NullPointerException");
 763             }
 764             catch (NullPointerException e) { pass(); }
 765             catch (Throwable t) { unexpected(t); }
 766 
 767             oneElement(c);
 768             try {
 769                 c.removeAll(null);
 770                 fail("Expected NullPointerException");
 771             }
 772             catch (NullPointerException e) { pass(); }
 773             catch (Throwable t) { unexpected(t); }
 774 
 775             clear(c);
 776             try {
 777                 c.retainAll(null);
 778                 fail("Expected NullPointerException");
 779             }
 780             catch (NullPointerException e) { pass(); }
 781             catch (Throwable t) { unexpected(t); }
 782             
 783             oneElement(c);
 784             try {
 785                 c.retainAll(null);
 786                 fail("Expected NullPointerException");
 787             }
 788             catch (NullPointerException e) { pass(); }
 789             catch (Throwable t) { unexpected(t); }
 790 
 791             oneElement(c);
 792             try {
 793                 c.addAll(null);
 794                 fail("Expected NullPointerException");
 795             }
 796             catch (NullPointerException e) { pass(); }
 797             catch (Throwable t) { unexpected(t); }
 798 
 799             oneElement(c);
 800             try {
 801                 c.containsAll(null);
 802                 fail("Expected NullPointerException");


1207     private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
1208           for (Fun f : fs)
1209               try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
1210               catch (Throwable t) {
1211                   if (k.isAssignableFrom(t.getClass())) pass();
1212                   else unexpected(t);}}
1213     static byte[] serializedForm(Object obj) {
1214         try {
1215             ByteArrayOutputStream baos = new ByteArrayOutputStream();
1216             new ObjectOutputStream(baos).writeObject(obj);
1217             return baos.toByteArray();
1218         } catch (IOException e) { throw new Error(e); }}
1219     static Object readObject(byte[] bytes)
1220         throws IOException, ClassNotFoundException {
1221         InputStream is = new ByteArrayInputStream(bytes);
1222         return new ObjectInputStream(is).readObject();}
1223     @SuppressWarnings("unchecked")
1224     static <T> T serialClone(T obj) {
1225         try { return (T) readObject(serializedForm(obj)); }
1226         catch (Exception e) { throw new Error(e); }}
1227     private static class NewAbstractCollection<E> extends AbstractCollection<E> {
1228         ArrayList<E> list = new ArrayList<>();
1229         public boolean remove(Object obj) {
1230             return list.remove(obj);
1231         }
1232         public boolean add(E e) {
1233             return list.add(e);
1234         }
1235         public Iterator<E> iterator() {
1236             return list.iterator();
1237         }
1238         public int size() {
1239             return list.size();
1240         }
1241     }
1242     private static class NewAbstractSet<E> extends NewAbstractCollection<E> {
1243     }
1244     
1245 }