KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > cleaner > OffsetTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: OffsetTest.java,v 1.5 2006/10/30 21:14:42 bostic Exp $
7  */

8
9 package com.sleepycat.je.cleaner;
10
11 import java.util.Arrays JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collections JavaDoc;
14 import junit.framework.TestCase;
15
16 import com.sleepycat.je.cleaner.OffsetList;
17 import com.sleepycat.je.cleaner.PackedOffsets;
18
19 /**
20  * Tests the OffsetList and PackedOffset classes.
21  */

22 public class OffsetTest extends TestCase {
23
24     public void testOffsets() {
25
26         doAllTest(new long[] {
27             1,
28             2,
29             0xfffe,
30             0xffff,
31             0xfffff,
32             Integer.MAX_VALUE - 1,
33             Integer.MAX_VALUE,
34
35             /*
36              * The following values don't work, which is probably a bug, but
37              * LSN offsets are not normally this large so the bug currently has
38              * little impact.
39              */

40             //Integer.MAX_VALUE + 1L,
41
//Long.MAX_VALUE - 100L,
42
//Long.MAX_VALUE,
43
});
44     }
45
46     private void doAllTest(long[] offsets) {
47
48         ArrayList JavaDoc list = list(offsets);
49
50         doOneTest(offsets);
51
52         Collections.reverse(list);
53         doOneTest(array(list));
54
55         Collections.shuffle(list);
56         doOneTest(array(list));
57     }
58
59     private void doOneTest(long[] offsets) {
60
61         OffsetList list = new OffsetList();
62         for (int i = 0; i < offsets.length; i += 1) {
63             list.add(offsets[i], true);
64         }
65         long[] array = list.toArray();
66         assertTrue("array=\n" + dump(array) + " offsets=\n" + dump(offsets),
67                    Arrays.equals(offsets, array));
68
69         long[] sorted = new long[array.length];
70         System.arraycopy(array, 0, sorted, 0, array.length);
71         Arrays.sort(sorted);
72
73         PackedOffsets packed = new PackedOffsets();
74         packed.pack(array);
75         assertTrue(Arrays.equals(sorted, packed.toArray()));
76     }
77
78     private ArrayList JavaDoc list(long[] array) {
79
80         ArrayList JavaDoc list = new ArrayList JavaDoc(array.length);
81         for (int i = 0; i < array.length; i += 1) {
82             list.add(new Long JavaDoc(array[i]));
83         }
84         return list;
85     }
86
87     private long[] array(ArrayList JavaDoc list) {
88
89         long[] array = new long[list.size()];
90         for (int i = 0; i < array.length; i += 1) {
91             array[i] = ((Long JavaDoc) list.get(i)).longValue();
92         }
93         return array;
94     }
95
96     private String JavaDoc dump(long[] array) {
97
98         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(array.length * 10);
99         for (int i = 0; i < array.length; i += 1) {
100             buf.append(Long.toString(array[i]));
101             buf.append(' ');
102         }
103         return buf.toString();
104     }
105 }
106
Popular Tags