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]));
}
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]));
}
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);
}
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);
}
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]);
}
}
Aggregations