Search in sources :

Example 21 with BInteger

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

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

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

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

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

the class VectorTest method testIsEmpty.

@Test(description = "Test case for testing isEmpty() function")
public void testIsEmpty() {
    final int booleanTrue = 1;
    int vectorSize = 10;
    boolean[] expectedVals = new boolean[] { false, true, true };
    BValue[] returns = BRunUtil.invoke(compileResult, "testIsEmpty", new BValue[] { new BInteger(vectorSize) });
    Assert.assertNotNull(returns);
    BBooleanArray isEmptyVals = (BBooleanArray) returns[0];
    for (int i = 0; i < expectedVals.length; i++) {
        Assert.assertEquals(isEmptyVals.get(i) == booleanTrue, expectedVals[i]);
    }
}
Also used : BBooleanArray(org.ballerinalang.model.values.BBooleanArray) BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

BInteger (org.ballerinalang.model.values.BInteger)364 BValue (org.ballerinalang.model.values.BValue)324 Test (org.testng.annotations.Test)305 BString (org.ballerinalang.model.values.BString)91 BFloat (org.ballerinalang.model.values.BFloat)55 BStruct (org.ballerinalang.model.values.BStruct)33 BBoolean (org.ballerinalang.model.values.BBoolean)24 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)18 CompileResult (org.ballerinalang.launcher.util.CompileResult)12 BBlob (org.ballerinalang.model.values.BBlob)11 BeforeTest (org.testng.annotations.BeforeTest)11 BIntArray (org.ballerinalang.model.values.BIntArray)9 BMap (org.ballerinalang.model.values.BMap)9 BRefType (org.ballerinalang.model.values.BRefType)8 BStringArray (org.ballerinalang.model.values.BStringArray)8 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)8 BType (org.ballerinalang.model.types.BType)6 BStructType (org.ballerinalang.model.types.BStructType)4 BJSON (org.ballerinalang.model.values.BJSON)4 UnsupportedFieldTypeException (org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException)4