use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.
the class GlobalVarFunctionTest method testChangeGlobalVarWithinFunction.
@Test(description = "Test change global var within functions")
public void testChangeGlobalVarWithinFunction() {
BValue[] args = { new BInteger(88) };
BValue[] returns = BRunUtil.invoke(result, "changeGlobalVar", args);
Assert.assertEquals(returns.length, 1);
Assert.assertSame(returns[0].getClass(), BFloat.class);
Assert.assertEquals(((BFloat) returns[0]).floatValue(), 165.0);
CompileResult resultGlobalVar = BCompileUtil.compile("test-src/statements/variabledef/global-var-function.bal");
BValue[] returnsChanged = BRunUtil.invoke(resultGlobalVar, "getGlobalFloatVar");
Assert.assertEquals(returnsChanged.length, 1);
Assert.assertSame(returnsChanged[0].getClass(), BFloat.class);
Assert.assertEquals(((BFloat) returnsChanged[0]).floatValue(), 80.0);
}
use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.
the class StructAccessWithIndexTest method testBasicStruct.
@Test(description = "Test Basic struct operations")
public void testBasicStruct() {
BValue[] returns = BRunUtil.invoke(compileResult, "testCreateStruct");
Assert.assertTrue(returns[0] instanceof BString);
Assert.assertEquals(returns[0].stringValue(), "Jack");
Assert.assertTrue(returns[1] instanceof BMap);
BMap<String, ?> adrsMap = ((BMap) returns[1]);
Assert.assertEquals(adrsMap.get("country"), new BString("USA"));
Assert.assertEquals(adrsMap.get("state"), new BString("CA"));
Assert.assertTrue(returns[2] instanceof BInteger);
Assert.assertEquals(((BInteger) returns[2]).intValue(), 25);
}
use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.
the class WhileStmtTest method testWhileStmtConditionTrue.
@Test(description = "Test while loop with a condition which evaluates to true")
public void testWhileStmtConditionTrue() {
BValue[] args = { new BInteger(10), new BInteger(1) };
BValue[] returns = BRunUtil.invoke(positiveCompileResult, "testWhileStmt", args);
Assert.assertEquals(returns.length, 1);
Assert.assertSame(returns[0].getClass(), BInteger.class);
long actual = ((BInteger) returns[0]).intValue();
long expected = 100;
Assert.assertEquals(actual, expected);
}
use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.
the class WhileStmtTest method testWhileBlockScopes.
@Test(description = "Check the scope managing in while block")
public void testWhileBlockScopes() {
BValue[] args = { new BInteger(1) };
BValue[] returns = BRunUtil.invoke(positiveCompileResult, "testWhileScope", args);
Assert.assertEquals(returns.length, 1);
Assert.assertSame(returns[0].getClass(), BInteger.class, "Class type mismatched");
BInteger actual = (BInteger) returns[0];
Assert.assertEquals(actual.intValue(), 200, "mismatched output value");
args = new BValue[] { new BInteger(2) };
returns = BRunUtil.invoke(positiveCompileResult, "testWhileScope", args);
Assert.assertEquals(returns.length, 1);
Assert.assertSame(returns[0].getClass(), BInteger.class, "Class type mismatched");
actual = (BInteger) returns[0];
Assert.assertEquals(actual.intValue(), 400, "mismatched output value");
}
use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.
the class MessageUtils method generateProtoMessage.
/**
* Returns protobuf message corresponding to the B7a message.
*
* @param responseValue B7a message.
* @param outputType protobuf message type.
* @return generated protobuf message.
*/
public static Message generateProtoMessage(BValue responseValue, Descriptors.Descriptor outputType) {
Message.Builder responseBuilder = Message.newBuilder(outputType.getName());
int stringIndex = 0;
int intIndex = 0;
int floatIndex = 0;
int boolIndex = 0;
int refIndex = 0;
for (Descriptors.FieldDescriptor fieldDescriptor : outputType.getFields()) {
String fieldName = fieldDescriptor.getName();
switch(fieldDescriptor.getType().toProto().getNumber()) {
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_DOUBLE_VALUE:
{
double value = 0F;
if (responseValue instanceof BStruct) {
value = ((BStruct) responseValue).getFloatField(floatIndex++);
} else {
if (responseValue instanceof BFloat) {
value = ((BFloat) responseValue).value();
}
}
responseBuilder.addField(fieldName, value);
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FLOAT_VALUE:
{
float value = 0F;
if (responseValue instanceof BStruct) {
value = Float.parseFloat(String.valueOf(((BStruct) responseValue).getFloatField(floatIndex++)));
} else {
if (responseValue instanceof BFloat) {
value = Float.parseFloat(String.valueOf(((BFloat) responseValue).value()));
}
}
responseBuilder.addField(fieldName, value);
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT64_VALUE:
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_UINT64_VALUE:
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED64_VALUE:
{
long value = 0;
if (responseValue instanceof BStruct) {
value = ((BStruct) responseValue).getIntField(intIndex++);
} else {
if (responseValue instanceof BInteger) {
value = ((BInteger) responseValue).value();
}
}
responseBuilder.addField(fieldName, value);
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32_VALUE:
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED32_VALUE:
{
int value = 0;
if (responseValue instanceof BStruct) {
value = Integer.parseInt(String.valueOf(((BStruct) responseValue).getIntField(intIndex++)));
} else {
if (responseValue instanceof BInteger) {
value = Integer.parseInt(String.valueOf(((BInteger) responseValue).value()));
}
}
responseBuilder.addField(fieldName, value);
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_BOOL_VALUE:
{
boolean value = false;
if (responseValue instanceof BStruct) {
value = ((BStruct) responseValue).getBooleanField(boolIndex++) > 0;
} else {
if (responseValue instanceof BBoolean) {
value = ((BBoolean) responseValue).value();
}
}
responseBuilder.addField(fieldName, value);
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_STRING_VALUE:
{
String value = null;
if (responseValue instanceof BStruct) {
value = ((BStruct) responseValue).getStringField(stringIndex++);
} else {
if (responseValue instanceof BString) {
value = ((BString) responseValue).value();
}
}
responseBuilder.addField(fieldName, value);
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE_VALUE:
{
if (responseValue instanceof BStruct) {
BValue bValue = ((BStruct) responseValue).getRefField(refIndex++);
responseBuilder.addField(fieldName, generateProtoMessage(bValue, fieldDescriptor.getMessageType()));
}
break;
}
default:
{
throw new UnsupportedFieldTypeException("Error while decoding request message. Field " + "type is not supported : " + fieldDescriptor.getType());
}
}
}
return responseBuilder.build();
}
Aggregations