package jp.hiuchida.bit_func;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class BitTest extends TestCase {

	public void test_WEIGHT() {
		for (int b=0; b<=BitFunc.MAX_BITS; b++) {
			int chk = 1 << b;
			assertEquals(BitFunc.WEIGHT[b], chk);
		}
	}
	
	public void test_test_bit() {
		for (int x=0; x<BitFunc.WEIGHT[BitFunc.MAX_BITS]; x++) {
			for (int b=0; b<BitFunc.MAX_BITS; b++) {
				int chk = ((x & BitFunc.WEIGHT[b]) != 0) ? 1:0;
				assertEquals(BitFunc.test_bit(x, b), chk);
			}
		}
	}
	
	public void test_set_bit() {
		for (int x=0; x<BitFunc.WEIGHT[BitFunc.MAX_BITS]; x++) {
			for (int b=0; b<BitFunc.MAX_BITS; b++) {
				int chk = x | BitFunc.WEIGHT[b];
				assertEquals(BitFunc.set_bit(x, b), chk);
			}
		}
	}
	
	public void test_reset_bit() {
		for (int x=0; x<BitFunc.WEIGHT[BitFunc.MAX_BITS]; x++) {
			for (int b=0; b<BitFunc.MAX_BITS; b++) {
				int chk = x & (~BitFunc.WEIGHT[b]);
				assertEquals(BitFunc.reset_bit(x, b), chk);
			}
		}
	}
	
	public void test_and_bit() {
		for (int x=0; x<BitFunc.WEIGHT[BitFunc.MAX_BITS]; x++) {
			for (int y=0; y<BitFunc.WEIGHT[BitFunc.MAX_BITS]; y++) {
				int chk = x & y;
				assertEquals(BitFunc.and_bit(x, y), chk);
			}
		}
	}
	
	public void test_or_bit() {
		for (int x=0; x<BitFunc.WEIGHT[BitFunc.MAX_BITS]; x++) {
			for (int y=0; y<BitFunc.WEIGHT[BitFunc.MAX_BITS]; y++) {
				int chk = x | y;
				assertEquals(BitFunc.or_bit(x, y), chk);
			}
		}
	}
	
	public void test_xor_bit() {
		for (int x=0; x<BitFunc.WEIGHT[BitFunc.MAX_BITS]; x++) {
			for (int y=0; y<BitFunc.WEIGHT[BitFunc.MAX_BITS]; y++) {
				int chk = x ^ y;
				assertEquals(BitFunc.xor_bit(x, y), chk);
			}
		}
	}
	
	public void test_not_bit() {
		for (int x=0; x<BitFunc.WEIGHT[BitFunc.MAX_BITS]; x++) {
			for (int y=0; y<BitFunc.WEIGHT[BitFunc.MAX_BITS]; y++) {
				int chk = (~x) & (BitFunc.WEIGHT[BitFunc.MAX_BITS] - 1);
				assertEquals(BitFunc.not_bit(x), chk);
			}
		}
	}
	
	public void test_shl_bit() {
		for (int x=0; x<BitFunc.WEIGHT[BitFunc.MAX_BITS]; x++) {
			for (int b=0; b<BitFunc.MAX_BITS; b++) {
				int chk = (x << b) & (BitFunc.WEIGHT[BitFunc.MAX_BITS] - 1);
				assertEquals(BitFunc.shl_bit(x, b), chk);
			}
		}
	}
	
	public void test_shr_bit() {
		for (int x=0; x<BitFunc.WEIGHT[BitFunc.MAX_BITS]; x++) {
			for (int b=0; b<BitFunc.MAX_BITS; b++) {
				int chk = x >>> b;
				assertEquals(BitFunc.shr_bit(x, b), chk);
			}
		}
	}
	
	public static Test suite() {
		return new TestSuite(BitTest.class);
	}
	
	public static void main(String[] args) {
		junit.textui.TestRunner.run(suite());
	}

}

