package Util;

import org.hamcrest.CoreMatchers;
import org.junit.Test;

import org.junit.Assert;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * Tests for Util.Uleb128
 */
public class Uleb128Test {
    // TODO(zcd): fuzz testing?
    @Test
    public void readFrom() throws Exception {
        Uleb128 parsed = Uleb128.fromByteStream(
                new ByteArrayInputStream(new byte[] {(byte) 0xe5, (byte) 0x8e, (byte) 0x26}));
        Assert.assertEquals(624485L, parsed.asLong());
    }

    @Test
    public void readFrom_InvalidEmptyBuffer() throws Exception {
        try {
            Uleb128.fromByteStream(new ByteArrayInputStream(new byte[] {}));
            Assert.fail();
        } catch (IOException expected) {
            Assert.assertThat(expected.getMessage(), CoreMatchers.containsString("Unexpected EOF"));
        }
    }

    @Test
    public void readFrom_ByteArrayOverflowsLongType() throws Exception {
        byte[] yuuuge = new byte[] {
                (byte) 0xff,
                (byte) 0xff,
                (byte) 0xff,
                (byte) 0xff,
                (byte) 0xff,
                (byte) 0xff,
                (byte) 0xff,
                (byte) 0xff,
                (byte) 0xff,
                (byte) 0x01,
        };
        try {
            Uleb128.fromByteStream(new ByteArrayInputStream(yuuuge));
            Assert.fail();
        } catch (ArithmeticException expected) {
            Assert.assertThat(expected.getMessage(), CoreMatchers.containsString("exceeds maximum value"));
        }
    }

    @Test
    public void asBytes() throws Exception {
        Assert.assertArrayEquals(
                new byte[] {(byte) 0xe5, (byte) 0x8e, (byte) 0x26}, Uleb128.fromLong(624485L).asBytes());
    }
}
@@ -19,33 +19,637 @@
 ort 
-org.hamcrest.CoreMatchers
+com.pholser.junit.quickcheck.From;%0Aimport com.pholser.junit.quickcheck.Property;%0Aimport com.pholser.junit.quickcheck.generator.GenerationStatus;%0Aimport com.pholser.junit.quickcheck.generator.Generator;%0Aimport com.pholser.junit.quickcheck.generator.GeneratorConfiguration;%0Aimport com.pholser.junit.quickcheck.generator.java.lang.ByteGenerator;%0Aimport com.pholser.junit.quickcheck.generator.java.util.ArrayListGenerator;%0Aimport com.pholser.junit.quickcheck.random.SourceOfRandomness;%0Aimport com.pholser.junit.quickcheck.runner.JUnitQuickcheck;%0Aimport org.hamcrest.CoreMatchers;%0Aimport org.hamcrest.Matchers;%0Aimport org.junit.Assume
 ;%0Aim
@@ -694,16 +694,49 @@
 .Assert;
+%0Aimport org.junit.runner.RunWith;
 %0A%0Aimport
@@ -793,16 +793,461 @@
 ception;
+%0Aimport java.lang.annotation.Retention;%0Aimport java.lang.annotation.Target;%0Aimport java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A%0Aimport static java.lang.annotation.ElementType.ANNOTATION_TYPE;%0Aimport static java.lang.annotation.ElementType.FIELD;%0Aimport static java.lang.annotation.ElementType.PARAMETER;%0Aimport static java.lang.annotation.ElementType.TYPE_USE;%0Aimport static java.lang.annotation.RetentionPolicy.RUNTIME;
 %0A%0A/**%0A *
@@ -1274,16 +1274,48 @@
 128%0A */%0A
+@RunWith(JUnitQuickcheck.class)%0A
 public c
@@ -1513,33 +1513,32 @@
 tream(new byte%5B%5D
- 
 %7B(byte) 0xe5, (b
@@ -1785,17 +1785,16 @@
 w byte%5B%5D
- 
 %7B%7D));%0A  
@@ -2094,17 +2094,16 @@
 w byte%5B%5D
- 
 %7B%0A      
@@ -2797,17 +2797,16 @@
 w byte%5B%5D
- 
 %7B(byte) 
@@ -2881,10 +2881,302 @@
 );%0A    %7D
+%0A%0A    @Property%0A    public void decodeFromEncode(long value) throws Exception %7B%0A        Assume.assumeThat(value, Matchers.greaterThan(0L));%0A        Assert.assertEquals(%0A                value, Uleb128.fromByteStream(new ByteArrayInputStream(Uleb128.fromLong(value).asBytes())).asLong());%0A    %7D
 %0A%7D

Add QuickCheck property tests for decoding and encoding