Number conversions.
This commit is contained in:
@@ -33,4 +33,8 @@ public final class NumberTest extends SingleFileTranslationTest {
|
||||
public void testDoubleConversions() throws Exception {
|
||||
fooBoxTest();
|
||||
}
|
||||
|
||||
public void testNumberConversions() throws Exception {
|
||||
fooBoxTest();
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -27,8 +27,8 @@ import org.jetbrains.k2js.translate.intrinsic.functions.basic.CallStandardMethod
|
||||
import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic;
|
||||
import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NameChecker;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.jetbrains.jet.lang.types.expressions.OperatorConventions.*;
|
||||
import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern;
|
||||
@@ -41,7 +41,7 @@ public final class NumberConversionFIF {
|
||||
private static final NameChecker SUPPORTED_CONVERSIONS;
|
||||
|
||||
static {
|
||||
HashSet<Name> supportedConversions = Sets.newHashSet(NUMBER_CONVERSIONS);
|
||||
Set<Name> supportedConversions = Sets.newHashSet(NUMBER_CONVERSIONS);
|
||||
//TODO: support longs and chars
|
||||
supportedConversions.remove(CHAR);
|
||||
supportedConversions.remove(LONG);
|
||||
@@ -71,8 +71,8 @@ public final class NumberConversionFIF {
|
||||
@NotNull
|
||||
public static final FunctionIntrinsicFactory INSTANCE = FIFBuilder.start()
|
||||
.add(pattern("Int", SUPPORTED_CONVERSIONS), RETURN_RECEIVER)
|
||||
.add(pattern("Double", INTEGER_CONVERSIONS), new CallStandardMethodIntrinsic("Math.floor", true, 0))
|
||||
.add(pattern("Double", FLOATING_POINT_CONVERSIONS), RETURN_RECEIVER)
|
||||
.add(pattern("Double|Number", INTEGER_CONVERSIONS), new CallStandardMethodIntrinsic("Math.floor", true, 0))
|
||||
.add(pattern("Double|Number", FLOATING_POINT_CONVERSIONS), RETURN_RECEIVER)
|
||||
.build();
|
||||
|
||||
private NumberConversionFIF() {
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package foo
|
||||
|
||||
fun testIntegerConversions(c: Number): 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 testFloatingPointConversions(c: Number): Boolean {
|
||||
if (c.toDouble() != 3.6) {
|
||||
return false
|
||||
}
|
||||
if (c.toFloat() != 3.6.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(): Boolean = testIntegerConversions(3) && testFloatingPointConversions(3.6) && testFloatingPointConversions(3.6.toFloat()) &&
|
||||
testIntegerConversions(3.toByte()) && testIntegerConversions(3.toShort())
|
||||
Reference in New Issue
Block a user