undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,82 @@
/*
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4946090
* @summary AllOf static factory is broken in Regular and Jumbo enum set
* @author Josh Bloch
*/
import java.util.*;
public class AllOf {
public static void main(String[] args) throws Exception {
test(Test0.class, 0);
test(Test33.class, 33);
test(Test127.class, 127);
test(Test128.class, 128);
}
static <T extends Enum<T>> void test(Class<T> enumClass,
int expected) {
EnumSet<T> set = EnumSet.allOf(enumClass);
if (set.size() != expected)
throw new RuntimeException(set.size() +" != " + expected);
set.toString();
}
public enum Test0 {}
public enum Test33 {
T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16,
T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,
T31, T32, T33
}
public enum Test127 {
T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,
T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,
T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,
T44, T45, T46, T47, T48, T49, T50, T51, T52, T53, T54, T55, T56, T57,
T58, T59, T60, T61, T62, T63, T64, T65, T66, T67, T68, T69, T70, T71,
T72, T73, T74, T75, T76, T77, T78, T79, T80, T81, T82, T83, T84, T85,
T86, T87, T88, T89, T90, T91, T92, T93, T94, T95, T96, T97, T98, T99,
T100, T101, T102, T103, T104, T105, T106, T107, T108, T109, T110, T111,
T112, T113, T114, T115, T116, T117, T118, T119, T120, T121, T122, T123,
T124, T125, T126
}
public enum Test128 {
T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,
T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,
T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,
T44, T45, T46, T47, T48, T49, T50, T51, T52, T53, T54, T55, T56, T57,
T58, T59, T60, T61, T62, T63, T64, T65, T66, T67, T68, T69, T70, T71,
T72, T73, T74, T75, T76, T77, T78, T79, T80, T81, T82, T83, T84, T85,
T86, T87, T88, T89, T90, T91, T92, T93, T94, T95, T96, T97, T98, T99,
T100, T101, T102, T103, T104, T105, T106, T107, T108, T109, T110, T111,
T112, T113, T114, T115, T116, T117, T118, T119, T120, T121, T122, T123,
T124, T125, T126, T127
}
}

View file

