1 package org.apache.jcs.utils.struct;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.TestCase;
23
24 /***
25 * Tests the SortedPrefArray used by the recycle bin.
26 * @author aaronsm
27 */
28 public class SortedPrefArrayUnitTest
29 extends TestCase
30 {
31
32 /***
33 * Constructor for the TestSimpleLoad object
34 * @param testName Description of the Parameter
35 */
36 public SortedPrefArrayUnitTest( String testName )
37 {
38 super( testName );
39 }
40
41 /***
42 * Description of the Method
43 * @param args Description of the Parameter
44 */
45 public static void main( String args[] )
46 {
47 String[] testCaseName = { SortedPrefArrayUnitTest.class.getName() };
48 junit.textui.TestRunner.main( testCaseName );
49 }
50
51 /***
52 * @exception Exception
53 */
54 public void testLargePref()
55 throws Exception
56 {
57 int maxSize = 25;
58
59 SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
60
61 array.setPreferLarge( true );
62 String[] elem = {
63 "10",
64 "11",
65 "01",
66 "02",
67 "03",
68 "04",
69 "05",
70 "08",
71 "07",
72 "06",
73 "09",
74 "12",
75 "13",
76 "15",
77 "14",
78 "20",
79 "25",
80 "29",
81 "28",
82 "16",
83 "17",
84 "96",
85 "00",
86 "72",
87 "39",
88 "55",
89 "44",
90 "26",
91 "22",
92 "59",
93 "38",
94 "16",
95 "27" };
96
97
98 for ( int i = 0; i < elem.length; i++ )
99 {
100 array.add( elem[i] );
101 System.out.println( array.dumpArray() );
102 }
103
104 assertEquals( "Size was not as expected.", maxSize, array.size() );
105
106
107 String smallest = (String) array.getSmallest();
108 assertEquals( "smallest should be 08", "08", smallest );
109
110 String largest = (String) array.getLargest();
111 assertEquals( "Largest should be 96", "96", largest );
112
113
114 String taken = (String) array.takeNearestLargerOrEqual( "95" );
115 assertEquals( "Taken should be 96", "96", taken );
116 assertEquals( "Size was not as expected.", ( maxSize - 1 ), array.size() );
117
118 System.out.println( array.dumpArray() );
119 }
120
121 /***
122 * Verify that we don't get an error when taking from an empty array.
123 * @throws Exception
124 */
125 public void testEmptyTake()
126 throws Exception
127 {
128 int maxSize = 25;
129 SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
130 array.setPreferLarge( true );
131 for ( int i = 0; i < maxSize; i++ )
132 {
133 String taken = (String) array.takeNearestLargerOrEqual( String.valueOf( i ) );
134 assertNull( "taken should be null, since nothing was in the array", taken );
135 }
136 }
137
138 /***
139 * Verify that we don't get a null pointer if we insert a null.
140 * @throws Exception
141 */
142 public void testNullInsertion()
143 throws Exception
144 {
145 int maxSize = 25;
146 SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
147 array.setPreferLarge( true );
148
149 String[] elem = {
150 "10",
151 "11",
152 "01",
153 "02",
154 "03",
155 "04",
156 "05",
157 "08",
158 "07",
159 "06",
160 "09",
161 "12",
162 "13",
163 "15",
164 "14",
165 "20",
166 "25",
167 "29",
168 "28",
169 "16",
170 "17",
171 "96",
172 "00",
173 "72",
174 "39",
175 "55",
176 "44",
177 "26",
178 "22",
179 "59",
180 "38",
181 "16",
182 "27" };
183
184
185 for ( int i = 0; i < elem.length; i++ )
186 {
187 array.add( elem[i] );
188 }
189 System.out.println( array.dumpArray() );
190
191 assertEquals( "Size was not as expected.", maxSize, array.size() );
192
193 try
194 {
195
196 array.add( null );
197 }
198 catch ( NullPointerException e )
199 {
200 fail( "Got a null pointer inserting a null" );
201 }
202
203 }
204
205 /***
206 * Verify that we don't get an npe when taking with a null
207 * @throws Exception
208 */
209 public void testNullTake()
210 throws Exception
211 {
212 int maxSize = 25;
213 SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
214 array.setPreferLarge( true );
215
216 try
217 {
218 String taken = (String) array.takeNearestLargerOrEqual( null );
219 assertNull( "taken should be null, since nothing was in the array", taken );
220 }
221 catch ( NullPointerException e )
222 {
223 fail( "Got a null pointer trying to take with a null" );
224 }
225 }
226
227 /***
228 * Verify that we don't get an npe when taking from an array of only one
229 * @throws Exception
230 */
231 public void testSingleItemTake()
232 throws Exception
233 {
234 int maxSize = 25;
235 SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
236 array.setPreferLarge( true );
237
238 array.add( "10" );
239 System.out.println( array.dumpArray() );
240
241 try
242 {
243 String taken = (String) array.takeNearestLargerOrEqual( "09" );
244 System.out.println( taken );
245 assertNotNull( "taken should not be null, since nothing was in the array", taken );
246 }
247 catch ( NullPointerException e )
248 {
249 fail( "Got a null pointer trying to take with a null" );
250 }
251 }
252
253 /***
254 * Verify that we don't get an npe when taking from an array of only one
255 * @throws Exception
256 */
257 public void testSingleItemTakeLarger()
258 throws Exception
259 {
260 int maxSize = 25;
261 SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
262 array.setPreferLarge( true );
263
264 array.add( "10" );
265
266 try
267 {
268 String taken = (String) array.takeNearestLargerOrEqual( "11" );
269 assertNull( "taken should be null, since nothing smaller was in the array", taken );
270 }
271 catch ( NullPointerException e )
272 {
273 fail( "Got a null pointer trying to take with a null" );
274 }
275 }
276
277 /***
278 * Verify that we don't get an npe when taking from an array of none
279 * @throws Exception
280 */
281 public void testSingleItemTakeLargerEmpty()
282 throws Exception
283 {
284 int maxSize = 25;
285 SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
286 array.setPreferLarge( true );
287
288 try
289 {
290 String taken = (String) array.takeNearestLargerOrEqual( "11" );
291 assertNull( "taken should be null, since nothing was in the array", taken );
292 }
293 catch ( NullPointerException e )
294 {
295 fail( "Got a null pointer trying to take with a null" );
296 }
297 }
298
299 /***
300 * Test taking the largest item.
301 * @exception Exception
302 */
303 public void testTakeLargestItem()
304 throws Exception
305 {
306 int maxSize = 9;
307
308 SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
309
310 array.setPreferLarge( true );
311 String[] elem = { "01", "02", "03", "04", "05", "08", "07", "06", "09", };
312
313
314 for ( int i = 0; i < elem.length; i++ )
315 {
316 array.add( elem[i] );
317 System.out.println( array.dumpArray() );
318 }
319
320 assertEquals( "Size was not as expected.", maxSize, array.size() );
321
322
323 String smallest = (String) array.getSmallest();
324 assertEquals( "smallest is not as expected", "01", smallest );
325
326 String largest = (String) array.getLargest();
327 assertEquals( "Largest is not as expected", "09", largest );
328
329
330 String taken = (String) array.takeNearestLargerOrEqual( "09" );
331 assertEquals( "Taken is not as expected", "09", taken );
332 assertEquals( "Size was not as expected.", ( maxSize - 1 ), array.size() );
333
334 System.out.println( "testTakeLastItem" + array.dumpArray() );
335 }
336
337 /***
338 * Test taking every last item.
339 * <p>
340 * @exception Exception
341 */
342 public void testTakeEveryLastItem()
343 throws Exception
344 {
345 int maxSize = 9;
346
347 SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
348
349 array.setPreferLarge( true );
350 String[] elem = { "01", "02", "03", "04", "05", "08", "07", "06", "09", };
351
352
353 for ( int i = 0; i < elem.length; i++ )
354 {
355 array.add( elem[i] );
356 System.out.println( array.dumpArray() );
357 }
358
359 assertEquals( "Size was not as expected.", maxSize, array.size() );
360
361
362 String smallest = (String) array.getSmallest();
363 assertEquals( "smallest is not as expected", "01", smallest );
364
365 String largest = (String) array.getLargest();
366 assertEquals( "Largest is not as expected", "09", largest );
367
368
369 String taken = (String) array.takeNearestLargerOrEqual( "09" );
370 assertEquals( "Taken is not as expected", "09", taken );
371 assertEquals( "Size was not as expected. " + array.dumpArray(), ( maxSize - 1 ), array.size() );
372
373 System.out.println( "testTakeEveryLastItem" + array.dumpArray() );
374
375
376
377 for ( int i = elem.length - 1; i >= 0; i-- )
378 {
379 array.takeNearestLargerOrEqual( elem[i] );
380 }
381 System.out.println( "testTakeEveryLastItem" + array.dumpArray() );
382
383 assertEquals( "There should nothing left. " + array.dumpArray(), 0, array.size() );
384 }
385
386 /***
387 * Try taking an item larger than the greatest.
388 */
389 public void testTakeLargerThanGreatest()
390 {
391 int maxSize = 3;
392
393 SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
394
395 array.setPreferLarge( true );
396 String[] elem = { "01", "02", "03" };
397
398
399 for ( int i = 0; i < elem.length; i++ )
400 {
401 array.add( elem[i] );
402 System.out.println( array.dumpArray() );
403 }
404
405
406 Comparable taken = array.takeNearestLargerOrEqual( "04" );
407 System.out.println( "testTakeLargerThanGreatest" + array.dumpArray() );
408
409 assertNull( "We should have nothing since the largest element was smaller than what we asked for. "
410 + " Instead we got " + taken, taken );
411 }
412
413 /***
414 * Try taking an item equal to the greatest. Make the last two the same size
415 */
416 public void testEqualToGreatest_LastTwoSameSize()
417 {
418 int maxSize = 3;
419
420 SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
421
422 array.setPreferLarge( true );
423 String[] elem = { "01", "02", "03", "03" };
424
425
426 for ( int i = 0; i < elem.length; i++ )
427 {
428 array.add( elem[i] );
429 System.out.println( array.dumpArray() );
430 }
431
432
433 Comparable taken = array.takeNearestLargerOrEqual( "03" );
434 System.out.println( "testEqualToGreatest_LastTwoSameSize" + array.dumpArray() );
435
436 assertNotNull( "We should have something since the largest element was equal to what we asked for.", taken );
437 }
438
439 /***
440 * Try taking an item equal to the greatest. The second to last should be smaller. This verifies the most basic funtionality.
441 */
442 public void testEqualToGreatest()
443 {
444 int maxSize = 3;
445
446 SortedPreferentialArray array = new SortedPreferentialArray( maxSize );
447
448 array.setPreferLarge( true );
449 String[] elem = { "01", "02", "03" };
450
451
452 for ( int i = 0; i < elem.length; i++ )
453 {
454 array.add( elem[i] );
455 System.out.println( array.dumpArray() );
456 }
457
458
459 Comparable taken = array.takeNearestLargerOrEqual( "03" );
460 System.out.println( "testEqualToGreatest" + array.dumpArray() );
461
462 assertNotNull( "We should have something since the largest element was equal to what we asked for.", taken );
463 }
464 }