package tachyon.util.io;

import org.junit.Assert;
import org.junit.Test;

/**
 * Unit tests for {@link ByteIOUtils}
 */
public class ByteIOUtilsTest {
  private byte[] mBuf = new byte[1024];

  @Test
  public void readWriteByteTest() throws Exception {
    long[] values = new long[] {0, 1, 2, 0x7f, 0xff};
    for (long i : values) {
      byte v = (byte) i;
      ByteIOUtils.writeByte(mBuf, 0, v);
      Assert.assertEquals(v, ByteIOUtils.readByte(mBuf, 0));
    }
  }

  @Test
  public void readWriteShortTest() throws Exception {
    long[] values = new long[] {0, 1, 2, 0x7f, 0xff, 0xffff};
    for (long i : values) {
      short v = (short) i;
      ByteIOUtils.writeShort(mBuf, 0, v);
      Assert.assertEquals(v, ByteIOUtils.readShort(mBuf, 0));
      ByteIOUtils.writeShort(mBuf, 1, v);
      Assert.assertEquals(v, ByteIOUtils.readShort(mBuf, 1));
    }
  }

  @Test
  public void readWriteIntTest() throws Exception {
    long[] values = new long[] {0, 1, 2, 0x7f, 0xff, 0xffff, 0xffffff, 0xffffffff};
    for (long i : values) {
      int v = (int) i;
      ByteIOUtils.writeInt(mBuf, 0, v);
      Assert.assertEquals(v, ByteIOUtils.readInt(mBuf, 0));
      ByteIOUtils.writeInt(mBuf, 1, v);
      Assert.assertEquals(v, ByteIOUtils.readInt(mBuf, 1));
      ByteIOUtils.writeInt(mBuf, 2, v);
      Assert.assertEquals(v, ByteIOUtils.readInt(mBuf, 2));
      ByteIOUtils.writeInt(mBuf, 3, v);
      Assert.assertEquals(v, ByteIOUtils.readInt(mBuf, 3));
    }
  }

  @Test
  public void readWriteLongTest() throws Exception {
    long[] values =
        new long[] {0, 1, 2, 0x7f, 0xff, 0xffff, 0xffffff, 0xffffffff, 0xffffffffffL,
            0xffffffffffffL, 0xffffffffffffffL, 0xffffffffffffffffL};
    for (long v : values) {
      ByteIOUtils.writeLong(mBuf, 0, v);
      Assert.assertEquals(v, ByteIOUtils.readLong(mBuf, 0));
      ByteIOUtils.writeLong(mBuf, 1, v);
      Assert.assertEquals(v, ByteIOUtils.readLong(mBuf, 1));
      ByteIOUtils.writeLong(mBuf, 2, v);
      Assert.assertEquals(v, ByteIOUtils.readLong(mBuf, 2));
      ByteIOUtils.writeLong(mBuf, 3, v);
      Assert.assertEquals(v, ByteIOUtils.readLong(mBuf, 3));
      ByteIOUtils.writeLong(mBuf, 4, v);
      Assert.assertEquals(v, ByteIOUtils.readLong(mBuf, 4));
      ByteIOUtils.writeLong(mBuf, 5, v);
      Assert.assertEquals(v, ByteIOUtils.readLong(mBuf, 5));
      ByteIOUtils.writeLong(mBuf, 6, v);
      Assert.assertEquals(v, ByteIOUtils.readLong(mBuf, 6));
      ByteIOUtils.writeLong(mBuf, 7, v);
      Assert.assertEquals(v, ByteIOUtils.readLong(mBuf, 7));
    }
  }


}

Add unit tests to ByteIOUtils