001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: PackedIntegerTest.java,v 1.3.2.2 2008/01/07 15:14:36 cwl Exp $
007: */
008:
009: package com.sleepycat.util.test;
010:
011: import com.sleepycat.util.PackedInteger;
012: import junit.framework.Test;
013: import junit.framework.TestCase;
014:
015: public class PackedIntegerTest extends TestCase {
016: static final int V119 = 119;
017: static final int BYTE_MAX = 0xFF;
018: static final int SHORT_MAX = 0xFFFF;
019: static final int THREE_MAX = 0xFFFFFF;
020:
021: public static void main(String[] args) throws Exception {
022:
023: junit.framework.TestResult tr = junit.textui.TestRunner
024: .run(suite());
025: if (tr.errorCount() > 0 || tr.failureCount() > 0) {
026: System.exit(1);
027: } else {
028: System.exit(0);
029: }
030: }
031:
032: public static Test suite() {
033:
034: return new PackedIntegerTest();
035: }
036:
037: public PackedIntegerTest() {
038:
039: super ("PackedIntegerTest");
040: }
041:
042: public void runTest() {
043:
044: testRange(-V119, V119, 1);
045:
046: testRange(-BYTE_MAX - V119, -1 - V119, 2);
047: testRange(1 + V119, BYTE_MAX + V119, 2);
048:
049: testRange(-SHORT_MAX - V119, -SHORT_MAX + 99, 3);
050: testRange(-BYTE_MAX - V119 - 99, -BYTE_MAX - V119 - 1, 3);
051: testRange(BYTE_MAX + V119 + 1, BYTE_MAX + V119 + 99, 3);
052: testRange(SHORT_MAX - 99, SHORT_MAX + V119, 3);
053:
054: testRange(-THREE_MAX - V119, -THREE_MAX + 99, 4);
055: testRange(-SHORT_MAX - V119 - 99, -SHORT_MAX - V119 - 1, 4);
056: testRange(SHORT_MAX + V119 + 1, SHORT_MAX + V119 + 99, 4);
057: testRange(THREE_MAX - 99, THREE_MAX + V119, 4);
058:
059: testRange(Integer.MIN_VALUE, Integer.MIN_VALUE + 99, 5);
060: testRange(Integer.MAX_VALUE - 99, Integer.MAX_VALUE, 5);
061: }
062:
063: private void testRange(long firstValue, long lastValue,
064: int bytesExpected) {
065:
066: byte[] buf = new byte[1000];
067: int off = 0;
068:
069: for (long longI = firstValue; longI <= lastValue; longI += 1) {
070: int i = (int) longI;
071: int before = off;
072: off = PackedInteger.writeInt(buf, off, i);
073: int bytes = off - before;
074: if (bytes != bytesExpected) {
075: fail("output of value=" + i + " bytes=" + bytes
076: + " bytesExpected=" + bytesExpected);
077: }
078: bytes = PackedInteger.getWriteIntLength(i);
079: if (bytes != bytesExpected) {
080: fail("count of value=" + i + " bytes=" + bytes
081: + " bytesExpected=" + bytesExpected);
082: }
083: }
084:
085: off = 0;
086:
087: for (long longI = firstValue; longI <= lastValue; longI += 1) {
088: int i = (int) longI;
089: int bytes = PackedInteger.getReadIntLength(buf, off);
090: if (bytes != bytesExpected) {
091: fail("count of value=" + i + " bytes=" + bytes
092: + " bytesExpected=" + bytesExpected);
093: }
094: int value = PackedInteger.readInt(buf, off);
095: if (value != i) {
096: fail("input of value=" + i + " but got=" + value);
097: }
098: off += bytes;
099: }
100: }
101: }
|