29import java.io.IOException;
32import org.jau.util.BitMath;
33import org.jau.util.Bitfield;
34import org.junit.Assert;
36import org.junit.FixMethodOrder;
37import org.junit.runners.MethodSorters;
39import jau.test.junit.util.JunitTracer;
44@FixMethodOrder(MethodSorters.NAME_ASCENDING)
50 for(
int i=0; i<pyramid32bit_one.length; i++) {
51 final int val0 = 1 << i;
52 final int oneBitCountI = Integer.bitCount(val0);
53 final String pattern0 = pyramid32bit_one[i];
57 final int oneBitCount1 = BitMath.bitCount(val0);
58 final String msg = String.format(
"Round %02d: 0x%08x %s, c %d / %d%n : 0x%08x %s, c %d%n",
59 i, val0, pattern0, oneBitCount0, oneBitCountI, val1, pattern1, oneBitCount1);
61 Assert.assertEquals(msg, val0, val1);
62 Assert.assertEquals(msg, pattern0, pattern1);
64 Assert.assertEquals(msg, oneBitCount0, oneBitCountI);
65 Assert.assertEquals(msg, oneBitCount0, oneBitCount1);
69 @SuppressWarnings(
"unused")
73 final long MAX_minus = MAX-0x1FF;
74 final long MAX_half = MAX/2;
75 final long MAX_half_minus = MAX_half-0x1FF;
76 final long MAX_half_plus = MAX_half+0x1FF;
79 for(
long l=0; l<=MAX; l++) {
80 test_BitCount32_Samples(l);
83 for(
long l=0; l>=0x1FF; l++) {
84 test_BitCount32_Samples(l);
86 for(
long l=MAX_half_minus; l<=MAX_half_plus; l++) {
87 test_BitCount32_Samples(l);
89 for(
long l=MAX_minus; l<=MAX; l++) {
90 test_BitCount32_Samples(l);
93 static void test_BitCount32_Samples(
final long l) {
94 final int oneBitCountL = Long.bitCount(l);
95 final int val0 = (int)l;
96 final int oneBitCountI = Integer.bitCount(val0);
97 final int oneBitCount1 = BitMath.bitCount(val0);
98 final String msg = String.format(
"Round 0x%08x, c %d / %d / %d", val0,
99 oneBitCountL, oneBitCountI, oneBitCount1);
100 Assert.assertEquals(msg, oneBitCountI, oneBitCountL);
101 Assert.assertEquals(msg, oneBitCountI, oneBitCount1);
104 static int[] testDataOneBit =
new int[] {
105 0,0, 1,1, 2,1, 3,2, 4,1, 5,2, 6,2, 7,3,
106 8,1, 9,2, 10,2, 11,3, 12,2, 13,3, 14,3, 15,4, 16,1, 17,2,
107 0x3F,6, 0x40,1, 0x41,2, 0x7f,7, 0x80,1, 0x81,2, 0xfe,7, 0xff,8,
108 0x4000,1, 0x4001,2, 0x7000,3, 0x7fff,15,
111 0x7F53F57B,23, 0xFEA7EAF6,23,
120 for(
int i = 0; i<testDataOneBit.length; i+=2) {
121 test_BitCount32_Data(testDataOneBit[i], testDataOneBit[i+1]);
124 static void test_BitCount32_Data(
final int i,
final int expOneBits) {
125 final int oneBitCountI = Integer.bitCount(i);
126 final int oneBitCount1 = BitMath.bitCount(i);
127 final String msg = String.format(
"Round 0x%08x, c %d / %d", i,
128 oneBitCountI, oneBitCount1);
129 Assert.assertEquals(msg, oneBitCount1, expOneBits);
130 Assert.assertEquals(msg, oneBitCountI, oneBitCount1);
135 final int bitSize32 = 32;
136 final int bitSize128 = 128;
137 final Bitfield bf1 = Bitfield.Factory.create(bitSize32);
138 Assert.assertEquals(bitSize32, bf1.size());
139 Assert.assertTrue(bf1 instanceof jau.util.Int32Bitfield);
140 final Bitfield bf2 = Bitfield.Factory.create(bitSize128);
141 Assert.assertEquals(bitSize128, bf2.size());
142 Assert.assertTrue(bf2 instanceof jau.util.Int32ArrayBitfield);
145 Assert.assertEquals(0, bf1.bitCount());
146 Assert.assertEquals(0, bf2.bitCount());
148 bf1.clearField(
true);
149 bf2.clearField(
true);
150 Assert.assertEquals(bf1.size(), bf1.bitCount());
151 Assert.assertEquals(bf2.size(), bf2.bitCount());
153 bf1.clearField(
false);
154 bf2.clearField(
false);
155 Assert.assertEquals(0, bf1.bitCount());
156 Assert.assertEquals(0, bf2.bitCount());
158 static class TestDataBF {
161 final String pattern;
162 public TestDataBF(
final int bitSize,
final int value,
final String pattern) {
163 this.bitSize = bitSize;
165 this.pattern = pattern;
168 static TestDataBF[] testDataBF32Bit = {
169 new TestDataBF(32, BitDemoData.testIntMSB, BitDemoData.testStringMSB),
170 new TestDataBF(32, BitDemoData.testIntMSB_rev, BitDemoData.testStringMSB_rev),
171 new TestDataBF(32, BitDemoData.testIntLSB, BitDemoData.testStringLSB),
172 new TestDataBF(32, BitDemoData.testIntLSB_revByte, BitDemoData.testStringLSB_revByte),
175 new TestDataBF(32, 0x04030201,
"00000100000000110000001000000001"),
178 new TestDataBF(32, 0xAFFECAFE,
"10101111111111101100101011111110"),
180 new TestDataBF(32, 0xDEADBEEF,
"11011110101011011011111011101111")
182 static TestDataBF[] testDataBF16Bit = {
184 new TestDataBF(16, 0x0201,
"0000001000000001"),
186 new TestDataBF(16, 0x0403,
"0000010000000011"),
189 new TestDataBF(16, 0xAFFE,
"1010111111111110"),
191 new TestDataBF(16, 0xCAFE,
"1100101011111110"),
194 new TestDataBF(16, 0xDEAD,
"1101111010101101"),
195 new TestDataBF(16, 0xBEEF,
"1011111011101111")
197 static TestDataBF[] testDataBF3Bit = {
198 new TestDataBF(3, 0x01,
"001"),
199 new TestDataBF(3, 0x02,
"010"),
200 new TestDataBF(3, 0x05,
"101")
205 for(
int i=0; i<testDataBF32Bit.length; i++) {
206 test_ValidateTestData( testDataBF32Bit[i] );
208 for(
int i=0; i<testDataBF16Bit.length; i++) {
209 test_ValidateTestData( testDataBF16Bit[i] );
211 for(
int i=0; i<testDataBF3Bit.length; i++) {
212 test_ValidateTestData( testDataBF3Bit[i] );
215 static void test_ValidateTestData(
final TestDataBF d) {
216 final int oneBitCount0 = BitMath.bitCount(d.val);
218 Assert.assertEquals(oneBitCount1, oneBitCount0);
220 Assert.assertEquals(d.pattern, pattern0);
222 Assert.assertEquals(d.val, val1);
223 Assert.assertEquals(d.bitSize, pattern0.length());
226 static void assertEquals(
final Bitfield bf,
final int bf_off,
final int v,
final String pattern,
final int oneBitCount) {
227 final int len = pattern.length();
228 for(
int i=0; i<len; i++) {
229 final boolean exp0 = 0 != ( v & ( 1 << i ) );
230 final boolean exp1 =
'1' == pattern.charAt(len-1-i);
231 final boolean has = bf.get(i+bf_off);
232 final String msg = String.format(
"Pos %04d: Value 0x%08x / %s, c %d", i, v, pattern, oneBitCount);
233 Assert.assertEquals(msg, exp0, has);
234 Assert.assertEquals(msg, exp1, has);
240 for(
int i=0; i<testDataBF32Bit.length; i++) {
241 test_Aligned32bit( testDataBF32Bit[i] );
243 for(
int i=0; i<testDataBF16Bit.length; i++) {
244 test_Aligned32bit( testDataBF16Bit[i] );
246 for(
int i=0; i<testDataBF3Bit.length; i++) {
247 test_Aligned32bit( testDataBF3Bit[i] );
250 static int get32BitStorageSize(
final int bits) {
251 final int units = Math.max(1, ( bits + 31 ) >>> 5);
254 static void test_Aligned32bit(
final TestDataBF d) {
255 final int oneBitCount = BitMath.bitCount(d.val);
257 final Bitfield bf1 = Bitfield.Factory.create(d.bitSize);
258 Assert.assertEquals(get32BitStorageSize(d.bitSize), bf1.size());
259 final Bitfield bf2 = Bitfield.Factory.create(d.bitSize+128);
260 Assert.assertEquals(get32BitStorageSize(d.bitSize+128), bf2.size());
262 bf1.put32( 0, d.bitSize, d.val);
263 Assert.assertEquals(d.val, bf1.get32( 0, d.bitSize));
264 Assert.assertEquals(oneBitCount, bf1.bitCount());
265 assertEquals(bf1, 0, d.val, d.pattern, oneBitCount);
267 bf2.put32( 0, d.bitSize, d.val);
268 Assert.assertEquals(d.val, bf2.get32( 0, d.bitSize));
269 Assert.assertEquals(oneBitCount*1, bf2.bitCount());
270 assertEquals(bf2, 0, d.val, d.pattern, oneBitCount);
271 bf2.put32(64, d.bitSize, d.val);
272 Assert.assertEquals(d.val, bf2.get32(64, d.bitSize));
273 Assert.assertEquals(oneBitCount*2, bf2.bitCount());
274 assertEquals(bf2, 64, d.val, d.pattern, oneBitCount);
276 Assert.assertEquals(d.val, bf2.copy32(0, 96, d.bitSize));
277 Assert.assertEquals(d.val, bf2.get32(96, d.bitSize));
278 Assert.assertEquals(oneBitCount*3, bf2.bitCount());
279 assertEquals(bf2, 96, d.val, d.pattern, oneBitCount);
284 for(
int i=0; i<testDataBF32Bit.length; i++) {
285 test_Unaligned(testDataBF32Bit[i]);
287 for(
int i=0; i<testDataBF16Bit.length; i++) {
288 test_Unaligned(testDataBF16Bit[i]);
290 for(
int i=0; i<testDataBF3Bit.length; i++) {
291 test_Unaligned( testDataBF3Bit[i] );
294 static void test_Unaligned(
final TestDataBF d) {
295 final Bitfield bf1 = Bitfield.Factory.create(d.bitSize);
296 final Bitfield bf2 = Bitfield.Factory.create(d.bitSize+128);
297 Assert.assertEquals(get32BitStorageSize(d.bitSize), bf1.size());
298 Assert.assertEquals(get32BitStorageSize(d.bitSize+128), bf2.size());
299 test_Unaligned( d, bf1 );
300 test_Unaligned( d, bf2 );
302 static void test_Unaligned(
final TestDataBF d,
final Bitfield bf) {
303 final int maxBitpos = bf.size()-d.bitSize;
304 for(
int i=0; i<=maxBitpos; i++) {
305 bf.clearField(
false);
306 test_Unaligned(d, bf, i);
309 static void test_Unaligned(
final TestDataBF d,
final Bitfield bf,
final int lowBitnum) {
310 final int maxBitpos = bf.size()-d.bitSize;
311 final int oneBitCount = BitMath.bitCount(d.val);
313 final String msg = String.format(
"Value 0x%08x / %s, l %d/%d, c %d, lbPos %d -> %d",
314 d.val, d.pattern, d.bitSize, bf.size(), oneBitCount, lowBitnum, maxBitpos);
319 bf.put32( lowBitnum, d.bitSize, d.val);
320 for(
int i=0; i<d.bitSize; i++) {
321 Assert.assertEquals(msg+
", bitpos "+i, 0 != ( d.val & ( 1 << i ) ), bf.get(lowBitnum+i));
323 Assert.assertEquals(msg, d.val, bf.get32( lowBitnum, d.bitSize));
324 Assert.assertEquals(msg, oneBitCount, bf.bitCount());
325 assertEquals(bf, lowBitnum, d.val, d.pattern, oneBitCount);
330 if( lowBitnum < maxBitpos ) {
333 Assert.assertEquals(msg, d.val, bf.copy32(lowBitnum, lowBitnum+1, d.bitSize));
335 Assert.assertEquals(msg, d.val, bf.get32( lowBitnum+1, d.bitSize));
336 Assert.assertEquals(msg, oneBitCount, bf.bitCount());
337 assertEquals(bf, lowBitnum+1, d.val, d.pattern, oneBitCount);
341 bf.clearField(
false);
342 Assert.assertEquals(msg+
", bitpos "+0,
false, bf.put(lowBitnum+0,
true));
343 Assert.assertEquals(msg+
", bitpos "+0,
true, bf.put(lowBitnum+0,
false));
348 for(
int i=0; i<d.bitSize; i++) {
349 Assert.assertEquals(msg+
", bitpos "+i,
false, bf.put(lowBitnum+i, 0 != ( d.val & ( 1 << i ) )));
351 Assert.assertEquals(msg, d.val, bf.get32(lowBitnum, d.bitSize));
352 for(
int i=0; i<d.bitSize; i++) {
353 Assert.assertEquals(msg+
", bitpos "+i, 0 != ( d.val & ( 1 << i ) ), bf.get(lowBitnum+i));
355 Assert.assertEquals(msg, oneBitCount, bf.bitCount());
356 assertEquals(bf, lowBitnum, d.val, d.pattern, oneBitCount);
361 if( lowBitnum < maxBitpos ) {
364 for(
int i=d.bitSize-1; i>=0; i--) {
365 Assert.assertEquals(msg+
", bitpos "+i, 0 != ( d.val & ( 1 << i ) ),
366 bf.copy(lowBitnum+i, lowBitnum+1+i) );
369 Assert.assertEquals(msg, d.val, bf.get32( lowBitnum+1, d.bitSize));
370 for(
int i=0; i<d.bitSize; i++) {
371 Assert.assertEquals(msg+
", bitpos "+i, 0 != ( d.val & ( 1 << i ) ), bf.get(lowBitnum+1+i));
373 Assert.assertEquals(msg, oneBitCount, bf.bitCount());
374 assertEquals(bf, lowBitnum+1, d.val, d.pattern, oneBitCount);
380 bf.clearField(
false);
381 for(
int i=0; i<d.bitSize; i++) {
382 if( 0 != ( d.val & ( 1 << i ) ) ) {
385 bf.clear(lowBitnum+i);
388 Assert.assertEquals(msg, d.val, bf.get32(lowBitnum, d.bitSize));
389 for(
int i=0; i<d.bitSize; i++) {
390 Assert.assertEquals(msg+
", bitpos "+i, 0 != ( d.val & ( 1 << i ) ), bf.get(lowBitnum+i));
392 Assert.assertEquals(msg, oneBitCount, bf.bitCount());
393 assertEquals(bf, lowBitnum, d.val, d.pattern, oneBitCount);
398 bf.clearField(
false);
399 bf.put32( lowBitnum, d.bitSize, d.val);
400 checkOtherBits(d, bf, lowBitnum, msg, 0);
403 bf.put32( lowBitnum, d.bitSize, d.val);
404 checkOtherBits(d, bf, lowBitnum, msg, BitMath.UNSIGNED_INT_MAX_VALUE);
407 static void checkOtherBits(
final TestDataBF d,
final Bitfield bf,
final int lowBitnum,
final String msg,
final int expBits) {
408 final int highBitnum = lowBitnum + d.bitSize - 1;
410 for(
int i=0; i<lowBitnum; i+=32) {
411 final int len = Math.min(32, lowBitnum-i);
412 final int val = bf.get32(i, len);
413 final int exp = expBits & BitMath.getBitMask(len);
415 Assert.assertEquals(msg+
", bitpos "+i, exp, val);
417 for(
int i=highBitnum+1; i<bf.size(); i+=32) {
418 final int len = Math.min(32, bf.size() - i);
419 final int val = bf.get32(i, len);
420 final int exp = expBits & BitMath.getBitMask(len);
422 Assert.assertEquals(msg+
", bitpos "+i, exp, val);
426 public static void main(
final String args[])
throws IOException {
428 org.junit.runner.JUnitCore.
main(tstname);
static int getOneBitCount(final String pattern)
static final long UNSIGNED_INT_MAX_VALUE
static final String[] pyramid32bit_one
static String toBinaryString(final int v, final int bitCount)
static int toInteger(final String bitPattern)
Test basic bitfield operations for Bitfield.
void test02_BitCount32_Samples()
void test01_BitCount32_One()
void test21_Aligned32bit()
void test20_ValidateTestData()
static void main(final String args[])
void test03_BitCount32_Data()