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    *      http://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   * Class to test BitField functionality
27   */
28  public 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      public void testByte() {
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  
57          assertFalse(new BitField(0x40).isSet(clearedBit));
58      }
59  
60      /**
61       * test the clear() method
62       */
63      @Test
64      public void testClear() {
65          assertEquals(bf_multi.clear(-1), 0xFFFFC07F);
66          assertEquals(bf_single.clear(-1), 0xFFFFBFFF);
67          assertEquals(bf_zero.clear(-1), 0xFFFFFFFF);
68      }
69  
70      /**
71       * test the clearShort() method
72       */
73      @Test
74      public void testClearShort() {
75          assertEquals(bf_multi.clearShort((short) - 1), (short) 0xC07F);
76          assertEquals(bf_single.clearShort((short) - 1), (short) 0xBFFF);
77          assertEquals(bf_zero.clearShort((short) -1), (short) 0xFFFF);
78      }
79  
80      /**
81       * test the getRawValue() method
82       */
83      @Test
84      public void testGetRawValue() {
85          assertEquals(bf_multi.getRawValue(-1), 0x3F80);
86          assertEquals(bf_multi.getRawValue(0), 0);
87          assertEquals(bf_single.getRawValue(-1), 0x4000);
88          assertEquals(bf_single.getRawValue(0), 0);
89          assertEquals(bf_zero.getRawValue(-1), 0);
90          assertEquals(bf_zero.getRawValue(0), 0);
91      }
92  
93      /**
94       * test the getShortRawValue() method
95       */
96      @Test
97      public void testGetShortRawValue() {
98          assertEquals(bf_multi.getShortRawValue((short) - 1), (short) 0x3F80);
99          assertEquals(bf_multi.getShortRawValue((short) 0), (short) 0);
100         assertEquals(bf_single.getShortRawValue((short) - 1), (short) 0x4000);
101         assertEquals(bf_single.getShortRawValue((short) 0), (short) 0);
102         assertEquals(bf_zero.getShortRawValue((short) -1), (short) 0);
103         assertEquals(bf_zero.getShortRawValue((short) 0), (short) 0);
104     }
105 
106     /**
107      * test the getShortValue() method
108      */
109     @Test
110     public void testGetShortValue() {
111         assertEquals(bf_multi.getShortValue((short) - 1), (short) 127);
112         assertEquals(bf_multi.getShortValue((short) 0), (short) 0);
113         assertEquals(bf_single.getShortValue((short) - 1), (short) 1);
114         assertEquals(bf_single.getShortValue((short) 0), (short) 0);
115         assertEquals(bf_zero.getShortValue((short) -1), (short) 0);
116         assertEquals(bf_zero.getShortValue((short) 0), (short) 0);
117     }
118 
119     /**
120      * test the getValue() method
121      */
122     @Test
123     public void testGetValue() {
124         assertEquals(bf_multi.getValue(-1), 127);
125         assertEquals(bf_multi.getValue(0), 0);
126         assertEquals(bf_single.getValue(-1), 1);
127         assertEquals(bf_single.getValue(0), 0);
128         assertEquals(bf_zero.getValue(-1), 0);
129         assertEquals(bf_zero.getValue(0), 0);
130     }
131 
132     /**
133      * test the isAllSet() method
134      */
135     @Test
136     public void testIsAllSet() {
137         for (int j = 0; j < 0x3F80; j += 0x80) {
138             assertFalse(bf_multi.isAllSet(j));
139             assertTrue(bf_zero.isAllSet(j));
140         }
141         assertTrue(bf_multi.isAllSet(0x3F80));
142         assertFalse(bf_single.isAllSet(0));
143         assertTrue(bf_single.isAllSet(0x4000));
144     }
145 
146     /**
147      * test the isSet() method
148      */
149     @Test
150     public void testIsSet() {
151         assertFalse(bf_multi.isSet(0));
152         assertFalse(bf_zero.isSet(0));
153         for (int j = 0x80; j <= 0x3F80; j += 0x80) {
154             assertTrue(bf_multi.isSet(j));
155         }
156         for (int j = 0x80; j <= 0x3F80; j += 0x80) {
157             assertFalse(bf_zero.isSet(j));
158         }
159         assertFalse(bf_single.isSet(0));
160         assertTrue(bf_single.isSet(0x4000));
161     }
162 
163     /**
164      * test the set() method
165      */
166     @Test
167     public void testSet() {
168         assertEquals(bf_multi.set(0), 0x3F80);
169         assertEquals(bf_single.set(0), 0x4000);
170         assertEquals(bf_zero.set(0), 0);
171     }
172 
173     /**
174      * test the setBoolean() method
175      */
176     @Test
177     public void testSetBoolean() {
178         assertEquals(bf_multi.set(0), bf_multi.setBoolean(0, true));
179         assertEquals(bf_single.set(0), bf_single.setBoolean(0, true));
180         assertEquals(bf_zero.set(0), bf_zero.setBoolean(0, true));
181         assertEquals(bf_multi.clear(-1), bf_multi.setBoolean(-1, false));
182         assertEquals(bf_single.clear(-1), bf_single.setBoolean(-1, false));
183         assertEquals(bf_zero.clear(-1), bf_zero.setBoolean(-1, false));
184     }
185 
186     /**
187      * test the setShort() method
188      */
189     @Test
190     public void testSetShort() {
191         assertEquals(bf_multi.setShort((short) 0), (short) 0x3F80);
192         assertEquals(bf_single.setShort((short) 0), (short) 0x4000);
193         assertEquals(bf_zero.setShort((short) 0), (short) 0);
194     }
195 
196     /**
197      * test the setShortBoolean() method
198      */
199     @Test
200     public void testSetShortBoolean() {
201         assertEquals(bf_multi.setShort((short) 0), bf_multi.setShortBoolean((short) 0, true));
202         assertEquals(bf_single.setShort((short) 0), bf_single.setShortBoolean((short) 0, true));
203         assertEquals(bf_zero.setShort((short) 0), bf_zero.setShortBoolean((short) 0, true));
204         assertEquals(bf_multi.clearShort((short) - 1), bf_multi.setShortBoolean((short) - 1, false));
205         assertEquals(bf_single.clearShort((short) - 1), bf_single.setShortBoolean((short) - 1, false));
206         assertEquals(bf_zero.clearShort((short) -1), bf_zero.setShortBoolean((short) -1, false));
207     }
208 
209     /**
210      * test the setShortValue() method
211      */
212     @Test
213     public void testSetShortValue() {
214         for (int j = 0; j < 128; j++) {
215             assertEquals(bf_multi.getShortValue(bf_multi.setShortValue((short) 0, (short) j)), (short) j);
216             assertEquals(bf_multi.setShortValue((short) 0, (short) j), (short) (j << 7));
217         }
218         for (int j = 0; j < 128; j++) {
219             assertEquals(bf_zero.getShortValue(bf_zero.setShortValue((short) 0, (short) j)), (short) 0);
220             assertEquals(bf_zero.setShortValue((short) 0, (short) j), (short) 0);
221         }
222 
223         // verify that excess bits are stripped off
224         assertEquals(bf_multi.setShortValue((short) 0x3f80, (short) 128), (short) 0);
225         for (int j = 0; j < 2; j++) {
226             assertEquals(bf_single.getShortValue(bf_single.setShortValue((short) 0, (short) j)), (short) j);
227             assertEquals(bf_single.setShortValue((short) 0, (short) j), (short) (j << 14));
228         }
229 
230         // verify that excess bits are stripped off
231         assertEquals(bf_single.setShortValue((short) 0x4000, (short) 2), (short) 0);
232     }
233 
234     /**
235      * test the setValue() method
236      */
237     @Test
238     public void testSetValue() {
239         for (int j = 0; j < 128; j++) {
240             assertEquals(bf_multi.getValue(bf_multi.setValue(0, j)), j);
241             assertEquals(bf_multi.setValue(0, j), j << 7);
242         }
243         for (int j = 0; j < 128; j++) {
244           assertEquals(bf_zero.getValue(bf_zero.setValue(0, j)), 0);
245           assertEquals(bf_zero.setValue(0, j), 0);
246       }
247 
248         // verify that excess bits are stripped off
249         assertEquals(bf_multi.setValue(0x3f80, 128), 0);
250         for (int j = 0; j < 2; j++) {
251             assertEquals(bf_single.getValue(bf_single.setValue(0, j)), j);
252             assertEquals(bf_single.setValue(0, j), j << 14);
253         }
254 
255         // verify that excess bits are stripped off
256         assertEquals(bf_single.setValue(0x4000, 2), 0);
257     }
258 
259 }