Search in sources :

Example 31 with BString

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);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BFloat(org.ballerinalang.model.values.BFloat) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 32 with BString

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);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BFloat(org.ballerinalang.model.values.BFloat) BString(org.ballerinalang.model.values.BString) BStringArray(org.ballerinalang.model.values.BStringArray) Test(org.testng.annotations.Test)

Example 33 with BString

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);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BFloat(org.ballerinalang.model.values.BFloat) BString(org.ballerinalang.model.values.BString) BStringArray(org.ballerinalang.model.values.BStringArray) Test(org.testng.annotations.Test)

Example 34 with BString

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;
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) BType(org.ballerinalang.model.types.BType) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BFloat(org.ballerinalang.model.values.BFloat) BBoolean(org.ballerinalang.model.values.BBoolean) BBlob(org.ballerinalang.model.values.BBlob)

Example 35 with BString

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;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BBoolean(org.ballerinalang.model.values.BBoolean) BString(org.ballerinalang.model.values.BString) BStructType(org.ballerinalang.model.types.BStructType) BType(org.ballerinalang.model.types.BType) BFloat(org.ballerinalang.model.values.BFloat) BBlob(org.ballerinalang.model.values.BBlob)

Aggregations

BString (org.ballerinalang.model.values.BString)418 BValue (org.ballerinalang.model.values.BValue)339 Test (org.testng.annotations.Test)321 BInteger (org.ballerinalang.model.values.BInteger)77 BStruct (org.ballerinalang.model.values.BStruct)76 BMap (org.ballerinalang.model.values.BMap)37 BBoolean (org.ballerinalang.model.values.BBoolean)36 BFloat (org.ballerinalang.model.values.BFloat)34 BJSON (org.ballerinalang.model.values.BJSON)28 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)21 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)18 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)11 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)11 CompileResult (org.ballerinalang.launcher.util.CompileResult)11 BBlob (org.ballerinalang.model.values.BBlob)11 BStringArray (org.ballerinalang.model.values.BStringArray)11 BType (org.ballerinalang.model.types.BType)10 BIntArray (org.ballerinalang.model.values.BIntArray)9 BRefType (org.ballerinalang.model.values.BRefType)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7