1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-10-23 01:47:38 +00:00

Fix binary handles reading in multiples of 8192

This commit is contained in:
SquidDev
2018-12-31 09:41:58 +00:00
parent ee3347afbd
commit 325459e336
8 changed files with 196 additions and 12 deletions

View File

@@ -54,6 +54,17 @@ public class ObjectWrapper implements ILuaContext
}
}
@SuppressWarnings( "unchecked" )
public <T> T callOf( String name, Object... args ) throws LuaException
{
return (T) call( name, args )[0];
}
public <T> T callOf( Class<T> klass, String name, Object... args ) throws LuaException
{
return klass.cast( call( name, args )[0] );
}
@Nonnull
@Override
public Object[] pullEvent( @Nullable String filter )

View File

@@ -0,0 +1,68 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2018. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.core.apis.handles;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.core.apis.ObjectWrapper;
import org.junit.Test;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
public class BinaryReadableHandleTest
{
@Test
public void testReadChar() throws LuaException
{
ObjectWrapper wrapper = fromLength( 5 );
assertEquals( 'A', (int) wrapper.callOf( Integer.class, "read" ) );
}
@Test
public void testReadShortComplete() throws LuaException
{
ObjectWrapper wrapper = fromLength( 10 );
assertEquals( 5, wrapper.<byte[]>callOf( "read", 5 ).length );
}
@Test
public void testReadShortPartial() throws LuaException
{
ObjectWrapper wrapper = fromLength( 5 );
assertEquals( 5, wrapper.<byte[]>callOf( "read", 10 ).length );
}
@Test
public void testReadLongComplete() throws LuaException
{
ObjectWrapper wrapper = fromLength( 10000 );
assertEquals( 9000, wrapper.<byte[]>callOf( "read", 9000 ).length );
}
@Test
public void testReadLongPartial() throws LuaException
{
ObjectWrapper wrapper = fromLength( 10000 );
assertEquals( 10000, wrapper.<byte[]>callOf( "read", 11000 ).length );
}
@Test
public void testReadLongPartialSmaller() throws LuaException
{
ObjectWrapper wrapper = fromLength( 1000 );
assertEquals( 1000, wrapper.<byte[]>callOf( "read", 11000 ).length );
}
private static ObjectWrapper fromLength( int length )
{
byte[] input = new byte[length];
Arrays.fill( input, (byte) 'A' );
return new ObjectWrapper( new BinaryReadableHandle( new ArrayByteChannel( input ) ) );
}
}

View File

@@ -0,0 +1,70 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2018. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.core.apis.handles;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.core.apis.ObjectWrapper;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
public class EncodedReadableHandleTest
{
@Test
public void testReadChar() throws LuaException
{
ObjectWrapper wrapper = fromLength( 5 );
assertEquals( "A", wrapper.callOf( "read" ) );
}
@Test
public void testReadShortComplete() throws LuaException
{
ObjectWrapper wrapper = fromLength( 10 );
assertEquals( "AAAAA", wrapper.callOf( "read", 5 ) );
}
@Test
public void testReadShortPartial() throws LuaException
{
ObjectWrapper wrapper = fromLength( 5 );
assertEquals( "AAAAA", wrapper.callOf( "read", 10 ) );
}
@Test
public void testReadLongComplete() throws LuaException
{
ObjectWrapper wrapper = fromLength( 10000 );
assertEquals( 9000, wrapper.<String>callOf( "read", 9000 ).length() );
}
@Test
public void testReadLongPartial() throws LuaException
{
ObjectWrapper wrapper = fromLength( 10000 );
assertEquals( 10000, wrapper.<String>callOf( "read", 11000 ).length() );
}
@Test
public void testReadLongPartialSmaller() throws LuaException
{
ObjectWrapper wrapper = fromLength( 1000 );
assertEquals( 1000, wrapper.<String>callOf( "read", 11000 ).length() );
}
private static ObjectWrapper fromLength( int length )
{
char[] input = new char[length];
Arrays.fill( input, 'A' );
return new ObjectWrapper( new EncodedReadableHandle( new BufferedReader( new CharArrayReader( input ) ) ) );
}
}