@ -0,0 +1,98 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6739302
* @summary Check that deserialization preserves EnumSet integrity
* @author Josh Bloch
*/
import java.util.*;
import java.io.*;
public class BogusEnumSet {
public static void main(String[] args) throws Throwable {
// This test tries to deserialize a bogus stream produced with
// hypothetical EnumSet without a writeReplace() method - i.e.
// not using serialization proxy pattern. It tests that such
// stream is not accepted as valid stream - the EnumSet class
// declares a readObject() method which throws exception.
byte[] serializedForm = {
(byte)0xac, (byte)0xed, 0x0, 0x5, 0x73, 0x72, 0x0, 0x18,
0x6a, 0x61, 0x76, 0x61, 0x2e, 0x75, 0x74, 0x69,
0x6c, 0x2e, 0x52, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x45,
0x6e, 0x75, 0x6d, 0x53, 0x65, 0x74, 0x2f, 0x58, 0x6f, (byte)0xc7,
0x7e, (byte)0xb0, (byte)0xd0, 0x7e, 0x2, 0x0, 0x1, 0x4a, 0x0, 0x8,
0x65, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x78, 0x72, 0x0,
0x11, 0x6a, 0x61, 0x76, 0x61, 0x2e, 0x75, 0x74, 0x69,
0x6c, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x53, 0x65, 0x74, 0xe,
0x3, 0x21, 0x6a, (byte)0xcd, (byte)0x8c, 0x29, (byte)0xdd, 0x2,
0x0, 0x2, 0x4c, 0x0, 0xb, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74,
0x54, 0x79, 0x70, 0x65, 0x74, 0x0, 0x11, 0x4c, 0x6a, 0x61, 0x76,
0x61, 0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 0x43, 0x6c, 0x61, 0x73,
0x73, 0x3b, 0x5b, 0x0, 0x8, 0x75, 0x6e, 0x69, 0x76, 0x65, 0x72,
0x73, 0x65, 0x74, 0x0, 0x11, 0x5b, 0x4c, 0x6a, 0x61, 0x76, 0x61,
0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 0x45, 0x6e, 0x75, 0x6d, 0x3b,
0x78, 0x70, 0x76, 0x72, 0x0, 0x16, 0x6a, 0x61, 0x76, 0x61, 0x2e,
0x6c, 0x61, 0x6e, 0x67, 0x2e, 0x54, 0x68, 0x72, 0x65, 0x61,
0x64, 0x24, 0x53, 0x74, 0x61, 0x74, 0x65, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x12, 0x0, 0x0, 0x78, 0x72, 0x0, 0xe, 0x6a, 0x61,
0x76, 0x61, 0x2e, 0x6c, 0x61, 0x6e, 0x67, 0x2e, 0x45, 0x6e, 0x75,
0x6d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x12, 0x0, 0x0, 0x78,
0x70, 0x75, 0x72, 0x0, 0x19, 0x5b, 0x4c, 0x6a, 0x61, 0x76, 0x61,
0x2e, 0x6c, 0x61, 0x6e, 0x67, 0x2e, 0x54, 0x68, 0x72, 0x65, 0x61,
0x64, 0x24, 0x53, 0x74, 0x61, 0x74, 0x65, 0x3b, 0x68, (byte)0xa3,
(byte)0xb5, (byte)0xd5, 0x11, 0x7d, 0x1b, (byte)0xb3, 0x2, 0x0,
0x0, 0x78, 0x70, 0x0, 0x0, 0x0, 0x6, 0x7e, 0x71, 0x0, 0x7e, 0x0,
0x5, 0x74, 0x0, 0x3, 0x4e, 0x45, 0x57, 0x7e, 0x71, 0x0, 0x7e, 0x0,
0x5, 0x74, 0x0, 0x8, 0x52, 0x55, 0x4e, 0x4e, 0x41, 0x42, 0x4c, 0x45,
0x7e, 0x71, 0x0, 0x7e, 0x0, 0x5, 0x74, 0x0, 0x7, 0x42, 0x4c, 0x4f,
0x43, 0x4b, 0x45, 0x44, 0x7e, 0x71, 0x0, 0x7e, 0x0, 0x5, 0x74, 0x0,
0x7, 0x57, 0x41, 0x49, 0x54, 0x49, 0x4e, 0x47, 0x7e, 0x71, 0x0,
0x7e, 0x0, 0x5, 0x74, 0x0, 0xd, 0x54, 0x49, 0x4d, 0x45, 0x44,
0x5f, 0x57, 0x41, 0x49, 0x54, 0x49, 0x4e, 0x47, 0x7e, 0x71, 0x0,
0x7e, 0x0, 0x5, 0x74, 0x0, 0xa, 0x54, 0x45, 0x52, 0x4d, 0x49,
0x4e, 0x41, 0x54, 0x45, 0x44, (byte)0xff, (byte)0xff, (byte)0xff,
(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff
};
try {
// Should fail, but instead creates corrupt EnumSet
@SuppressWarnings("unchecked")
EnumSet<Thread.State> es = (EnumSet<Thread.State>)
deserialize(serializedForm);
// Demonstrates corruption
System.out.println("Enum size: " + Thread.State.values().length); // 6
System.out.println("Set size: " + es.size()); // 64
System.out.println("Set: " + es); // Throws IndexOutOfBoundsException
throw new AssertionError("Expected exception InvalidObjectException not thrown");
} catch (java.io.InvalidObjectException expected) { /* OK */ }
}
private static Object deserialize(byte[] sf) throws Throwable {
return new ObjectInputStream(
new ByteArrayInputStream(sf))
.readObject();
}
}

View file

@ -0,0 +1,78 @@
/*
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4946096
* @summary ComplementOf static factory is broken in Regular and Jumbo enum set
* @author Josh Bloch
*/
import java.util.*;
public class ComplementOf {
public static void main(String[] args) throws Exception {
test(Test0.class);
test(MonthTest10.class);
test(Test127.class);
test(Test128.class);
}
static <T extends Enum<T>> void test(Class<T> enumClass) {
EnumSet<T> set = EnumSet.allOf(enumClass);
EnumSet<T> setComplement = EnumSet.complementOf(set);
if (setComplement.size() != 0)
throw new RuntimeException(setComplement.size() + " != 0");
setComplement.toString();
}
public enum Test0 { }
public enum MonthTest10 { Jan,Feb,Mar,Apr,May,Jun,July,Aug,Sep,Oct }
public enum Test127 {
T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,
T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,
T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,
T44, T45, T46, T47, T48, T49, T50, T51, T52, T53, T54, T55, T56, T57,
T58, T59, T60, T61, T62, T63, T64, T65, T66, T67, T68, T69, T70, T71,
T72, T73, T74, T75, T76, T77, T78, T79, T80, T81, T82, T83, T84, T85,
T86, T87, T88, T89, T90, T91, T92, T93, T94, T95, T96, T97, T98, T99,
T100, T101, T102, T103, T104, T105, T106, T107, T108, T109, T110, T111,
T112, T113, T114, T115, T116, T117, T118, T119, T120, T121, T122, T123,
T124, T125, T126
}
public enum Test128 {
T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,
T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,
T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,
T44, T45, T46, T47, T48, T49, T50, T51, T52, T53, T54, T55, T56, T57,
T58, T59, T60, T61, T62, T63, T64, T65, T66, T67, T68, T69, T70, T71,
T72, T73, T74, T75, T76, T77, T78, T79, T80, T81, T82, T83, T84, T85,
T86, T87, T88, T89, T90, T91, T92, T93, T94, T95, T96, T97, T98, T99,
T100, T101, T102, T103, T104, T105, T106, T107, T108, T109, T110, T111,
T112, T113, T114, T115, T116, T117, T118, T119, T120, T121, T122, T123,
T124, T125, T126, T127
}
}

View file

@ -0,0 +1,317 @@
/*
* Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4904135 4923181
* @summary Unit test for EnumSet
* @author Josh Bloch
* @author Neal Gafter
* @author Yo Ma Ma
* @key randomness
*/
import java.util.*;
import java.io.*;
public class EnumSetBash {
static Random rnd = new Random();
public static void main(String[] args) {
bash(Silly0.class);
bash(Silly1.class);
bash(Silly31.class);
bash(Silly32.class);
bash(Silly33.class);
bash(Silly63.class);
bash(Silly64.class);
bash(Silly65.class);
bash(Silly127.class);
bash(Silly128.class);
bash(Silly129.class);
bash(Silly500.class);
}
static <T extends Enum<T>> void bash(Class<T> enumClass) {
Enum[] universe = EnumSet.allOf(enumClass).toArray(new Enum[0]);
int numItr = 1000;
for (int i=0; i<numItr; i++) {
EnumSet<T> s1 = EnumSet.noneOf(enumClass);
EnumSet<T> s2 = clone(s1, enumClass);
AddRandoms(s1, universe);
AddRandoms(s2, universe);
EnumSet<T> intersection = clone(s1, enumClass);
intersection.retainAll(s2);
EnumSet<T> diff1 = clone(s1, enumClass); diff1.removeAll(s2);
EnumSet<T> diff2 = clone(s2, enumClass); diff2.removeAll(s1);
EnumSet<T> union = clone(s1, enumClass); union.addAll(s2);
if (diff1.removeAll(diff2))
fail("Set algebra identity 2 failed");
if (diff1.removeAll(intersection))
fail("Set algebra identity 3 failed");
if (diff2.removeAll(diff1))
fail("Set algebra identity 4 failed");
if (diff2.removeAll(intersection))
fail("Set algebra identity 5 failed");
if (intersection.removeAll(diff1))
fail("Set algebra identity 6 failed");
if (intersection.removeAll(diff1))
fail("Set algebra identity 7 failed");
intersection.addAll(diff1); intersection.addAll(diff2);
if (!intersection.equals(union))
fail("Set algebra identity 1 failed");
if (new HashSet<T>(union).hashCode() != union.hashCode())
fail("Incorrect hashCode computation.");
Iterator e = union.iterator();
while (e.hasNext())
if (!intersection.remove(e.next()))
fail("Couldn't remove element from copy.");
if (!intersection.isEmpty())
fail("Copy nonempty after deleting all elements.");
e = union.iterator();
while (e.hasNext()) {
Object o = e.next();
if (!union.contains(o))
fail("Set doesn't contain one of its elements.");
e.remove();
if (union.contains(o))
fail("Set contains element after deletion.");
}
if (!union.isEmpty())
fail("Set nonempty after deleting all elements.");
s1.clear();
if (!s1.isEmpty())
fail("Set nonempty after clear.");
}
}
// Done inefficiently so as to exercise various functions
static <E extends Enum<E>> EnumSet<E> clone(EnumSet<E> s, Class<E> cl) {
EnumSet<E> clone = null;
int method = rnd.nextInt(6);
switch(method) {
case 0:
clone = s.clone();
break;
case 1:
clone = EnumSet.noneOf(cl);
Collection arrayList = (Collection)Arrays.asList(s.toArray());
clone.addAll((Collection<E>)arrayList);
break;
case 2:
clone = EnumSet.copyOf(s);
break;
case 3:
clone = EnumSet.copyOf((Collection<E>)s);
break;
case 4:
if (s.isEmpty())
clone = EnumSet.copyOf((Collection<E>)s);
else
clone = EnumSet.copyOf((Collection<E>)(Collection)
Arrays.asList(s.toArray()));
break;
case 5:
clone = (EnumSet<E>) deepCopy(s);
}
if (!s.equals(clone))
fail("Set not equal to copy. " + method);
if (!s.containsAll(clone))
fail("Set does not contain copy. " + method);
if (!clone.containsAll(s))
fail("Copy does not contain set. " + method);
return clone;
}
// Utility method to do a deep copy of an object *very slowly* using
// serialization/deserialization
static <T> T deepCopy(T oldObj) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(oldObj);
oos.flush();
ByteArrayInputStream bin = new ByteArrayInputStream(
bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bin);
return (T) ois.readObject();
} catch(Exception e) {
throw new IllegalArgumentException(e.toString());
}
}
static <T extends Enum<T>> void AddRandoms(EnumSet<T> s, Enum[] universe) {
for (int i=0; i < universe.length * 2 / 3; i++) {
T e = (T) universe[rnd.nextInt(universe.length)];
boolean prePresent = s.contains(e);
int preSize = s.size();
boolean added = s.add(e);
if (!s.contains(e))
fail ("Element not present after addition.");
if (added == prePresent)
fail ("added == alreadyPresent");
int postSize = s.size();
if (added && preSize == postSize)
fail ("Add returned true, but size didn't change.");
if (!added && preSize != postSize)
fail ("Add returned false, but size changed.");
}
}
static void fail(String s) {
throw new RuntimeException(s);
}
public enum Silly0 { };
public enum Silly1 { e1 }
public enum Silly31 {
e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,
e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30
}
public enum Silly32 {
e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,
e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31
}
public enum Silly33 {
e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,
e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31,
e32
}
public enum Silly63 {
e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,
e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31,
e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46,
e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61,
e62
}
public enum Silly64 {
e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,
e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31,
e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46,
e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61,
e62, e63
}
public enum Silly65 {
e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,
e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31,
e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46,
e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61,
e62, e63, e64
}
public enum Silly127 {
e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,
e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31,
e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46,
e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61,
e62, e63, e64, e65, e66, e67, e68, e69, e70, e71, e72, e73, e74, e75, e76,
e77, e78, e79, e80, e81, e82, e83, e84, e85, e86, e87, e88, e89, e90, e91,
e92, e93, e94, e95, e96, e97, e98, e99, e100, e101, e102, e103, e104, e105,
e106, e107, e108, e109, e110, e111, e112, e113, e114, e115, e116, e117,
e118, e119, e120, e121, e122, e123, e124, e125, e126
}
public enum Silly128 {
e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,
e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31,
e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46,
e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61,
e62, e63, e64, e65, e66, e67, e68, e69, e70, e71, e72, e73, e74, e75, e76,
e77, e78, e79, e80, e81, e82, e83, e84, e85, e86, e87, e88, e89, e90, e91,
e92, e93, e94, e95, e96, e97, e98, e99, e100, e101, e102, e103, e104, e105,
e106, e107, e108, e109, e110, e111, e112, e113, e114, e115, e116, e117,
e118, e119, e120, e121, e122, e123, e124, e125, e126, e127
}
public enum Silly129 {
e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,
e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31,
e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46,
e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61,
e62, e63, e64, e65, e66, e67, e68, e69, e70, e71, e72, e73, e74, e75, e76,
e77, e78, e79, e80, e81, e82, e83, e84, e85, e86, e87, e88, e89, e90, e91,
e92, e93, e94, e95, e96, e97, e98, e99, e100, e101, e102, e103, e104, e105,
e106, e107, e108, e109, e110, e111, e112, e113, e114, e115, e116, e117,
e118, e119, e120, e121, e122, e123, e124, e125, e126, e127, e128
}
public enum Silly500 {
e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,
e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31,
e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46,
e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61,
e62, e63, e64, e65, e66, e67, e68, e69, e70, e71, e72, e73, e74, e75, e76,
e77, e78, e79, e80, e81, e82, e83, e84, e85, e86, e87, e88, e89, e90, e91,
e92, e93, e94, e95, e96, e97, e98, e99, e100, e101, e102, e103, e104, e105,
e106, e107, e108, e109, e110, e111, e112, e113, e114, e115, e116, e117,
e118, e119, e120, e121, e122, e123, e124, e125, e126, e127, e128, e129,
e130, e131, e132, e133, e134, e135, e136, e137, e138, e139, e140, e141,
e142, e143, e144, e145, e146, e147, e148, e149, e150, e151, e152, e153,
e154, e155, e156, e157, e158, e159, e160, e161, e162, e163, e164, e165,
e166, e167, e168, e169, e170, e171, e172, e173, e174, e175, e176, e177,
e178, e179, e180, e181, e182, e183, e184, e185, e186, e187, e188, e189,
e190, e191, e192, e193, e194, e195, e196, e197, e198, e199, e200, e201,
e202, e203, e204, e205, e206, e207, e208, e209, e210, e211, e212, e213,
e214, e215, e216, e217, e218, e219, e220, e221, e222, e223, e224, e225,
e226, e227, e228, e229, e230, e231, e232, e233, e234, e235, e236, e237,
e238, e239, e240, e241, e242, e243, e244, e245, e246, e247, e248, e249,
e250, e251, e252, e253, e254, e255, e256, e257, e258, e259, e260, e261,
e262, e263, e264, e265, e266, e267, e268, e269, e270, e271, e272, e273,
e274, e275, e276, e277, e278, e279, e280, e281, e282, e283, e284, e285,
e286, e287, e288, e289, e290, e291, e292, e293, e294, e295, e296, e297,
e298, e299, e300, e301, e302, e303, e304, e305, e306, e307, e308, e309,
e310, e311, e312, e313, e314, e315, e316, e317, e318, e319, e320, e321,
e322, e323, e324, e325, e326, e327, e328, e329, e330, e331, e332, e333,
e334, e335, e336, e337, e338, e339, e340, e341, e342, e343, e344, e345,
e346, e347, e348, e349, e350, e351, e352, e353, e354, e355, e356, e357,
e358, e359, e360, e361, e362, e363, e364, e365, e366, e367, e368, e369,
e370, e371, e372, e373, e374, e375, e376, e377, e378, e379, e380, e381,
e382, e383, e384, e385, e386, e387, e388, e389, e390, e391, e392, e393,
e394, e395, e396, e397, e398, e399, e400, e401, e402, e403, e404, e405,
e406, e407, e408, e409, e410, e411, e412, e413, e414, e415, e416, e417,
e418, e419, e420, e421, e422, e423, e424, e425, e426, e427, e428, e429,
e430, e431, e432, e433, e434, e435, e436, e437, e438, e439, e440, e441,
e442, e443, e444, e445, e446, e447, e448, e449, e450, e451, e452, e453,
e454, e455, e456, e457, e458, e459, e460, e461, e462, e463, e464, e465,
e466, e467, e468, e469, e470, e471, e472, e473, e474, e475, e476, e477,
e478, e479, e480, e481, e482, e483, e484, e485, e486, e487, e488, e489,
e490, e491, e492, e493, e494, e495, e496, e497, e498, e499
}
}

