View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Tests {@link BitField} constructed with long masks.
28   */
29  class BitFieldLongTest {
30  
31      private static final BitField BF_MULTI  = new BitField(0x3F80L);
32      private static final BitField BF_MULTI_L  = new BitField(0x3F80_0000_0000_0000L);
33      private static final BitField BF_SINGLE = new BitField(0x4000L);
34      private static final BitField BF_SINGLE_L = new BitField(0x4000_0000_0000_0000L);
35      private static final BitField BF_ZERO = new BitField(0L);
36      private static final BitField BF_ZERO_L = new BitField(0L);
37  
38      @Test
39      void testByteBoolean() {
40          assertEquals(0, new BitField(0L).setByteBoolean((byte) 0, true));
41          assertEquals(1, new BitField(1L).setByteBoolean((byte) 0, true));
42          assertEquals(2, new BitField(2L).setByteBoolean((byte) 0, true));
43          assertEquals(4, new BitField(4L).setByteBoolean((byte) 0, true));
44          assertEquals(8, new BitField(8L).setByteBoolean((byte) 0, true));
45          assertEquals(16, new BitField(16L).setByteBoolean((byte) 0, true));
46          assertEquals(32, new BitField(32L).setByteBoolean((byte) 0, true));
47          assertEquals(64, new BitField(64L).setByteBoolean((byte) 0, true));
48          assertEquals(-128, new BitField(128L).setByteBoolean((byte) 0, true));
49          assertEquals(1, new BitField(0L).setByteBoolean((byte) 1, false));
50          assertEquals(0, new BitField(1L).setByteBoolean((byte) 1, false));
51          assertEquals(0, new BitField(2L).setByteBoolean((byte) 2, false));
52          assertEquals(0, new BitField(4L).setByteBoolean((byte) 4, false));
53          assertEquals(0, new BitField(8L).setByteBoolean((byte) 8, false));
54          assertEquals(0, new BitField(16L).setByteBoolean((byte) 16, false));
55          assertEquals(0, new BitField(32L).setByteBoolean((byte) 32, false));
56          assertEquals(0, new BitField(64L).setByteBoolean((byte) 64, false));
57          assertEquals(0, new BitField(128L).setByteBoolean((byte) 128, false));
58          assertEquals(-2, new BitField(1L).setByteBoolean((byte) 255, false));
59          final byte clearedBit = new BitField(0x40L).setByteBoolean((byte) - 63, false);
60          assertFalse(new BitField(0x40).isSet(clearedBit));
61      }
62  
63      /**
64       * Tests the {@link BitField#clear()} method.
65       */
66      @Test
67      void testClearInt() {
68          assertEquals(BF_MULTI.clear(-1), 0xFFFF_C07F);
69          assertEquals(BF_SINGLE.clear(-1), 0xFFFF_BFFF);
70          assertEquals(BF_ZERO.clear(-1), 0xFFFF_FFFF);
71          assertEquals(BF_MULTI_L.clear(-1), 0xFFFF_FFFF);
72          assertEquals(BF_SINGLE_L.clear(-1), 0xFFFF_FFFF);
73          assertEquals(BF_ZERO_L.clear(-1), 0xFFFF_FFFF);
74      }
75  
76      /**
77       * Tests the {@link BitField#clear()} method.
78       */
79      @Test
80      void testClearLong() {
81          assertEquals(BF_MULTI.clear(-1L), 0xFFFF_FFFF_FFFF_C07FL);
82          assertEquals(BF_SINGLE.clear(-1L), 0xFFFF_FFFF_FFFF_BFFFL);
83          assertEquals(BF_ZERO.clear(-1L), 0xFFFF_FFFF);
84          assertEquals(BF_MULTI_L.clear(-1L), 0xC07F_FFFF_FFFF_FFFFL);
85          assertEquals(BF_SINGLE_L.clear(-1L), 0xBFFF_FFFF_FFFF_FFFFL);
86          assertEquals(BF_ZERO_L.clear(-1L), 0xFFFF_FFFF_FFFF_FFFFL);
87      }
88  
89      /**
90       * Tests the {@link BitField#clearShort()} method.
91       */
92      @Test
93      void testClearShort() {
94          assertEquals(BF_MULTI.clearShort((short) - 1), (short) 0xC07F);
95          assertEquals(BF_SINGLE.clearShort((short) - 1), (short) 0xBFFF);
96          assertEquals(BF_ZERO.clearShort((short) -1), (short) 0xFFFF);
97          assertEquals(BF_MULTI_L.clearShort((short) - 1), (short) 0xFFFF);
98          assertEquals(BF_SINGLE_L.clearShort((short) - 1), (short) 0xFFFF);
99          assertEquals(BF_ZERO_L.clearShort((short) -1), (short) 0xFFFF);
100     }
101 
102     @Test
103     void testEdgeCases() {
104         final BitField field = new BitField(0x1L); // bit 0
105         assertEquals(1L, field.set(0L));
106         assertEquals(0L, field.clear(1L));
107         final BitField highField = new BitField(0x8000_0000_0000_0000L); // highest bit
108         assertEquals(0x8000_0000_0000_0000L, highField.set(0L));
109         assertEquals(0L, highField.clear(0x8000_0000_0000_0000L));
110     }
111 
112     /**
113      * Tests the {@link BitField#getRawValue()} method.
114      */
115     @Test
116     void testGetRawValue() {
117         // mask < max int and int input
118         assertEquals(BF_MULTI.getRawValue(-1), 0x3F80);
119         assertEquals(BF_MULTI.getRawValue(0), 0);
120         assertEquals(BF_SINGLE.getRawValue(-1), 0x4000);
121         assertEquals(BF_SINGLE.getRawValue(0), 0);
122         assertEquals(BF_ZERO.getRawValue(-1), 0);
123         assertEquals(BF_ZERO.getRawValue(0), 0);
124         // mask > max int and int input
125         assertEquals(BF_MULTI_L.getRawValue(-1), 0);
126         assertEquals(BF_MULTI_L.getRawValue(0), 0);
127         assertEquals(BF_SINGLE_L.getRawValue(-1), 0);
128         assertEquals(BF_SINGLE_L.getRawValue(0), 0);
129         assertEquals(BF_ZERO_L.getRawValue(-1), 0);
130         assertEquals(BF_ZERO_L.getRawValue(0), 0);
131         // mask > max int and long input
132         assertEquals(BF_MULTI_L.getRawValue(-1L), 0x3F80_0000_0000_0000L);
133         assertEquals(BF_MULTI_L.getRawValue(0L), 0);
134         assertEquals(BF_SINGLE_L.getRawValue(-1L), 0x4000_0000_0000_0000L);
135         assertEquals(BF_SINGLE_L.getRawValue(0L), 0);
136         assertEquals(BF_ZERO_L.getRawValue(-1L), 0);
137         assertEquals(BF_ZERO_L.getRawValue(0L), 0);
138     }
139 
140     /**
141      * Tests the {@link BitField#getShortRawValue()} method.
142      */
143     @Test
144     void testGetShortRawValue() {
145         // mask < max int and short input
146         assertEquals(BF_MULTI.getShortRawValue((short) - 1), (short) 0x3F80);
147         assertEquals(BF_MULTI.getShortRawValue((short) 0), (short) 0);
148         assertEquals(BF_SINGLE.getShortRawValue((short) - 1), (short) 0x4000);
149         assertEquals(BF_SINGLE.getShortRawValue((short) 0), (short) 0);
150         assertEquals(BF_ZERO.getShortRawValue((short) -1), (short) 0);
151         assertEquals(BF_ZERO.getShortRawValue((short) 0), (short) 0);
152         // mask > max int and short input
153         assertEquals(BF_MULTI_L.getShortRawValue((short) - 1), (short) 0);
154         assertEquals(BF_MULTI_L.getShortRawValue((short) 0), (short) 0);
155         assertEquals(BF_SINGLE_L.getShortRawValue((short) - 1), (short) 0);
156         assertEquals(BF_SINGLE_L.getShortRawValue((short) 0), (short) 0);
157         assertEquals(BF_ZERO_L.getShortRawValue((short) -1), (short) 0);
158         assertEquals(BF_ZERO_L.getShortRawValue((short) 0), (short) 0);
159     }
160 
161     /**
162      * Tests the getRawValue() and getValue() methods
163      */
164     @Test
165     void testGetValueAndRawValueIntRange() {
166         final BitField field = new BitField(0xFF00L); // bits 8-15
167         final long holder = 0x1234L;
168         // raw value: bits & mask
169         assertEquals(0x1200L, field.getRawValue(holder));
170         // shifted value: shifted right to LSB
171         assertEquals(0x12L, field.getValue(holder));
172     }
173 
174     /**
175      * Tests the isSet() and isAllSet() methods
176      */
177     @Test
178     void testIsSetAndIsAllSet() {
179         final BitField field1 = new BitField(0x3000L); // bits 12-13
180         final long holder = 0x3000L;
181         assertTrue(field1.isSet(holder));
182         assertTrue(field1.isAllSet(holder));
183         final long holderPartial = 0x1000L;
184         assertTrue(field1.isSet(holderPartial));
185         assertFalse(field1.isAllSet(holderPartial));
186     }
187 
188     @Test
189     void testMultipleBits() {
190         final BitField field = new BitField(0xF0F0L); // multiple bits
191         final long holder = 0xAAAA;
192         final long newValue = 0x55;
193         final long result = field.setValue(holder, newValue);
194         assertEquals(holder & ~0xF0F0L | newValue << 4 & 0xF0F0L, result);
195     }
196 
197     /**
198      * Tests the set() method
199      */
200     @Test
201     void testSet() {
202         final BitField field = new BitField(0x1000L); // bit 12
203         final long holder = 0x0000L;
204         final long result = field.set(holder);
205         assertEquals(0x1000L, result);
206     }
207 
208     /**
209      * Tests the setBoolean() method
210      */
211     @Test
212     void testSetBoolean() {
213         final BitField field = new BitField(0x1000L); // bit 12
214         final long holder = 0x0000L;
215         final long setTrue = field.setBoolean(holder, true);
216         assertEquals(0x1000L, setTrue);
217         final long setFalse = field.setBoolean(setTrue, false);
218         assertEquals(0x0000L, setFalse);
219     }
220 
221     /**
222      * Tests the setValue() method
223      */
224     @Test
225     void testSetValue() {
226         final BitField field = new BitField(0xFF00L); // bits 8-15
227         final long holder = 0x1200L;
228         final long result = field.setValue(holder, 0x34L); // replace bits 8-15 with 0x34
229         assertEquals(0x3400L, result);
230     }
231 }