001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.util;
005:
006: import com.tc.test.TCTestCase;
007:
008: import java.util.Random;
009:
010: public class OidLongArrayTest extends TCTestCase {
011: Random r = new Random();
012:
013: private OidLongArray bitSetTest(int arySize) {
014: long oid = 1000;
015: OidLongArray bits = new OidLongArray(arySize, oid);
016:
017: for (int i = 0; i < 100; ++i) {
018: int index = r.nextInt(bits.totalBits());
019: bits.setBit(index);
020: Assert.assertTrue("Failed setBit", bits.isSet(index));
021: }
022: return bits;
023: }
024:
025: private OidLongArray bitClrTest(int arySize) {
026: long oid = 1000;
027: byte[] bytes = new byte[arySize * 8];
028: for (int i = 0; i < bytes.length; ++i) {
029: bytes[i] |= 0xff;
030: }
031: byte[] key = Conversion.long2Bytes(oid);
032: OidLongArray bits = new OidLongArray(key, bytes);
033:
034: for (int i = 0; i < 100; ++i) {
035: int index = r.nextInt(bits.totalBits());
036: bits.clrBit(index);
037: Assert.assertTrue("Failed clrBit", !bits.isSet(index));
038: }
039:
040: return bits;
041: }
042:
043: public void testBitsOperations() {
044: bitSetTest(4);
045: bitSetTest(128);
046:
047: bitClrTest(4);
048: bitClrTest(128);
049: }
050:
051: public void testIsZero() {
052: OidLongArray bits = bitClrTest(64);
053: for (int i = 0; i < bits.length(); ++i) {
054: bits.set(i, 0L);
055: }
056: Assert.assertTrue("Failed isZero", bits.isZero());
057: }
058:
059: public void testIsEnded() {
060: OidLongArray bits = new OidLongArray(null, null);
061: Assert.assertTrue("Failed isEnded", bits.isEnded());
062: }
063:
064: public void testCopyOutApplyIn() {
065: long oid = 1000;
066: int arySize = 128;
067: int destSize = 4;
068: OidLongArray bits = new OidLongArray(arySize, oid);
069: for (int i = 0; i < bits.length(); ++i) {
070: bits.set(i, (long) i);
071: }
072:
073: OidLongArray dest = new OidLongArray(destSize, oid);
074: int offset = r.nextInt(bits.length() / destSize) * destSize;
075: bits.copyOut(dest, offset);
076:
077: // verify
078: long expected = (long) offset;
079: for (int i = 0; i < destSize; ++i) {
080: Assert.assertTrue("Failed copyOut" + expected + " got "
081: + dest.get(i), expected == dest.get(i));
082: ++expected;
083: }
084:
085: OidLongArray src = new OidLongArray(destSize, oid);
086: for (int i = 0; i < destSize; ++i) {
087: src.set(i, dest.get(i) * 2);
088: }
089: bits.applyIn(src, offset);
090:
091: // verify
092: for (int i = 0; i < destSize; ++i) {
093: expected = offset + i;
094: expected |= (offset + i) * 2;
095: Assert.assertTrue("Failed applyIn expected " + expected
096: + " got " + bits.get(offset + i), expected == bits
097: .get(offset + i));
098: }
099: }
100:
101: }
|