EnumSetBash.java revision 0:37a05a11f281
1184610Salfred/* 2184610Salfred * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved. 3184610Salfred * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4184610Salfred * 5184610Salfred * This code is free software; you can redistribute it and/or modify it 6184610Salfred * under the terms of the GNU General Public License version 2 only, as 7184610Salfred * published by the Free Software Foundation. 8184610Salfred * 9184610Salfred * This code is distributed in the hope that it will be useful, but WITHOUT 10184610Salfred * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11184610Salfred * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12184610Salfred * version 2 for more details (a copy is included in the LICENSE file that 13184610Salfred * accompanied this code). 14184610Salfred * 15184610Salfred * You should have received a copy of the GNU General Public License version 16184610Salfred * 2 along with this work; if not, write to the Free Software Foundation, 17184610Salfred * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18184610Salfred * 19184610Salfred * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20184610Salfred * CA 95054 USA or visit www.sun.com if you need additional information or 21184610Salfred * have any questions. 22184610Salfred */ 23184610Salfred 24184610Salfred/* 25184610Salfred * @test 26184610Salfred * @bug 4904135 4923181 27184610Salfred * @summary Unit test for EnumSet 28184610Salfred * @author Josh Bloch 29194677Sthompsa * @author Neal Gafter 30194677Sthompsa * @author Yo Ma Ma 31194677Sthompsa * 32194677Sthompsa * @compile -source 1.5 EnumSetBash.java 33194677Sthompsa * @run main EnumSetBash 34194677Sthompsa */ 35194677Sthompsa 36194677Sthompsaimport java.util.*; 37194677Sthompsaimport java.io.*; 38194677Sthompsa 39194677Sthompsapublic class EnumSetBash { 40194677Sthompsa static Random rnd = new Random(); 41194677Sthompsa 42194677Sthompsa public static void main(String[] args) { 43194677Sthompsa bash(Silly0.class); 44194677Sthompsa bash(Silly1.class); 45194677Sthompsa bash(Silly31.class); 46194677Sthompsa bash(Silly32.class); 47194677Sthompsa bash(Silly33.class); 48188942Sthompsa bash(Silly63.class); 49188942Sthompsa bash(Silly64.class); 50194677Sthompsa bash(Silly65.class); 51188746Sthompsa bash(Silly127.class); 52184610Salfred bash(Silly128.class); 53194228Sthompsa bash(Silly129.class); 54188942Sthompsa bash(Silly500.class); 55188942Sthompsa } 56184610Salfred 57188942Sthompsa static <T extends Enum<T>> void bash(Class<T> enumClass) { 58184610Salfred Enum[] universe = EnumSet.allOf(enumClass).toArray(new Enum[0]); 59188942Sthompsa int numItr = 1000; 60188942Sthompsa 61184610Salfred for (int i=0; i<numItr; i++) { 62275605Shselasky EnumSet<T> s1 = EnumSet.noneOf(enumClass); 63184610Salfred EnumSet<T> s2 = clone(s1, enumClass); 64184610Salfred AddRandoms(s1, universe); 65194228Sthompsa AddRandoms(s2, universe); 66184610Salfred 67184610Salfred EnumSet<T> intersection = clone(s1, enumClass); 68184610Salfred intersection.retainAll(s2); 69184610Salfred EnumSet<T> diff1 = clone(s1, enumClass); diff1.removeAll(s2); 70184610Salfred EnumSet<T> diff2 = clone(s2, enumClass); diff2.removeAll(s1); 71184610Salfred EnumSet<T> union = clone(s1, enumClass); union.addAll(s2); 72184610Salfred 73194228Sthompsa if (diff1.removeAll(diff2)) 74184610Salfred fail("Set algebra identity 2 failed"); 75201071Sthompsa if (diff1.removeAll(intersection)) 76201071Sthompsa fail("Set algebra identity 3 failed"); 77201071Sthompsa if (diff2.removeAll(diff1)) 78200887Sthompsa fail("Set algebra identity 4 failed"); 79201071Sthompsa if (diff2.removeAll(intersection)) 80201071Sthompsa fail("Set algebra identity 5 failed"); 81194228Sthompsa if (intersection.removeAll(diff1)) 82201071Sthompsa fail("Set algebra identity 6 failed"); 83201071Sthompsa if (intersection.removeAll(diff1)) 84201071Sthompsa fail("Set algebra identity 7 failed"); 85201071Sthompsa 86201071Sthompsa intersection.addAll(diff1); intersection.addAll(diff2); 87201071Sthompsa if (!intersection.equals(union)) 88201071Sthompsa fail("Set algebra identity 1 failed"); 89201071Sthompsa 90201071Sthompsa if (new HashSet<T>(union).hashCode() != union.hashCode()) 91201071Sthompsa fail("Incorrect hashCode computation."); 92201071Sthompsa 93201071Sthompsa Iterator e = union.iterator(); 94201071Sthompsa while (e.hasNext()) 95201071Sthompsa if (!intersection.remove(e.next())) 96262732Shselasky fail("Couldn't remove element from copy."); 97201071Sthompsa if (!intersection.isEmpty()) 98252501Shrs fail("Copy nonempty after deleting all elements."); 99218988Shselasky 100201071Sthompsa e = union.iterator(); 101201071Sthompsa while (e.hasNext()) { 102201071Sthompsa Object o = e.next(); 103201071Sthompsa if (!union.contains(o)) 104201071Sthompsa fail("Set doesn't contain one of its elements."); 105201071Sthompsa e.remove(); 106201071Sthompsa if (union.contains(o)) 107184610Salfred fail("Set contains element after deletion."); 108201071Sthompsa } 109201071Sthompsa if (!union.isEmpty()) 110201071Sthompsa fail("Set nonempty after deleting all elements."); 111210516Sgavin 112201071Sthompsa s1.clear(); 113271159Skevlo if (!s1.isEmpty()) 114201071Sthompsa fail("Set nonempty after clear."); 115201071Sthompsa } 116210543Sgavin } 117245947Shselasky 118201071Sthompsa // Done inefficiently so as to exercise various functions 119201071Sthompsa static <E extends Enum<E>> EnumSet<E> clone(EnumSet<E> s, Class<E> cl) { 120201071Sthompsa EnumSet<E> clone = null; 121201071Sthompsa int method = rnd.nextInt(6); 122214800Shselasky switch(method) { 123184610Salfred case 0: 124201071Sthompsa clone = s.clone(); 125201071Sthompsa break; 126210515Sgavin case 1: 127210515Sgavin clone = EnumSet.noneOf(cl); 128201071Sthompsa Collection arrayList = (Collection)Arrays.asList(s.toArray()); 129184610Salfred clone.addAll((Collection<E>)arrayList); 130238718Semaste break; 131201071Sthompsa case 2: 132239298Shselasky clone = EnumSet.copyOf(s); 133239298Shselasky break; 134271018Shselasky case 3: 135271017Shselasky clone = EnumSet.copyOf((Collection<E>)s); 136200395Sthompsa break; 137201071Sthompsa case 4: 138201071Sthompsa if (s.isEmpty()) 139201071Sthompsa clone = EnumSet.copyOf((Collection<E>)s); 140201071Sthompsa else 141201071Sthompsa clone = EnumSet.copyOf((Collection<E>)(Collection) 142201071Sthompsa Arrays.asList(s.toArray())); 143201071Sthompsa break; 144218422Sn_hibma case 5: 145201071Sthompsa clone = (EnumSet<E>) deepCopy(s); 146230238Shselasky } 147200395Sthompsa if (!s.equals(clone)) 148200886Sthompsa fail("Set not equal to copy. " + method); 149201071Sthompsa if (!s.containsAll(clone)) 150201071Sthompsa fail("Set does not contain copy. " + method); 151201071Sthompsa if (!clone.containsAll(s)) 152201071Sthompsa fail("Copy does not contain set. " + method); 153201071Sthompsa return clone; 154201071Sthompsa } 155201071Sthompsa 156201071Sthompsa // Utility method to do a deep copy of an object *very slowly* using 157201071Sthompsa // serialization/deserialization 158201071Sthompsa static <T> T deepCopy(T oldObj) { 159201071Sthompsa try { 160201071Sthompsa ByteArrayOutputStream bos = new ByteArrayOutputStream(); 161201071Sthompsa ObjectOutputStream oos = new ObjectOutputStream(bos); 162201071Sthompsa oos.writeObject(oldObj); 163213856Shselasky oos.flush(); 164213856Shselasky ByteArrayInputStream bin = new ByteArrayInputStream( 165210576Stijl bos.toByteArray()); 166210576Stijl ObjectInputStream ois = new ObjectInputStream(bin); 167201071Sthompsa return (T) ois.readObject(); 168201071Sthompsa } catch(Exception e) { 169201071Sthompsa throw new IllegalArgumentException(e.toString()); 170201071Sthompsa } 171261134Shselasky } 172201071Sthompsa 173201071Sthompsa static <T extends Enum<T>> void AddRandoms(EnumSet<T> s, Enum[] universe) { 174201071Sthompsa for (int i=0; i < universe.length * 2 / 3; i++) { 175201071Sthompsa T e = (T) universe[rnd.nextInt(universe.length)]; 176201071Sthompsa 177201071Sthompsa boolean prePresent = s.contains(e); 178201071Sthompsa int preSize = s.size(); 179201071Sthompsa boolean added = s.add(e); 180201071Sthompsa if (!s.contains(e)) 181201071Sthompsa fail ("Element not present after addition."); 182201071Sthompsa if (added == prePresent) 183201071Sthompsa fail ("added == alreadyPresent"); 184201071Sthompsa int postSize = s.size(); 185201071Sthompsa if (added && preSize == postSize) 186201071Sthompsa fail ("Add returned true, but size didn't change."); 187241551Seadler if (!added && preSize != postSize) 188201071Sthompsa fail ("Add returned false, but size changed."); 189201071Sthompsa } 190201071Sthompsa } 191201071Sthompsa 192201071Sthompsa static void fail(String s) { 193201071Sthompsa throw new RuntimeException(s); 194241552Seadler } 195201071Sthompsa 196201071Sthompsa public enum Silly0 { }; 197213856Shselasky 198201071Sthompsa public enum Silly1 { e1 } 199201071Sthompsa 200201071Sthompsa public enum Silly31 { 201201071Sthompsa e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, 202201071Sthompsa e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30 203200886Sthompsa } 204201071Sthompsa 205201071Sthompsa public enum Silly32 { 206201071Sthompsa e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, 207201071Sthompsa e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31 208201071Sthompsa } 209201071Sthompsa 210201071Sthompsa public enum Silly33 { 211201071Sthompsa e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, 212201071Sthompsa e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, 213201071Sthompsa e32 214201071Sthompsa } 215201071Sthompsa 216201071Sthompsa public enum Silly63 { 217201071Sthompsa e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, 218201071Sthompsa e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, 219201071Sthompsa e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46, 220201071Sthompsa e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61, 221201071Sthompsa e62 222201071Sthompsa } 223201071Sthompsa 224201071Sthompsa public enum Silly64 { 225201071Sthompsa e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, 226201071Sthompsa e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, 227201071Sthompsa e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46, 228201071Sthompsa e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61, 229201071Sthompsa e62, e63 230201071Sthompsa } 231201071Sthompsa 232201071Sthompsa public enum Silly65 { 233201071Sthompsa e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, 234201071Sthompsa e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, 235201071Sthompsa e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46, 236201071Sthompsa e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61, 237201071Sthompsa e62, e63, e64 238201071Sthompsa } 239201701Sthompsa 240201701Sthompsa public enum Silly127 { 241201701Sthompsa e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, 242264653Shselasky e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, 243201071Sthompsa e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46, 244201071Sthompsa e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61, 245201071Sthompsa e62, e63, e64, e65, e66, e67, e68, e69, e70, e71, e72, e73, e74, e75, e76, 246201071Sthompsa e77, e78, e79, e80, e81, e82, e83, e84, e85, e86, e87, e88, e89, e90, e91, 247201071Sthompsa e92, e93, e94, e95, e96, e97, e98, e99, e100, e101, e102, e103, e104, e105, 248201071Sthompsa e106, e107, e108, e109, e110, e111, e112, e113, e114, e115, e116, e117, 249201071Sthompsa e118, e119, e120, e121, e122, e123, e124, e125, e126 250201071Sthompsa } 251201071Sthompsa 252201071Sthompsa public enum Silly128 { 253201071Sthompsa e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, 254201071Sthompsa e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, 255201071Sthompsa e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46, 256275110Shselasky e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61, 257201071Sthompsa e62, e63, e64, e65, e66, e67, e68, e69, e70, e71, e72, e73, e74, e75, e76, 258201071Sthompsa e77, e78, e79, e80, e81, e82, e83, e84, e85, e86, e87, e88, e89, e90, e91, 259201071Sthompsa e92, e93, e94, e95, e96, e97, e98, e99, e100, e101, e102, e103, e104, e105, 260201071Sthompsa e106, e107, e108, e109, e110, e111, e112, e113, e114, e115, e116, e117, 261201071Sthompsa e118, e119, e120, e121, e122, e123, e124, e125, e126, e127 262201071Sthompsa } 263201071Sthompsa 264201071Sthompsa public enum Silly129 { 265201071Sthompsa e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, 266228243Semaste e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, 267201071Sthompsa e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46, 268201071Sthompsa e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61, 269201071Sthompsa e62, e63, e64, e65, e66, e67, e68, e69, e70, e71, e72, e73, e74, e75, e76, 270201071Sthompsa e77, e78, e79, e80, e81, e82, e83, e84, e85, e86, e87, e88, e89, e90, e91, 271201071Sthompsa e92, e93, e94, e95, e96, e97, e98, e99, e100, e101, e102, e103, e104, e105, 272201071Sthompsa e106, e107, e108, e109, e110, e111, e112, e113, e114, e115, e116, e117, 273201071Sthompsa e118, e119, e120, e121, e122, e123, e124, e125, e126, e127, e128 274201071Sthompsa } 275201071Sthompsa 276201071Sthompsa public enum Silly500 { 277201071Sthompsa e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, 278201071Sthompsa e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31, 279201071Sthompsa e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46, 280201071Sthompsa e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61, 281201071Sthompsa e62, e63, e64, e65, e66, e67, e68, e69, e70, e71, e72, e73, e74, e75, e76, 282201071Sthompsa e77, e78, e79, e80, e81, e82, e83, e84, e85, e86, e87, e88, e89, e90, e91, 283201071Sthompsa e92, e93, e94, e95, e96, e97, e98, e99, e100, e101, e102, e103, e104, e105, 284201071Sthompsa e106, e107, e108, e109, e110, e111, e112, e113, e114, e115, e116, e117, 285201071Sthompsa e118, e119, e120, e121, e122, e123, e124, e125, e126, e127, e128, e129, 286201071Sthompsa e130, e131, e132, e133, e134, e135, e136, e137, e138, e139, e140, e141, 287201071Sthompsa e142, e143, e144, e145, e146, e147, e148, e149, e150, e151, e152, e153, 288201071Sthompsa e154, e155, e156, e157, e158, e159, e160, e161, e162, e163, e164, e165, 289201071Sthompsa e166, e167, e168, e169, e170, e171, e172, e173, e174, e175, e176, e177, 290201071Sthompsa e178, e179, e180, e181, e182, e183, e184, e185, e186, e187, e188, e189, 291201071Sthompsa e190, e191, e192, e193, e194, e195, e196, e197, e198, e199, e200, e201, 292201071Sthompsa e202, e203, e204, e205, e206, e207, e208, e209, e210, e211, e212, e213, 293201071Sthompsa e214, e215, e216, e217, e218, e219, e220, e221, e222, e223, e224, e225, 294201071Sthompsa e226, e227, e228, e229, e230, e231, e232, e233, e234, e235, e236, e237, 295201071Sthompsa e238, e239, e240, e241, e242, e243, e244, e245, e246, e247, e248, e249, 296261003Shselasky e250, e251, e252, e253, e254, e255, e256, e257, e258, e259, e260, e261, 297201071Sthompsa e262, e263, e264, e265, e266, e267, e268, e269, e270, e271, e272, e273, 298201071Sthompsa e274, e275, e276, e277, e278, e279, e280, e281, e282, e283, e284, e285, 299201071Sthompsa e286, e287, e288, e289, e290, e291, e292, e293, e294, e295, e296, e297, 300201071Sthompsa e298, e299, e300, e301, e302, e303, e304, e305, e306, e307, e308, e309, 301201071Sthompsa e310, e311, e312, e313, e314, e315, e316, e317, e318, e319, e320, e321, 302201071Sthompsa e322, e323, e324, e325, e326, e327, e328, e329, e330, e331, e332, e333, 303201071Sthompsa e334, e335, e336, e337, e338, e339, e340, e341, e342, e343, e344, e345, 304201071Sthompsa e346, e347, e348, e349, e350, e351, e352, e353, e354, e355, e356, e357, 305201071Sthompsa e358, e359, e360, e361, e362, e363, e364, e365, e366, e367, e368, e369, 306201071Sthompsa e370, e371, e372, e373, e374, e375, e376, e377, e378, e379, e380, e381, 307201071Sthompsa e382, e383, e384, e385, e386, e387, e388, e389, e390, e391, e392, e393, 308201071Sthompsa e394, e395, e396, e397, e398, e399, e400, e401, e402, e403, e404, e405, 309201071Sthompsa e406, e407, e408, e409, e410, e411, e412, e413, e414, e415, e416, e417, 310201071Sthompsa e418, e419, e420, e421, e422, e423, e424, e425, e426, e427, e428, e429, 311201071Sthompsa e430, e431, e432, e433, e434, e435, e436, e437, e438, e439, e440, e441, 312201071Sthompsa e442, e443, e444, e445, e446, e447, e448, e449, e450, e451, e452, e453, 313201071Sthompsa e454, e455, e456, e457, e458, e459, e460, e461, e462, e463, e464, e465, 314201071Sthompsa e466, e467, e468, e469, e470, e471, e472, e473, e474, e475, e476, e477, 315245725Shselasky e478, e479, e480, e481, e482, e483, e484, e485, e486, e487, e488, e489, 316201071Sthompsa e490, e491, e492, e493, e494, e495, e496, e497, e498, e499 317201071Sthompsa } 318201071Sthompsa 319201071Sthompsa} 320201071Sthompsa