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