View file

@ -0,0 +1,101 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8227368
* @summary Test deserialization of a stream containing EnumSet.class object
*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.EnumSet;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class EnumSetClassSerialization {
public static void main(String[] args) throws Exception {
// EnumSet.class object serialized with JDK 8
int[] bytes = {
0xac, 0xed, 0x00, 0x05, 0x76, 0x72, 0x00, 0x11, 0x6a, 0x61, 0x76, 0x61, 0x2e, 0x75, 0x74, 0x69,
0x6c, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x53, 0x65, 0x74, 0x0e, 0x03, 0x21, 0x6a, 0xcd, 0x8c, 0x29,
0xdd, 0x02, 0x00, 0x02, 0x4c, 0x00, 0x0b, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x54, 0x79,
0x70, 0x65, 0x74, 0x00, 0x11, 0x4c, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f,
0x43, 0x6c, 0x61, 0x73, 0x73, 0x3b, 0x5b, 0x00, 0x08, 0x75, 0x6e, 0x69, 0x76, 0x65, 0x72, 0x73,
0x65, 0x74, 0x00, 0x11, 0x5b, 0x4c, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f,
0x45, 0x6e, 0x75, 0x6d, 0x3b, 0x78, 0x70
};
InputStream in = new InputStream() {
int i = 0;
@Override
public int read() {
return i < bytes.length ? bytes[i++] & 0xFF : -1;
}
};
ObjectInputStream ois = new ObjectInputStream(in);
Object res = ois.readObject();
if (res != EnumSet.class) {
throw new AssertionError(
"Expected: " + EnumSet.class + ", got: " + res);
}
}
/**
* This class can be used to print out lines that constitute
* the 'bytes' variable initializer in the test.
*/
public static class Serializer {
public static void main(String[] args) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(EnumSet.class);
oos.close();
byte[] bytes = baos.toByteArray();
int bpl = 16;
System.out.print(
IntStream
.range(0, (bytes.length + bpl - 1) / bpl)
.mapToObj(i -> IntStream
.range(
i * bpl,
Math.min(i * bpl + bpl, bytes.length)
)
.mapToObj(ii -> {
String s = Integer.toHexString(bytes[ii] & 0xFF);
return s.length() == 1 ? "0x0" + s : "0x" + s;
})
.collect(Collectors.joining(", "))
)
.collect(Collectors.joining(",\n ", "int[] bytes = {\n ", "\n};"))
);
}
}
}

