/** * Copyright 2015 JogAmp Community. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of JogAmp Community. */ package com.jogamp.common.util; import java.io.IOException; import org.junit.Test; import org.junit.Assert; import com.jogamp.junit.util.SingletonJunitCase; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; /** * Test basic bitfield operations for {@link Bitfield} */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class TestBitfield00 extends SingletonJunitCase { @Test public void test01_BitCount32_One() { final String[] pyramid32bit_one = BitDemoData.pyramid32bit_one; for(int i=0; i=0x1FF; l++) { test_BitCount32_Samples(l); } for(long l=MAX_half_minus; l<=MAX_half_plus; l++) { test_BitCount32_Samples(l); } for(long l=MAX_minus; l<=MAX; l++) { test_BitCount32_Samples(l); } } static void test_BitCount32_Samples(final long l) { final int oneBitCountL = Long.bitCount(l); final int val0 = (int)l; final int oneBitCountI = Integer.bitCount(val0); final int oneBitCount1 = Bitfield.Util.bitCount(val0); final String msg = String.format("Round 0x%08x, c %d / %d / %d", val0, oneBitCountL, oneBitCountI, oneBitCount1); Assert.assertEquals(msg, oneBitCountI, oneBitCountL); Assert.assertEquals(msg, oneBitCountI, oneBitCount1); } static int[] testDataOneBit = new int[] { 0,0, 1,1, 2,1, 3,2, 4,1, 5,2, 6,2, 7,3, 8,1, 9,2, 10,2, 11,3, 12,2, 13,3, 14,3, 15,4, 16,1, 17,2, 0x3F,6, 0x40,1, 0x41,2, 0x7f,7, 0x80,1, 0x81,2, 0xfe,7, 0xff,8, 0x4000,1, 0x4001,2, 0x7000,3, 0x7fff,15, 0x0FFFFFF0,24, 0x55555555,16, 0x7F53F57B,23, 0xFEA7EAF6,23, /* << 1 */ 0x80000000, 1, 0xAAAAAAAA,16, 0xC0C0C0C0, 8, 0xFF000000, 8, 0xFFFFFFFF,32 }; @Test public void test03_BitCount32_Data() { for(int i = 0; iL : 0x04030201: 00000100 00000011 00000010 00000001 new TestDataBF(32, 0x04030201, "00000100000000110000001000000001"), // H->L : 0xAFFECAFE: 10101111 11111110 11001010 11111110 new TestDataBF(32, 0xAFFECAFE, "10101111111111101100101011111110"), // H->L : 0xDEADBEEF: 11011110 10101101 10111110 11101111 new TestDataBF(32, 0xDEADBEEF, "11011110101011011011111011101111") }; static TestDataBF[] testDataBF16Bit = { // H->L : 0x0201: 00000100 00000011 00000010 00000001 new TestDataBF(16, 0x0201, "0000001000000001"), // H->L : 0x0403: 00000100 00000011 new TestDataBF(16, 0x0403, "0000010000000011"), // H->L : 0xAFFE: 10101111 11111110 new TestDataBF(16, 0xAFFE, "1010111111111110"), // H->L : 0xCAFE: 11001010 11111110 new TestDataBF(16, 0xCAFE, "1100101011111110"), // H->L : 0xDEADBEEF: 11011110 10101101 10111110 11101111 new TestDataBF(16, 0xDEAD, "1101111010101101"), new TestDataBF(16, 0xBEEF, "1011111011101111") }; static TestDataBF[] testDataBF3Bit = { new TestDataBF(3, 0x01, "001"), new TestDataBF(3, 0x02, "010"), new TestDataBF(3, 0x05, "101") }; @Test public void test20_ValidateTestData() { for(int i=0; i>> 5); return units << 5; } static void test_Aligned32bit(final TestDataBF d) { final int oneBitCount = Bitfield.Util.bitCount(d.val); final Bitfield bf1 = Bitfield.Factory.create(d.bitSize); Assert.assertEquals(get32BitStorageSize(d.bitSize), bf1.size()); final Bitfield bf2 = Bitfield.Factory.create(d.bitSize+128); Assert.assertEquals(get32BitStorageSize(d.bitSize+128), bf2.size()); bf1.put32( 0, d.bitSize, d.val); Assert.assertEquals(d.val, bf1.get32( 0, d.bitSize)); Assert.assertEquals(oneBitCount, bf1.bitCount()); assertEquals(bf1, 0, d.val, d.pattern, oneBitCount); bf2.put32( 0, d.bitSize, d.val); Assert.assertEquals(d.val, bf2.get32( 0, d.bitSize)); Assert.assertEquals(oneBitCount*1, bf2.bitCount()); assertEquals(bf2, 0, d.val, d.pattern, oneBitCount); bf2.put32(64, d.bitSize, d.val); Assert.assertEquals(d.val, bf2.get32(64, d.bitSize)); Assert.assertEquals(oneBitCount*2, bf2.bitCount()); assertEquals(bf2, 64, d.val, d.pattern, oneBitCount); Assert.assertEquals(d.val, bf2.copy32(0, 96, d.bitSize)); Assert.assertEquals(d.val, bf2.get32(96, d.bitSize)); Assert.assertEquals(oneBitCount*3, bf2.bitCount()); assertEquals(bf2, 96, d.val, d.pattern, oneBitCount); } @Test public void test21_Unaligned() { for(int i=0; i %d", d.val, d.pattern, d.bitSize, bf.size(), oneBitCount, lowBitnum, maxBitpos); // // via put32 // bf.put32( lowBitnum, d.bitSize, d.val); for(int i=0; i=0; i--) { Assert.assertEquals(msg+", bitpos "+i, 0 != ( d.val & ( 1 << i ) ), bf.copy(lowBitnum+i, lowBitnum+1+i) ); } bf.clear(lowBitnum); Assert.assertEquals(msg, d.val, bf.get32( lowBitnum+1, d.bitSize)); for(int i=0; i, exp "+BitDemoData.toHexString(exp)); Assert.assertEquals(msg+", bitpos "+i, exp, val); } for(int i=highBitnum+1; i, exp "+BitDemoData.toHexString(exp)); Assert.assertEquals(msg+", bitpos "+i, exp, val); } } public static void main(final String args[]) throws IOException { final String tstname = TestBitfield00.class.getName(); org.junit.runner.JUnitCore.main(tstname); } }