forked from apache/commons-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitFieldLongTest.java
More file actions
231 lines (213 loc) · 9.46 KB
/
BitFieldLongTest.java
File metadata and controls
231 lines (213 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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 extends AbstractLangTest {
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);
}
}