Search in sources :

Example 51 with BValue

use of org.ballerinalang.model.values.BValue in project ballerina by ballerina-lang.

the class VectorNegativeTest method testInsertIndexOutOfRange.

@Test(description = "Test case for testing insertion of elements outside the bounds of a vector.")
public void testInsertIndexOutOfRange() {
    long[] values = new long[] { 100, 110, 120 };
    // 1 valid index and 2 invalid indices
    long[] indices = new long[] { 3, 12, -1 };
    int vectorSize = 10;
    long[] expectedFinalValues = new long[] { 10, 20, 30, 100, 40, 50, 60, 70, 80, 90, 100 };
    String[] expectedErrMsgs = new String[] { "Index out of range: 12", "Index out of range: -1" };
    BValue[] returns = BRunUtil.invoke(compileResult, "testInsertIndexOutOfRange", new BValue[] { buildIntArray(values), buildIntArray(indices), new BInteger(vectorSize) });
    Assert.assertNotNull(returns);
    BStruct vector = (BStruct) returns[0];
    BRefValueArray vectorEntries = (BRefValueArray) vector.getRefField(0);
    long finalVectorSize = vector.getIntField(0);
    Assert.assertEquals(finalVectorSize, vectorSize + 1);
    for (int i = 0; i < expectedFinalValues.length; i++) {
        Assert.assertEquals(vectorEntries.get(i).value(), expectedFinalValues[i]);
    }
    BRefValueArray errors = (BRefValueArray) returns[1];
    // Since there are 3 insertions
    Assert.assertEquals(errors.size(), 3);
    // Since first insertion is valid, the error is null
    Assert.assertNull(errors.get(0).value());
    Assert.assertTrue(errors.get(1).stringValue().contains(expectedErrMsgs[0]));
    Assert.assertTrue(errors.get(2).stringValue().contains(expectedErrMsgs[1]));
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BeforeTest(org.testng.annotations.BeforeTest) Test(org.testng.annotations.Test)

Example 52 with BValue

use of org.ballerinalang.model.values.BValue in project ballerina by ballerina-lang.

the class VectorTest method testReplace.

@Test(description = "Test case for testing replacement of elements in a vector.")
public void testReplace() {
    long[] values = new long[] { 100, 110, 120 };
    long[] indices = new long[] { 3, 4, 9 };
    int vectorSize = 10;
    long[] expectedFinalValues = new long[] { 10, 20, 30, 100, 110, 60, 70, 80, 90, 120 };
    BValue[] returns = BRunUtil.invoke(compileResult, "testReplace", new BValue[] { buildIntArray(values), buildIntArray(indices), new BInteger(vectorSize) });
    Assert.assertNotNull(returns);
    BStruct vector = (BStruct) returns[0];
    BRefValueArray vectorEntries = (BRefValueArray) vector.getRefField(0);
    long finalVectorSize = vector.getIntField(0);
    Assert.assertEquals(finalVectorSize, vectorSize);
    for (int i = 0; i < expectedFinalValues.length; i++) {
        Assert.assertEquals(vectorEntries.get(i).value(), expectedFinalValues[i]);
    }
    BRefValueArray replacedVals = (BRefValueArray) returns[1];
    Assert.assertEquals(replacedVals.size(), values.length);
    Assert.assertEquals(((BInteger) replacedVals.get(0)).intValue(), 40);
    Assert.assertEquals(((BInteger) replacedVals.get(1)).intValue(), 50);
    Assert.assertEquals(((BInteger) replacedVals.get(2)).intValue(), 100);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 53 with BValue

use of org.ballerinalang.model.values.BValue in project ballerina by ballerina-lang.

the class VectorTest method testClear.

@Test(description = "Test case for testing clearing of the vector.")
public void testClear() {
    long initialSize = 10;
    BValue[] returns = BRunUtil.invoke(compileResult, "testClear", new BValue[] { new BInteger(initialSize) });
    Assert.assertNotNull(returns);
    BStruct vector = (BStruct) returns[0];
    BRefValueArray vectorEntries = (BRefValueArray) vector.getRefField(0);
    long vectorSize = vector.getIntField(0);
    Assert.assertEquals(vectorSize, 0);
    // Since the array is reinitialized in clear(), this should be 0
    Assert.assertEquals(vectorEntries.size(), 0);
    BStruct vectorRef = (BStruct) returns[1];
    BRefValueArray vectorEntriesRef = (BRefValueArray) vectorRef.getRefField(0);
    long vectorRefSize = vectorRef.getIntField(0);
    Assert.assertEquals(vectorRefSize, 0);
    // Since the second return value is a reference to the original vector, this should also be empty
    Assert.assertEquals(vectorEntriesRef.size(), 0);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 54 with BValue

use of org.ballerinalang.model.values.BValue in project ballerina by ballerina-lang.

the class VectorTest method testInsert.

@Test(description = "Test case for testing insertion of elements to a vector.")
public void testInsert() {
    long[] values = new long[] { 0, 100, 110, 120, 130 };
    long[] indices = new long[] { 0, 3, 4, 5, 13 };
    int vectorSize = 10;
    BValue[] returns = BRunUtil.invoke(compileResult, "testInsert", new BValue[] { buildIntArray(values), buildIntArray(indices), new BInteger(vectorSize) });
    Assert.assertNotNull(returns);
    BStruct vector = (BStruct) returns[0];
    BRefValueArray vectorEntries = (BRefValueArray) vector.getRefField(0);
    long finalVectorSize = vector.getIntField(0);
    Assert.assertEquals(finalVectorSize, vectorSize + values.length);
    for (int i = 0; i < indices.length; i++) {
        Assert.assertEquals(vectorEntries.get(indices[i]).value(), values[i]);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 55 with BValue

use of org.ballerinalang.model.values.BValue in project ballerina by ballerina-lang.

the class VectorTest method testAdd.

@Test(description = "Test case for testing addition of elements to a vector.")
public void testAdd() {
    String[] args = new String[] { "Foo", "Bar", "Ballerina" };
    BValue[] returns = BRunUtil.invoke(compileResult, "testAdd", new BValue[] { buildStringArray(args) });
    Assert.assertNotNull(returns);
    BStruct vector = (BStruct) returns[0];
    BRefValueArray vectorEntries = (BRefValueArray) vector.getRefField(0);
    long vectorSize = vector.getIntField(0);
    Assert.assertEquals(vectorSize, args.length);
    for (int i = 0; i < args.length; i++) {
        Assert.assertEquals(vectorEntries.get(i).value(), args[i]);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

BValue (org.ballerinalang.model.values.BValue)1043 Test (org.testng.annotations.Test)923 BString (org.ballerinalang.model.values.BString)437 BInteger (org.ballerinalang.model.values.BInteger)323 BStruct (org.ballerinalang.model.values.BStruct)188 BFloat (org.ballerinalang.model.values.BFloat)118 BJSON (org.ballerinalang.model.values.BJSON)112 BBoolean (org.ballerinalang.model.values.BBoolean)79 CompileResult (org.ballerinalang.launcher.util.CompileResult)60 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)45 BMap (org.ballerinalang.model.values.BMap)43 BXMLItem (org.ballerinalang.model.values.BXMLItem)42 BXML (org.ballerinalang.model.values.BXML)40 BStringArray (org.ballerinalang.model.values.BStringArray)30 BIntArray (org.ballerinalang.model.values.BIntArray)25 BBlob (org.ballerinalang.model.values.BBlob)23 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)19 BeforeTest (org.testng.annotations.BeforeTest)19 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)19 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)16