Byte, Float, Short conversions.

This commit is contained in:
Pavel V. Talanov
2012-07-18 15:10:23 +04:00
parent c363d54d0a
commit bd6f38021f
5 changed files with 77 additions and 4 deletions
@@ -37,4 +37,8 @@ public final class NumberTest extends SingleFileTranslationTest {
public void testNumberConversions() throws Exception {
fooBoxTest();
}
public void testByteAndShortConversions() throws Exception {
fooBoxTest();
}
}
@@ -83,7 +83,13 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
if (value instanceof Character) {
return context.program().getStringLiteral(value.toString());
}
//TODO: all values
if (value instanceof Byte) {
return context.program().getNumberLiteral((Byte) value);
}
if (value instanceof Short) {
return context.program().getNumberLiteral((Short) value);
}
//TODO: long and char
throw new AssertionError(message(expression, "Unsupported constant expression"));
}
@@ -68,11 +68,16 @@ public final class NumberConversionFIF {
}
};
@NotNull
public static final String INTEGER_NUMBER_TYPES = "Int|Byte|Short";
//NOTE: treat Number as if it is floating point type
@NotNull
public static final String FLOATING_POINT_NUMBER_TYPES = "Float|Double|Number";
@NotNull
public static final FunctionIntrinsicFactory INSTANCE = FIFBuilder.start()
.add(pattern("Int", SUPPORTED_CONVERSIONS), RETURN_RECEIVER)
.add(pattern("Double|Number", INTEGER_CONVERSIONS), new CallStandardMethodIntrinsic("Math.floor", true, 0))
.add(pattern("Double|Number", FLOATING_POINT_CONVERSIONS), RETURN_RECEIVER)
.add(pattern(INTEGER_NUMBER_TYPES, SUPPORTED_CONVERSIONS), RETURN_RECEIVER)
.add(pattern(FLOATING_POINT_NUMBER_TYPES, INTEGER_CONVERSIONS), new CallStandardMethodIntrinsic("Math.floor", true, 0))
.add(pattern(FLOATING_POINT_NUMBER_TYPES, FLOATING_POINT_CONVERSIONS), RETURN_RECEIVER)
.build();
private NumberConversionFIF() {
@@ -0,0 +1,41 @@
package foo
fun testShortConversions(c: Short): Boolean {
if (c.toDouble() != 3.0) {
return false
}
if (c.toFloat() != 3.toFloat()) {
return false
}
if (c.toByte() != 3.toByte()) {
return false
}
if (c.toInt() != 3) {
return false
}
if (c.toShort() != 3.toShort()) {
return false
}
return true
}
fun testByteConversions(c: Byte): Boolean {
if (c.toDouble() != 3.0) {
return false
}
if (c.toFloat() != 3.toFloat()) {
return false
}
if (c.toByte() != 3.toByte()) {
return false
}
if (c.toInt() != 3) {
return false
}
if (c.toShort() != 3.toShort()) {
return false
}
return true
}
fun box() = testShortConversions(3) && testByteConversions(3)
@@ -17,5 +17,22 @@ fun box(): Boolean {
if (c.toShort() != 3.toShort()) {
return false
}
val f: Float = 3.6.toFloat()
if (f.toDouble() != 3.6) {
return false
}
if (f.toFloat() != 3.6.toFloat()) {
return false
}
if (f.toByte() != 3.toByte()) {
return false
}
if (f.toInt() != 3) {
return false
}
if (f.toShort() != 3.toShort()) {
return false
}
return true
}