Results 1 to 5 of 5
Thread: JUnit testing
- 10-03-2009, 09:35 AM #1
Member
- Join Date
- Jun 2009
- Posts
- 35
- Rep Power
- 0
JUnit testing
Hi, I am trying to run a JUnit test for the following code. But the assertEquals(0x2084, DataNetwork.getShort(data, 1));
is throwing an error, couldn't figure out. Any help would be highly appreciated.
Thanks
Java Code:......... byte[] data = new byte[] {(byte) 0x00, (byte) 0x20, (byte) 0x84}; assertEquals(0x2084, DataNetwork.getShort(data, 1)); ............Java Code:public class DataNetwork { /** * Get the byte in the specific offset. */ public static byte get(byte[] data, int offset) { return data[offset]; } /** * Get the short in the specific offset. */ public static short getShort(byte[] data, int offset) { return (short) (get(data, offset) << 8 | get(data, offset + 1)); } }
- 10-03-2009, 01:46 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Print out the value of DataNetwork.getShort(data, 1) to see what it is then.
- 10-03-2009, 02:24 PM #3
Member
- Join Date
- Jun 2009
- Posts
- 35
- Rep Power
- 0
I did and i get a value of -124. The getShort() function should have returned 132 which is the decimal representation of 0x84. Maybe it has to do something with converting the output of getShort() method to 'short' data type....I'm lost here.
- 10-03-2009, 03:05 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Why do you have that get method? All it does is easily done using data[offset]. No need for a method for that.
Did you work out (byte)0x20 | (byte)0x84 to see what the result is because that is what your call effectively says.
And yes, the cast to byte will truncate if the value is out of range for bytes.
- 10-04-2009, 12:06 PM #5
Member
- Join Date
- Jun 2009
- Posts
- 35
- Rep Power
- 0
Thank you. You are right, I was trying out for [(byte)0x20 <<8| (byte)0x84]. I was searching why i was getting different answer and it seems that truncation to byte is the reason. I want to learn more about it..i googled about short data types but all it mentioned was, "short takes 16-bit 2's complementary......" not much helpful.
Similar Threads
-
Testing tools (Profiling and JUnit)
By ericpinet in forum EclipseReplies: 1Last Post: 04-21-2009, 10:23 PM -
Junit
By Azndaddy in forum New To JavaReplies: 6Last Post: 06-15-2008, 06:47 AM -
Help with testing methods and classes in JUnit, using BlueJ
By Jonasse in forum New To JavaReplies: 1Last Post: 04-17-2008, 02:30 PM -
Testing JUnit PDE with Ant
By keynan in forum EclipseReplies: 0Last Post: 02-14-2008, 11:35 PM -
TRying to use jUnit for Tesing
By sandor in forum New To JavaReplies: 3Last Post: 04-18-2007, 11:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks