Package org.helidb.lang.serializer

Source Code of org.helidb.lang.serializer.FixedSizeBigIntegerSerializerTest

/* HeliDB -- A simple database for Java, http://www.helidb.org
* Copyright (C) 2008, 2009 Karl Gustafsson
*
* This file is a part of HeliDB.
*
* HeliDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeliDB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package org.helidb.lang.serializer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.math.BigInteger;
import java.util.Arrays;

import org.entityfs.util.io.ByteArrayRandomAccess;
import org.junit.Test;

/**
* @author Karl Gustafsson
* @since 1.0
*/
public class FixedSizeBigIntegerSerializerTest
{
  @Test
  public void testInterpretWithOffset()
  {
    FixedSizeBigIntegerSerializer s = new FixedSizeBigIntegerSerializer(3);
    assertEquals(BigInteger.ZERO, s.interpret(new byte[] { (byte) 0, (byte) 1, (byte) 0, (byte) 0, (byte) 0 }, 1, 3));
    assertEquals(BigInteger.ZERO, s.interpret(new byte[] { (byte) 1, (byte) 1, (byte) 0, (byte) 0, (byte) 1 }, 1, 3));
    assertEquals(BigInteger.ONE, s.interpret(new byte[] { (byte) 0, (byte) 0, (byte) 1, (byte) 1, (byte) 0 }, 2, 3));
    assertEquals((1 << 8) + 2, s.interpret(new byte[] { (byte) 0, (byte) 2, (byte) 1, (byte) 2, (byte) 0 }, 1, 3).intValue());
  }

  @Test
  public void testSerializeAndInterpret()
  {
    FixedSizeBigIntegerSerializer s = new FixedSizeBigIntegerSerializer(3);
    assertEquals(0, s.interpret(new byte[] { (byte) 1, (byte) 0, (byte) 0 }).intValue());
    assertEquals(12, s.interpret(new byte[] { (byte) 1, (byte) 12, (byte) 0 }).intValue());
    assertEquals(0x0A0F, s.interpret(new byte[] { (byte) 2, (byte) 0x0A, (byte) 0x0F }).intValue());
    assertEquals(0, s.interpret(s.serialize(BigInteger.ZERO)).intValue());
    assertEquals(-1, s.interpret(s.serialize(BigInteger.valueOf(-1))).intValue());
    assertEquals(12, s.interpret(s.serialize(BigInteger.valueOf(12))).intValue());
    assertEquals(0x0A0F, s.interpret(s.serialize(BigInteger.valueOf(0x0A0F))).intValue());
    assertTrue(Arrays.equals(new byte[] { (byte) 1, (byte) 0, (byte) 0 }, s.serialize(BigInteger.valueOf(0))));

    byte[] barr = new byte[5];
    for (int i = 0; i < 3; i++)
    {
      assertEquals(3, s.serialize(BigInteger.ONE, barr, i));
      assertEquals(1, s.interpret(barr, i, 3).intValue());
      assertEquals(3, s.serialize(BigInteger.valueOf(31124), barr, i));
      assertEquals(31124, s.interpret(barr, i, 3).intValue());
    }

    try
    {
      // Invalid length
      s.interpret(barr, 0, 2);
      fail();
    }
    catch (SerializationException e)
    {
      // ok
    }

    try
    {
      s.serialize(BigInteger.valueOf(0x10000));
      fail();
    }
    catch (SerializationException e)
    {
      // ok
    }

    try
    {
      s.serialize(null);
      fail();
    }
    catch (NullPointerException e)
    {
      // ok
    }

    try
    {
      s.interpret(null);
      fail();
    }
    catch (NullPointerException e)
    {
      // ok
    }
  }

  @Test
  public void testReadBigInteger()
  {
    FixedSizeBigIntegerSerializer s = new FixedSizeBigIntegerSerializer(2);
    byte[] barr = s.serialize(BigInteger.ZERO);
    assertEquals(0, s.readBigInteger(new ByteArrayInputStream(barr)).intValue());
    assertEquals(0, s.readBigInteger(new ByteArrayRandomAccess(barr)).intValue());
    barr = s.serialize(BigInteger.ONE);
    assertEquals(1, s.readBigInteger(new ByteArrayInputStream(barr)).intValue());
    assertEquals(1, s.readBigInteger(new ByteArrayRandomAccess(barr)).intValue());
    barr = s.serialize(BigInteger.valueOf(127));
    assertEquals(127, s.readBigInteger(new ByteArrayInputStream(barr)).intValue());
    assertEquals(127, s.readBigInteger(new ByteArrayRandomAccess(barr)).intValue());
    barr = s.serialize(BigInteger.valueOf(-128));
    assertEquals(-128, s.readBigInteger(new ByteArrayInputStream(barr)).intValue());
    assertEquals(-128, s.readBigInteger(new ByteArrayRandomAccess(barr)).intValue());
  }

  @Test
  public void testSerializeLongNumber()
  {
    FixedSizeBigIntegerSerializer s = new FixedSizeBigIntegerSerializer(13);
    BigInteger bi = new BigInteger("233232560138448523533070160");
    byte[] barr = s.serialize(bi);
    assertEquals(bi, s.interpret(barr));
  }
}
TOP

Related Classes of org.helidb.lang.serializer.FixedSizeBigIntegerSerializerTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.