Test code for Byte Operations
//
// Lapetus Ltd Java Class. Copyright (c) Lapetus Systems Ltd, 2009, 2010.
// -----------------------------------------------------------------------
// This is the intellectual property of Lapetus Systems Ltd, Artemis, Greece.
// --------------------------------------------------------------------------
// www.lapetus-ltd.com, www.lapetus.com.gr, www.lapetus.eu
// -------------------------------------------------------
// $LastChangedRevision: 1168 $
// $LastChangedDate:: 2010-11-06 16:41:19#$
// ----------------------------------------
import com.lapetus_ltd.api.TLptsMainJut;
import com.lapetus_ltd.api.common.TLptsBytesUtil;
import com.lapetus_ltd.api.common.logger.TLptsLogger;
import junit.framework.TestCase;
import java.io.*;
import java.util.Random;
// ######################################################################################################
// #### Test code for the JUT build process. This code will stop the build process on ERROR ####
// ######################################################################################################
// Class Description : This tests the byte operations.
//
// This module test all the functions of the byte util, including Object/Byte operations.
public class TestByteUtil extends TestCase
{
public static void main(String[] args)
{
TestByteUtil test = new TestByteUtil();
test.testByteOperations();
}
public void testByteOperations()
{
TLptsMainJut.init();
System.out.println("Testing byte operations - converting MyObject");
MyObject myObject = new MyObject(10,"Java is cool!");
byte[] bOut;
for (byte bt : bOut = TLptsBytesUtil.object2Bytes(myObject))
System.out.print(String.format("%02X",bt));
String hexStr = TLptsBytesUtil.getHexString(bOut).toUpperCase();
byte[] bIn = TLptsBytesUtil.bytesFromHexString(hexStr);
Object obj = TLptsBytesUtil.bytes2Object(bOut);
System.out.println();
if (obj instanceof MyObject)
System.out.println("My Object int & string = " + ((MyObject)obj).myInt + " " + ((MyObject)obj).myString);
else
fail("Wrong object returned");
if (bOut.length!= bIn.length)
fail("Object did not convert correctly - invalid length!");
int i=0;
for (byte b : bOut)
if (b != bIn[i++])
fail("Object did not convert correctly - comparison not the same!");
/////////////////// I N T E G E R //////////////////////////////
System.out.println("Testing Integer 1234567890");
int x = 1234567890;
byte[] intBytes = TLptsBytesUtil.int2Bytes(x);
System.out.println();
System.out.print("Bytes of integer are :");
for (byte b : intBytes)
System.out.print(String.format("%02X",b));
System.out.println();
int x1 = 0;
try
{
x1 = TLptsBytesUtil.bytes2Int(intBytes);
System.out.println("Returned number is " + x1);
} catch (IOException e)
{
TLptsLogger.logError("Exception during byte2Int conversion.",e);
fail("Exception during byte2Int conversion");
}
if (x!=x1)
fail("Integer values not the same.");
/////////////////////// D O U B L E /////////////////////////////////////
System.out.println("Testing Long 1234567890.0123456789");
double d = 1234567890.0123456789;
byte[] doubleBytes = TLptsBytesUtil.double2Bytes(d);
System.out.println();
System.out.print("Bytes of double are :");
for (byte b : doubleBytes)
System.out.print(String.format("%02X",b));
System.out.println();
double d1 = 0;
try
{
d1 = TLptsBytesUtil.bytes2Double(doubleBytes);
System.out.println("Returned number is " + d1);
} catch (IOException e)
{
TLptsLogger.logError("Exception during byte2Double conversion.",e);
fail("Exception during byte2Double conversion");
}
if(d!=d1)
fail("double values not the same.");
/////////////////////// L O N G /////////////////////////////////////
System.out.println("Testing Long 3214569870");
long y = 3214569870L;
byte[] longBytes = TLptsBytesUtil.long2Bytes(y);
System.out.println();
System.out.print("Bytes of long are :");
for (byte b : longBytes)
System.out.print(String.format("%02X",b));
System.out.println();
long y1 = 0;
try
{
y1 = TLptsBytesUtil.bytes2Long(longBytes);
System.out.println("Returned number is " + y1);
} catch (IOException e)
{
TLptsLogger.logError("Exception during byte2Long conversion.",e);
fail("Exception during byte2Long conversion");
}
if(y!=y1)
fail("Long values not the same.");
/////////////////////// S T R I N G //////////////////////////////////
System.out.println("Testing String : This is my test string");
String str = "This is my test string";
byte[] stringBytes = TLptsBytesUtil.string2Bytes(str);
System.out.println();
System.out.print("Bytes of string are :");
for (byte b : stringBytes)
System.out.print(String.format("%02X",b));
System.out.println();
String str1;
str1 = TLptsBytesUtil.bytes2String(stringBytes);
System.out.println("Returned string is " + str1);
if(!str.equals(str1))
fail("Strings are not the same.");
//////////////////// H E X S T R I N G ////////////////////////////////
System.out.println("Testing String : 2ecead19db");
hexStr = "2ecead19db";
byte[] hexStringBytes = TLptsBytesUtil.bytesFromHexString(hexStr);
System.out.println();
System.out.print("Bytes of hexString are :");
for (byte b : hexStringBytes)
System.out.print(String.format("%02X",b));
System.out.println();
String hexStr1;
hexStr1 = TLptsBytesUtil.getHexString(hexStringBytes);
System.out.println("Returned string is " + hexStr1);
if(!hexStr.equals(hexStr1))
fail("Strings are not the same");
///////////////// B Y T E X O R /////////////////////////////////
System.out.println("Testing XOR bytes");
byte[] b1 = new byte[256];
byte[] b2 = new byte[256];
Random rand = new Random();
for(int j = 0; j<256; j++)
b1[j] = TLptsBytesUtil.initByteFromInteger(rand.nextInt());
for(int j = 0; j<256; j++)
b2[j] = TLptsBytesUtil.initByteFromInteger(rand.nextInt());
System.out.println();
System.out.print("Bytes of b1 are :");
for (byte b : b1)
System.out.print(String.format("%02X",b));
System.out.println();
System.out.print("Bytes of b2 are :");
for (byte b : b2)
System.out.print(String.format("%02X",b));
byte[] xor = TLptsBytesUtil.xor(b1,b2);
xor = TLptsBytesUtil.xor(b1,xor);
System.out.println();
System.out.print("Bytes of xor are :");
for (byte b : xor)
System.out.print(String.format("%02X",b));
for(int j = 0; j < 256; j++ )
if(xor[j]!=b2[j])
fail("Bytes are not the same ("+j+")");
System.out.println();
//////////////////// I N I T B Y T E F R O M I N T E G E R /////////////
System.out.println("Testing int byte from integer");
String strBytes = "";
for(int j = 0; j < 256; j++)
strBytes += String.format("%02X", TLptsBytesUtil.initByteFromInteger(j));
System.out.println(strBytes);
if(!("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D" +
"1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B" +
"3C3D3E3F404142434445464748494A4B4C4D4E4F50515253545556575859" +
"5A5B5C5D5E5F606162636465666768696A6B6C6D6E6F7071727374757677" +
"78797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495" +
"969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3" +
"B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1" +
"D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF" +
"F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF").equals(strBytes))
fail("Bytes are not the same");
}
static public class MyObject implements Externalizable, Serializable
{
public int myInt;
public String myString;
public MyObject()
{
myInt = -1;
myString = "";
}
public MyObject(MyObject obj)
{
myInt = obj.myInt;
myString = obj.myString;
}
public MyObject(int myInt, String myString)
{
this.myInt = myInt;
this.myString = myString;
}
@Override
protected Object clone() throws CloneNotSupportedException
{
return new MyObject(myInt,myString);
}
// the order of the reading and writing is vital. They must match.
public void writeExternal(ObjectOutput out) throws IOException
{
out.writeInt(this.myInt);
out.writeUTF(this.myString);
}
// the order of the reading and writing is vital. They must match.
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
this.myInt = in.readInt();
this.myString = in.readUTF();
}
}
}