01: /*-
02: * See the file LICENSE for redistribution information.
03: *
04: * Copyright (c) 2002,2008 Oracle. All rights reserved.
05: *
06: * $Id: OffsetTest.java,v 1.5.2.2 2008/01/07 15:14:25 cwl Exp $
07: */
08:
09: package com.sleepycat.je.cleaner;
10:
11: import java.util.Arrays;
12: import java.util.ArrayList;
13: import java.util.Collections;
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[] { 1, 2, 0xfffe, 0xffff, 0xfffff,
27: Integer.MAX_VALUE - 1, Integer.MAX_VALUE,
28:
29: /*
30: * The following values don't work, which is probably a bug, but
31: * LSN offsets are not normally this large so the bug currently has
32: * little impact.
33: */
34: //Integer.MAX_VALUE + 1L,
35: //Long.MAX_VALUE - 100L,
36: //Long.MAX_VALUE,
37: });
38: }
39:
40: private void doAllTest(long[] offsets) {
41:
42: ArrayList list = list(offsets);
43:
44: doOneTest(offsets);
45:
46: Collections.reverse(list);
47: doOneTest(array(list));
48:
49: Collections.shuffle(list);
50: doOneTest(array(list));
51: }
52:
53: private void doOneTest(long[] offsets) {
54:
55: OffsetList list = new OffsetList();
56: for (int i = 0; i < offsets.length; i += 1) {
57: list.add(offsets[i], true);
58: }
59: long[] array = list.toArray();
60: assertTrue("array=\n" + dump(array) + " offsets=\n"
61: + dump(offsets), Arrays.equals(offsets, array));
62:
63: long[] sorted = new long[array.length];
64: System.arraycopy(array, 0, sorted, 0, array.length);
65: Arrays.sort(sorted);
66:
67: PackedOffsets packed = new PackedOffsets();
68: packed.pack(array);
69: assertTrue(Arrays.equals(sorted, packed.toArray()));
70: }
71:
72: private ArrayList list(long[] array) {
73:
74: ArrayList list = new ArrayList(array.length);
75: for (int i = 0; i < array.length; i += 1) {
76: list.add(new Long(array[i]));
77: }
78: return list;
79: }
80:
81: private long[] array(ArrayList list) {
82:
83: long[] array = new long[list.size()];
84: for (int i = 0; i < array.length; i += 1) {
85: array[i] = ((Long) list.get(i)).longValue();
86: }
87: return array;
88: }
89:
90: private String dump(long[] array) {
91:
92: StringBuffer buf = new StringBuffer(array.length * 10);
93: for (int i = 0; i < array.length; i += 1) {
94: buf.append(Long.toString(array[i]));
95: buf.append(' ');
96: }
97: return buf.toString();
98: }
99: }
|