package study.hard.javalib.nativelib;

import static org.junit.Assert.*;

import org.junit.Test;

/**
 * A test case for shift operator.
 * @author iandmyhand@gmail.com
 */
public class ShiftOperatorTest {

	@Test
	public void testShiftOperator() {
		int i = 0;

		i = 2;
		System.out.print("i: " + i);
		System.out.println(" [" + getBit(i) + "]");

		i = 2;
		i = i << 1;
		System.out.print("i: " + i);
		System.out.println(" [" + getBit(i) + "]");
		assertEquals(4, i);

		i = 2;
		i = i << 2;
		System.out.print("i: " + i);
		System.out.println(" [" + getBit(i) + "]");
		assertEquals(8, i);

		i = 8;
		i = i >> 2;
		System.out.print("i: " + i);
		System.out.println(" [" + getBit(i) + "]");
		assertEquals(2, i);
	}

	String getBit(int n) {
		int[] array = new int[8];
		int iattr = (int)((byte)n & 0xFF);
		int bitWise = 1;
		for (int i = 0; i < 8; i++) {
			if ((bitWise & iattr) > 0)
				array[i] = 1;
			else
				array[i] = 0;
			bitWise = (bitWise << 1) & 0xfe;
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < array.length; ++i)
			sb.append(array[7 - i]);
		return sb.toString();
	}
}

@@ -205,13 +205,19 @@
 %7B%0A%0A%09
-@Test
+int i = 0;%0A
 %0A%09pu
@@ -230,183 +230,233 @@
 oid 
-testShiftOperator
+setUpTo2
 () %7B%0A%09%09
-int 
 i = 
-0
+2
 ;%0A
+%09%7D%0A%0A%09public void setUpTo8() %7B
 %0A%09%09i = 
-2;%0A%09%09System.out.print(%22i: %22 + i);%0A%09%09System.out.println(%22 %5B%22 + getBit(i) + %22%5D%22);%0A%0A%09%09i = 2;%0A%09%09i = i %3C%3C 1;%0A%09%09System.out.print(%22i: %22 + i)
+8;%0A%09%7D%0A%0A%09@Test%0A%09public void testLeftShiftOperator() %7B%0A%09%09System.out.println(%22Test left shift operator.%22);%0A%0A%09%09setUpTo2();%0A%09%09System.out.print(i + %22 %3C%3C 1 = %22);%0A%09%09i = i %3C%3C 1
 ;%0A%09%09
@@ -466,32 +466,36 @@
 tem.out.println(
+i + 
 %22 %5B%22 + getBit(i)
@@ -532,27 +532,18 @@
 %0A%0A%09%09
-i = 2;%0A%09%09i = i %3C%3C 2
+setUpTo2()
 ;%0A%09%09
@@ -555,34 +555,53 @@
 m.out.print(
-%22i: %22 + i)
+i + %22 %3C%3C 2 = %22);%0A%09%09i = i %3C%3C 2
 ;%0A%09%09System.o
@@ -603,32 +603,36 @@
 tem.out.println(
+i + 
 %22 %5B%22 + getBit(i)
@@ -666,61 +666,174 @@
 i);%0A
-%0A%09%09i = 8;%0A%09%09i = i %3E%3E 2;%0A%09%09System.out.print(%22i: %22 + i)
+%09%7D%0A%0A%09@Test%0A%09public void testRightShiftOperator() %7B%0A%09%09System.out.println(%22Test right shift operator.%22);%0A%0A%09%09setUpTo8();%0A%09%09System.out.print(i + %22 %3E%3E 2 = %22);%0A%09%09i = i %3E%3E 2
 ;%0A%09%09
@@ -851,16 +851,20 @@
 println(
+i + 
 %22 %5B%22 + g

Add a test case of right shift operator.