1 |
/* |
2 |
* Copyright (c) 1999, 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 |
/* |
25 |
* @test |
26 |
* @bug 4252490 |
27 |
* @summary The firstKey and lastKey |
28 |
*/ |
29 |
|
30 |
import java.util.NoSuchElementException; |
31 |
import java.util.SortedMap; |
32 |
import java.util.SortedSet; |
33 |
import java.util.TreeMap; |
34 |
import java.util.TreeSet; |
35 |
|
36 |
public class SubMap { |
37 |
public static void main(String[] args) throws Exception { |
38 |
SortedMap m = new TreeMap(); |
39 |
m.put(new Integer(1), new Integer(1)); |
40 |
m.put(new Integer(2), new Integer(2)); |
41 |
m.put(new Integer(3), new Integer(3)); |
42 |
SortedMap m2 = m.subMap(new Integer(2), new Integer(2)); |
43 |
|
44 |
boolean exc = false; |
45 |
try { |
46 |
m2.firstKey(); |
47 |
} catch (NoSuchElementException e) { |
48 |
exc = true; |
49 |
} |
50 |
if (!exc) |
51 |
throw new Exception("first key"); |
52 |
|
53 |
exc = false; |
54 |
try { |
55 |
m2.lastKey(); |
56 |
} catch (NoSuchElementException e) { |
57 |
exc = true; |
58 |
} |
59 |
if (!exc) |
60 |
throw new Exception("last key"); |
61 |
|
62 |
SortedMap m3 = m.subMap(new Integer(2), new Integer(3)); |
63 |
if (!m3.firstKey().equals(new Integer(2))) |
64 |
throw new Exception("first key wrong"); |
65 |
if (!m3.lastKey().equals(new Integer(2))) |
66 |
throw new Exception("last key wrong"); |
67 |
|
68 |
SortedSet s = new TreeSet(); |
69 |
s.add(new Integer(1)); |
70 |
s.add(new Integer(2)); |
71 |
s.add(new Integer(3)); |
72 |
SortedSet s2 = s.subSet(new Integer(2), new Integer(2)); |
73 |
|
74 |
exc = false; |
75 |
try { |
76 |
s2.first(); |
77 |
} catch (NoSuchElementException e) { |
78 |
exc = true; |
79 |
} |
80 |
if (!exc) |
81 |
throw new Exception("first element"); |
82 |
|
83 |
exc = false; |
84 |
try { |
85 |
s2.last(); |
86 |
} catch (NoSuchElementException e) { |
87 |
exc = true; |
88 |
} |
89 |
if (!exc) |
90 |
throw new Exception("last element"); |
91 |
|
92 |
SortedSet s3 = s.subSet(new Integer(2), new Integer(3)); |
93 |
if (!s3.first().equals(new Integer(2))) |
94 |
throw new Exception("first element wrong"); |
95 |
if (!s3.last().equals(new Integer(2))) |
96 |
throw new Exception("last element wrong"); |
97 |
} |
98 |
} |