View file

@ -0,0 +1,97 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @run junit EnumSetSpliteratorTest
* @bug 8179918
* @summary EnumSet spliterator should report SORTED, ORDERED, NONNULL
*/
import java.util.EnumSet;
import java.util.List;
import java.util.Spliterator;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class EnumSetSpliteratorTest {
private enum Empty {}
private enum Small {
a, b, c, d
}
private enum Large {
e00, e01, e02, e03, e04, e05, e06, e07,
e08, e09, e0A, e0B, e0C, e0D, e0E, e0F,
e10, e11, e12, e13, e14, e15, e16, e17,
e18, e19, e1A, e1B, e1C, e1D, e1E, e1F,
e20, e21, e22, e23, e24, e25, e26, e27,
e28, e29, e2A, e2B, e2C, e2D, e2E, e2F,
e30, e31, e32, e33, e34, e35, e36, e37,
e38, e39, e3A, e3B, e3C, e3D, e3E, e3F,
e40, e41, e42, e43, e44, e45, e46, e47,
e48, e49, e4A, e4B, e4C, e4D, e4E, e4F
}
@Test
public void testSpliteratorCharacteristics() {
assertSpliteratorCharacteristics(EnumSet.allOf(Empty.class));
assertSpliteratorCharacteristics(EnumSet.allOf(Small.class));
assertSpliteratorCharacteristics(EnumSet.allOf(Large.class));
assertSpliteratorCharacteristics(EnumSet.noneOf(Empty.class));
assertSpliteratorCharacteristics(EnumSet.noneOf(Small.class));
assertSpliteratorCharacteristics(EnumSet.noneOf(Large.class));
assertSpliteratorCharacteristics(EnumSet.of(Small.a, Small.d));
assertSpliteratorCharacteristics(EnumSet.range(Small.a, Small.c));
assertSpliteratorCharacteristics(EnumSet.range(Large.e02, Large.e4D));
assertSpliteratorCharacteristics(EnumSet.complementOf(EnumSet.of(Small.c)));
assertSpliteratorCharacteristics(EnumSet.complementOf(EnumSet.of(Large.e00, Large.e4F)));
}
@Test
public void testEncounterOrder() {
assertEquals(List.of(Small.values()), EnumSet.allOf(Small.class).stream().toList());
assertEquals(List.of(Large.values()), EnumSet.allOf(Large.class).stream().toList());
}
private static final int EXPECTED_CHARACTERISTICS = (
Spliterator.DISTINCT | Spliterator.SORTED | Spliterator.ORDERED |
Spliterator.NONNULL | Spliterator.SIZED | Spliterator.SUBSIZED);
private static void assertSpliteratorCharacteristics(EnumSet<?> enumSet) {
Spliterator<?> spliterator = enumSet.spliterator();
assertTrue(spliterator.hasCharacteristics(Spliterator.DISTINCT), "Missing DISTINCT");
assertTrue(spliterator.hasCharacteristics(Spliterator.SORTED), "Missing SORTED");
assertTrue(spliterator.hasCharacteristics(Spliterator.ORDERED), "Missing ORDERED");
assertTrue(spliterator.hasCharacteristics(Spliterator.NONNULL), "Missing NONNULL");
assertTrue(spliterator.hasCharacteristics(Spliterator.SIZED), "Missing SIZED");
assertTrue(spliterator.hasCharacteristics(Spliterator.SUBSIZED), "Missing SUBSIZED");
assertEquals(EXPECTED_CHARACTERISTICS, spliterator.characteristics(), "Unexpected characteristics");
}
}

