Path: blob/master/test/jdk/java/util/EnumMap/EnumMapBash.java
41149 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 490414026* @summary Unit test for EnumMap27* @author Josh Bloch28* @author Yo Yo Ma29*/3031import java.util.*;32import java.io.*;3334public class EnumMapBash {35static Random rnd = new Random();3637public static void main(String[] args) {38bash(Silly31.class);39bash(Silly32.class);40bash(Silly33.class);41bash(Silly63.class);42bash(Silly64.class);43bash(Silly65.class);44bash(Silly127.class);45bash(Silly128.class);46bash(Silly129.class);47bash(Silly500.class);48}4950static <T extends Enum<T>> void bash(Class<T> enumClass) {51Enum[] universe = enumClass.getEnumConstants();5253int numItr = 100;5455// Linked List test56for (int i=0; i<numItr; i++) {57int mapSize = universe.length * 7 / 8;5859// Build the linked list60Map<T, T> m = new EnumMap<T, T>(enumClass);61if (!m.isEmpty())62fail("New instance non empty.");63Enum[] perm = (Enum[]) universe.clone();64Collections.shuffle(Arrays.asList(perm));65T head = (T) perm[0];66for (int j = 0; j < mapSize; j++)67m.put((T)perm[j], (T)perm[j + 1]);68T nil = (T)perm[mapSize];6970if (m.size() != mapSize)71fail("Size not as expected.");7273Map<T, T> tm = new TreeMap<T, T>(m);7475if (m.hashCode() != tm.hashCode())76fail("Incorrect hashCode computation.");77if (!m.toString().equals(tm.toString()))78fail("Incorrect toString computation.");79if (!tm.equals(m))80fail("Incorrect equals (1).");81if (!m.equals(tm))82fail("Incorrect equals (2).");8384Map<T, T> m2 = new EnumMap<T, T>(enumClass); m2.putAll(m);85m2.values().removeAll(m.keySet());86if (m2.size()!= 1 || !m2.containsValue(nil))87fail("Collection views test failed.");8889int j=0;90while (head != nil) {91if (!m.containsKey(head))92fail("Linked list doesn't contain a link.");93T newHead = m.get(head);94if (newHead == null)95fail("Could not retrieve a link.");96m.remove(head);97head = newHead;98j++;99}100if (!m.isEmpty())101fail("Map nonempty after removing all links.");102if (j != mapSize)103fail("Linked list size not as expected.");104}105106EnumMap<T, T> m = new EnumMap<T, T>(enumClass);107int mapSize = 0;108for (int i=0; i<universe.length; i += 2) {109if (m.put((T)universe[i], (T)universe[i]) != null)110fail("put returns a non-null value erroenously.");111mapSize++;112}113for (int i=0; i<universe.length; i++)114if (m.containsValue(universe[i]) != (i%2==0))115fail("contains value "+i);116if (m.put((T)universe[0], (T)universe[0]) == null)117fail("put returns a null value erroenously.");118119Map<T, T> m2 = m.clone();120cloneTest(m, m2);121122m2 = new EnumMap<T,T>(enumClass);123m2.putAll(m);124cloneTest(m, m2);125126m2 = new EnumMap<T, T>(m);127cloneTest(m, m2);128129m2 = new EnumMap<T, T>((Map<T, T>) m);130cloneTest(m, m2);131if (!m.isEmpty()) {132m2 = new EnumMap<T, T>(new HashMap<T, T>(m));133cloneTest(m, m2);134}135136m2 = deepCopy(m);137cloneTest(m, m2);138139if (!m.equals(m2))140fail("Clone not equal to original. (1)");141if (!m2.equals(m))142fail("Clone not equal to original. (2)");143144Set<Map.Entry<T,T>> s = m.entrySet(), s2 = m2.entrySet();145146if (!s.equals(s2))147fail("Clone not equal to original. (3)");148if (!s2.equals(s))149fail("Clone not equal to original. (4)");150if (!s.containsAll(s2))151fail("Original doesn't contain clone!");152if (!s2.containsAll(s))153fail("Clone doesn't contain original!");154155s2.removeAll(s);156if (!m2.isEmpty()) {157System.out.println(m2.size());158System.out.println(m2);159fail("entrySet().removeAll failed.");160}161162m2.putAll(m);163m2.clear();164if (!m2.isEmpty())165fail("clear failed.");166167Iterator i = m.entrySet().iterator();168while(i.hasNext()) {169i.next();170i.remove();171}172if (!m.isEmpty())173fail("Iterator.remove() failed");174}175176// Done inefficiently so as to exercise various functions177static <K, V> void cloneTest(Map<K, V> m, Map<K, V> clone) {178if (!m.equals(clone))179fail("Map not equal to copy.");180if (!clone.equals(m))181fail("Copy not equal to map.");182if (!m.entrySet().containsAll(clone.entrySet()))183fail("Set does not contain copy.");184if (!clone.entrySet().containsAll(m.entrySet()))185fail("Copy does not contain set.");186if (!m.entrySet().equals(clone.entrySet()))187fail("Set not equal clone set");188if (!clone.entrySet().equals(m.entrySet()))189fail("Clone set not equal set");190}191192// Utility method to do a deep copy of an object *very slowly* using193// serialization/deserialization194static <T> T deepCopy(T oldObj) {195try {196ByteArrayOutputStream bos = new ByteArrayOutputStream();197ObjectOutputStream oos = new ObjectOutputStream(bos);198oos.writeObject(oldObj);199oos.flush();200ByteArrayInputStream bin = new ByteArrayInputStream(201bos.toByteArray());202ObjectInputStream ois = new ObjectInputStream(bin);203return (T) ois.readObject();204} catch(Exception e) {205throw new IllegalArgumentException(e.toString());206}207}208209static void fail(String s) {210throw new RuntimeException(s);211}212213public enum Silly31 {214e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,215e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30216}217218public enum Silly32 {219e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,220e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31221}222223public enum Silly33 {224e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,225e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31,226e32227}228229public enum Silly63 {230e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,231e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31,232e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46,233e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61,234e62235}236237public enum Silly64 {238e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,239e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31,240e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46,241e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61,242e62, e63243}244245public enum Silly65 {246e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,247e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31,248e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46,249e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61,250e62, e63, e64251}252253public enum Silly127 {254e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,255e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31,256e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46,257e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61,258e62, e63, e64, e65, e66, e67, e68, e69, e70, e71, e72, e73, e74, e75, e76,259e77, e78, e79, e80, e81, e82, e83, e84, e85, e86, e87, e88, e89, e90, e91,260e92, e93, e94, e95, e96, e97, e98, e99, e100, e101, e102, e103, e104, e105,261e106, e107, e108, e109, e110, e111, e112, e113, e114, e115, e116, e117,262e118, e119, e120, e121, e122, e123, e124, e125, e126263}264265public enum Silly128 {266e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,267e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31,268e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46,269e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61,270e62, e63, e64, e65, e66, e67, e68, e69, e70, e71, e72, e73, e74, e75, e76,271e77, e78, e79, e80, e81, e82, e83, e84, e85, e86, e87, e88, e89, e90, e91,272e92, e93, e94, e95, e96, e97, e98, e99, e100, e101, e102, e103, e104, e105,273e106, e107, e108, e109, e110, e111, e112, e113, e114, e115, e116, e117,274e118, e119, e120, e121, e122, e123, e124, e125, e126, e127275}276277public enum Silly129 {278e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,279e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31,280e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46,281e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61,282e62, e63, e64, e65, e66, e67, e68, e69, e70, e71, e72, e73, e74, e75, e76,283e77, e78, e79, e80, e81, e82, e83, e84, e85, e86, e87, e88, e89, e90, e91,284e92, e93, e94, e95, e96, e97, e98, e99, e100, e101, e102, e103, e104, e105,285e106, e107, e108, e109, e110, e111, e112, e113, e114, e115, e116, e117,286e118, e119, e120, e121, e122, e123, e124, e125, e126, e127, e128287}288289public enum Silly500 {290e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16,291e17, e18, e19, e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, e30, e31,292e32, e33, e34, e35, e36, e37, e38, e39, e40, e41, e42, e43, e44, e45, e46,293e47, e48, e49, e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, e60, e61,294e62, e63, e64, e65, e66, e67, e68, e69, e70, e71, e72, e73, e74, e75, e76,295e77, e78, e79, e80, e81, e82, e83, e84, e85, e86, e87, e88, e89, e90, e91,296e92, e93, e94, e95, e96, e97, e98, e99, e100, e101, e102, e103, e104, e105,297e106, e107, e108, e109, e110, e111, e112, e113, e114, e115, e116, e117,298e118, e119, e120, e121, e122, e123, e124, e125, e126, e127, e128, e129,299e130, e131, e132, e133, e134, e135, e136, e137, e138, e139, e140, e141,300e142, e143, e144, e145, e146, e147, e148, e149, e150, e151, e152, e153,301e154, e155, e156, e157, e158, e159, e160, e161, e162, e163, e164, e165,302e166, e167, e168, e169, e170, e171, e172, e173, e174, e175, e176, e177,303e178, e179, e180, e181, e182, e183, e184, e185, e186, e187, e188, e189,304e190, e191, e192, e193, e194, e195, e196, e197, e198, e199, e200, e201,305e202, e203, e204, e205, e206, e207, e208, e209, e210, e211, e212, e213,306e214, e215, e216, e217, e218, e219, e220, e221, e222, e223, e224, e225,307e226, e227, e228, e229, e230, e231, e232, e233, e234, e235, e236, e237,308e238, e239, e240, e241, e242, e243, e244, e245, e246, e247, e248, e249,309e250, e251, e252, e253, e254, e255, e256, e257, e258, e259, e260, e261,310e262, e263, e264, e265, e266, e267, e268, e269, e270, e271, e272, e273,311e274, e275, e276, e277, e278, e279, e280, e281, e282, e283, e284, e285,312e286, e287, e288, e289, e290, e291, e292, e293, e294, e295, e296, e297,313e298, e299, e300, e301, e302, e303, e304, e305, e306, e307, e308, e309,314e310, e311, e312, e313, e314, e315, e316, e317, e318, e319, e320, e321,315e322, e323, e324, e325, e326, e327, e328, e329, e330, e331, e332, e333,316e334, e335, e336, e337, e338, e339, e340, e341, e342, e343, e344, e345,317e346, e347, e348, e349, e350, e351, e352, e353, e354, e355, e356, e357,318e358, e359, e360, e361, e362, e363, e364, e365, e366, e367, e368, e369,319e370, e371, e372, e373, e374, e375, e376, e377, e378, e379, e380, e381,320e382, e383, e384, e385, e386, e387, e388, e389, e390, e391, e392, e393,321e394, e395, e396, e397, e398, e399, e400, e401, e402, e403, e404, e405,322e406, e407, e408, e409, e410, e411, e412, e413, e414, e415, e416, e417,323e418, e419, e420, e421, e422, e423, e424, e425, e426, e427, e428, e429,324e430, e431, e432, e433, e434, e435, e436, e437, e438, e439, e440, e441,325e442, e443, e444, e445, e446, e447, e448, e449, e450, e451, e452, e453,326e454, e455, e456, e457, e458, e459, e460, e461, e462, e463, e464, e465,327e466, e467, e468, e469, e470, e471, e472, e473, e474, e475, e476, e477,328e478, e479, e480, e481, e482, e483, e484, e485, e486, e487, e488, e489,329e490, e491, e492, e493, e494, e495, e496, e497, e498, e499330}331332}333334335