package co.phoenixlab.common.lang.io;

import org.junit.*;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;

import static org.junit.Assert.*;

public class LittleEndianDataInputStreamTest {

    @Test
    public void testReadByte() throws Exception {
        byte b = 0x12;
        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[]{b});
        DataInputStream dis = new DataInputStream(bais);
        LittleEndianDataInputStream ledis = new LittleEndianDataInputStream(dis);
        assertEquals(b, ledis.readByte());
    }

    @Test
    public void testReadUnsignedByte() throws Exception {
        int i = 200;
        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[]{(byte) i});
        DataInputStream dis = new DataInputStream(bais);
        LittleEndianDataInputStream ledis = new LittleEndianDataInputStream(dis);
        assertEquals(i, ledis.readUnsignedByte());
    }

    @Test
    public void testReadShort() throws Exception {
        short i = 500;
        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[]{(byte) (i & 0xFF), (byte) ((i >> 8) & 0xFF)});
        DataInputStream dis = new DataInputStream(bais);
        LittleEndianDataInputStream ledis = new LittleEndianDataInputStream(dis);
        assertEquals(i, ledis.readShort());
    }

    @Test
    public void testReadUnsignedShort() throws Exception {
        int i = 0xFF20;
        short c = (short) i;
        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[]{(byte) (c & 0xFF), (byte) ((c >> 8) & 0xFF)});
        DataInputStream dis = new DataInputStream(bais);
        LittleEndianDataInputStream ledis = new LittleEndianDataInputStream(dis);
        assertEquals(i, ledis.readUnsignedShort());
    }

    @Test
    public void testReadChar() throws Exception {
        char c = 0xFF20;
        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[]{(byte) (c & 0xFF), (byte) ((c >> 8) & 0xFF)});
        DataInputStream dis = new DataInputStream(bais);
        LittleEndianDataInputStream ledis = new LittleEndianDataInputStream(dis);
        assertEquals(c, ledis.readChar());
    }

    @Test
    public void testReadInt() throws Exception {
        int i = 0xDEADBEEF;
        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[]{(byte) (i & 0xFF), (byte) ((i >> 8) & 0xFF),
                (byte) ((i >> 16) & 0xFF), (byte) ((i >> 24) & 0xFF)});
        DataInputStream dis = new DataInputStream(bais);
        LittleEndianDataInputStream ledis = new LittleEndianDataInputStream(dis);
        assertEquals(i, ledis.readInt());
    }

    @Test
    public void testReadUnsignedInt() throws Exception {
        int i = 0xFAFEBABE;
        long l = Integer.toUnsignedLong(i);
        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[]{(byte) (i & 0xFF), (byte) ((i >> 8) & 0xFF),
                (byte) ((i >> 16) & 0xFF), (byte) ((i >> 24) & 0xFF)});
        DataInputStream dis = new DataInputStream(bais);
        LittleEndianDataInputStream ledis = new LittleEndianDataInputStream(dis);
        assertEquals(l, ledis.readUnsignedInt());
    }

    @Test
    public void testReadLong() throws Exception {
        long l = 0xCAFEBABEDEADBEEFL;
        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[]{
                (byte) (l & 0xFF), (byte) ((l >> 8) & 0xFF),
                (byte) ((l >> 16) & 0xFF), (byte) ((l >> 24) & 0xFF),
                (byte) ((l >> 32) & 0xFF), (byte) ((l >> 40) & 0xFF),
                (byte) ((l >> 48) & 0xFF), (byte) ((l >> 56) & 0xFF)
        });
        DataInputStream dis = new DataInputStream(bais);
        LittleEndianDataInputStream ledis = new LittleEndianDataInputStream(dis);
        assertEquals(l, ledis.readLong());
    }

    @Test
    public void testReadFloat() throws Exception {

    }

    @Test
    public void testReadDouble() throws Exception {

    }
}

@@ -3845,78 +3845,1148 @@
 n %7B%0A
-%0A    %7D%0A%0A    @Test%0A    public void testReadDouble() throws Exception %7B%0A
+        float f = 1.123F;%0A        int i = Float.floatToRawIntBits(f);%0A        ByteArrayInputStream bais = new ByteArrayInputStream(new byte%5B%5D%7B(byte) (i & 0xFF), (byte) ((i %3E%3E 8) & 0xFF),%0A                (byte) ((i %3E%3E 16) & 0xFF), (byte) ((i %3E%3E 24) & 0xFF)%7D);%0A        DataInputStream dis = new DataInputStream(bais);%0A        LittleEndianDataInputStream ledis = new LittleEndianDataInputStream(dis);%0A        assertEquals(f, ledis.readFloat(), 0.0005F);%0A    %7D%0A%0A    @Test%0A    public void testReadDouble() throws Exception %7B%0A        double d = 1.123;%0A        long l = Double.doubleToRawLongBits(d);%0A        ByteArrayInputStream bais = new ByteArrayInputStream(new byte%5B%5D%7B%0A                (byte) (l & 0xFF), (byte) ((l %3E%3E 8) & 0xFF),%0A                (byte) ((l %3E%3E 16) & 0xFF), (byte) ((l %3E%3E 24) & 0xFF),%0A                (byte) ((l %3E%3E 32) & 0xFF), (byte) ((l %3E%3E 40) & 0xFF),%0A                (byte) ((l %3E%3E 48) & 0xFF), (byte) ((l %3E%3E 56) & 0xFF)%0A        %7D);%0A        DataInputStream dis = new DataInputStream(bais);%0A        LittleEndianDataInputStream ledis = new LittleEndianDataInputStream(dis);%0A        assertEquals(d, ledis.readDouble(), 0.0005);
 %0A   

Add readFloat and Double tests