mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-12 02:10:30 +00:00
parent
87aa839b60
commit
53477fd3a1
@ -24,7 +24,7 @@ public final class IntCache<T>
|
||||
{
|
||||
if( index < 0 ) throw new IllegalArgumentException( "index < 0" );
|
||||
|
||||
if( index <= cache.length )
|
||||
if( index < cache.length )
|
||||
{
|
||||
T current = (T) cache[index];
|
||||
if( current != null ) return current;
|
||||
@ -32,7 +32,7 @@ public final class IntCache<T>
|
||||
|
||||
synchronized( this )
|
||||
{
|
||||
if( index > cache.length ) cache = Arrays.copyOf( cache, Math.max( cache.length * 2, index ) );
|
||||
if( index >= cache.length ) cache = Arrays.copyOf( cache, Math.max( cache.length * 2, index + 1 ) );
|
||||
T current = (T) cache[index];
|
||||
if( current == null ) cache[index] = current = factory.apply( index );
|
||||
return current;
|
||||
|
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2020. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
|
||||
package dan200.computercraft.core.asm;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class IntCacheTest
|
||||
{
|
||||
@Test
|
||||
public void testCache()
|
||||
{
|
||||
IntCache<Object> c = new IntCache<>( i -> new Object() );
|
||||
assertEquals( c.get( 0 ), c.get( 0 ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMassive()
|
||||
{
|
||||
assertEquals( 40, new IntCache<>( i -> i ).get( 40 ) );
|
||||
}
|
||||
}
|
@ -78,6 +78,15 @@ public class MethodTest
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMany()
|
||||
{
|
||||
ComputerBootstrap.run(
|
||||
"assert(many.method_0)\n" +
|
||||
"assert(many.method_39)",
|
||||
x -> x.addApi( new ManyMethods() ), 50 );
|
||||
}
|
||||
|
||||
public static class MainThread implements ILuaAPI, IPeripheral
|
||||
{
|
||||
public final String thread = Thread.currentThread().getName();
|
||||
@ -205,4 +214,29 @@ public class MethodTest
|
||||
return this == other;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ManyMethods implements IDynamicLuaObject, ILuaAPI
|
||||
{
|
||||
@Nonnull
|
||||
@Override
|
||||
public String[] getMethodNames()
|
||||
{
|
||||
String[] methods = new String[40];
|
||||
for( int i = 0; i < methods.length; i++ ) methods[i] = "method_" + i;
|
||||
return methods;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public MethodResult callMethod( @Nonnull ILuaContext context, int method, @Nonnull IArguments arguments ) throws LuaException
|
||||
{
|
||||
return MethodResult.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getNames()
|
||||
{
|
||||
return new String[] { "many" };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user