View file

@ -0,0 +1,245 @@
/*
* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 5050285
* @summary Inserting enum of wrong type does horrible things to EnumSet/Map
* @author Josh Bloch
*/
import java.util.*;
public class InsertWrongType {
public static void main(String[] args) throws Exception {
// Ordinal in range
addIllTypedElt(Test32.class, Test33.T1); // Regular
addIllTypedElt(Test127.class, Test128.T1); // Jumbo
// Ordinal out of range
addIllTypedElt(Test32.class, Test33.T33); // Regular
addIllTypedElt(Test127.class, Test128.T128); // Jumbo
// Ordinal in range
addAllIllTypedElt(Test32.class, Test33.T1); // Regular
addAllIllTypedElt(Test127.class, Test128.T1); // Jumbo
// Ordinal out of range
addAllIllTypedElt(Test32.class, Test33.T33); // Regular
addAllIllTypedElt(Test127.class, Test128.T128); // Jumbo
addAllEmptyMistypedEnumSet(Test32.class, Test33.class); // Regular
addAllEmptyMistypedEnumSet(Test127.class, Test128.class); // Jumbo
heterogeneousCopyOf(Test32.T1, Test33.T2); // Regular
heterogeneousCopyOf(Test127.T1, Test128.T2); // Jumbo
heterogeneousOf2(Test32.T1, Test33.T2); // Regular
heterogeneousOf2(Test127.T1, Test128.T2); // Jumbo
heterogeneousOf3(Test32.T1, Test33.T2); // Regular
heterogeneousOf3(Test127.T1, Test128.T2); // Jumbo
heterogeneousOf4(Test32.T1, Test33.T2); // Regular
heterogeneousOf4(Test127.T1, Test128.T2); // Jumbo
heterogeneousOf5(Test32.T1, Test33.T2); // Regular
heterogeneousOf5(Test127.T1, Test128.T2); // Jumbo
heterogeneousOfVar(Test32.T1, Test33.T2); // Regular
heterogeneousOfVar(Test127.T1, Test128.T2); // Jumbo
putIllTypedKey(Test32.class, Test33.T1);
putIllTypedKey(Test32.class, Test33.T33);
putAllIllTypedKey(Test32.class, Test33.T1);
putAllIllTypedKey(Test32.class, Test33.T33);
putAllIllTypedKeyEnumMap(Test32.class, Test33.T1);
putAllIllTypedKeyEnumMap(Test32.class, Test33.T33);
putAllEmptyMistypedEnumMap(Test32.class, Test33.class);
}
static void addIllTypedElt(Class enumClass, Enum elt) {
EnumSet set = EnumSet.noneOf(enumClass);
try {
set.add(elt);
} catch(ClassCastException e) {
return;
}
throw new RuntimeException("Incorrectly typed add succeeded");
}
static void addAllIllTypedElt(Class enumClass, Enum elt) {
EnumSet set = EnumSet.noneOf(enumClass);
try {
set.addAll(Collections.singleton(elt));
} catch(ClassCastException e) {
return;
}
throw new RuntimeException("Incorrectly typed addAll succeeded");
}
static void addAllEmptyMistypedEnumSet(Class destClass, Class srcClass) {
EnumSet dest = EnumSet.noneOf(destClass);
EnumSet src = EnumSet.noneOf(srcClass);
dest.addAll(src);
}
static void heterogeneousCopyOf(Enum e1, Enum e2) {
List list = new ArrayList();
list.add(e1);
list.add(e2);
try {
EnumSet.copyOf(list);
} catch(ClassCastException e) {
return;
}
throw new RuntimeException("heterogeneous copyOf succeeded");
}
static void heterogeneousOf2(Enum e1, Enum e2) {
try {
EnumSet.of(e1, e2);
} catch(ClassCastException e) {
return;
}
throw new RuntimeException("heterogeneous of (2 args) succeeded");
}
static void heterogeneousOf3(Enum e1, Enum e2) {
try {
EnumSet.of(e1, e1, e2);
} catch(ClassCastException e) {
return;
}
throw new RuntimeException("heterogeneous of (3 args) succeeded");
}
static void heterogeneousOf4(Enum e1, Enum e2) {
try {
EnumSet.of(e1, e1, e1, e2);
} catch(ClassCastException e) {
return;
}
throw new RuntimeException("heterogeneous of (4 args) succeeded");
}
static void heterogeneousOf5(Enum e1, Enum e2) {
try {
EnumSet.of(e1, e1, e1, e1, e2);
} catch(ClassCastException e) {
return;
}
throw new RuntimeException("heterogeneous of (5 args) succeeded");
}
static void heterogeneousOfVar(Enum e1, Enum e2) {
try {
EnumSet.of(e1, e1, e1, e1, e1, e2);
} catch(ClassCastException e) {
return;
}
throw new RuntimeException("heterogeneous of (Var args) succeeded");
}
static void putIllTypedKey(Class enumClass, Enum elt) {
EnumMap map = new EnumMap(enumClass);
try {
map.put(elt, "foofy");
} catch(ClassCastException e) {
return;
}
throw new RuntimeException("Incorrectly typed put succeeded");
}
static void putAllIllTypedKey(Class enumClass, Enum elt) {
EnumMap dest = new EnumMap(enumClass);
Map src = new HashMap();
src.put(elt, "goofy");
try {
dest.putAll(src);
} catch(ClassCastException e) {
return;
}
throw new RuntimeException("Incorrectly typed putAll succeeded");
}
static void putAllIllTypedKeyEnumMap(Class enumClass, Enum elt) {
EnumMap dest = new EnumMap(enumClass);
Map src = new EnumMap(elt.getClass());
src.put(elt, "goofy");
try {
dest.putAll(src);
} catch(ClassCastException e) {
return;
}
throw new RuntimeException("Incorrectly typed enum put All succeeded");
}
static void putAllEmptyMistypedEnumMap(Class destClass, Class srcClass) {
EnumMap dest = new EnumMap(destClass);
EnumMap src = new EnumMap(srcClass);
dest.putAll(src);
}
enum Test32 {
T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16,
T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,
T31, T32
}
enum Test33 {
T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16,
T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,
T31, T32, T33
}
enum Test127 {
T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,
T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,
T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,
T44, T45, T46, T47, T48, T49, T50, T51, T52, T53, T54, T55, T56, T57,
T58, T59, T60, T61, T62, T63, T64, T65, T66, T67, T68, T69, T70, T71,
T72, T73, T74, T75, T76, T77, T78, T79, T80, T81, T82, T83, T84, T85,
T86, T87, T88, T89, T90, T91, T92, T93, T94, T95, T96, T97, T98, T99,
T100, T101, T102, T103, T104, T105, T106, T107, T108, T109, T110, T111,
T112, T113, T114, T115, T116, T117, T118, T119, T120, T121, T122, T123,
T124, T125, T126, T127
}
enum Test128 {
T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,
T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,
T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,
T44, T45, T46, T47, T48, T49, T50, T51, T52, T53, T54, T55, T56, T57,
T58, T59, T60, T61, T62, T63, T64, T65, T66, T67, T68, T69, T70, T71,
T72, T73, T74, T75, T76, T77, T78, T79, T80, T81, T82, T83, T84, T85,
T86, T87, T88, T89, T90, T91, T92, T93, T94, T95, T96, T97, T98, T99,
T100, T101, T102, T103, T104, T105, T106, T107, T108, T109, T110, T111,
T112, T113, T114, T115, T116, T117, T118, T119, T120, T121, T122, T123,
T124, T125, T126, T127, T128
}
}

