1 8 9 package com.sleepycat.util.test; 10 11 import com.sleepycat.util.PackedInteger; 12 import junit.framework.Test; 13 import junit.framework.TestCase; 14 15 public class PackedIntegerTest extends TestCase 16 { 17 static final int V119 = 119; 18 static final int BYTE_MAX = 0xFF; 19 static final int SHORT_MAX = 0xFFFF; 20 static final int THREE_MAX = 0xFFFFFF; 21 22 public static void main(String [] args) 23 throws Exception { 24 25 junit.framework.TestResult tr = 26 junit.textui.TestRunner.run(suite()); 27 if (tr.errorCount() > 0 || 28 tr.failureCount() > 0) { 29 System.exit(1); 30 } else { 31 System.exit(0); 32 } 33 } 34 35 public static Test suite() { 36 37 return new PackedIntegerTest(); 38 } 39 40 public PackedIntegerTest() { 41 42 super("PackedIntegerTest"); 43 } 44 45 public void runTest() { 46 47 testRange(-V119, V119, 1); 48 49 testRange(-BYTE_MAX - V119, -1 - V119, 2); 50 testRange(1 + V119, BYTE_MAX + V119, 2); 51 52 testRange(-SHORT_MAX - V119, -SHORT_MAX + 99, 3); 53 testRange(-BYTE_MAX - V119 - 99, -BYTE_MAX - V119 - 1, 3); 54 testRange(BYTE_MAX + V119 + 1, BYTE_MAX + V119 + 99, 3); 55 testRange(SHORT_MAX - 99, SHORT_MAX + V119, 3); 56 57 testRange(-THREE_MAX - V119, -THREE_MAX + 99, 4); 58 testRange(-SHORT_MAX - V119 - 99, -SHORT_MAX - V119 - 1, 4); 59 testRange(SHORT_MAX + V119 + 1, SHORT_MAX + V119 + 99, 4); 60 testRange(THREE_MAX - 99, THREE_MAX + V119, 4); 61 62 testRange(Integer.MIN_VALUE, Integer.MIN_VALUE + 99, 5); 63 testRange(Integer.MAX_VALUE - 99, Integer.MAX_VALUE, 5); 64 } 65 66 private void testRange(long firstValue, 67 long lastValue, 68 int bytesExpected) { 69 70 byte[] buf = new byte[1000]; 71 int off = 0; 72 73 for (long longI = firstValue; longI <= lastValue; longI += 1) { 74 int i = (int) longI; 75 int before = off; 76 off = PackedInteger.writeInt(buf, off, i); 77 int bytes = off - before; 78 if (bytes != bytesExpected) { 79 fail("output of value=" + i + " bytes=" + bytes + 80 " bytesExpected=" + bytesExpected); 81 } 82 bytes = PackedInteger.getWriteIntLength(i); 83 if (bytes != bytesExpected) { 84 fail("count of value=" + i + " bytes=" + bytes + 85 " bytesExpected=" + bytesExpected); 86 } 87 } 88 89 off = 0; 90 91 for (long longI = firstValue; longI <= lastValue; longI += 1) { 92 int i = (int) longI; 93 int bytes = PackedInteger.getReadIntLength(buf, off); 94 if (bytes != bytesExpected) { 95 fail("count of value=" + i + " bytes=" + bytes + 96 " bytesExpected=" + bytesExpected); 97 } 98 int value = PackedInteger.readInt(buf, off); 99 if (value != i) { 100 fail("input of value=" + i + " but got=" + value); 101 } 102 off += bytes; 103 } 104 } 105 } 106 | Popular Tags |