Search in sources :

Example 11 with BStruct

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

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

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

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

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

Aggregations

BStruct (org.ballerinalang.model.values.BStruct)460 BValue (org.ballerinalang.model.values.BValue)187 Test (org.testng.annotations.Test)161 BString (org.ballerinalang.model.values.BString)131 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)53 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)39 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)37 BInteger (org.ballerinalang.model.values.BInteger)33 BMap (org.ballerinalang.model.values.BMap)29 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)26 BStructType (org.ballerinalang.model.types.BStructType)25 IOException (java.io.IOException)23 BBoolean (org.ballerinalang.model.values.BBoolean)23 BJSON (org.ballerinalang.model.values.BJSON)22 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)21 StructInfo (org.ballerinalang.util.codegen.StructInfo)21 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)20 File (java.io.File)17 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)17 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)16