View file

@ -0,0 +1,59 @@
/*
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4958003
* @summary Range static factory fails to compute size in Jumbo enum set
* @author Josh Bloch
*/
import java.util.*;
public class JumboRange {
public static void main(String[] args) {
test(Test127.class, Test127.T2, Test127.T6);
test(Test127.class, Test127.T126, Test127.T126);
test(Test127.class, Test127.T0, Test127.T126);
}
static <T extends Enum<T>> void test(Class<T> enumClass, T e0,T e1) {
EnumSet<T> range = EnumSet.range(e0, e1);
if (range.size() != e1.ordinal() - e0.ordinal() + 1)
throw new RuntimeException(range.size() + " != " +
(e1.ordinal() - e0.ordinal() + 1));
}
public enum Test127 {
T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,
T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,
T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,
T44, T45, T46, T47, T48, T49, T50, T51, T52, T53, T54, T55, T56, T57,
T58, T59, T60, T61, T62, T63, T64, T65, T66, T67, T68, T69, T70, T71,
T72, T73, T74, T75, T76, T77, T78, T79, T80, T81, T82, T83, T84, T85,
T86, T87, T88, T89, T90, T91, T92, T93, T94, T95, T96, T97, T98, T99,
T100, T101, T102, T103, T104, T105, T106, T107, T108, T109, T110, T111,
T112, T113, T114, T115, T116, T117, T118, T119, T120, T121, T122, T123,
T124, T125, T126
}
}

