KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > output2 > IntMapTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.output2;
21
22 import java.util.Arrays JavaDoc;
23 import junit.framework.TestCase;
24
25 /**
26  *
27  * @author Tim Boudreau
28  */

29 public class IntMapTest extends TestCase {
30
31     public IntMapTest(String JavaDoc testName) {
32         super(testName);
33     }
34
35     public void testFirst() {
36         System.out.println("testFirst");
37         IntMap map = new IntMap();
38
39         int[] indices = new int [] { 5, 12, 23, 62, 247, 375, 489, 5255};
40
41         Object JavaDoc[] values = new Object JavaDoc[] {
42             "zeroth", "first", "second", "third", "fourth", "fifth", "sixth",
43             "seventh"};
44         
45         assert indices.length == values.length;
46         
47         for (int i=0; i < indices.length; i++) {
48             map.put (indices[i], values[i]);
49         }
50         
51         assertTrue ("First entry should be 5", map.first() == 5);
52     }
53     
54     public void testNextEntry() {
55         System.out.println("testNextEntry");
56         IntMap map = new IntMap();
57         
58         int[] indices = new int [] { 5, 12, 23, 62, 247, 375, 489, 5255};
59         
60         Object JavaDoc[] values = new Object JavaDoc[] {
61             "zeroth", "first", "second", "third", "fourth", "fifth", "sixth",
62             "seventh"};
63         
64         assert indices.length == values.length;
65         
66         for (int i=0; i < indices.length; i++) {
67             map.put (indices[i], values[i]);
68         }
69         
70         for (int i=0; i < indices.length-1; i++) {
71             int val = indices[i+1];
72             int next = map.nextEntry (indices[i]);
73             assertTrue ("Entry after " + indices[i] + " should be " + val + " not " + next, next == val);
74         }
75     }
76     
77     public void testPrevEntry() {
78         System.out.println("testPrevEntry");
79         IntMap map = new IntMap();
80         
81         int[] indices = new int [] { 5, 12, 23, 62, 247, 375, 489, 5255};
82         
83         Object JavaDoc[] values = new Object JavaDoc[] {
84             "zeroth", "first", "second", "third", "fourth", "fifth", "sixth",
85             "seventh"};
86         
87         assert indices.length == values.length;
88         
89         for (int i=0; i < indices.length; i++) {
90             map.put (indices[i], values[i]);
91         }
92         
93         for (int i=indices.length-1; i > 0; i--) {
94             int val = indices[i-1];
95             int next = map.prevEntry (indices[i]);
96             assertTrue ("Entry before " + indices[i] + " should be " + val + " not " + next, next == val);
97         }
98     }
99     
100     public void testNearest() {
101         System.out.println("testNearest");
102         IntMap map = new IntMap();
103         
104         int[] indices = new int [] { 5, 12, 23, 62, 247, 375, 489, 5255};
105         
106         Object JavaDoc[] values = new Object JavaDoc[] {
107             "zeroth", "first", "second", "third", "fourth", "fifth", "sixth",
108             "seventh"};
109         
110         assert indices.length == values.length;
111         
112         for (int i=0; i < indices.length; i++) {
113             map.put (indices[i], values[i]);
114         }
115         
116         for (int i=0; i < indices.length-1; i++) {
117             int toTest = indices[i] + ((indices[i+1] - indices[i]) / 2);
118             int next = map.nearest (toTest, false);
119             assertTrue ("Nearest value to " + toTest + " should be " + indices[i+1] + ", not " + next, next == indices[i+1]);
120         }
121         
122         assertTrue ("Value after last entry should be 0th", map.nearest (indices[indices.length-1] + 1000, false) == indices[0]);
123         
124         assertTrue ("Value before first entry should be last", map.nearest (-1, true) == indices[indices.length-1]);
125         
126         assertTrue ("Value after < first entry should be 0th", map.nearest (-1, false) == indices[0]);
127         
128         for (int i = indices.length-1; i > 0; i--) {
129 // int toTest = indices[i] - (indices[i-1] + ((indices[i] - indices[i-1]) / 2));
130
int toTest = indices[i-1] + ((indices[i] - indices[i-1]) / 2);
131             int prev = map.nearest (toTest, true);
132             assertTrue ("Nearest value to " + toTest + " should be " + indices[i-1] + ", not " + prev, prev == indices[i-1]);
133         }
134         
135         assertTrue ("Entry previous to value lower than first entry should be last entry",
136             map.nearest(indices[0] - 1, true) == indices[indices.length -1]);
137         
138         assertTrue ("Value after > last entry should be last 0th", map.nearest(indices[indices.length-1] + 100, false) == indices[0]);
139         
140         assertTrue ("Value before > last entry should be last entry", map.nearest(indices[indices.length-1] + 100, true) == indices[indices.length-1]);
141         
142         assertTrue ("Value after < first entry should be 0th", map.nearest(-10, false) == indices[0]);
143         
144     }
145     
146     
147     /**
148      * Test of get method, of class org.netbeans.core.output2.IntMap.
149      */

150     public void testGet() {
151         System.out.println("testGet");
152         
153         IntMap map = new IntMap();
154         
155         int[] indices = new int [] { 5, 12, 23, 62, 247, 375, 489, 5255};
156         
157         Object JavaDoc[] values = new Object JavaDoc[] {
158             "zeroth", "first", "second", "third", "fourth", "fifth", "sixth",
159             "seventh"};
160         
161         assert indices.length == values.length;
162         
163         for (int i=0; i < indices.length; i++) {
164             map.put (indices[i], values[i]);
165         }
166         
167         for (int i=0; i < indices.length; i++) {
168             assertTrue (map.get(indices[i]) == values[i]);
169         }
170     }
171     
172     public void testGetKeys() {
173         IntMap map = new IntMap();
174         
175         int[] indices = new int [] { 5, 12, 23, 62, 247, 375, 489, 5255};
176         
177         Object JavaDoc[] values = new Object JavaDoc[] {
178             "zeroth", "first", "second", "third", "fourth", "fifth", "sixth",
179             "seventh"};
180             
181         for (int i=0; i < indices.length; i++) {
182             map.put (indices[i], values[i]);
183         }
184
185         int[] keys = map.getKeys();
186         assertTrue ("Keys returned should match those written. Expected: " + i2s(indices) + " Got: " + i2s(keys), Arrays.equals(keys, indices));
187     }
188     
189     private static String JavaDoc i2s (int[] a) {
190         StringBuffer JavaDoc result = new StringBuffer JavaDoc(a.length*2);
191         for (int i=0; i < a.length; i++) {
192             result.append (a[i]);
193             if (i != a.length-1) {
194                 result.append(',');
195             }
196         }
197         return result.toString();
198     }
199 }
200
Popular Tags