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  package org.apache.commons.lang3;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Tests {@link BitField} constructed with int masks.
27   */
28  class BitFieldTest extends AbstractLangTest {
29  
30      private static final BitField BF_MULTI  = new BitField(0x3F80);
31      private static final BitField BF_SINGLE = new BitField(0x4000);
32      private static final BitField BF_ZERO = new BitField(0);
33  
34      @Test
35      void testByteBoolean() {
36          assertEquals(0, new BitField(0).setByteBoolean((byte) 0, true));
37          assertEquals(1, new BitField(1).setByteBoolean((byte) 0, true));
38          assertEquals(2, new BitField(2).setByteBoolean((byte) 0, true));
39          assertEquals(4, new BitField(4).setByteBoolean((byte) 0, true));
40          assertEquals(8, new BitField(8).setByteBoolean((byte) 0, true));
41          assertEquals(16, new BitField(16).setByteBoolean((byte) 0, true));
42          assertEquals(32, new BitField(32).setByteBoolean((byte) 0, true));
43          assertEquals(64, new BitField(64).setByteBoolean((byte) 0, true));
44          assertEquals(-128, new BitField(128).setByteBoolean((byte) 0, true));
45          assertEquals(1, new BitField(0).setByteBoolean((byte) 1, false));
46          assertEquals(0, new BitField(1).setByteBoolean((byte) 1, false));
47          assertEquals(0, new BitField(2).setByteBoolean((byte) 2, false));
48          assertEquals(0, new BitField(4).setByteBoolean((byte) 4, false));
49          assertEquals(0, new BitField(8).setByteBoolean((byte) 8, false));
50          assertEquals(0, new BitField(16).setByteBoolean((byte) 16, false));
51          assertEquals(0, new BitField(32).setByteBoolean((byte) 32, false));
52          assertEquals(0, new BitField(64).setByteBoolean((byte) 64, false));
53          assertEquals(0, new BitField(128).setByteBoolean((byte) 128, false));
54          assertEquals(-2, new BitField(1).setByteBoolean((byte) 255, false));
55          final byte clearedBit = new BitField(0x40).setByteBoolean((byte) - 63, false);
56          assertFalse(new BitField(0x40).isSet(clearedBit));
57      }
58  
59      /**
60       * Tests the {@link BitField#clear()} method.
61       */
62      @Test
63      void testClearInt() {
64          assertEquals(BF_MULTI.clear(-1), 0xFFFFC07F);
65          assertEquals(BF_SINGLE.clear(-1), 0xFFFFBFFF);
66          assertEquals(BF_ZERO.clear(-1), 0xFFFFFFFF);
67      }
68  
69      /**
70       * Tests the {@link BitField#clear()} method.
71       */
72      @Test
73      void testClearLong() {
74          assertEquals(BF_MULTI.clear(-1L), 0xFFFFC07F);
75          assertEquals(BF_SINGLE.clear(-1L), 0xFFFFBFFF);
76          assertEquals(BF_ZERO.clear(-1L), 0xFFFFFFFF);
77      }
78  
79      /**
80       * Tests the {@link BitField#clearShort()} method.
81       */
82      @Test
83      void testClearShort() {
84          assertEquals(BF_MULTI.clearShort((short) - 1), (short) 0xC07F);
85          assertEquals(BF_SINGLE.clearShort((short) - 1), (short) 0xBFFF);
86          assertEquals(BF_ZERO.clearShort((short) -1), (short) 0xFFFF);
87      }
88  
89      /**
90       * Tests the {@link BitField#getRawValue()} method.
91       */
92      @Test
93      void testGetRawValue() {
94          assertEquals(BF_MULTI.getRawValue(-1), 0x3F80);
95          assertEquals(BF_MULTI.getRawValue(0), 0);
96          assertEquals(BF_SINGLE.getRawValue(-1), 0x4000);
97          assertEquals(BF_SINGLE.getRawValue(0), 0);
98          assertEquals(BF_ZERO.getRawValue(-1), 0);
99          assertEquals(BF_ZERO.getRawValue(0), 0);
100     }
101 
102     /**
103      * Tests the {@link BitField#getShortRawValue()} method.
104      */
105     @Test
106     void testGetShortRawValue() {
107         assertEquals(BF_MULTI.getShortRawValue((short) - 1), (short) 0x3F80);
108         assertEquals(BF_MULTI.getShortRawValue((short) 0), (short) 0);
109         assertEquals(BF_SINGLE.getShortRawValue((short) - 1), (short) 0x4000);
110         assertEquals(BF_SINGLE.getShortRawValue((short) 0), (short) 0);
111         assertEquals(BF_ZERO.getShortRawValue((short) -1), (short) 0);
112         assertEquals(BF_ZERO.getShortRawValue((short) 0), (short) 0);
113     }
114 
115     /**
116      * Tests the {@link BitField#getShortValue()} method.
117      */
118     @Test
119     void testGetShortValue() {
120         assertEquals(BF_MULTI.getShortValue((short) - 1), (short) 127);
121         assertEquals(BF_MULTI.getShortValue((short) 0), (short) 0);
122         assertEquals(BF_SINGLE.getShortValue((short) - 1), (short) 1);
123         assertEquals(BF_SINGLE.getShortValue((short) 0), (short) 0);
124         assertEquals(BF_ZERO.getShortValue((short) -1), (short) 0);
125         assertEquals(BF_ZERO.getShortValue((short) 0), (short) 0);
126     }
127 
128     /**
129      * Tests the {@link BitField#getValue()} method.
130      */
131     @Test
132     void testGetValue() {
133         assertEquals(BF_MULTI.getValue(-1), 127);
134         assertEquals(BF_MULTI.getValue(0), 0);
135         assertEquals(BF_SINGLE.getValue(-1), 1);
136         assertEquals(BF_SINGLE.getValue(0), 0);
137         assertEquals(BF_ZERO.getValue(-1), 0);
138         assertEquals(BF_ZERO.getValue(0), 0);
139     }
140 
141     /**
142      * Tests the {@link BitField#isAllSet()} method.
143      */
144     @Test
145     void testIsAllSet() {
146         for (int j = 0; j < 0x3F80; j += 0x80) {
147             assertFalse(BF_MULTI.isAllSet(j));
148             assertTrue(BF_ZERO.isAllSet(j));
149         }
150         assertTrue(BF_MULTI.isAllSet(0x3F80));
151         assertFalse(BF_SINGLE.isAllSet(0));
152         assertTrue(BF_SINGLE.isAllSet(0x4000));
153     }
154 
155     /**
156      * test the isSet() method.
157      */
158     @Test
159     void testIsSet() {
160         assertFalse(BF_MULTI.isSet(0));
161         assertFalse(BF_ZERO.isSet(0));
162         for (int j = 0x80; j <= 0x3F80; j += 0x80) {
163             assertTrue(BF_MULTI.isSet(j));
164         }
165         for (int j = 0x80; j <= 0x3F80; j += 0x80) {
166             assertFalse(BF_ZERO.isSet(j));
167         }
168         assertFalse(BF_SINGLE.isSet(0));
169         assertTrue(BF_SINGLE.isSet(0x4000));
170     }
171 
172     /**
173      * Tests the {@link BitField#set()} method.
174      */
175     @Test
176     void testSet() {
177         assertEquals(BF_MULTI.set(0), 0x3F80);
178         assertEquals(BF_SINGLE.set(0), 0x4000);
179         assertEquals(BF_ZERO.set(0), 0);
180     }
181 
182     /**
183      * Tests the {@link BitField#setBoolean()} method.
184      */
185     @Test
186     void testSetBoolean() {
187         assertEquals(BF_MULTI.set(0), BF_MULTI.setBoolean(0, true));
188         assertEquals(BF_SINGLE.set(0), BF_SINGLE.setBoolean(0, true));
189         assertEquals(BF_ZERO.set(0), BF_ZERO.setBoolean(0, true));
190         assertEquals(BF_MULTI.clear(-1), BF_MULTI.setBoolean(-1, false));
191         assertEquals(BF_SINGLE.clear(-1), BF_SINGLE.setBoolean(-1, false));
192         assertEquals(BF_ZERO.clear(-1), BF_ZERO.setBoolean(-1, false));
193     }
194 
195     /**
196      * Tests the {@link BitField#setShort()} method.
197      */
198     @Test
199     void testSetShort() {
200         assertEquals(BF_MULTI.setShort((short) 0), (short) 0x3F80);
201         assertEquals(BF_SINGLE.setShort((short) 0), (short) 0x4000);
202         assertEquals(BF_ZERO.setShort((short) 0), (short) 0);
203     }
204 
205     /**
206      * test the setShortBoolean() method
207      */
208     @Test
209     void testSetShortBoolean() {
210         assertEquals(BF_MULTI.setShort((short) 0), BF_MULTI.setShortBoolean((short) 0, true));
211         assertEquals(BF_SINGLE.setShort((short) 0), BF_SINGLE.setShortBoolean((short) 0, true));
212         assertEquals(BF_ZERO.setShort((short) 0), BF_ZERO.setShortBoolean((short) 0, true));
213         assertEquals(BF_MULTI.clearShort((short) - 1), BF_MULTI.setShortBoolean((short) - 1, false));
214         assertEquals(BF_SINGLE.clearShort((short) - 1), BF_SINGLE.setShortBoolean((short) - 1, false));
215         assertEquals(BF_ZERO.clearShort((short) -1), BF_ZERO.setShortBoolean((short) -1, false));
216     }
217 
218     /**
219      * test the setShortValue() method
220      */
221     @Test
222     void testSetShortValue() {
223         for (int j = 0; j < 128; j++) {
224             assertEquals(BF_MULTI.getShortValue(BF_MULTI.setShortValue((short) 0, (short) j)), (short) j);
225             assertEquals(BF_MULTI.setShortValue((short) 0, (short) j), (short) (j << 7));
226         }
227         for (int j = 0; j < 128; j++) {
228             assertEquals(BF_ZERO.getShortValue(BF_ZERO.setShortValue((short) 0, (short) j)), (short) 0);
229             assertEquals(BF_ZERO.setShortValue((short) 0, (short) j), (short) 0);
230         }
231 
232         // verify that excess bits are stripped off
233         assertEquals(BF_MULTI.setShortValue((short) 0x3f80, (short) 128), (short) 0);
234         for (int j = 0; j < 2; j++) {
235             assertEquals(BF_SINGLE.getShortValue(BF_SINGLE.setShortValue((short) 0, (short) j)), (short) j);
236             assertEquals(BF_SINGLE.setShortValue((short) 0, (short) j), (short) (j << 14));
237         }
238 
239         // verify that excess bits are stripped off
240         assertEquals(BF_SINGLE.setShortValue((short) 0x4000, (short) 2), (short) 0);
241     }
242 
243     /**
244      * test the setValue() method
245      */
246     @Test
247     void testSetValue() {
248         for (int j = 0; j < 128; j++) {
249             assertEquals(BF_MULTI.getValue(BF_MULTI.setValue(0, j)), j);
250             assertEquals(BF_MULTI.setValue(0, j), j << 7);
251         }
252         for (int j = 0; j < 128; j++) {
253           assertEquals(BF_ZERO.getValue(BF_ZERO.setValue(0, j)), 0);
254           assertEquals(BF_ZERO.setValue(0, j), 0);
255       }
256 
257         // verify that excess bits are stripped off
258         assertEquals(BF_MULTI.setValue(0x3f80, 128), 0);
259         for (int j = 0; j < 2; j++) {
260             assertEquals(BF_SINGLE.getValue(BF_SINGLE.setValue(0, j)), j);
261             assertEquals(BF_SINGLE.setValue(0, j), j << 14);
262         }
263 
264         // verify that excess bits are stripped off
265         assertEquals(BF_SINGLE.setValue(0x4000, 2), 0);
266     }
267 
268 }