1 |
/* |
2 |
* Copyright (c) 1997, 1998, Oracle and/or its affiliates. All rights reserved. |
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
5 |
* This code is free software; you can redistribute it and/or modify it |
6 |
* under the terms of the GNU General Public License version 2 only, as |
7 |
* published by the Free Software Foundation. |
8 |
* |
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
13 |
* accompanied this code). |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License version |
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
* |
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
21 |
* questions. |
22 |
*/ |
23 |
|
24 |
/* @test |
25 |
@bug 4093817 4189594 |
26 |
@summary Test for an illegalargumentexception on loadFactor |
27 |
*/ |
28 |
|
29 |
import java.util.HashMap; |
30 |
import java.util.HashSet; |
31 |
import java.util.Hashtable; |
32 |
import java.util.Map; |
33 |
import java.util.Set; |
34 |
import java.util.WeakHashMap; |
35 |
|
36 |
/** |
37 |
* This class tests to see if creating a hash table with an |
38 |
* illegal value of loadFactor results in an IllegalArgumentException |
39 |
*/ |
40 |
public class IllegalLoadFactor { |
41 |
|
42 |
public static void main(String[] args) throws Exception { |
43 |
boolean testSucceeded = false; |
44 |
try { |
45 |
// this should generate an IllegalArgumentException |
46 |
Hashtable bad1 = new Hashtable(100, -3); |
47 |
} |
48 |
catch (IllegalArgumentException e1) { |
49 |
testSucceeded = true; |
50 |
} |
51 |
if (!testSucceeded) |
52 |
throw new Exception("Hashtable, negative load factor"); |
53 |
|
54 |
testSucceeded = false; |
55 |
try { |
56 |
// this should generate an IllegalArgumentException |
57 |
Hashtable bad1 = new Hashtable(100, Float.NaN); |
58 |
} |
59 |
catch (IllegalArgumentException e1) { |
60 |
testSucceeded = true; |
61 |
} |
62 |
if (!testSucceeded) |
63 |
throw new Exception("Hashtable, NaN load factor"); |
64 |
|
65 |
testSucceeded = false; |
66 |
try { |
67 |
// this should generate an IllegalArgumentException |
68 |
HashMap bad1 = new HashMap(100, -3); |
69 |
} |
70 |
catch (IllegalArgumentException e1) { |
71 |
testSucceeded = true; |
72 |
} |
73 |
if (!testSucceeded) |
74 |
throw new Exception("HashMap, negative load factor"); |
75 |
|
76 |
testSucceeded = false; |
77 |
try { |
78 |
// this should generate an IllegalArgumentException |
79 |
HashMap bad1 = new HashMap(100, Float.NaN); |
80 |
} |
81 |
catch (IllegalArgumentException e1) { |
82 |
testSucceeded = true; |
83 |
} |
84 |
if (!testSucceeded) |
85 |
throw new Exception("HashMap, NaN load factor"); |
86 |
|
87 |
|
88 |
testSucceeded = false; |
89 |
try { |
90 |
// this should generate an IllegalArgumentException |
91 |
HashSet bad1 = new HashSet(100, -3); |
92 |
} |
93 |
catch (IllegalArgumentException e1) { |
94 |
testSucceeded = true; |
95 |
} |
96 |
if (!testSucceeded) |
97 |
throw new Exception("HashSet, negative load factor"); |
98 |
|
99 |
testSucceeded = false; |
100 |
try { |
101 |
// this should generate an IllegalArgumentException |
102 |
HashSet bad1 = new HashSet(100, Float.NaN); |
103 |
} |
104 |
catch (IllegalArgumentException e1) { |
105 |
testSucceeded = true; |
106 |
} |
107 |
if (!testSucceeded) |
108 |
throw new Exception("HashSet, NaN load factor"); |
109 |
|
110 |
testSucceeded = false; |
111 |
try { |
112 |
// this should generate an IllegalArgumentException |
113 |
WeakHashMap bad1 = new WeakHashMap(100, -3); |
114 |
} |
115 |
catch (IllegalArgumentException e1) { |
116 |
testSucceeded = true; |
117 |
} |
118 |
if (!testSucceeded) |
119 |
throw new Exception("WeakHashMap, negative load factor"); |
120 |
|
121 |
testSucceeded = false; |
122 |
try { |
123 |
// this should generate an IllegalArgumentException |
124 |
WeakHashMap bad1 = new WeakHashMap(100, Float.NaN); |
125 |
} |
126 |
catch (IllegalArgumentException e1) { |
127 |
testSucceeded = true; |
128 |
} |
129 |
if (!testSucceeded) |
130 |
throw new Exception("WeakHashMap, NaN load factor"); |
131 |
|
132 |
// Make sure that legal creates don't throw exceptions |
133 |
Map goodMap = new Hashtable(100, .69f); |
134 |
goodMap = new HashMap(100, .69f); |
135 |
Set goodSet = new HashSet(100, .69f); |
136 |
goodMap = new WeakHashMap(100, .69f); |
137 |
} |
138 |
} |