1 8 9 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 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 40 }); 44 } 45 46 private void doAllTest(long[] offsets) { 47 48 ArrayList 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 list(long[] array) { 79 80 ArrayList list = new ArrayList (array.length); 81 for (int i = 0; i < array.length; i += 1) { 82 list.add(new Long (array[i])); 83 } 84 return list; 85 } 86 87 private long[] array(ArrayList list) { 88 89 long[] array = new long[list.size()]; 90 for (int i = 0; i < array.length; i += 1) { 91 array[i] = ((Long ) list.get(i)).longValue(); 92 } 93 return array; 94 } 95 96 private String dump(long[] array) { 97 98 StringBuffer buf = new StringBuffer (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 |