Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Hashtable/IllegalLoadFactor.java
38812 views
/*1* Copyright (c) 1997, 1998, 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/* @test24@bug 4093817 418959425@summary Test for an illegalargumentexception on loadFactor26*/27282930import java.util.*;3132/**33* This class tests to see if creating a hash table with an34* illegal value of loadFactor results in an IllegalArgumentException35*/36public class IllegalLoadFactor {3738public static void main(String argv[]) throws Exception {39boolean testSucceeded = false;40try{41// this should generate an IllegalArgumentException42Hashtable bad1 = new Hashtable(100, -3);43}44catch (IllegalArgumentException e1) {45testSucceeded = true;46}47if(!testSucceeded)48throw new Exception("Hashtable, negative load factor");4950testSucceeded = false;51try{52// this should generate an IllegalArgumentException53Hashtable bad1 = new Hashtable(100, Float.NaN);54}55catch (IllegalArgumentException e1) {56testSucceeded = true;57}58if(!testSucceeded)59throw new Exception("Hashtable, NaN load factor");6061testSucceeded = false;62try{63// this should generate an IllegalArgumentException64HashMap bad1 = new HashMap(100, -3);65}66catch (IllegalArgumentException e1) {67testSucceeded = true;68}69if(!testSucceeded)70throw new Exception("HashMap, negative load factor");7172testSucceeded = false;73try{74// this should generate an IllegalArgumentException75HashMap bad1 = new HashMap(100, Float.NaN);76}77catch (IllegalArgumentException e1) {78testSucceeded = true;79}80if(!testSucceeded)81throw new Exception("HashMap, NaN load factor");828384testSucceeded = false;85try{86// this should generate an IllegalArgumentException87HashSet bad1 = new HashSet(100, -3);88}89catch (IllegalArgumentException e1) {90testSucceeded = true;91}92if(!testSucceeded)93throw new Exception("HashSet, negative load factor");9495testSucceeded = false;96try{97// this should generate an IllegalArgumentException98HashSet bad1 = new HashSet(100, Float.NaN);99}100catch (IllegalArgumentException e1) {101testSucceeded = true;102}103if(!testSucceeded)104throw new Exception("HashSet, NaN load factor");105106testSucceeded = false;107try{108// this should generate an IllegalArgumentException109WeakHashMap bad1 = new WeakHashMap(100, -3);110}111catch (IllegalArgumentException e1) {112testSucceeded = true;113}114if(!testSucceeded)115throw new Exception("WeakHashMap, negative load factor");116117testSucceeded = false;118try{119// this should generate an IllegalArgumentException120WeakHashMap bad1 = new WeakHashMap(100, Float.NaN);121}122catch (IllegalArgumentException e1) {123testSucceeded = true;124}125if(!testSucceeded)126throw new Exception("WeakHashMap, NaN load factor");127128// Make sure that legal creates don't throw exceptions129Map goodMap = new Hashtable(100, .69f);130goodMap = new HashMap(100, .69f);131Set goodSet = new HashSet(100, .69f);132goodMap = new WeakHashMap(100, .69f);133}134135}136137138