use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.
the class CachingTest method testCacheEviction1.
@Test
public void testCacheEviction1() {
String cacheName = "userCache";
int expiryTime = 20000;
int capacity = 10;
float evictionFactor = 0.2f;
BValue[] args = new BValue[4];
args[0] = new BString(cacheName);
args[1] = new BInteger(expiryTime);
args[2] = new BInteger(capacity);
args[3] = new BFloat(evictionFactor);
BValue[] returns = BRunUtil.invoke(compileResult, "testCacheEviction1", args);
Assert.assertTrue(returns.length == 2);
Assert.assertTrue(returns[0] instanceof BStringArray);
Assert.assertEquals(((BStringArray) returns[0]).get(0), "C");
Assert.assertEquals(((BStringArray) returns[0]).get(1), "D");
Assert.assertEquals(((BStringArray) returns[0]).get(2), "E");
Assert.assertEquals(((BStringArray) returns[0]).get(3), "F");
Assert.assertEquals(((BStringArray) returns[0]).get(4), "G");
Assert.assertEquals(((BStringArray) returns[0]).get(5), "H");
Assert.assertEquals(((BStringArray) returns[0]).get(6), "I");
Assert.assertEquals(((BStringArray) returns[0]).get(7), "J");
Assert.assertEquals(((BStringArray) returns[0]).get(8), "K");
Assert.assertEquals(((BInteger) returns[1]).intValue(), 9);
}
use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.
the class CachingTest method testCacheEviction4.
@Test
public void testCacheEviction4() {
String cacheName = "userCache";
int expiryTime = 20000;
int capacity = 5;
float evictionFactor = 0.2f;
BValue[] args = new BValue[4];
args[0] = new BString(cacheName);
args[1] = new BInteger(expiryTime);
args[2] = new BInteger(capacity);
args[3] = new BFloat(evictionFactor);
BValue[] returns = BRunUtil.invoke(compileResult, "testCacheEviction4", args);
Assert.assertTrue(returns.length == 2);
Assert.assertTrue(returns[0] instanceof BStringArray);
Assert.assertEquals(((BStringArray) returns[0]).get(0), "A");
Assert.assertEquals(((BStringArray) returns[0]).get(1), "B");
Assert.assertEquals(((BStringArray) returns[0]).get(2), "C");
Assert.assertEquals(((BStringArray) returns[0]).get(3), "D");
Assert.assertEquals(((BStringArray) returns[0]).get(4), "F");
Assert.assertEquals(((BInteger) returns[1]).intValue(), 5);
}
use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.
the class VectorNegativeTest method testRemoveIndexOutOfRange.
@Test(description = "Test case for testing removal of elements from outside of the bounds of a vector.")
public void testRemoveIndexOutOfRange() {
long vectorSize = 10;
long[] indices = new long[] { 0, vectorSize - 1, -1 };
long[] expectedFinalValues = new long[] { 20, 30, 40, 50, 60, 70, 80, 90, 100 };
String[] expectedErrMsgs = new String[] { "Index out of range: 9", "Index out of range: -1" };
BValue[] returns = BRunUtil.invoke(compileResult, "testRemoveIndexOutOfRange", new BValue[] { 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, expectedFinalValues.length);
for (int i = 0; i < finalVectorSize; i++) {
Assert.assertEquals(vectorEntries.get(i).value(), expectedFinalValues[i]);
}
BRefValueArray removedVals = (BRefValueArray) returns[1];
// only 1 valid index
Assert.assertEquals(removedVals.size(), 1);
Assert.assertEquals(((BInteger) removedVals.get(0)).intValue(), 10);
BRefValueArray errors = (BRefValueArray) returns[2];
Assert.assertEquals(errors.size(), expectedErrMsgs.length);
for (int i = 0; i < expectedErrMsgs.length; i++) {
Assert.assertTrue(errors.get(i).stringValue().contains(expectedErrMsgs[i]));
}
}
use of org.ballerinalang.model.values.BInteger 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]));
}
}
use of org.ballerinalang.model.values.BInteger 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]));
}
Aggregations