View file

@ -0,0 +1,96 @@
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* Portions Copyright (c) 2011 IBM Corporation
*/
/*
* @test
* @bug 7014637
* @summary EnumSet's iterator.remove() can be resilient to set's modification.
* @author Neil Richards <neil.richards@ngmr.net>, <neil_richards@uk.ibm.com>
*/
import java.util.EnumSet;
import java.util.Iterator;
import java.util.Set;
public class LargeEnumIteratorRemoveResilience {
// enum with more than 64 values
private static enum LargeEnum {
e00, e01, e02, e03, e04, e05, e06, e07,
e08, e09, e0A, e0B, e0C, e0D, e0E, e0F,
e10, e11, e12, e13, e14, e15, e16, e17,
e18, e19, e1A, e1B, e1C, e1D, e1E, e1F,
e20, e21, e22, e23, e24, e25, e26, e27,
e28, e29, e2A, e2B, e2C, e2D, e2E, e2F,
e30, e31, e32, e33, e34, e35, e36, e37,
e38, e39, e3A, e3B, e3C, e3D, e3E, e3F,
e40, e41, e42, e43, e44, e45, e46, e47,
e48, e49, e4A, e4B, e4C, e4D, e4E, e4F,
}
public static void main(final String[] args) throws Exception {
final Set<LargeEnum> set = EnumSet.noneOf(LargeEnum.class);
set.add(LargeEnum.e2D);
set.add(LargeEnum.e42);
final Iterator<LargeEnum> iterator = set.iterator();
int size = set.size();
LargeEnum element = iterator.next();
iterator.remove();
checkSetAfterRemoval(set, size, element);
size = set.size();
element = iterator.next();
set.remove(element);
checkSetAfterRemoval(set, size, element);
// The Java API declares that the behaviour here - to call
// iterator.remove() after the underlying collection has been
// modified - is "unspecified".
// However, in the case of iterators for EnumSet, it is easy to
// implement their remove() operation such that the set is
// unmodified if it is called for an element that has already been
// removed from the set - this being the naturally "resilient"
// behaviour.
iterator.remove();
checkSetAfterRemoval(set, size, element);
}
private static void checkSetAfterRemoval(final Set<LargeEnum> set,
final int origSize, final LargeEnum removedElement)
throws Exception {
if (set.size() != (origSize - 1)) {
throw new Exception("Test FAILED: Unexpected set size after removal; expected '" + (origSize - 1) + "' but found '" + set.size() + "'");
}
if (set.contains(removedElement)) {
throw new Exception("Test FAILED: Element returned from iterator unexpectedly still in set after removal.");
}
}
}

View file

