Search in sources :

Example 6 with BFloat

use of org.ballerinalang.model.values.BFloat 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();
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BString(org.ballerinalang.model.values.BString) BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) BBoolean(org.ballerinalang.model.values.BBoolean) UnsupportedFieldTypeException(org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException) BString(org.ballerinalang.model.values.BString) BFloat(org.ballerinalang.model.values.BFloat) Descriptors(com.google.protobuf.Descriptors)

Example 7 with BFloat

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

the class SystemTest method testFormatFloat.

@Test
public void testFormatFloat() {
    BRefValueArray fArgs = new BRefValueArray();
    fArgs.add(0, new BFloat(3.25));
    BValue[] args = { new BString("%f"), fArgs };
    BValue[] returns = BRunUtil.invoke(compileResult, "testSprintf", args);
    Assert.assertEquals(returns[0].stringValue(), "3.250000");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BFloat(org.ballerinalang.model.values.BFloat) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) Test(org.testng.annotations.Test)

Example 8 with BFloat

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

the class SystemTest method testFloatPrintAndPrintln.

@Test
public void testFloatPrintAndPrintln() throws IOException {
    ByteArrayOutputStream outContent = new ByteArrayOutputStream();
    try {
        System.setOut(new PrintStream(outContent));
        final float v1 = 1000;
        final float v2 = 1;
        final String expected = v1 + "\n" + v2;
        BValueType[] args = { new BFloat(v1), new BFloat(v2) };
        BRunUtil.invoke(compileResult, printFuncName + "Float", args);
        Assert.assertEquals(outContent.toString().replace("\r", ""), expected);
    } finally {
        outContent.close();
        System.setOut(original);
    }
}
Also used : PrintStream(java.io.PrintStream) BValueType(org.ballerinalang.model.values.BValueType) BFloat(org.ballerinalang.model.values.BFloat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 9 with BFloat

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

the class MathTest method testMathSqrt.

@Test(description = "Test 'sqrt' function in ballerina.lang.math package")
public void testMathSqrt() {
    BValue[] args = { new BFloat(25.0) };
    BValue[] returns = BRunUtil.invoke(compileResult, "sqrtTest", args);
    Assert.assertEquals(returns.length, 1);
    Assert.assertEquals(((BFloat) returns[0]).floatValue(), 5.0, DELTA);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BFloat(org.ballerinalang.model.values.BFloat) Test(org.testng.annotations.Test)

Example 10 with BFloat

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

the class MathTest method testSinh.

@Test(description = "Test 'sinh' function in ballerina.lang.math package")
public void testSinh() {
    BValue[] args = { new BFloat(-3.141592653589793) };
    BValue[] returns = BRunUtil.invoke(compileResult, "sinhTest", args);
    Assert.assertEquals(returns.length, 1);
    Assert.assertEquals(((BFloat) returns[0]).floatValue(), -11.548739357257748, DELTA);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BFloat(org.ballerinalang.model.values.BFloat) Test(org.testng.annotations.Test)

Aggregations

BFloat (org.ballerinalang.model.values.BFloat)158 BValue (org.ballerinalang.model.values.BValue)119 Test (org.testng.annotations.Test)107 BInteger (org.ballerinalang.model.values.BInteger)55 BString (org.ballerinalang.model.values.BString)41 BBoolean (org.ballerinalang.model.values.BBoolean)21 BRefType (org.ballerinalang.model.values.BRefType)7 BStruct (org.ballerinalang.model.values.BStruct)7 BType (org.ballerinalang.model.types.BType)6 BBlob (org.ballerinalang.model.values.BBlob)6 BStringArray (org.ballerinalang.model.values.BStringArray)5 BStructType (org.ballerinalang.model.types.BStructType)4 BIntArray (org.ballerinalang.model.values.BIntArray)4 UnsupportedFieldTypeException (org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException)4 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)4 BJSON (org.ballerinalang.model.values.BJSON)3 CompileResult (org.ballerinalang.launcher.util.CompileResult)2 BMap (org.ballerinalang.model.values.BMap)2 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)2 Message (org.ballerinalang.net.grpc.Message)2