ByteArrayBitStreamVarIntTest.java

  1. package zserio.runtime.io;

  2. import static org.junit.jupiter.api.Assertions.assertEquals;

  3. import java.io.IOException;

  4. import org.junit.jupiter.api.Test;

  5. public class ByteArrayBitStreamVarIntTest
  6. {
  7.     @Test
  8.     public void readWriteZero() throws IOException
  9.     {
  10.         readWriteTest(0, 1);
  11.     }

  12.     @Test
  13.     public void readWriteLongMin() throws IOException
  14.     {
  15.         readWriteTest(Long.MIN_VALUE, 1); // special case, encoded as -0
  16.     }

  17.     @Test
  18.     public void readWriteLongMax() throws IOException
  19.     {
  20.         readWriteTest(Long.MAX_VALUE, 9);
  21.     }

  22.     @Test
  23.     public void readWriteMinusOne() throws IOException
  24.     {
  25.         readWriteTest(-1L, 1);
  26.     }

  27.     @Test
  28.     public void readWriteOne() throws IOException
  29.     {
  30.         readWriteTest(1L, 1);
  31.     }

  32.     @Test
  33.     public void readWriteByte1NegativeMax() throws IOException
  34.     {
  35.         readWriteTest(-(1L << 6) + 1, 1);
  36.     }

  37.     @Test
  38.     public void readWriteByte1PositiveMax() throws IOException
  39.     {
  40.         readWriteTest((1L << 6) - 1, 1);
  41.     }

  42.     @Test
  43.     public void readWriteByte2NegativeMin() throws IOException
  44.     {
  45.         readWriteTest(-(1L << 6), 2);
  46.     }

  47.     @Test
  48.     public void readWriteByte2PositiveMin() throws IOException
  49.     {
  50.         readWriteTest((1L << 6), 2);
  51.     }

  52.     @Test
  53.     public void readWriteByte2NegativeMax() throws IOException
  54.     {
  55.         readWriteTest(-(1L << 13) + 1, 2);
  56.     }

  57.     @Test
  58.     public void readWriteByte2PositiveMax() throws IOException
  59.     {
  60.         readWriteTest((1L << 13) - 1, 2);
  61.     }

  62.     @Test
  63.     public void readWriteByte3NegativeMin() throws IOException
  64.     {
  65.         readWriteTest(-(1L << 13), 3);
  66.     }

  67.     @Test
  68.     public void readWriteByte3PositiveMin() throws IOException
  69.     {
  70.         readWriteTest((1L << 13), 3);
  71.     }

  72.     @Test
  73.     public void readWriteByte3NegativeMax() throws IOException
  74.     {
  75.         readWriteTest(-(1L << 20) + 1, 3);
  76.     }

  77.     @Test
  78.     public void readWriteByte3PositiveMax() throws IOException
  79.     {
  80.         readWriteTest((1L << 20) - 1, 3);
  81.     }

  82.     @Test
  83.     public void readWriteByte4NegativeMin() throws IOException
  84.     {
  85.         readWriteTest(-(1L << 20), 4);
  86.     }

  87.     @Test
  88.     public void readWriteByte4PositiveMin() throws IOException
  89.     {
  90.         readWriteTest((1L << 20), 4);
  91.     }

  92.     @Test
  93.     public void readWriteByte4NegativeMax() throws IOException
  94.     {
  95.         readWriteTest(-(1L << 27) + 1, 4);
  96.     }

  97.     @Test
  98.     public void readWriteByte4PositiveMax() throws IOException
  99.     {
  100.         readWriteTest((1L << 27) - 1, 4);
  101.     }

  102.     @Test
  103.     public void readWriteByte5NegativeMin() throws IOException
  104.     {
  105.         readWriteTest(-(1L << 27), 5);
  106.     }

  107.     @Test
  108.     public void readWriteByte5PositiveMin() throws IOException
  109.     {
  110.         readWriteTest((1L << 27), 5);
  111.     }

  112.     @Test
  113.     public void readWriteByte5NegativeMax() throws IOException
  114.     {
  115.         readWriteTest(-(1L << 34) + 1, 5);
  116.     }

  117.     @Test
  118.     public void readWriteByte5PositiveMax() throws IOException
  119.     {
  120.         readWriteTest((1L << 34) - 1, 5);
  121.     }

  122.     @Test
  123.     public void readWriteByte6NegativeMin() throws IOException
  124.     {
  125.         readWriteTest(-(1L << 34), 6);
  126.     }

  127.     @Test
  128.     public void readWriteByte6PositiveMin() throws IOException
  129.     {
  130.         readWriteTest((1L << 34), 6);
  131.     }

  132.     @Test
  133.     public void readWriteByte6NegativeMax() throws IOException
  134.     {
  135.         readWriteTest(-(1L << 41) + 1, 6);
  136.     }

  137.     @Test
  138.     public void readWriteByte6PositiveMax() throws IOException
  139.     {
  140.         readWriteTest((1L << 41) - 1, 6);
  141.     }

  142.     @Test
  143.     public void readWriteByte7NegativeMin() throws IOException
  144.     {
  145.         readWriteTest(-(1L << 41), 7);
  146.     }

  147.     @Test
  148.     public void readWriteByte7PositiveMin() throws IOException
  149.     {
  150.         readWriteTest((1L << 41), 7);
  151.     }

  152.     @Test
  153.     public void readWriteByte7NegativeMax() throws IOException
  154.     {
  155.         readWriteTest(-(1L << 48) + 1, 7);
  156.     }

  157.     @Test
  158.     public void readWriteByte7PositiveMax() throws IOException
  159.     {
  160.         readWriteTest((1L << 48) - 1, 7);
  161.     }

  162.     @Test
  163.     public void readWriteByte8NegativeMin() throws IOException
  164.     {
  165.         readWriteTest(-(1L << 48), 8);
  166.     }

  167.     @Test
  168.     public void readWriteByte8PositiveMin() throws IOException
  169.     {
  170.         readWriteTest((1L << 48), 8);
  171.     }

  172.     @Test
  173.     public void readWriteByte8NegativeMax() throws IOException
  174.     {
  175.         readWriteTest(-(1L << 55) + 1, 8);
  176.     }

  177.     @Test
  178.     public void readWriteByte8PositiveMax() throws IOException
  179.     {
  180.         readWriteTest((1L << 55) - 1, 8);
  181.     }

  182.     @Test
  183.     public void readWriteByte9NegativeMin() throws IOException
  184.     {
  185.         readWriteTest(-(1L << 55), 9);
  186.     }

  187.     @Test
  188.     public void readWriteByte9PositiveMin() throws IOException
  189.     {
  190.         readWriteTest((1L << 55), 9);
  191.     }

  192.     @Test
  193.     public void readWriteByte9NegativeMax() throws IOException
  194.     {
  195.         readWriteTest(-(1L << 63) + 1, 9);
  196.     }

  197.     @Test
  198.     public void readWriteByte9PositiveMax() throws IOException
  199.     {
  200.         readWriteTest((1L << 63) - 1, 9);
  201.     }

  202.     private void readWriteTest(long value, int expectedNumBytes) throws IOException
  203.     {
  204.         try (final ByteArrayBitStreamWriter writer = new ByteArrayBitStreamWriter())
  205.         {
  206.             writer.writeVarInt(value);
  207.             assertEquals(0, writer.getBitPosition() % 8);
  208.             assertEquals(expectedNumBytes, writer.getBytePosition());
  209.             final byte[] buffer = writer.toByteArray();
  210.             try (final ByteArrayBitStreamReader reader = new ByteArrayBitStreamReader(buffer))
  211.             {
  212.                 final long readValue = reader.readVarInt();
  213.                 assertEquals(value, readValue);
  214.             }
  215.         }
  216.     }
  217. }