@ -0,0 +1,66 @@
/*
* Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6276988
* @summary All enum constants in a class should share a single "universe".
* @modules java.base/java.util:open
*/
import java.lang.reflect.Field;
import java.math.RoundingMode;
import java.util.EnumSet;
public class OneUniverse {
private static final Field universeField;
static {
try {
universeField = EnumSet.class.getDeclaredField("universe");
} catch (NoSuchFieldException e) {
throw new AssertionError(e);
}
universeField.setAccessible(true);
}
public static void main(String... args) {
EnumSet<RoundingMode> noneSet = EnumSet.noneOf(RoundingMode.class);
EnumSet<RoundingMode> allSet = EnumSet.allOf(RoundingMode.class);
if (getUniverse(noneSet) != getUniverse(allSet)) {
throw new AssertionError(
"Multiple universes detected. Inform the bridge!");
}
}
private static <E extends Enum<E>> Enum<E>[] getUniverse(EnumSet<E> set) {
try {
return (Enum<E>[]) universeField.get(set);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
}
}

View file

@ -0,0 +1,66 @@
/*
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4952736
* @summary Range static factory is broken in Regular and Jumbo enum set
* @author Josh Bloch
*/
import java.util.*;
public class Range {
public static void main(String[] args) {
test(Test33.class, Test33.T6, Test33.T2);
test(Test127.class, Test127.T6, Test127.T2);
}
static <T extends Enum<T>> void test(Class<T> enumClass, T e0,T e1) {
try {
EnumSet<T> range = EnumSet.range(e0, e1);
} catch(IllegalArgumentException e) {
return;
}
throw new RuntimeException(enumClass.toString());
}
public enum Test33 {
T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16,
T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,
T31, T32, T33
}
public enum Test127 {
T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,
T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,
T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,
T44, T45, T46, T47, T48, T49, T50, T51, T52, T53, T54, T55, T56, T57,
T58, T59, T60, T61, T62, T63, T64, T65, T66, T67, T68, T69, T70, T71,
T72, T73, T74, T75, T76, T77, T78, T79, T80, T81, T82, T83, T84, T85,
T86, T87, T88, T89, T90, T91, T92, T93, T94, T95, T96, T97, T98, T99,
T100, T101, T102, T103, T104, T105, T106, T107, T108, T109, T110, T111,
T112, T113, T114, T115, T116, T117, T118, T119, T120, T121, T122, T123,
T124, T125, T126
}
}

View file

@ -0,0 +1,84 @@
/*
* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6377356
* @summary Test EnumSet.retainAll
* @author Josh Bloch, Martin Buchholz
*/
import java.util.*;
public class RetainAll {
enum RegularA { A0, A2 }
enum RegularB { B0, B1 }
enum JumboA {
A00, A01, A02, A03, A04, A05, A06, A07, A08, A09,
A10, A11, A12, A13, A14, A15, A16, A17, A18, A19,
A20, A21, A22, A23, A24, A25, A26, A27, A28, A29,
A30, A31, A32, A33, A34, A35, A36, A37, A38, A39,
A40, A41, A42, A43, A44, A45, A46, A47, A48, A49,
A50, A51, A52, A53, A54, A55, A56, A57, A58, A59,
A60, A61, A62, A63, A64, A65, A66, A67, A68, A69,
}
enum JumboB {
B00, B01, B02, B03, B04, B05, B06, B07, B08, B09,
B10, B11, B12, B13, B14, B15, B16, B17, B18, B19,
B20, B21, B22, B23, B24, B25, B26, B27, B28, B29,
B30, B31, B32, B33, B34, B35, B36, B37, B38, B39,
B40, B41, B42, B43, B44, B45, B46, B47, B48, B49,
B50, B51, B52, B53, B54, B55, B56, B57, B58, B59,
B60, B61, B62, B63, B64, B65, B66, B67, B68, B69,
}
private static void realMain(String[] args) throws Throwable {
Set<RegularA> ras = EnumSet.noneOf(RegularA.class);
Set<RegularB> rbs = EnumSet.noneOf(RegularB.class);
Set<JumboA> jas = EnumSet.noneOf(JumboA.class);
Set<JumboB> jbs = EnumSet.noneOf(JumboB.class);
check(ras.getClass().getName().matches(".*Regular.*"));
check(jas.getClass().getName().matches(".*Jumbo.*"));
check(! ras.retainAll(ras));
check(! ras.retainAll(rbs));
check(! jas.retainAll(jas));
check(! jas.retainAll(jbs));
check(! ras.retainAll(jas));
check(! jas.retainAll(ras));
}
//--------------------- Infrastructure ---------------------------
static volatile int passed = 0, failed = 0;
static void pass() {passed++;}
static void fail() {failed++; Thread.dumpStack();}
static void fail(String msg) {System.out.println(msg); fail();}
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
static void check(boolean cond) {if (cond) pass(); else fail();}
static void equal(Object x, Object y) {
if (x == null ? y == null : x.equals(y)) pass();
else fail(x + " not equal to " + y);}
public static void main(String[] args) throws Throwable {
try {realMain(args);} catch (Throwable t) {unexpected(t);}
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
if (failed > 0) throw new AssertionError("Some tests failed");}
}

View file

@ -0,0 +1,85 @@
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* Portions Copyright (c) 2011 IBM Corporation
*/
/*
* @test
* @bug 7014637
* @summary EnumSet's iterator.remove() can be resilient to set's modification.
* @author Neil Richards <neil.richards@ngmr.net>, <neil_richards@uk.ibm.com>
*/
import java.util.EnumSet;
import java.util.Iterator;
import java.util.Set;
public class SmallEnumIteratorRemoveResilience {
// enum with less than 64 values
private static enum SmallEnum { e0, e1, e2 }
public static void main(final String[] args) throws Exception {
final Set<SmallEnum> set = EnumSet.noneOf(SmallEnum.class);
set.add(SmallEnum.e0);
set.add(SmallEnum.e1);
final Iterator<SmallEnum> iterator = set.iterator();
int size = set.size();
SmallEnum element = iterator.next();
iterator.remove();
checkSetAfterRemoval(set, size, element);
size = set.size();
element = iterator.next();
set.remove(element);
checkSetAfterRemoval(set, size, element);
// The Java API declares that the behaviour here - to call
// iterator.remove() after the underlying collection has been
// modified - is "unspecified".
// However, in the case of iterators for EnumSet, it is easy to
// implement their remove() operation such that the set is
// unmodified if it is called for an element that has already been
// removed from the set - this being the naturally "resilient"
// behaviour.
iterator.remove();
checkSetAfterRemoval(set, size, element);
}
private static void checkSetAfterRemoval(final Set<SmallEnum> set,
final int origSize, final SmallEnum removedElement)
throws Exception {
if (set.size() != (origSize - 1)) {
throw new Exception("Test FAILED: Unexpected set size after removal; expected '" + (origSize - 1) + "' but found '" + set.size() + "'");
}
if (set.contains(removedElement)) {
throw new Exception("Test FAILED: Element returned from iterator unexpectedly still in set after removal.");
}
}
}