Search in sources :

Example 6 with BRefValueArray

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

the class VectorNegativeTest method testGetIndexOutOfRange.

@Test(description = "Test case for testing retrieval of elements from outside the bounds of a vector.")
public void testGetIndexOutOfRange() {
    long[] expectedVals = new long[] { 10, 30 };
    long vectorSize = 10;
    long[] indices = new long[] { 0, 2, vectorSize, -1 };
    String[] expectedErrMsgs = new String[] { "Index out of range: 10", "Index out of range: -1" };
    BValue[] returns = BRunUtil.invoke(compileResult, "testGetIndexOutOfRange", new BValue[] { buildIntArray(indices), new BInteger(vectorSize) });
    Assert.assertNotNull(returns);
    BRefValueArray returnedVals = (BRefValueArray) returns[0];
    Assert.assertEquals(returnedVals.size(), expectedVals.length);
    for (int i = 0; i < expectedVals.length; i++) {
        Assert.assertEquals(((BInteger) returnedVals.get(i)).intValue(), expectedVals[i]);
    }
    BRefValueArray errors = (BRefValueArray) returns[1];
    Assert.assertEquals(errors.size(), expectedErrMsgs.length);
    for (int i = 0; i < expectedErrMsgs.length; i++) {
        Assert.assertTrue(errors.get(i).stringValue().contains(expectedErrMsgs[i]));
    }
}
Also used : 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 7 with BRefValueArray

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

the class VectorNegativeTest method testReplaceIndexOutOfRange.

@Test(description = "Test case for testing replacement of elements outside the bounds of a vector.")
public void testReplaceIndexOutOfRange() {
    long[] values = new long[] { 100, 110, 120 };
    // 1 valid index and 2 invalid indices
    long[] indices = new long[] { 3, 10, -1 };
    int vectorSize = 10;
    long[] expectedFinalValues = new long[] { 10, 20, 30, 100, 50, 60, 70, 80, 90, 100 };
    String[] expectedErrMsgs = new String[] { "Index out of range: 10", "Index out of range: -1" };
    BValue[] returns = BRunUtil.invoke(compileResult, "testReplaceIndexOutOfRange", 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 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 8 with BRefValueArray

use of org.ballerinalang.model.values.BRefValueArray 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 9 with BRefValueArray

use of org.ballerinalang.model.values.BRefValueArray 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 10 with BRefValueArray

use of org.ballerinalang.model.values.BRefValueArray 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)

Aggregations

BRefValueArray (org.ballerinalang.model.values.BRefValueArray)83 BValue (org.ballerinalang.model.values.BValue)45 Test (org.testng.annotations.Test)40 BStruct (org.ballerinalang.model.values.BStruct)37 BString (org.ballerinalang.model.values.BString)22 BInteger (org.ballerinalang.model.values.BInteger)18 BXMLSequence (org.ballerinalang.model.values.BXMLSequence)9 BeforeTest (org.testng.annotations.BeforeTest)9 BIntArray (org.ballerinalang.model.values.BIntArray)7 BMap (org.ballerinalang.model.values.BMap)7 BStringArray (org.ballerinalang.model.values.BStringArray)7 BXML (org.ballerinalang.model.values.BXML)7 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)7 HashMap (java.util.HashMap)6 BStructType (org.ballerinalang.model.types.BStructType)6 BRefType (org.ballerinalang.model.values.BRefType)5 SQLDatasource (org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource)5 BBooleanArray (org.ballerinalang.model.values.BBooleanArray)4 BFloatArray (org.ballerinalang.model.values.BFloatArray)4 IOException (java.io.IOException)3