more tests

This commit is contained in:
Greg Wilkins 2014-06-04 12:05:31 +02:00
parent 953655af31
commit d19eef3403
6 changed files with 152 additions and 5 deletions

View File

@ -18,6 +18,11 @@
<artifactId>jetty-util</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-test-helper</artifactId>

View File

@ -396,9 +396,23 @@ public class Huffman
return out.toString();
}
static public void encode(ByteBuffer buffer,String s) throws IOException
static public int octetsNeeded(String s)
{
int needed=0;
int len = s.length();
for (int i=0;i<len;i++)
{
char c=s.charAt(i);
if (c>=128)
throw new IllegalArgumentException();
needed += CODES[c][1];
}
return (needed+7) / 8;
}
static public void encode(ByteBuffer buffer,String s)
{
long current = 0;
int n = 0;

View File

@ -22,7 +22,19 @@ import java.nio.ByteBuffer;
public class NBitInteger
{
public static int octectsNeeded(int n,int i)
{
int nbits = 0xFF >>> (8 - n);
i=i-nbits;
if (i<0)
return n==8?1:0;
if (i==0)
return n==8?2:1;
int lz=Integer.numberOfLeadingZeros(i);
int log=32-lz;
return (log+6)/7;
}
public static void encode(ByteBuffer buf, int n, int i)
{
if (n==8)
@ -86,7 +98,7 @@ public class NBitInteger
{
int nbits = 0xFF >>> (8 - n);
int i=buf.get(buf.position()-n==8?0:1)&nbits;
int i=buf.get(buf.position()-(n==8?0:1))&nbits;
if (i == nbits)
{

View File

@ -38,6 +38,7 @@ public class HuffmanTest
{"D.6.2te","abdd97ff","gzip"},
{"D.4.2cookie","e0d6cf9f6e8f9fd3e5f6fa76fefd3c7edf9eff1f2f0f3cfe9f6fcf7f8f879f61ad4f4cc9a973a2200ec3725e18b1b74e3f","foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"},
{"D.6.2date","d6dbb29884de2a718805062098513111b56ba3","Mon, 21 Oct 2013 20:13:22 GMT"},
{"404","abdd97ff","404"},
};
@Test

View File

@ -18,6 +18,8 @@
package org.eclipse.jetty.hpack;
import static org.junit.Assert.assertEquals;
import java.nio.ByteBuffer;
import org.eclipse.jetty.util.BufferUtil;
@ -28,6 +30,84 @@ import org.junit.Test;
public class NBitIntegerTest
{
@Test
public void testOctetsNeeded()
{
assertEquals(0,NBitInteger.octectsNeeded(5,10));
assertEquals(2,NBitInteger.octectsNeeded(5,1337));
assertEquals(1,NBitInteger.octectsNeeded(8,42));
assertEquals(0,NBitInteger.octectsNeeded(6,62));
assertEquals(1,NBitInteger.octectsNeeded(6,63));
assertEquals(1,NBitInteger.octectsNeeded(6,64));
assertEquals(2,NBitInteger.octectsNeeded(6,63+0x00+0x80*0x01));
assertEquals(3,NBitInteger.octectsNeeded(6,63+0x00+0x80*0x80));
assertEquals(4,NBitInteger.octectsNeeded(6,63+0x00+0x80*0x80*0x80));
}
@Test
public void testEncode()
{
testEncode(6,0,"00");
testEncode(6,1,"01");
testEncode(6,62,"3e");
testEncode(6,63,"3f00");
testEncode(6,63+1,"3f01");
testEncode(6,63+0x7e,"3f7e");
testEncode(6,63+0x7f,"3f7f");
testEncode(6,63+0x00+0x80*0x01,"3f8001");
testEncode(6,63+0x01+0x80*0x01,"3f8101");
testEncode(6,63+0x7f+0x80*0x01,"3fFf01");
testEncode(6,63+0x00+0x80*0x02,"3f8002");
testEncode(6,63+0x01+0x80*0x02,"3f8102");
testEncode(6,63+0x7f+0x80*0x7f,"3fFf7f");
testEncode(6,63+0x00+0x80*0x80, "3f808001");
testEncode(6,63+0x7f+0x80*0x80*0x7f,"3fFf807f");
testEncode(6,63+0x00+0x80*0x80*0x80,"3f80808001");
}
public void testEncode(int n,int i,String expected)
{
ByteBuffer buf = BufferUtil.allocate(16);
int p=BufferUtil.flipToFill(buf);
buf.put((byte)0x00);
NBitInteger.encode(buf,n,i);
BufferUtil.flipToFlush(buf,p);
String r=TypeUtil.toHexString(BufferUtil.toArray(buf));
assertEquals(expected,r);
assertEquals(expected.length()/2,1+NBitInteger.octectsNeeded(n,i));
}
@Test
public void testDecode()
{
testDecode(6,0,"00");
testDecode(6,1,"01");
testDecode(6,62,"3e");
testDecode(6,63,"3f00");
testDecode(6,63+1,"3f01");
testDecode(6,63+0x7e,"3f7e");
testDecode(6,63+0x7f,"3f7f");
testDecode(6,63+0x80,"3f8001");
testDecode(6,63+0x81,"3f8101");
testDecode(6,63+0x7f+0x80*0x01,"3fFf01");
testDecode(6,63+0x00+0x80*0x02,"3f8002");
testDecode(6,63+0x01+0x80*0x02,"3f8102");
testDecode(6,63+0x7f+0x80*0x7f,"3fFf7f");
testDecode(6,63+0x00+0x80*0x80, "3f808001");
testDecode(6,63+0x7f+0x80*0x80*0x7f,"3fFf807f");
testDecode(6,63+0x00+0x80*0x80*0x80,"3f80808001");
}
public void testDecode(int n,int expected,String encoded)
{
ByteBuffer buf = ByteBuffer.wrap(TypeUtil.fromHexString(encoded));
buf.position(n==8?0:1);
Assert.assertEquals(expected,NBitInteger.decode(buf,n));
}
@Test
public void testEncodeExampleD_1_1()
{
@ -40,7 +120,7 @@ public class NBitIntegerTest
String r=TypeUtil.toHexString(BufferUtil.toArray(buf));
Assert.assertEquals("77Ea",r);
assertEquals("77Ea",r);
}

View File

@ -85,5 +85,40 @@ public class HttpField
return false;
}
@Override
public boolean equals(Object o)
{
if (o==this)
return true;
if (!(o instanceof HttpField))
return false;
HttpField field=(HttpField)o;
if (_header!=field.getHeader())
return false;
if (!_name.equalsIgnoreCase(field.getName()))
return false;
if (_value==null && field.getValue()!=null)
return false;
if (!_value.equals(field.getValue()))
return false;
return true;
}
@Override
public int hashCode()
{
if (_header==null)
{
int hash=13;
int len = _name.length();
for (int i = 0; i < len; i++)
{
char c = Character.toUpperCase(_name.charAt(i));
hash = 31 * hash + c;
}
return _value.hashCode() ^ hash;
}
return _value.hashCode() ^ _header.hashCode();
}
}