undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
57
test/jdk/java/util/TreeMap/Clone.java
Normal file
57
test/jdk/java/util/TreeMap/Clone.java
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 7198904
|
||||
* @summary Verify that cloned TreeMap gets new keyset
|
||||
* @author david.buck@oracle.com
|
||||
* @run main/othervm Clone
|
||||
*/
|
||||
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class Clone {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
TreeMap<String,Object> m1 = new TreeMap<String,Object>();
|
||||
m1.put( "one", 1 );
|
||||
m1.keySet();
|
||||
TreeMap<String,Object> m2 = (TreeMap<String,Object>)m1.clone();
|
||||
m1.put( "two", 2 );
|
||||
m2.put( "three", 3 );
|
||||
// iterate over the clone (m2) and we should get "one" and "three"
|
||||
for( final String key : m2.keySet() ) {
|
||||
if( !"one".equals( key ) && !"three".equals( key ) ) {
|
||||
throw new IllegalStateException( "Unexpected key: " + key );
|
||||
}
|
||||
}
|
||||
// iterate over the original (m1) and we should get "one" and "two"
|
||||
for( final String key : m1.keySet() ) {
|
||||
if( !"one".equals( key ) && !"two".equals( key ) ) {
|
||||
throw new IllegalStateException( "Unexpected key: " + key );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
52
test/jdk/java/util/TreeMap/ContainsValue.java
Normal file
52
test/jdk/java/util/TreeMap/ContainsValue.java
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4212425
|
||||
* @summary TreeMap.containsValue throws NullPointerExc for empty TreeMap
|
||||
*/
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class ContainsValue {
|
||||
public static void main(String[] args) {
|
||||
Map map = new TreeMap();
|
||||
|
||||
if (map.containsValue ("gemutlichkeit"))
|
||||
throw new RuntimeException("containsValue optimistic (non-null)");
|
||||
|
||||
if (map.containsValue (null))
|
||||
throw new RuntimeException("containsValue optimistic (null)");
|
||||
|
||||
map.put("a", null);
|
||||
map.put("b", "gemutlichkeit");
|
||||
|
||||
if (!map.containsValue ("gemutlichkeit"))
|
||||
throw new RuntimeException("containsValue pessimistic (non-null)");
|
||||
|
||||
if (!map.containsValue (null))
|
||||
throw new RuntimeException("containsValue pessimistic (null)");
|
||||
}
|
||||
}
|
||||
120
test/jdk/java/util/TreeMap/EmptyMapAndNulls.java
Normal file
120
test/jdk/java/util/TreeMap/EmptyMapAndNulls.java
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
/*
|
||||
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 5045147
|
||||
* @summary Test handling of null with empty Map
|
||||
* @author Mike Duigou
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import java.lang.reflect.*;
|
||||
|
||||
public class EmptyMapAndNulls {
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
static void realMain(String[] args) throws Throwable {
|
||||
// No comparator
|
||||
Map<String,String> comparable = new TreeMap<>();
|
||||
|
||||
// insert null into empty map (5045147 failure)
|
||||
try {
|
||||
comparable.put(null, "anything");
|
||||
fail("null shouldn't be accepted");
|
||||
} catch (NullPointerException failed) {
|
||||
pass();
|
||||
}
|
||||
|
||||
// insert non-null into empty map
|
||||
try {
|
||||
comparable.put("test", "anything");
|
||||
pass();
|
||||
} catch (NullPointerException failed) {
|
||||
fail();
|
||||
}
|
||||
|
||||
// insert null into non-empty map
|
||||
try {
|
||||
comparable.put(null, "anything");
|
||||
fail("null shouldn't be accepted");
|
||||
} catch (NullPointerException failed) {
|
||||
pass();
|
||||
}
|
||||
|
||||
// Comparator (String.CASE_INSENSITIVE_ORDER). Intentionally a raw type.
|
||||
Map comparator = new TreeMap(String.CASE_INSENSITIVE_ORDER);
|
||||
|
||||
// insert null into empty map (5045147 failure)
|
||||
try {
|
||||
comparator.put(null, "anything");
|
||||
fail("null shouldn't be accepted");
|
||||
} catch (NullPointerException failed) {
|
||||
pass();
|
||||
}
|
||||
|
||||
// insert non-null into empty map
|
||||
try {
|
||||
comparator.put("test", "anything");
|
||||
pass();
|
||||
} catch (NullPointerException failed) {
|
||||
fail();
|
||||
}
|
||||
|
||||
// insert null into non-empty map
|
||||
try {
|
||||
comparator.put(null, "anything");
|
||||
fail("null shouldn't be accepted");
|
||||
} catch (NullPointerException failed) {
|
||||
pass();
|
||||
}
|
||||
|
||||
comparator.clear();
|
||||
|
||||
// insert non-String into empty map (5045147 failure)
|
||||
try {
|
||||
comparator.put(new Object(), "anything");
|
||||
fail("Object shouldn't be accepted");
|
||||
} catch (ClassCastException failed) {
|
||||
pass();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//--------------------- Infrastructure ---------------------------
|
||||
static volatile int passed = 0, failed = 0;
|
||||
static void pass() {passed++;}
|
||||
static void fail() {failed++; Thread.dumpStack();}
|
||||
static void fail(String msg) {System.out.println(msg); fail();}
|
||||
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
||||
static void check(boolean cond) {if (cond) pass(); else fail();}
|
||||
static void equal(Object x, Object y) {
|
||||
if (x == null ? y == null : x.equals(y)) pass();
|
||||
else fail(x + " not equal to " + y);}
|
||||
public static void main(String[] args) throws Throwable {
|
||||
try {realMain(args);} catch (Throwable t) {unexpected(t);}
|
||||
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
||||
if (failed > 0) throw new AssertionError("Some tests failed");}
|
||||
}
|
||||
166
test/jdk/java/util/TreeMap/HeadTailTypeError.java
Normal file
166
test/jdk/java/util/TreeMap/HeadTailTypeError.java
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
/*
|
||||
* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4251519 4251520
|
||||
* @summary indexOf and lastIndex of used to let you look outside the
|
||||
* valid range in the backing array
|
||||
*/
|
||||
|
||||
import java.util.SortedMap;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class HeadTailTypeError {
|
||||
public static void main(String[] args) throws Exception {
|
||||
try {
|
||||
SortedMap m = new TreeMap();
|
||||
m.headMap(new Object());
|
||||
throw new Exception("headMap, natural ordering");
|
||||
} catch (ClassCastException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
SortedMap m = new TreeMap();
|
||||
m.tailMap(new Object());
|
||||
throw new Exception("tailMap, natural ordering");
|
||||
} catch (ClassCastException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
SortedMap m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
|
||||
m.headMap(new Integer(0));
|
||||
throw new Exception("headMap, explicit comparator");
|
||||
} catch (ClassCastException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
SortedMap m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
|
||||
m.tailMap(new Integer(0));
|
||||
throw new Exception("tailMap, explicit comparator");
|
||||
} catch (ClassCastException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
SortedSet m = new TreeSet();
|
||||
m.headSet(new Object());
|
||||
throw new Exception("headSet, natural ordering");
|
||||
} catch (ClassCastException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
SortedSet m = new TreeSet();
|
||||
m.tailSet(new Object());
|
||||
throw new Exception("tailSet, natural ordering");
|
||||
} catch (ClassCastException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
SortedSet m = new TreeSet(String.CASE_INSENSITIVE_ORDER);
|
||||
m.headSet(new Integer(0));
|
||||
throw new Exception("headSet, explicit comparator");
|
||||
} catch (ClassCastException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
SortedSet m = new TreeSet(String.CASE_INSENSITIVE_ORDER);
|
||||
m.tailSet(new Integer(0));
|
||||
throw new Exception("tailSet, explicit comparator");
|
||||
} catch (ClassCastException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
SortedMap m = new TreeMap();
|
||||
m.headMap(null);
|
||||
throw new Exception("(null endpoint)headMap, natural ordering");
|
||||
} catch (NullPointerException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
SortedMap m = new TreeMap();
|
||||
m.tailMap(null);
|
||||
throw new Exception("(null endpoint)tailMap, natural ordering");
|
||||
} catch (NullPointerException e) {
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
SortedMap m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
|
||||
m.headMap(null);
|
||||
throw new Exception("(null endpoint)headMap, explicit comparator");
|
||||
} catch (NullPointerException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
SortedMap m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
|
||||
m.tailMap(null);
|
||||
throw new Exception("(null endpoint)tailMap, explicit comparator");
|
||||
} catch (NullPointerException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
SortedSet m = new TreeSet();
|
||||
m.headSet(null);
|
||||
throw new Exception("(null endpoint)headSet, natural ordering");
|
||||
} catch (NullPointerException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
SortedSet m = new TreeSet();
|
||||
m.tailSet(null);
|
||||
throw new Exception("(null endpoint)tailSet, natural ordering");
|
||||
} catch (NullPointerException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
SortedSet m = new TreeSet(String.CASE_INSENSITIVE_ORDER);
|
||||
m.headSet(null);
|
||||
throw new Exception("(null endpoint)headSet, explicit comparator");
|
||||
} catch (NullPointerException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
SortedSet m = new TreeSet(String.CASE_INSENSITIVE_ORDER);
|
||||
m.tailSet(null);
|
||||
throw new Exception("(null endpoint)tailSet, explicit comparator");
|
||||
} catch (NullPointerException e) {
|
||||
}
|
||||
|
||||
// These should not fail
|
||||
SortedMap m = new TreeMap();
|
||||
m.headMap(new Integer(0));
|
||||
m.tailMap(new Integer(0));
|
||||
m = new TreeMap(String.CASE_INSENSITIVE_ORDER);
|
||||
m.headMap("llama");
|
||||
m.tailMap("llama");
|
||||
|
||||
SortedSet s = new TreeSet();
|
||||
s.headSet(new Integer(0));
|
||||
s.tailSet(new Integer(0));
|
||||
s = new TreeSet(String.CASE_INSENSITIVE_ORDER);
|
||||
s.headSet("drama");
|
||||
s.tailSet("drama");
|
||||
}
|
||||
}
|
||||
109
test/jdk/java/util/TreeMap/NullAtEnd.java
Normal file
109
test/jdk/java/util/TreeMap/NullAtEnd.java
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
/*
|
||||
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 5018254
|
||||
* @summary Test null-allowing Comparators
|
||||
* @author Martin Buchholz
|
||||
*/
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class NullAtEnd {
|
||||
static volatile int passed = 0, failed = 0;
|
||||
|
||||
static void fail(String msg) {
|
||||
failed++;
|
||||
new AssertionError(msg).printStackTrace();
|
||||
}
|
||||
|
||||
static void pass() {
|
||||
passed++;
|
||||
}
|
||||
|
||||
static void unexpected(Throwable t) {
|
||||
failed++;
|
||||
t.printStackTrace();
|
||||
}
|
||||
|
||||
static void check(boolean condition, String msg) {
|
||||
if (condition)
|
||||
passed++;
|
||||
else
|
||||
fail(msg);
|
||||
}
|
||||
|
||||
static void check(boolean condition) {
|
||||
check(condition, "Assertion failure");
|
||||
}
|
||||
|
||||
private static boolean eq(Object x, Object y) {
|
||||
return x == null ? y == null : x.equals(y);
|
||||
}
|
||||
|
||||
private static final Comparator<String> NULL_AT_END
|
||||
= new Comparator<>() {
|
||||
/**
|
||||
* Allows for nulls. Null is greater than anything non-null.
|
||||
*/
|
||||
public int compare(String x, String y) {
|
||||
if (x == null && y == null) return 0;
|
||||
if (x == null && y != null) return 1;
|
||||
if (x != null && y == null) return -1;
|
||||
return x.compareTo(y);
|
||||
}
|
||||
};
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
SortedMap<String,String> m1 = new TreeMap<>(NULL_AT_END);
|
||||
check(eq(m1.put("a", "a"), null));
|
||||
check(eq(m1.put("b", "b"), null));
|
||||
check(eq(m1.put("c", "c"), null));
|
||||
check(eq(m1.put(null, "d"), null));
|
||||
|
||||
SortedMap<String,String> m2 = new TreeMap<>(m1);
|
||||
|
||||
check(eq(m1.lastKey(), null));
|
||||
check(eq(m1.get(m1.lastKey()), "d"));
|
||||
check(eq(m1.remove(m1.lastKey()), "d"));
|
||||
check(eq(m1.lastKey(), "c"));
|
||||
|
||||
check(eq(m2.entrySet().toString(), "[a=a, b=b, c=c, null=d]"));
|
||||
|
||||
SortedMap<String,String> m3 = m2.tailMap("b");
|
||||
|
||||
check(eq(m3.lastKey(), null));
|
||||
check(eq(m3.get(m3.lastKey()), "d"));
|
||||
check(eq(m3.remove(m3.lastKey()), "d"));
|
||||
check(eq(m3.lastKey(), "c"));
|
||||
|
||||
} catch (Throwable t) { unexpected(t); }
|
||||
|
||||
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
||||
if (failed > 0) throw new Error("Some tests failed");
|
||||
}
|
||||
}
|
||||
112
test/jdk/java/util/TreeMap/NullPermissiveComparator.java
Normal file
112
test/jdk/java/util/TreeMap/NullPermissiveComparator.java
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6467933
|
||||
* @summary Test proper handling of comparators permitting nulls
|
||||
* @author Martin Buchholz
|
||||
*/
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class NullPermissiveComparator {
|
||||
|
||||
static void equal(Map m, String s) {
|
||||
equal(m.toString(), s);
|
||||
}
|
||||
|
||||
static void realMain(String[] args) throws Throwable {
|
||||
final Comparator nullLow = new Comparator() {
|
||||
public int compare(Object x, Object y) {
|
||||
return x == y ? 0 :
|
||||
x == null ? -1 :
|
||||
y == null ? 1 :
|
||||
((Comparable)x).compareTo(y); }};
|
||||
|
||||
final Comparator nullHigh = new Comparator() {
|
||||
public int compare(Object x, Object y) {
|
||||
return x == y ? 0 :
|
||||
x == null ? 1 :
|
||||
y == null ? -1 :
|
||||
((Comparable)x).compareTo(y); }};
|
||||
|
||||
TreeMap m = new TreeMap(nullLow);
|
||||
|
||||
m.put("a", "A");
|
||||
m.put("b", "B");
|
||||
m.put("c", "C");
|
||||
|
||||
equal(m, "{a=A, b=B, c=C}");
|
||||
equal(m.headMap("b"), "{a=A}");
|
||||
equal(m.tailMap("b"), "{b=B, c=C}");
|
||||
equal(m.headMap(null), "{}");
|
||||
equal(m.tailMap(null), "{a=A, b=B, c=C}");
|
||||
|
||||
m.put(null, "NULL");
|
||||
|
||||
equal(m, "{null=NULL, a=A, b=B, c=C}");
|
||||
equal(m.headMap("b"), "{null=NULL, a=A}");
|
||||
equal(m.tailMap("b"), "{b=B, c=C}");
|
||||
equal(m.headMap(null), "{}");
|
||||
equal(m.tailMap(null), "{null=NULL, a=A, b=B, c=C}");
|
||||
|
||||
m = new TreeMap(nullHigh);
|
||||
|
||||
m.put("a", "A");
|
||||
m.put("b", "B");
|
||||
m.put("c", "C");
|
||||
|
||||
equal(m, "{a=A, b=B, c=C}");
|
||||
equal(m.headMap("b"), "{a=A}");
|
||||
equal(m.tailMap("b"), "{b=B, c=C}");
|
||||
equal(m.headMap(null), "{a=A, b=B, c=C}");
|
||||
equal(m.tailMap(null), "{}");
|
||||
|
||||
m.put(null, "NULL");
|
||||
|
||||
equal(m, "{a=A, b=B, c=C, null=NULL}");
|
||||
equal(m.headMap("b"), "{a=A}");
|
||||
equal(m.tailMap("b"), "{b=B, c=C, null=NULL}");
|
||||
equal(m.headMap(null), "{a=A, b=B, c=C}");
|
||||
equal(m.tailMap(null), "{null=NULL}");
|
||||
}
|
||||
|
||||
//--------------------- Infrastructure ---------------------------
|
||||
static volatile int passed = 0, failed = 0;
|
||||
static void pass() {passed++;}
|
||||
static void fail() {failed++; Thread.dumpStack();}
|
||||
static void fail(String msg) {System.out.println(msg); fail();}
|
||||
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
||||
static void check(boolean cond) {if (cond) pass(); else fail();}
|
||||
static void equal(Object x, Object y) {
|
||||
if (x == null ? y == null : x.equals(y)) pass();
|
||||
else fail(x + " not equal to " + y);}
|
||||
public static void main(String[] args) throws Throwable {
|
||||
try {realMain(args);} catch (Throwable t) {unexpected(t);}
|
||||
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
||||
if (failed > 0) throw new AssertionError("Some tests failed");}
|
||||
}
|
||||
98
test/jdk/java/util/TreeMap/SubMap.java
Normal file
98
test/jdk/java/util/TreeMap/SubMap.java
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4252490
|
||||
* @summary The firstKey and lastKey
|
||||
*/
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.SortedMap;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class SubMap {
|
||||
public static void main(String[] args) throws Exception {
|
||||
SortedMap m = new TreeMap();
|
||||
m.put(new Integer(1), new Integer(1));
|
||||
m.put(new Integer(2), new Integer(2));
|
||||
m.put(new Integer(3), new Integer(3));
|
||||
SortedMap m2 = m.subMap(new Integer(2), new Integer(2));
|
||||
|
||||
boolean exc = false;
|
||||
try {
|
||||
m2.firstKey();
|
||||
} catch (NoSuchElementException e) {
|
||||
exc = true;
|
||||
}
|
||||
if (!exc)
|
||||
throw new Exception("first key");
|
||||
|
||||
exc = false;
|
||||
try {
|
||||
m2.lastKey();
|
||||
} catch (NoSuchElementException e) {
|
||||
exc = true;
|
||||
}
|
||||
if (!exc)
|
||||
throw new Exception("last key");
|
||||
|
||||
SortedMap m3 = m.subMap(new Integer(2), new Integer(3));
|
||||
if (!m3.firstKey().equals(new Integer(2)))
|
||||
throw new Exception("first key wrong");
|
||||
if (!m3.lastKey().equals(new Integer(2)))
|
||||
throw new Exception("last key wrong");
|
||||
|
||||
SortedSet s = new TreeSet();
|
||||
s.add(new Integer(1));
|
||||
s.add(new Integer(2));
|
||||
s.add(new Integer(3));
|
||||
SortedSet s2 = s.subSet(new Integer(2), new Integer(2));
|
||||
|
||||
exc = false;
|
||||
try {
|
||||
s2.first();
|
||||
} catch (NoSuchElementException e) {
|
||||
exc = true;
|
||||
}
|
||||
if (!exc)
|
||||
throw new Exception("first element");
|
||||
|
||||
exc = false;
|
||||
try {
|
||||
s2.last();
|
||||
} catch (NoSuchElementException e) {
|
||||
exc = true;
|
||||
}
|
||||
if (!exc)
|
||||
throw new Exception("last element");
|
||||
|
||||
SortedSet s3 = s.subSet(new Integer(2), new Integer(3));
|
||||
if (!s3.first().equals(new Integer(2)))
|
||||
throw new Exception("first element wrong");
|
||||
if (!s3.last().equals(new Integer(2)))
|
||||
throw new Exception("last element wrong");
|
||||
}
|
||||
}
|
||||
51
test/jdk/java/util/TreeMap/SubMapClear.java
Normal file
51
test/jdk/java/util/TreeMap/SubMapClear.java
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4468802
|
||||
* @summary Submap clear tickled a bug in an optimization suggested by
|
||||
* Prof. William Collins (Lafayette College)
|
||||
* @author Josh Bloch
|
||||
*/
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class SubMapClear {
|
||||
public static void main(String[] args) {
|
||||
SortedSet treeSet = new TreeSet();
|
||||
for (int i = 1; i <=10; i++)
|
||||
treeSet.add(new Integer(i));
|
||||
Set subSet = treeSet.subSet(new Integer(4),new Integer(10));
|
||||
subSet.clear(); // Used to throw exception
|
||||
|
||||
int[] a = { 1, 2, 3, 10 };
|
||||
Set s = new TreeSet();
|
||||
for (int i = 0; i < a.length; i++)
|
||||
s.add(new Integer(a[i]));
|
||||
if (!treeSet.equals(s))
|
||||
throw new RuntimeException(treeSet.toString());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue