This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new f2bdb0cb7 Add long support to `BitField` (#1561)
f2bdb0cb7 is described below
commit f2bdb0cb74a08b15439abf18df6ac2b52549394a
Author: Gary Gregory <[email protected]>
AuthorDate: Mon Jan 19 16:19:55 2026 -0500
Add long support to `BitField` (#1561)
* Add testLang1641()
* Rename some test methods
* Add long support to BitField
* Remove unnecessary blank line in BitFieldTest
* Fix missing newline at end of file
Add missing newline at the end of BitFieldLongTest.java
* Fix return value
* Use final
* Add more tests
---
.../java/org/apache/commons/lang3/BitField.java | 118 ++++++++++-
.../org/apache/commons/lang3/BitFieldLongTest.java | 231 +++++++++++++++++++++
.../org/apache/commons/lang3/BitFieldTest.java | 195 ++++++++---------
3 files changed, 449 insertions(+), 95 deletions(-)
diff --git a/src/main/java/org/apache/commons/lang3/BitField.java
b/src/main/java/org/apache/commons/lang3/BitField.java
index edf4fdb4e..d84120218 100644
--- a/src/main/java/org/apache/commons/lang3/BitField.java
+++ b/src/main/java/org/apache/commons/lang3/BitField.java
@@ -72,7 +72,7 @@
*/
public class BitField {
- private final int mask;
+ private final long mask;
private final int shiftCount;
@@ -86,6 +86,17 @@ public BitField(final int mask) {
this.shiftCount = mask == 0 ? 0 : Integer.numberOfTrailingZeros(mask);
}
+ /**
+ * Creates a BitField instance.
+ *
+ * @param mask the mask specifying which bits apply to this BitField. Bits
that are set in this mask are the bits that this BitField operates on.
+ * @since 3.21.0
+ */
+ public BitField(final long mask) {
+ this.mask = mask;
+ this.shiftCount = mask == 0 ? 0 : Long.numberOfTrailingZeros(mask);
+ }
+
/**
* Clears the bits.
*
@@ -93,6 +104,17 @@ public BitField(final int mask) {
* @return the value of holder with the specified bits cleared (set to
{@code 0}).
*/
public int clear(final int holder) {
+ return (int) (holder & ~mask);
+ }
+
+ /**
+ * Clears the bits.
+ *
+ * @param holder the long data containing the bits we're interested in.
+ * @return the value of holder with the specified bits cleared (set to
{@code 0}).
+ * @since 3.21.0
+ */
+ public long clear(final long holder) {
return holder & ~mask;
}
@@ -123,6 +145,17 @@ public short clearShort(final short holder) {
* @return the selected bits.
*/
public int getRawValue(final int holder) {
+ return (int) (holder & mask);
+ }
+
+ /**
+ * Gets the value for the specified BitField, unshifted.
+ *
+ * @param holder the long data containing the bits we're interested in.
+ * @return the selected bits.
+ * @since 3.21.0
+ */
+ public long getRawValue(final long holder) {
return holder & mask;
}
@@ -166,6 +199,22 @@ public int getValue(final int holder) {
return getRawValue(holder) >> shiftCount;
}
+ /**
+ * Gets the value for the specified BitField, appropriately shifted right.
+ * <p>
+ * Many users of a BitField will want to treat the specified bits as an
long value, and will not want to be aware that the value is stored as a
BitField (and
+ * so shifted left so many bits).
+ * </p>
+ *
+ * @param holder the long data containing the bits we're interested in.
+ * @return the selected bits, shifted right appropriately.
+ * @see #setValue(long,long)
+ * @since 3.21.0
+ */
+ public long getValue(final long holder) {
+ return getRawValue(holder) >> shiftCount;
+ }
+
/**
* Tests whether all of the bits are set or not.
* <p>
@@ -179,11 +228,25 @@ public boolean isAllSet(final int holder) {
return (holder & mask) == mask;
}
+ /**
+ * Tests whether all of the bits are set or not.
+ * <p>
+ * This is a stricter test than {@link #isSet(long)}, in that all of the
bits in a multi-bit set must be set for this method to return {@code true}.
+ * </p>
+ *
+ * @param holder the long data containing the bits we're interested in.
+ * @return {@code true} if all of the bits are set, else {@code false}.
+ * @since 3.21.0
+ */
+ public boolean isAllSet(final long holder) {
+ return (holder & mask) == mask;
+ }
+
/**
* Tests whether the field is set or not.
* <p>
* This is most commonly used for a single-bit field, which is often used
to represent a boolean value; the results of using it for a multi-bit field is
to
- * determine whether *any* of its bits are set.
+ * determine whether <em>any</em> of its bits are set.
* </p>
*
* @param holder the int data containing the bits we're interested in
@@ -193,6 +256,21 @@ public boolean isSet(final int holder) {
return (holder & mask) != 0;
}
+ /**
+ * Tests whether the field is set or not.
+ * <p>
+ * This is most commonly used for a single-bit field, which is often used
to represent a boolean value; the results of using it for a multi-bit field is
to
+ * determine whether <em>any</em> of its bits are set.
+ * </p>
+ *
+ * @param holder the long data containing the bits we're interested in
+ * @return {@code true} if any of the bits are set, else {@code false}
+ * @since 3.21.0
+ */
+ public boolean isSet(final long holder) {
+ return (holder & mask) != 0;
+ }
+
/**
* Sets the bits.
*
@@ -200,6 +278,17 @@ public boolean isSet(final int holder) {
* @return the value of holder with the specified bits set to {@code 1}.
*/
public int set(final int holder) {
+ return (int) (holder | mask);
+ }
+
+ /**
+ * Sets the bits.
+ *
+ * @param holder the long data containing the bits we're interested in.
+ * @return the value of holder with the specified bits set to {@code 1}.
+ * @since 3.21.0
+ */
+ public long set(final long holder) {
return holder | mask;
}
@@ -214,6 +303,18 @@ public int setBoolean(final int holder, final boolean
flag) {
return flag ? set(holder) : clear(holder);
}
+ /**
+ * Sets a boolean BitField.
+ *
+ * @param holder the long data containing the bits we're interested in.
+ * @param flag indicating whether to set or clear the bits.
+ * @return the value of holder with the specified bits set or cleared.
+ * @since 3.21.0
+ */
+ public long setBoolean(final long holder, final boolean flag) {
+ return flag ? set(holder) : clear(holder);
+ }
+
/**
* Sets the bits.
*
@@ -277,6 +378,19 @@ public short setShortValue(final short holder, final short
value) {
* @see #getValue(int)
*/
public int setValue(final int holder, final int value) {
+ return (int) (holder & ~mask | value << shiftCount & mask);
+ }
+
+ /**
+ * Sets the bits with new values.
+ *
+ * @param holder the long data containing the bits we're interested in.
+ * @param value the new value for the specified bits.
+ * @return the value of holder with the bits from the value parameter
replacing the old bits.
+ * @see #getValue(long)
+ * @since 3.21.0
+ */
+ public long setValue(final long holder, final long value) {
return holder & ~mask | value << shiftCount & mask;
}
}
diff --git a/src/test/java/org/apache/commons/lang3/BitFieldLongTest.java
b/src/test/java/org/apache/commons/lang3/BitFieldLongTest.java
new file mode 100644
index 000000000..d43d3f31a
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/BitFieldLongTest.java
@@ -0,0 +1,231 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.lang3;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link BitField} constructed with long masks.
+ */
+class BitFieldLongTest {
+
+ private static final BitField BF_MULTI = new BitField(0x3F80L);
+ private static final BitField BF_MULTI_L = new
BitField(0x3F80_0000_0000_0000L);
+ private static final BitField BF_SINGLE = new BitField(0x4000L);
+ private static final BitField BF_SINGLE_L = new
BitField(0x4000_0000_0000_0000L);
+ private static final BitField BF_ZERO = new BitField(0L);
+ private static final BitField BF_ZERO_L = new BitField(0L);
+
+ @Test
+ void testByteBoolean() {
+ assertEquals(0, new BitField(0L).setByteBoolean((byte) 0, true));
+ assertEquals(1, new BitField(1L).setByteBoolean((byte) 0, true));
+ assertEquals(2, new BitField(2L).setByteBoolean((byte) 0, true));
+ assertEquals(4, new BitField(4L).setByteBoolean((byte) 0, true));
+ assertEquals(8, new BitField(8L).setByteBoolean((byte) 0, true));
+ assertEquals(16, new BitField(16L).setByteBoolean((byte) 0, true));
+ assertEquals(32, new BitField(32L).setByteBoolean((byte) 0, true));
+ assertEquals(64, new BitField(64L).setByteBoolean((byte) 0, true));
+ assertEquals(-128, new BitField(128L).setByteBoolean((byte) 0, true));
+ assertEquals(1, new BitField(0L).setByteBoolean((byte) 1, false));
+ assertEquals(0, new BitField(1L).setByteBoolean((byte) 1, false));
+ assertEquals(0, new BitField(2L).setByteBoolean((byte) 2, false));
+ assertEquals(0, new BitField(4L).setByteBoolean((byte) 4, false));
+ assertEquals(0, new BitField(8L).setByteBoolean((byte) 8, false));
+ assertEquals(0, new BitField(16L).setByteBoolean((byte) 16, false));
+ assertEquals(0, new BitField(32L).setByteBoolean((byte) 32, false));
+ assertEquals(0, new BitField(64L).setByteBoolean((byte) 64, false));
+ assertEquals(0, new BitField(128L).setByteBoolean((byte) 128, false));
+ assertEquals(-2, new BitField(1L).setByteBoolean((byte) 255, false));
+ final byte clearedBit = new BitField(0x40L).setByteBoolean((byte) -
63, false);
+ assertFalse(new BitField(0x40).isSet(clearedBit));
+ }
+
+ /**
+ * Tests the {@link BitField#clear()} method.
+ */
+ @Test
+ void testClearInt() {
+ assertEquals(BF_MULTI.clear(-1), 0xFFFF_C07F);
+ assertEquals(BF_SINGLE.clear(-1), 0xFFFF_BFFF);
+ assertEquals(BF_ZERO.clear(-1), 0xFFFF_FFFF);
+ assertEquals(BF_MULTI_L.clear(-1), 0xFFFF_FFFF);
+ assertEquals(BF_SINGLE_L.clear(-1), 0xFFFF_FFFF);
+ assertEquals(BF_ZERO_L.clear(-1), 0xFFFF_FFFF);
+ }
+
+ /**
+ * Tests the {@link BitField#clear()} method.
+ */
+ @Test
+ void testClearLong() {
+ assertEquals(BF_MULTI.clear(-1L), 0xFFFF_FFFF_FFFF_C07FL);
+ assertEquals(BF_SINGLE.clear(-1L), 0xFFFF_FFFF_FFFF_BFFFL);
+ assertEquals(BF_ZERO.clear(-1L), 0xFFFF_FFFF);
+ assertEquals(BF_MULTI_L.clear(-1L), 0xC07F_FFFF_FFFF_FFFFL);
+ assertEquals(BF_SINGLE_L.clear(-1L), 0xBFFF_FFFF_FFFF_FFFFL);
+ assertEquals(BF_ZERO_L.clear(-1L), 0xFFFF_FFFF_FFFF_FFFFL);
+ }
+
+ /**
+ * Tests the {@link BitField#clearShort()} method.
+ */
+ @Test
+ void testClearShort() {
+ assertEquals(BF_MULTI.clearShort((short) - 1), (short) 0xC07F);
+ assertEquals(BF_SINGLE.clearShort((short) - 1), (short) 0xBFFF);
+ assertEquals(BF_ZERO.clearShort((short) -1), (short) 0xFFFF);
+ assertEquals(BF_MULTI_L.clearShort((short) - 1), (short) 0xFFFF);
+ assertEquals(BF_SINGLE_L.clearShort((short) - 1), (short) 0xFFFF);
+ assertEquals(BF_ZERO_L.clearShort((short) -1), (short) 0xFFFF);
+ }
+
+ @Test
+ void testEdgeCases() {
+ final BitField field = new BitField(0x1L); // bit 0
+ assertEquals(1L, field.set(0L));
+ assertEquals(0L, field.clear(1L));
+ final BitField highField = new BitField(0x8000_0000_0000_0000L); //
highest bit
+ assertEquals(0x8000_0000_0000_0000L, highField.set(0L));
+ assertEquals(0L, highField.clear(0x8000_0000_0000_0000L));
+ }
+
+ /**
+ * Tests the {@link BitField#getRawValue()} method.
+ */
+ @Test
+ void testGetRawValue() {
+ // mask < max int and int input
+ assertEquals(BF_MULTI.getRawValue(-1), 0x3F80);
+ assertEquals(BF_MULTI.getRawValue(0), 0);
+ assertEquals(BF_SINGLE.getRawValue(-1), 0x4000);
+ assertEquals(BF_SINGLE.getRawValue(0), 0);
+ assertEquals(BF_ZERO.getRawValue(-1), 0);
+ assertEquals(BF_ZERO.getRawValue(0), 0);
+ // mask > max int and int input
+ assertEquals(BF_MULTI_L.getRawValue(-1), 0);
+ assertEquals(BF_MULTI_L.getRawValue(0), 0);
+ assertEquals(BF_SINGLE_L.getRawValue(-1), 0);
+ assertEquals(BF_SINGLE_L.getRawValue(0), 0);
+ assertEquals(BF_ZERO_L.getRawValue(-1), 0);
+ assertEquals(BF_ZERO_L.getRawValue(0), 0);
+ // mask > max int and long input
+ assertEquals(BF_MULTI_L.getRawValue(-1L), 0x3F80_0000_0000_0000L);
+ assertEquals(BF_MULTI_L.getRawValue(0L), 0);
+ assertEquals(BF_SINGLE_L.getRawValue(-1L), 0x4000_0000_0000_0000L);
+ assertEquals(BF_SINGLE_L.getRawValue(0L), 0);
+ assertEquals(BF_ZERO_L.getRawValue(-1L), 0);
+ assertEquals(BF_ZERO_L.getRawValue(0L), 0);
+ }
+
+ /**
+ * Tests the {@link BitField#getShortRawValue()} method.
+ */
+ @Test
+ void testGetShortRawValue() {
+ // mask < max int and short input
+ assertEquals(BF_MULTI.getShortRawValue((short) - 1), (short) 0x3F80);
+ assertEquals(BF_MULTI.getShortRawValue((short) 0), (short) 0);
+ assertEquals(BF_SINGLE.getShortRawValue((short) - 1), (short) 0x4000);
+ assertEquals(BF_SINGLE.getShortRawValue((short) 0), (short) 0);
+ assertEquals(BF_ZERO.getShortRawValue((short) -1), (short) 0);
+ assertEquals(BF_ZERO.getShortRawValue((short) 0), (short) 0);
+ // mask > max int and short input
+ assertEquals(BF_MULTI_L.getShortRawValue((short) - 1), (short) 0);
+ assertEquals(BF_MULTI_L.getShortRawValue((short) 0), (short) 0);
+ assertEquals(BF_SINGLE_L.getShortRawValue((short) - 1), (short) 0);
+ assertEquals(BF_SINGLE_L.getShortRawValue((short) 0), (short) 0);
+ assertEquals(BF_ZERO_L.getShortRawValue((short) -1), (short) 0);
+ assertEquals(BF_ZERO_L.getShortRawValue((short) 0), (short) 0);
+ }
+
+ /**
+ * Tests the getRawValue() and getValue() methods
+ */
+ @Test
+ void testGetValueAndRawValueIntRange() {
+ final BitField field = new BitField(0xFF00L); // bits 8-15
+ final long holder = 0x1234L;
+ // raw value: bits & mask
+ assertEquals(0x1200L, field.getRawValue(holder));
+ // shifted value: shifted right to LSB
+ assertEquals(0x12L, field.getValue(holder));
+ }
+
+ /**
+ * Tests the isSet() and isAllSet() methods
+ */
+ @Test
+ void testIsSetAndIsAllSet() {
+ final BitField field1 = new BitField(0x3000L); // bits 12-13
+ final long holder = 0x3000L;
+ assertTrue(field1.isSet(holder));
+ assertTrue(field1.isAllSet(holder));
+ final long holderPartial = 0x1000L;
+ assertTrue(field1.isSet(holderPartial));
+ assertFalse(field1.isAllSet(holderPartial));
+ }
+
+ @Test
+ void testMultipleBits() {
+ final BitField field = new BitField(0xF0F0L); // multiple bits
+ final long holder = 0xAAAA;
+ final long newValue = 0x55;
+ final long result = field.setValue(holder, newValue);
+ assertEquals(holder & ~0xF0F0L | newValue << 4 & 0xF0F0L, result);
+ }
+
+ /**
+ * Tests the set() method
+ */
+ @Test
+ void testSet() {
+ final BitField field = new BitField(0x1000L); // bit 12
+ final long holder = 0x0000L;
+ final long result = field.set(holder);
+ assertEquals(0x1000L, result);
+ }
+
+ /**
+ * Tests the setBoolean() method
+ */
+ @Test
+ void testSetBoolean() {
+ final BitField field = new BitField(0x1000L); // bit 12
+ final long holder = 0x0000L;
+ final long setTrue = field.setBoolean(holder, true);
+ assertEquals(0x1000L, setTrue);
+ final long setFalse = field.setBoolean(setTrue, false);
+ assertEquals(0x0000L, setFalse);
+ }
+
+ /**
+ * Tests the setValue() method
+ */
+ @Test
+ void testSetValue() {
+ final BitField field = new BitField(0xFF00L); // bits 8-15
+ final long holder = 0x1200L;
+ final long result = field.setValue(holder, 0x34L); // replace bits
8-15 with 0x34
+ assertEquals(0x3400L, result);
+ }
+}
diff --git a/src/test/java/org/apache/commons/lang3/BitFieldTest.java
b/src/test/java/org/apache/commons/lang3/BitFieldTest.java
index 27025cccb..785cb2ac0 100644
--- a/src/test/java/org/apache/commons/lang3/BitFieldTest.java
+++ b/src/test/java/org/apache/commons/lang3/BitFieldTest.java
@@ -23,16 +23,16 @@
import org.junit.jupiter.api.Test;
/**
- * Class to test BitField functionality
+ * Tests {@link BitField} constructed with int masks.
*/
class BitFieldTest extends AbstractLangTest {
- private static final BitField bf_multi = new BitField(0x3F80);
- private static final BitField bf_single = new BitField(0x4000);
- private static final BitField bf_zero = new BitField(0);
+ private static final BitField BF_MULTI = new BitField(0x3F80);
+ private static final BitField BF_SINGLE = new BitField(0x4000);
+ private static final BitField BF_ZERO = new BitField(0);
@Test
- void testByte() {
+ void testByteBoolean() {
assertEquals(0, new BitField(0).setByteBoolean((byte) 0, true));
assertEquals(1, new BitField(1).setByteBoolean((byte) 0, true));
assertEquals(2, new BitField(2).setByteBoolean((byte) 0, true));
@@ -53,144 +53,153 @@ void testByte() {
assertEquals(0, new BitField(128).setByteBoolean((byte) 128, false));
assertEquals(-2, new BitField(1).setByteBoolean((byte) 255, false));
final byte clearedBit = new BitField(0x40).setByteBoolean((byte) - 63,
false);
-
assertFalse(new BitField(0x40).isSet(clearedBit));
}
/**
- * test the clear() method
+ * Tests the {@link BitField#clear()} method.
+ */
+ @Test
+ void testClearInt() {
+ assertEquals(BF_MULTI.clear(-1), 0xFFFFC07F);
+ assertEquals(BF_SINGLE.clear(-1), 0xFFFFBFFF);
+ assertEquals(BF_ZERO.clear(-1), 0xFFFFFFFF);
+ }
+
+ /**
+ * Tests the {@link BitField#clear()} method.
*/
@Test
- void testClear() {
- assertEquals(bf_multi.clear(-1), 0xFFFFC07F);
- assertEquals(bf_single.clear(-1), 0xFFFFBFFF);
- assertEquals(bf_zero.clear(-1), 0xFFFFFFFF);
+ void testClearLong() {
+ assertEquals(BF_MULTI.clear(-1L), 0xFFFFC07F);
+ assertEquals(BF_SINGLE.clear(-1L), 0xFFFFBFFF);
+ assertEquals(BF_ZERO.clear(-1L), 0xFFFFFFFF);
}
/**
- * test the clearShort() method
+ * Tests the {@link BitField#clearShort()} method.
*/
@Test
void testClearShort() {
- assertEquals(bf_multi.clearShort((short) - 1), (short) 0xC07F);
- assertEquals(bf_single.clearShort((short) - 1), (short) 0xBFFF);
- assertEquals(bf_zero.clearShort((short) -1), (short) 0xFFFF);
+ assertEquals(BF_MULTI.clearShort((short) - 1), (short) 0xC07F);
+ assertEquals(BF_SINGLE.clearShort((short) - 1), (short) 0xBFFF);
+ assertEquals(BF_ZERO.clearShort((short) -1), (short) 0xFFFF);
}
/**
- * test the getRawValue() method
+ * Tests the {@link BitField#getRawValue()} method.
*/
@Test
void testGetRawValue() {
- assertEquals(bf_multi.getRawValue(-1), 0x3F80);
- assertEquals(bf_multi.getRawValue(0), 0);
- assertEquals(bf_single.getRawValue(-1), 0x4000);
- assertEquals(bf_single.getRawValue(0), 0);
- assertEquals(bf_zero.getRawValue(-1), 0);
- assertEquals(bf_zero.getRawValue(0), 0);
+ assertEquals(BF_MULTI.getRawValue(-1), 0x3F80);
+ assertEquals(BF_MULTI.getRawValue(0), 0);
+ assertEquals(BF_SINGLE.getRawValue(-1), 0x4000);
+ assertEquals(BF_SINGLE.getRawValue(0), 0);
+ assertEquals(BF_ZERO.getRawValue(-1), 0);
+ assertEquals(BF_ZERO.getRawValue(0), 0);
}
/**
- * test the getShortRawValue() method
+ * Tests the {@link BitField#getShortRawValue()} method.
*/
@Test
void testGetShortRawValue() {
- assertEquals(bf_multi.getShortRawValue((short) - 1), (short) 0x3F80);
- assertEquals(bf_multi.getShortRawValue((short) 0), (short) 0);
- assertEquals(bf_single.getShortRawValue((short) - 1), (short) 0x4000);
- assertEquals(bf_single.getShortRawValue((short) 0), (short) 0);
- assertEquals(bf_zero.getShortRawValue((short) -1), (short) 0);
- assertEquals(bf_zero.getShortRawValue((short) 0), (short) 0);
+ assertEquals(BF_MULTI.getShortRawValue((short) - 1), (short) 0x3F80);
+ assertEquals(BF_MULTI.getShortRawValue((short) 0), (short) 0);
+ assertEquals(BF_SINGLE.getShortRawValue((short) - 1), (short) 0x4000);
+ assertEquals(BF_SINGLE.getShortRawValue((short) 0), (short) 0);
+ assertEquals(BF_ZERO.getShortRawValue((short) -1), (short) 0);
+ assertEquals(BF_ZERO.getShortRawValue((short) 0), (short) 0);
}
/**
- * test the getShortValue() method
+ * Tests the {@link BitField#getShortValue()} method.
*/
@Test
void testGetShortValue() {
- assertEquals(bf_multi.getShortValue((short) - 1), (short) 127);
- assertEquals(bf_multi.getShortValue((short) 0), (short) 0);
- assertEquals(bf_single.getShortValue((short) - 1), (short) 1);
- assertEquals(bf_single.getShortValue((short) 0), (short) 0);
- assertEquals(bf_zero.getShortValue((short) -1), (short) 0);
- assertEquals(bf_zero.getShortValue((short) 0), (short) 0);
+ assertEquals(BF_MULTI.getShortValue((short) - 1), (short) 127);
+ assertEquals(BF_MULTI.getShortValue((short) 0), (short) 0);
+ assertEquals(BF_SINGLE.getShortValue((short) - 1), (short) 1);
+ assertEquals(BF_SINGLE.getShortValue((short) 0), (short) 0);
+ assertEquals(BF_ZERO.getShortValue((short) -1), (short) 0);
+ assertEquals(BF_ZERO.getShortValue((short) 0), (short) 0);
}
/**
- * test the getValue() method
+ * Tests the {@link BitField#getValue()} method.
*/
@Test
void testGetValue() {
- assertEquals(bf_multi.getValue(-1), 127);
- assertEquals(bf_multi.getValue(0), 0);
- assertEquals(bf_single.getValue(-1), 1);
- assertEquals(bf_single.getValue(0), 0);
- assertEquals(bf_zero.getValue(-1), 0);
- assertEquals(bf_zero.getValue(0), 0);
+ assertEquals(BF_MULTI.getValue(-1), 127);
+ assertEquals(BF_MULTI.getValue(0), 0);
+ assertEquals(BF_SINGLE.getValue(-1), 1);
+ assertEquals(BF_SINGLE.getValue(0), 0);
+ assertEquals(BF_ZERO.getValue(-1), 0);
+ assertEquals(BF_ZERO.getValue(0), 0);
}
/**
- * test the isAllSet() method
+ * Tests the {@link BitField#isAllSet()} method.
*/
@Test
void testIsAllSet() {
for (int j = 0; j < 0x3F80; j += 0x80) {
- assertFalse(bf_multi.isAllSet(j));
- assertTrue(bf_zero.isAllSet(j));
+ assertFalse(BF_MULTI.isAllSet(j));
+ assertTrue(BF_ZERO.isAllSet(j));
}
- assertTrue(bf_multi.isAllSet(0x3F80));
- assertFalse(bf_single.isAllSet(0));
- assertTrue(bf_single.isAllSet(0x4000));
+ assertTrue(BF_MULTI.isAllSet(0x3F80));
+ assertFalse(BF_SINGLE.isAllSet(0));
+ assertTrue(BF_SINGLE.isAllSet(0x4000));
}
/**
- * test the isSet() method
+ * test the isSet() method.
*/
@Test
void testIsSet() {
- assertFalse(bf_multi.isSet(0));
- assertFalse(bf_zero.isSet(0));
+ assertFalse(BF_MULTI.isSet(0));
+ assertFalse(BF_ZERO.isSet(0));
for (int j = 0x80; j <= 0x3F80; j += 0x80) {
- assertTrue(bf_multi.isSet(j));
+ assertTrue(BF_MULTI.isSet(j));
}
for (int j = 0x80; j <= 0x3F80; j += 0x80) {
- assertFalse(bf_zero.isSet(j));
+ assertFalse(BF_ZERO.isSet(j));
}
- assertFalse(bf_single.isSet(0));
- assertTrue(bf_single.isSet(0x4000));
+ assertFalse(BF_SINGLE.isSet(0));
+ assertTrue(BF_SINGLE.isSet(0x4000));
}
/**
- * test the set() method
+ * Tests the {@link BitField#set()} method.
*/
@Test
void testSet() {
- assertEquals(bf_multi.set(0), 0x3F80);
- assertEquals(bf_single.set(0), 0x4000);
- assertEquals(bf_zero.set(0), 0);
+ assertEquals(BF_MULTI.set(0), 0x3F80);
+ assertEquals(BF_SINGLE.set(0), 0x4000);
+ assertEquals(BF_ZERO.set(0), 0);
}
/**
- * test the setBoolean() method
+ * Tests the {@link BitField#setBoolean()} method.
*/
@Test
void testSetBoolean() {
- assertEquals(bf_multi.set(0), bf_multi.setBoolean(0, true));
- assertEquals(bf_single.set(0), bf_single.setBoolean(0, true));
- assertEquals(bf_zero.set(0), bf_zero.setBoolean(0, true));
- assertEquals(bf_multi.clear(-1), bf_multi.setBoolean(-1, false));
- assertEquals(bf_single.clear(-1), bf_single.setBoolean(-1, false));
- assertEquals(bf_zero.clear(-1), bf_zero.setBoolean(-1, false));
+ assertEquals(BF_MULTI.set(0), BF_MULTI.setBoolean(0, true));
+ assertEquals(BF_SINGLE.set(0), BF_SINGLE.setBoolean(0, true));
+ assertEquals(BF_ZERO.set(0), BF_ZERO.setBoolean(0, true));
+ assertEquals(BF_MULTI.clear(-1), BF_MULTI.setBoolean(-1, false));
+ assertEquals(BF_SINGLE.clear(-1), BF_SINGLE.setBoolean(-1, false));
+ assertEquals(BF_ZERO.clear(-1), BF_ZERO.setBoolean(-1, false));
}
/**
- * test the setShort() method
+ * Tests the {@link BitField#setShort()} method.
*/
@Test
void testSetShort() {
- assertEquals(bf_multi.setShort((short) 0), (short) 0x3F80);
- assertEquals(bf_single.setShort((short) 0), (short) 0x4000);
- assertEquals(bf_zero.setShort((short) 0), (short) 0);
+ assertEquals(BF_MULTI.setShort((short) 0), (short) 0x3F80);
+ assertEquals(BF_SINGLE.setShort((short) 0), (short) 0x4000);
+ assertEquals(BF_ZERO.setShort((short) 0), (short) 0);
}
/**
@@ -198,12 +207,12 @@ void testSetShort() {
*/
@Test
void testSetShortBoolean() {
- assertEquals(bf_multi.setShort((short) 0),
bf_multi.setShortBoolean((short) 0, true));
- assertEquals(bf_single.setShort((short) 0),
bf_single.setShortBoolean((short) 0, true));
- assertEquals(bf_zero.setShort((short) 0),
bf_zero.setShortBoolean((short) 0, true));
- assertEquals(bf_multi.clearShort((short) - 1),
bf_multi.setShortBoolean((short) - 1, false));
- assertEquals(bf_single.clearShort((short) - 1),
bf_single.setShortBoolean((short) - 1, false));
- assertEquals(bf_zero.clearShort((short) -1),
bf_zero.setShortBoolean((short) -1, false));
+ assertEquals(BF_MULTI.setShort((short) 0),
BF_MULTI.setShortBoolean((short) 0, true));
+ assertEquals(BF_SINGLE.setShort((short) 0),
BF_SINGLE.setShortBoolean((short) 0, true));
+ assertEquals(BF_ZERO.setShort((short) 0),
BF_ZERO.setShortBoolean((short) 0, true));
+ assertEquals(BF_MULTI.clearShort((short) - 1),
BF_MULTI.setShortBoolean((short) - 1, false));
+ assertEquals(BF_SINGLE.clearShort((short) - 1),
BF_SINGLE.setShortBoolean((short) - 1, false));
+ assertEquals(BF_ZERO.clearShort((short) -1),
BF_ZERO.setShortBoolean((short) -1, false));
}
/**
@@ -212,23 +221,23 @@ void testSetShortBoolean() {
@Test
void testSetShortValue() {
for (int j = 0; j < 128; j++) {
- assertEquals(bf_multi.getShortValue(bf_multi.setShortValue((short)
0, (short) j)), (short) j);
- assertEquals(bf_multi.setShortValue((short) 0, (short) j), (short)
(j << 7));
+ assertEquals(BF_MULTI.getShortValue(BF_MULTI.setShortValue((short)
0, (short) j)), (short) j);
+ assertEquals(BF_MULTI.setShortValue((short) 0, (short) j), (short)
(j << 7));
}
for (int j = 0; j < 128; j++) {
- assertEquals(bf_zero.getShortValue(bf_zero.setShortValue((short)
0, (short) j)), (short) 0);
- assertEquals(bf_zero.setShortValue((short) 0, (short) j), (short)
0);
+ assertEquals(BF_ZERO.getShortValue(BF_ZERO.setShortValue((short)
0, (short) j)), (short) 0);
+ assertEquals(BF_ZERO.setShortValue((short) 0, (short) j), (short)
0);
}
// verify that excess bits are stripped off
- assertEquals(bf_multi.setShortValue((short) 0x3f80, (short) 128),
(short) 0);
+ assertEquals(BF_MULTI.setShortValue((short) 0x3f80, (short) 128),
(short) 0);
for (int j = 0; j < 2; j++) {
-
assertEquals(bf_single.getShortValue(bf_single.setShortValue((short) 0, (short)
j)), (short) j);
- assertEquals(bf_single.setShortValue((short) 0, (short) j),
(short) (j << 14));
+
assertEquals(BF_SINGLE.getShortValue(BF_SINGLE.setShortValue((short) 0, (short)
j)), (short) j);
+ assertEquals(BF_SINGLE.setShortValue((short) 0, (short) j),
(short) (j << 14));
}
// verify that excess bits are stripped off
- assertEquals(bf_single.setShortValue((short) 0x4000, (short) 2),
(short) 0);
+ assertEquals(BF_SINGLE.setShortValue((short) 0x4000, (short) 2),
(short) 0);
}
/**
@@ -237,23 +246,23 @@ void testSetShortValue() {
@Test
void testSetValue() {
for (int j = 0; j < 128; j++) {
- assertEquals(bf_multi.getValue(bf_multi.setValue(0, j)), j);
- assertEquals(bf_multi.setValue(0, j), j << 7);
+ assertEquals(BF_MULTI.getValue(BF_MULTI.setValue(0, j)), j);
+ assertEquals(BF_MULTI.setValue(0, j), j << 7);
}
for (int j = 0; j < 128; j++) {
- assertEquals(bf_zero.getValue(bf_zero.setValue(0, j)), 0);
- assertEquals(bf_zero.setValue(0, j), 0);
+ assertEquals(BF_ZERO.getValue(BF_ZERO.setValue(0, j)), 0);
+ assertEquals(BF_ZERO.setValue(0, j), 0);
}
// verify that excess bits are stripped off
- assertEquals(bf_multi.setValue(0x3f80, 128), 0);
+ assertEquals(BF_MULTI.setValue(0x3f80, 128), 0);
for (int j = 0; j < 2; j++) {
- assertEquals(bf_single.getValue(bf_single.setValue(0, j)), j);
- assertEquals(bf_single.setValue(0, j), j << 14);
+ assertEquals(BF_SINGLE.getValue(BF_SINGLE.setValue(0, j)), j);
+ assertEquals(BF_SINGLE.setValue(0, j), j << 14);
}
// verify that excess bits are stripped off
- assertEquals(bf_single.setValue(0x4000, 2), 0);
+ assertEquals(BF_SINGLE.setValue(0x4000, 2), 0);
}
}