### Question:
VarInt { public static int decodeInt(InputStream buf) throws IOException { long r = decodeLong(buf); if (r < 0 || r >= 1L << 32) { throw new IOException("var int overflow " + r); } return (int) r; }  static void encode(int v, OutputStream buf); static void encode(long v, OutputStream buf); static int decodeInt(InputStream buf); static long decodeLong(InputStream buf); static int getLength(int v); static int getLength(long v);  }### Answer:
@Test public void decodeThrowsExceptionForIntOverflow() throws IOException { byte[] encoded = encodeLong(1L << 32); thrown.expect(IOException.class); decodeInt(encoded); }
@Test public void decodeThrowsExceptionForIntUnderflow() throws IOException { byte[] encoded = encodeLong(-1); thrown.expect(IOException.class); decodeInt(encoded); }
@Test public void endOfFileThrowsException() throws Exception { ByteArrayInputStream inStream = new ByteArrayInputStream(new byte[0]); thrown.expect(EOFException.class); VarInt.decodeInt(inStream); }
@Test public void unterminatedThrowsException() throws Exception { byte[] e = encodeLong(Long.MAX_VALUE); byte[] s = new byte[1]; s[0] = e[0]; ByteArrayInputStream inStream = new ByteArrayInputStream(s); thrown.expect(IOException.class); VarInt.decodeInt(inStream); }