use of org.ballerinalang.model.values.BString in project ballerina by ballerina-lang.
the class CachingTest method testRemove.
@Test
public void testRemove() {
String cacheName = "userCache";
int expiryTime = 20000;
int capacity = 10;
float evictionFactor = 0.1f;
String key = "Ballerina";
String value = "Rocks";
BValue[] args = new BValue[6];
args[0] = new BString(cacheName);
args[1] = new BInteger(expiryTime);
args[2] = new BInteger(capacity);
args[3] = new BFloat(evictionFactor);
args[4] = new BString(key);
args[5] = new BString(value);
BValue[] returns = BRunUtil.invoke(compileResult, "testRemove", args);
Assert.assertTrue(returns.length == 1);
Assert.assertEquals(((BInteger) returns[0]).intValue(), 0);
}
use of org.ballerinalang.model.values.BString in project ballerina by ballerina-lang.
the class CachingTest method testCacheEviction1.
@Test
public void testCacheEviction1() {
String cacheName = "userCache";
int expiryTime = 20000;
int capacity = 10;
float evictionFactor = 0.2f;
BValue[] args = new BValue[4];
args[0] = new BString(cacheName);
args[1] = new BInteger(expiryTime);
args[2] = new BInteger(capacity);
args[3] = new BFloat(evictionFactor);
BValue[] returns = BRunUtil.invoke(compileResult, "testCacheEviction1", args);
Assert.assertTrue(returns.length == 2);
Assert.assertTrue(returns[0] instanceof BStringArray);
Assert.assertEquals(((BStringArray) returns[0]).get(0), "C");
Assert.assertEquals(((BStringArray) returns[0]).get(1), "D");
Assert.assertEquals(((BStringArray) returns[0]).get(2), "E");
Assert.assertEquals(((BStringArray) returns[0]).get(3), "F");
Assert.assertEquals(((BStringArray) returns[0]).get(4), "G");
Assert.assertEquals(((BStringArray) returns[0]).get(5), "H");
Assert.assertEquals(((BStringArray) returns[0]).get(6), "I");
Assert.assertEquals(((BStringArray) returns[0]).get(7), "J");
Assert.assertEquals(((BStringArray) returns[0]).get(8), "K");
Assert.assertEquals(((BInteger) returns[1]).intValue(), 9);
}
use of org.ballerinalang.model.values.BString in project ballerina by ballerina-lang.
the class CachingTest method testCacheEviction4.
@Test
public void testCacheEviction4() {
String cacheName = "userCache";
int expiryTime = 20000;
int capacity = 5;
float evictionFactor = 0.2f;
BValue[] args = new BValue[4];
args[0] = new BString(cacheName);
args[1] = new BInteger(expiryTime);
args[2] = new BInteger(capacity);
args[3] = new BFloat(evictionFactor);
BValue[] returns = BRunUtil.invoke(compileResult, "testCacheEviction4", args);
Assert.assertTrue(returns.length == 2);
Assert.assertTrue(returns[0] instanceof BStringArray);
Assert.assertEquals(((BStringArray) returns[0]).get(0), "A");
Assert.assertEquals(((BStringArray) returns[0]).get(1), "B");
Assert.assertEquals(((BStringArray) returns[0]).get(2), "C");
Assert.assertEquals(((BStringArray) returns[0]).get(3), "D");
Assert.assertEquals(((BStringArray) returns[0]).get(4), "F");
Assert.assertEquals(((BInteger) returns[1]).intValue(), 5);
}
use of org.ballerinalang.model.values.BString in project ballerina by ballerina-lang.
the class CPU method extractValues.
@SuppressWarnings("rawtypes")
private static BRefType[] extractValues(WorkerData data, BType[] types, int[] regs) {
BRefType[] result = new BRefType[types.length];
for (int i = 0; i < regs.length; i++) {
BType paramType = types[i];
int argReg = regs[i];
switch(paramType.getTag()) {
case TypeTags.INT_TAG:
result[i] = new BInteger(data.longRegs[argReg]);
break;
case TypeTags.FLOAT_TAG:
result[i] = new BFloat(data.doubleRegs[argReg]);
break;
case TypeTags.STRING_TAG:
result[i] = new BString(data.stringRegs[argReg]);
break;
case TypeTags.BOOLEAN_TAG:
result[i] = new BBoolean(data.intRegs[argReg] > 0);
break;
case TypeTags.BLOB_TAG:
result[i] = new BBlob(data.byteRegs[argReg]);
break;
default:
result[i] = data.refRegs[argReg];
}
}
return result;
}
use of org.ballerinalang.model.values.BString in project ballerina by ballerina-lang.
the class CPU method convertStructToMap.
private static void convertStructToMap(WorkerExecutionContext ctx, int[] operands, WorkerData sf) {
int i = operands[0];
int j = operands[1];
BStruct bStruct = (BStruct) sf.refRegs[i];
if (bStruct == null) {
handleNullRefError(ctx);
return;
}
int longRegIndex = -1;
int doubleRegIndex = -1;
int stringRegIndex = -1;
int booleanRegIndex = -1;
int blobRegIndex = -1;
int refRegIndex = -1;
BStructType.StructField[] structFields = (bStruct.getType()).getStructFields();
BMap<String, BValue> map = BTypes.typeMap.getEmptyValue();
for (BStructType.StructField structField : structFields) {
String key = structField.getFieldName();
BType fieldType = structField.getFieldType();
switch(fieldType.getTag()) {
case TypeTags.INT_TAG:
map.put(key, new BInteger(bStruct.getIntField(++longRegIndex)));
break;
case TypeTags.FLOAT_TAG:
map.put(key, new BFloat(bStruct.getFloatField(++doubleRegIndex)));
break;
case TypeTags.STRING_TAG:
map.put(key, new BString(bStruct.getStringField(++stringRegIndex)));
break;
case TypeTags.BOOLEAN_TAG:
map.put(key, new BBoolean(bStruct.getBooleanField(++booleanRegIndex) == 1));
break;
case TypeTags.BLOB_TAG:
map.put(key, new BBlob(bStruct.getBlobField(++blobRegIndex)));
break;
default:
BValue value = bStruct.getRefField(++refRegIndex);
map.put(key, value == null ? null : value.copy());
}
}
sf.refRegs[j] = map;
}
Aggregations