Results 1 to 5 of 5

Thread: JUnit testing

  1. #1
    hedonist is offline Member
    Join Date
    Jun 2009
    Posts
    35
    Rep Power
    0

    Default 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));
    	}
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Print out the value of DataNetwork.getShort(data, 1) to see what it is then.

  3. #3
    hedonist is offline Member
    Join Date
    Jun 2009
    Posts
    35
    Rep Power
    0

    Default

    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.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  5. #5
    hedonist is offline Member
    Join Date
    Jun 2009
    Posts
    35
    Rep Power
    0

    Default

    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

  1. Testing tools (Profiling and JUnit)
    By ericpinet in forum Eclipse
    Replies: 1
    Last Post: 04-21-2009, 10:23 PM
  2. Junit
    By Azndaddy in forum New To Java
    Replies: 6
    Last Post: 06-15-2008, 06:47 AM
  3. Replies: 1
    Last Post: 04-17-2008, 02:30 PM
  4. Testing JUnit PDE with Ant
    By keynan in forum Eclipse
    Replies: 0
    Last Post: 02-14-2008, 11:35 PM
  5. TRying to use jUnit for Tesing
    By sandor in forum New To Java
    Replies: 3
    Last Post: 04-18-2007, 11:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •