Path: blob/master/test/jdk/java/util/Hashtable/IllegalLoadFactor.java
41149 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*/2728import java.util.HashMap;29import java.util.HashSet;30import java.util.Hashtable;31import java.util.Map;32import java.util.Set;33import java.util.WeakHashMap;3435/**36* This class tests to see if creating a hash table with an37* illegal value of loadFactor results in an IllegalArgumentException38*/39public class IllegalLoadFactor {4041public static void main(String[] args) throws Exception {42boolean testSucceeded = false;43try {44// this should generate an IllegalArgumentException45Hashtable bad1 = new Hashtable(100, -3);46}47catch (IllegalArgumentException e1) {48testSucceeded = true;49}50if (!testSucceeded)51throw new Exception("Hashtable, negative load factor");5253testSucceeded = false;54try {55// this should generate an IllegalArgumentException56Hashtable bad1 = new Hashtable(100, Float.NaN);57}58catch (IllegalArgumentException e1) {59testSucceeded = true;60}61if (!testSucceeded)62throw new Exception("Hashtable, NaN load factor");6364testSucceeded = false;65try {66// this should generate an IllegalArgumentException67HashMap bad1 = new HashMap(100, -3);68}69catch (IllegalArgumentException e1) {70testSucceeded = true;71}72if (!testSucceeded)73throw new Exception("HashMap, negative load factor");7475testSucceeded = false;76try {77// this should generate an IllegalArgumentException78HashMap bad1 = new HashMap(100, Float.NaN);79}80catch (IllegalArgumentException e1) {81testSucceeded = true;82}83if (!testSucceeded)84throw new Exception("HashMap, NaN load factor");858687testSucceeded = false;88try {89// this should generate an IllegalArgumentException90HashSet bad1 = new HashSet(100, -3);91}92catch (IllegalArgumentException e1) {93testSucceeded = true;94}95if (!testSucceeded)96throw new Exception("HashSet, negative load factor");9798testSucceeded = false;99try {100// this should generate an IllegalArgumentException101HashSet bad1 = new HashSet(100, Float.NaN);102}103catch (IllegalArgumentException e1) {104testSucceeded = true;105}106if (!testSucceeded)107throw new Exception("HashSet, NaN load factor");108109testSucceeded = false;110try {111// this should generate an IllegalArgumentException112WeakHashMap bad1 = new WeakHashMap(100, -3);113}114catch (IllegalArgumentException e1) {115testSucceeded = true;116}117if (!testSucceeded)118throw new Exception("WeakHashMap, negative load factor");119120testSucceeded = false;121try {122// this should generate an IllegalArgumentException123WeakHashMap bad1 = new WeakHashMap(100, Float.NaN);124}125catch (IllegalArgumentException e1) {126testSucceeded = true;127}128if (!testSucceeded)129throw new Exception("WeakHashMap, NaN load factor");130131// Make sure that legal creates don't throw exceptions132Map goodMap = new Hashtable(100, .69f);133goodMap = new HashMap(100, .69f);134Set goodSet = new HashSet(100, .69f);135goodMap = new WeakHashMap(100, .69f);136}137}138139140