Author: akarasulu
Date: Sun Jul 11 22:22:26 2004
New Revision: 22828
Modified:
incubator/directory/snickers/trunk/ber-codec/src/test/org/apache/snickers/ber/primitives/PrimitiveUtilsTest.java
Log:
test cases for the encodeInt function
Modified:
incubator/directory/snickers/trunk/ber-codec/src/test/org/apache/snickers/ber/primitives/PrimitiveUtilsTest.java
==============================================================================
---
incubator/directory/snickers/trunk/ber-codec/src/test/org/apache/snickers/ber/primitives/PrimitiveUtilsTest.java
(original)
+++
incubator/directory/snickers/trunk/ber-codec/src/test/org/apache/snickers/ber/primitives/PrimitiveUtilsTest.java
Sun Jul 11 22:22:26 2004
@@ -64,6 +64,19 @@
*/
public void testDecodeInt()
{
+ byte[] bites = new byte[1];
+ bites[0] = (byte)0x80;
+ assertEquals( -128, PrimitiveUtils.decodeInt( bites, 0, 1 ) );
+
+ bites = new byte[2];
+ bites[0] = 0;
+ bites[1] = (byte)0x80;
+ assertEquals( 128, PrimitiveUtils.decodeInt( bites, 0, 2 ) );
+
+ bites = new byte[1];
+ bites[0] = (byte)0x80;
+ assertEquals( -128, PrimitiveUtils.decodeInt( bites, 0, 1 ) );
+
assertEquals( 0, PrimitiveUtils.decodeInt( null, 0, 0 ) ) ;
for ( int ii = 0; ii < byteArrays.length; ii++ )
@@ -87,20 +100,172 @@
public void testEncodeInt()
{
-// for ( int ii = 0; ii < values.length; ii++ )
-// {
-// byte[] encoded = PrimitiveUtils.encodeInt( values[ii] ) ;
-// assertTrue( ArrayUtils.isEquals( byteArrays[ii], encoded ) );
-// }
-//
-// try
-// {
-// PrimitiveUtils.encodeInt( values[0] ) ;
-// fail( "should never get here due to an exception" ) ;
-// }
-// catch( IllegalArgumentException e )
-// {
-// assertNotNull( e ) ;
-// }
+ byte[] encoded = PrimitiveUtils.encodeInt( 0 );
+ byte[] actual = new BigInteger( "0" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( 0, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
+
+ encoded = PrimitiveUtils.encodeInt( -1 );
+ actual = new BigInteger( "-1" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( -1, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
+
+ encoded = PrimitiveUtils.encodeInt( 1 );
+ actual = new BigInteger( "1" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( 1, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
+
+ encoded = PrimitiveUtils.encodeInt( -100 );
+ actual = new BigInteger( "-100" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( -100, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
+
+ encoded = PrimitiveUtils.encodeInt( 100 );
+ actual = new BigInteger( "100" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( 100, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
+
+ encoded = PrimitiveUtils.encodeInt( -128 );
+ actual = new BigInteger( "-128" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( -128, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
+
+ encoded = PrimitiveUtils.encodeInt( 127 );
+ actual = new BigInteger( "127" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( 127, PrimitiveUtils.decodeInt( encoded, 0, 1 ) );
+
+ // --------------------------------------------------------------------
+ // TWO BYTES
+ // --------------------------------------------------------------------
+
+ encoded = PrimitiveUtils.encodeInt( 128 );
+ actual = new BigInteger( "128" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( 128, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
+
+ encoded = PrimitiveUtils.encodeInt( -129 );
+ actual = new BigInteger( "-129" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( -129, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
+
+ encoded = PrimitiveUtils.encodeInt( 129 );
+ actual = new BigInteger( "129" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( 129, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
+
+ encoded = PrimitiveUtils.encodeInt( -1000 );
+ actual = new BigInteger( "-1000" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( -1000, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
+
+ encoded = PrimitiveUtils.encodeInt( 1000 );
+ actual = new BigInteger( "1000" ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( 1000, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
+
+ encoded = PrimitiveUtils.encodeInt( -(1<<15) );
+ actual = new BigInteger( new Integer(-(1<<15))
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( -(1<<15), PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
+
+ encoded = PrimitiveUtils.encodeInt( (1<<15)-1 );
+ actual = new BigInteger( new Integer((1<<15)-1)
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (1<<15)-1, PrimitiveUtils.decodeInt( encoded, 0, 2 ) );
+
+
+ // --------------------------------------------------------------------
+ // THREE BYTES
+ // --------------------------------------------------------------------
+
+ encoded = PrimitiveUtils.encodeInt( (1<<15) );
+ actual = new BigInteger( new Integer((1<<15))
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (1<<15), PrimitiveUtils.decodeInt( encoded, 0, 3 ) );
+
+ encoded = PrimitiveUtils.encodeInt( (-(1<<15))-1 );
+ actual = new BigInteger( new Integer((-(1<<15))-1)
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (-(1<<15))-1, PrimitiveUtils.decodeInt( encoded, 0, 3 )
);
+
+ encoded = PrimitiveUtils.encodeInt( (1<<15)+1000 );
+ actual = new BigInteger( new Integer((1<<15)+1000)
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (1<<15)+1000, PrimitiveUtils.decodeInt( encoded, 0, 3 )
);
+
+ encoded = PrimitiveUtils.encodeInt( (-(1<<15))-1000 );
+ actual = new BigInteger( new Integer((-(1<<15))-1000)
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (-(1<<15))-1000, PrimitiveUtils
+ .decodeInt( encoded, 0, 3 ) );
+
+ encoded = PrimitiveUtils.encodeInt( (1<<23)-1 );
+ actual = new BigInteger( new Integer((1<<23)-1)
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (1<<23)-1, PrimitiveUtils.decodeInt( encoded, 0, 3 ) );
+
+ encoded = PrimitiveUtils.encodeInt( (-(1<<23) ));
+ actual = new BigInteger( new Integer(-(1<<23))
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( -(1<<23), PrimitiveUtils.decodeInt( encoded, 0, 3 ) );
+
+ // --------------------------------------------------------------------
+ // FOUR BYTES
+ // --------------------------------------------------------------------
+
+ encoded = PrimitiveUtils.encodeInt( (1<<23) );
+ actual = new BigInteger( new Integer((1<<23))
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (1<<23), PrimitiveUtils.decodeInt( encoded, 0, 4 ) );
+
+ encoded = PrimitiveUtils.encodeInt( (-(1<<23))-1 );
+ actual = new BigInteger( new Integer((-(1<<23))-1)
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (-(1<<23))-1, PrimitiveUtils.decodeInt( encoded, 0, 4 )
);
+
+ encoded = PrimitiveUtils.encodeInt( (1<<23) + 10000 );
+ actual = new BigInteger( new Integer((1<<23) + 10000 )
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (1<<23) + 10000,
+ PrimitiveUtils.decodeInt( encoded, 0, 4 ) );
+
+ encoded = PrimitiveUtils.encodeInt( (-(1<<23))-10000 );
+ actual = new BigInteger( new Integer((-(1<<23))-10000 )
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( (-(1<<23))-10000,
+ PrimitiveUtils.decodeInt( encoded, 0, 4 ) );
+
+ encoded = PrimitiveUtils.encodeInt( Integer.MAX_VALUE );
+ actual = new BigInteger( new Integer( Integer.MAX_VALUE )
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( Integer.MAX_VALUE,
+ PrimitiveUtils.decodeInt( encoded, 0, 4 ) );
+
+ encoded = PrimitiveUtils.encodeInt( Integer.MIN_VALUE );
+ actual = new BigInteger( new Integer( Integer.MIN_VALUE )
+ .toString() ).toByteArray();
+ assertTrue( ArrayUtils.isEquals( actual, encoded ) );
+ assertEquals( Integer.MIN_VALUE,
+ PrimitiveUtils.decodeInt( encoded, 0, 4 ) );
+
+ for ( int ii = 0; ii < values.length; ii++ )
+ {
+ encoded = PrimitiveUtils.encodeInt( values[ii] ) ;
+ assertTrue( ArrayUtils.isEquals( byteArrays[ii], encoded ) );
+ }
}
}