From 5a829809d919af07893e49db17033663a6fffd94 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Mon, 12 Dec 2016 17:49:58 +0300 Subject: [PATCH] Add operator 'rem' as extension to BigInteger #KT-14650 Fixed Note that after this change behaviour of '%' on BigInteger is changed, now it works like a proper remainder --- .../percentAsModOnBigIntegerWithoutRem.kt | 11 +++++++++++ .../percentAsModOnBigIntegerWithoutRem.txt | 3 +++ .../codegen/ir/IrBlackBoxCodegenTestGenerated.java | 6 ++++++ .../kotlin/codegen/BlackBoxCodegenTestGenerated.java | 6 ++++++ .../LightAnalysisModeCodegenTestGenerated.java | 6 ++++++ libraries/stdlib/src/kotlin/util/BigNumbers.kt | 7 +++++++ libraries/stdlib/test/numbers/MathJVMTest.kt | 3 ++- 7 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/operatorConventions/percentAsModOnBigIntegerWithoutRem.kt create mode 100644 compiler/testData/codegen/light-analysis/operatorConventions/percentAsModOnBigIntegerWithoutRem.txt diff --git a/compiler/testData/codegen/box/operatorConventions/percentAsModOnBigIntegerWithoutRem.kt b/compiler/testData/codegen/box/operatorConventions/percentAsModOnBigIntegerWithoutRem.kt new file mode 100644 index 00000000000..0d1cdd857dc --- /dev/null +++ b/compiler/testData/codegen/box/operatorConventions/percentAsModOnBigIntegerWithoutRem.kt @@ -0,0 +1,11 @@ +// TARGET_BACKEND: JVM + +// LANGUAGE_VERSION: 1.0 +// FULL_JDK + +import java.math.BigInteger + +fun box(): String { + val m = BigInteger.valueOf(-2) % BigInteger.valueOf(3) + return if (m != BigInteger.valueOf(1)) "Fail: BigInteger(-2) mod BigInteger(3) == $m" else "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/light-analysis/operatorConventions/percentAsModOnBigIntegerWithoutRem.txt b/compiler/testData/codegen/light-analysis/operatorConventions/percentAsModOnBigIntegerWithoutRem.txt new file mode 100644 index 00000000000..b675c2d93a6 --- /dev/null +++ b/compiler/testData/codegen/light-analysis/operatorConventions/percentAsModOnBigIntegerWithoutRem.txt @@ -0,0 +1,3 @@ +public final class PercentAsModOnBigIntegerWithoutRemKt { + public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String +} diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index f6e4af09a4a..e822b89f7a3 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -10547,6 +10547,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("percentAsModOnBigIntegerWithoutRem.kt") + public void testPercentAsModOnBigIntegerWithoutRem() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/operatorConventions/percentAsModOnBigIntegerWithoutRem.kt"); + doTest(fileName); + } + @TestMetadata("remAssignmentOperation.kt") public void testRemAssignmentOperation() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/operatorConventions/remAssignmentOperation.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index e3d56947c17..d768a3eb983 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -10547,6 +10547,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("percentAsModOnBigIntegerWithoutRem.kt") + public void testPercentAsModOnBigIntegerWithoutRem() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/operatorConventions/percentAsModOnBigIntegerWithoutRem.kt"); + doTest(fileName); + } + @TestMetadata("remAssignmentOperation.kt") public void testRemAssignmentOperation() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/operatorConventions/remAssignmentOperation.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java index 8e539eee417..fbf5ce38e04 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java @@ -10547,6 +10547,12 @@ public class LightAnalysisModeCodegenTestGenerated extends AbstractLightAnalysis doTest(fileName); } + @TestMetadata("percentAsModOnBigIntegerWithoutRem.kt") + public void testPercentAsModOnBigIntegerWithoutRem() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/operatorConventions/percentAsModOnBigIntegerWithoutRem.kt"); + doTest(fileName); + } + @TestMetadata("remAssignmentOperation.kt") public void testRemAssignmentOperation() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/operatorConventions/remAssignmentOperation.kt"); diff --git a/libraries/stdlib/src/kotlin/util/BigNumbers.kt b/libraries/stdlib/src/kotlin/util/BigNumbers.kt index 3a205686a96..cd3fe0a0840 100644 --- a/libraries/stdlib/src/kotlin/util/BigNumbers.kt +++ b/libraries/stdlib/src/kotlin/util/BigNumbers.kt @@ -31,6 +31,13 @@ public inline operator fun BigInteger.times(other: BigInteger) : BigInteger = th @kotlin.internal.InlineOnly public inline operator fun BigInteger.div(other: BigInteger) : BigInteger = this.divide(other) +/** + * Enables the use of the `%` operator for [BigInteger] instances. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline operator fun BigInteger.rem(other: BigInteger) : BigInteger = this.remainder(other) + /** * Enables the use of the unary `-` operator for [BigInteger] instances. */ diff --git a/libraries/stdlib/test/numbers/MathJVMTest.kt b/libraries/stdlib/test/numbers/MathJVMTest.kt index d0836b8a06e..a668ba1a3ff 100644 --- a/libraries/stdlib/test/numbers/MathJVMTest.kt +++ b/libraries/stdlib/test/numbers/MathJVMTest.kt @@ -17,7 +17,8 @@ class MathTest { assertEquals(BigInteger("6"), a * b) assertEquals(BigInteger("0"), a / b) assertEquals(BigInteger("-2"), -a) - assertEquals(BigInteger("1"), -a % b) + assertEquals(BigInteger("-2"), -a % b) + assertEquals(BigInteger("1"), (-a).mod(b)) assertEquals(BigInteger("-2"), (-a).remainder(b)) }