diff --git a/libraries/stdlib/common/build.gradle b/libraries/stdlib/common/build.gradle index cc906f8632e..c81df719acb 100644 --- a/libraries/stdlib/common/build.gradle +++ b/libraries/stdlib/common/build.gradle @@ -71,6 +71,14 @@ compileCoroutinesKotlinCommon { } } +compileTestKotlinCommon { + kotlinOptions { + freeCompilerArgs += [ + "-Xuse-experimental=kotlin.ExperimentalUnsignedTypes" + ] + } +} + kotlin.experimental.coroutines 'enable' jar { diff --git a/libraries/stdlib/jdk7/build.gradle b/libraries/stdlib/jdk7/build.gradle index f670b2da37d..95b198648f0 100644 --- a/libraries/stdlib/jdk7/build.gradle +++ b/libraries/stdlib/jdk7/build.gradle @@ -70,7 +70,7 @@ compileKotlin { } compileTestKotlin { - kotlinOptions.freeCompilerArgs = ["-Xallow-kotlin-package", "-Xmulti-platform", "-XXLanguage:-ReleaseCoroutines"] + kotlinOptions.freeCompilerArgs = ["-Xallow-kotlin-package", "-Xmulti-platform", "-XXLanguage:-ReleaseCoroutines", "-Xuse-experimental=kotlin.ExperimentalUnsignedTypes"] } compileJava9Sources(project, 'kotlin.stdlib.jdk7') diff --git a/libraries/stdlib/jdk8/build.gradle b/libraries/stdlib/jdk8/build.gradle index f904dac41d5..8820b8fe889 100644 --- a/libraries/stdlib/jdk8/build.gradle +++ b/libraries/stdlib/jdk8/build.gradle @@ -73,7 +73,7 @@ compileKotlin { } compileTestKotlin { - kotlinOptions.freeCompilerArgs = ["-Xallow-kotlin-package", "-Xmulti-platform", "-XXLanguage:-ReleaseCoroutines"] + kotlinOptions.freeCompilerArgs = ["-Xallow-kotlin-package", "-Xmulti-platform", "-XXLanguage:-ReleaseCoroutines", "-Xuse-experimental=kotlin.ExperimentalUnsignedTypes"] } compileJava9Sources(project, 'kotlin.stdlib.jdk8') diff --git a/libraries/stdlib/jre7/build.gradle b/libraries/stdlib/jre7/build.gradle index 415cc7f40df..4fe35d16315 100644 --- a/libraries/stdlib/jre7/build.gradle +++ b/libraries/stdlib/jre7/build.gradle @@ -63,7 +63,7 @@ compileKotlin { } compileTestKotlin { - kotlinOptions.freeCompilerArgs = ["-Xallow-kotlin-package", "-Xmulti-platform", "-XXLanguage:-ReleaseCoroutines"] + kotlinOptions.freeCompilerArgs = ["-Xallow-kotlin-package", "-Xmulti-platform", "-XXLanguage:-ReleaseCoroutines", "-Xuse-experimental=kotlin.ExperimentalUnsignedTypes"] } kotlin.experimental.coroutines 'enable' diff --git a/libraries/stdlib/jre8/build.gradle b/libraries/stdlib/jre8/build.gradle index 0c001246998..16efd6e0f98 100644 --- a/libraries/stdlib/jre8/build.gradle +++ b/libraries/stdlib/jre8/build.gradle @@ -66,7 +66,7 @@ compileKotlin { } compileTestKotlin { - kotlinOptions.freeCompilerArgs = ["-Xallow-kotlin-package", "-Xnormalize-constructor-calls=enable", "-Xmulti-platform", "-XXLanguage:-ReleaseCoroutines"] + kotlinOptions.freeCompilerArgs = ["-Xallow-kotlin-package", "-Xnormalize-constructor-calls=enable", "-Xmulti-platform", "-XXLanguage:-ReleaseCoroutines", "-Xuse-experimental=kotlin.ExperimentalUnsignedTypes"] } kotlin.experimental.coroutines 'enable' diff --git a/libraries/stdlib/js/build.gradle b/libraries/stdlib/js/build.gradle index 18a3edae600..493c4e476e5 100644 --- a/libraries/stdlib/js/build.gradle +++ b/libraries/stdlib/js/build.gradle @@ -181,7 +181,8 @@ compileTestKotlin2Js { kotlinOptions { moduleKind = "umd" freeCompilerArgs += [ - "-XXLanguage:-ReleaseCoroutines" + "-XXLanguage:-ReleaseCoroutines", + "-Xuse-experimental=kotlin.ExperimentalUnsignedTypes" ] } } diff --git a/libraries/stdlib/jvm/build.gradle b/libraries/stdlib/jvm/build.gradle index f27ca5b2f6d..50a04500616 100644 --- a/libraries/stdlib/jvm/build.gradle +++ b/libraries/stdlib/jvm/build.gradle @@ -253,4 +253,12 @@ compileUnsignedKotlin { } } +compileTestKotlin { + kotlinOptions { + freeCompilerArgs += [ + "-Xuse-experimental=kotlin.ExperimentalUnsignedTypes" + ] + } +} + kotlin.experimental.coroutines 'enable' diff --git a/libraries/stdlib/test/unsigned/UIntTest.kt b/libraries/stdlib/test/unsigned/UIntTest.kt new file mode 100644 index 00000000000..c0475cdefc3 --- /dev/null +++ b/libraries/stdlib/test/unsigned/UIntTest.kt @@ -0,0 +1,104 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package test.unsigned + +import kotlin.math.sign +import kotlin.random.Random +import kotlin.test.* + +class UIntTest { + + private fun identity(u: UInt): UInt = + (u.toInt() + 0).toUInt() + + val zero = 0u + val one = 1u + val max = UInt.MAX_VALUE + + @Test + fun equality() { + + fun testEqual(uv1: UInt, uv2: UInt) { + assertEquals(uv1, uv2, "Boxed values should be equal") + assertTrue(uv1.equals(uv2), "Boxed values should be equal: $uv1, $uv2") + assertTrue(uv1 == uv2, "Values should be equal: $uv1, $uv2") + assertEquals(uv1.hashCode(), uv2.hashCode()) + assertEquals((uv1 as Any).hashCode(), (uv2 as Any).hashCode()) + assertEquals(uv1.toString(), uv2.toString()) + assertEquals((uv1 as Any).toString(), (uv2 as Any).toString()) + } + + testEqual(one, identity(one)) + testEqual(max, identity(max)) + + fun testNotEqual(uv1: UInt, uv2: UInt) { + assertNotEquals(uv1, uv2, "Boxed values should be equal") + assertTrue(uv1 != uv2, "Values should be not equal: $uv1, $uv2") + assertNotEquals(uv1.toString(), uv2.toString()) + assertNotEquals((uv1 as Any).toString(), (uv2 as Any).toString()) + } + + testNotEqual(one, zero) + testNotEqual(max, zero) + } + + @Test + fun convertToString() { + fun testToString(expected: String, u: UInt) { + assertEquals(expected, u.toString()) + assertEquals(expected, (u as Any).toString(), "Boxed toString") + assertEquals(expected, "$u", "String template") + } + + repeat(100) { + val v = Random.nextBits(UInt.SIZE_BITS - 1) + testToString(v.toString(), v.toUInt()) + } + + repeat(100) { + val v = Random.nextInt(294967295 + 1) + testToString("4${v.toString().padStart(9, '0')}", (2_000_000_000.toUInt() * 2.toUInt() + v.toUInt())) + } + + testToString("4294967295", UInt.MAX_VALUE) + } + + @Test + fun comparisons() { + fun compare(op1: Comparable, op2: T) = op1.compareTo(op2) + + fun testComparison(uv1: UInt, uv2: UInt, expected: Int) { + val desc = "${uv1.toString()}, ${uv2.toString()}" + assertEquals(expected, uv1.compareTo(uv2).sign, "compareTo: $desc") + assertEquals(expected, (uv1 as Comparable).compareTo(uv2).sign, "Comparable.compareTo: $desc") + assertEquals(expected, compare(uv1, uv2).sign, "Generic compareTo: $desc") + + assertEquals(expected < 0, uv1 < uv2) + assertEquals(expected <= 0, uv1 <= uv2) + assertEquals(expected > 0, uv1 > uv2) + assertEquals(expected >= 0, uv1 >= uv2) + } + + fun testEquals(uv1: UInt, uv2: UInt) = testComparison(uv1, uv2, 0) + fun testCompare(uv1: UInt, uv2: UInt, expected12: Int) { + testComparison(uv1, uv2, expected12) + testComparison(uv2, uv1, -expected12) + } + + testEquals(one, identity(one)) + testEquals(max, identity(max)) + + testCompare(zero, one, -1) + testCompare(Int.MAX_VALUE.toUInt(), zero, 1) + + testCompare(zero, UInt.MAX_VALUE, -1) + testCompare((Int.MAX_VALUE).toUInt() + one, UInt.MAX_VALUE, -1) + } + + + + +} \ No newline at end of file diff --git a/libraries/stdlib/test/unsigned/ULongTest.kt b/libraries/stdlib/test/unsigned/ULongTest.kt new file mode 100644 index 00000000000..5c72b171921 --- /dev/null +++ b/libraries/stdlib/test/unsigned/ULongTest.kt @@ -0,0 +1,104 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package test.unsigned + +import kotlin.math.sign +import kotlin.random.Random +import kotlin.test.* + +class ULongTest { + + private fun identity(u: ULong): ULong = + (u.toLong() + 0).toULong() + + val zero = 0uL + val one = 1uL + val max = ULong.MAX_VALUE + + @Test + fun equality() { + + fun testEqual(uv1: ULong, uv2: ULong) { + assertEquals(uv1, uv2, "Boxed values should be equal") + assertTrue(uv1.equals(uv2), "Boxed values should be equal: $uv1, $uv2") + assertTrue(uv1 == uv2, "Values should be equal: $uv1, $uv2") + assertEquals(uv1.hashCode(), uv2.hashCode()) + assertEquals((uv1 as Any).hashCode(), (uv2 as Any).hashCode()) + assertEquals(uv1.toString(), uv2.toString()) + assertEquals((uv1 as Any).toString(), (uv2 as Any).toString()) + } + + testEqual(one, identity(one)) + testEqual(max, identity(max)) + + fun testNotEqual(uv1: ULong, uv2: ULong) { + assertNotEquals(uv1, uv2, "Boxed values should be equal") + assertTrue(uv1 != uv2, "Values should be not equal: $uv1, $uv2") + assertNotEquals(uv1.toString(), uv2.toString()) + assertNotEquals((uv1 as Any).toString(), (uv2 as Any).toString()) + } + + testNotEqual(one, zero) + testNotEqual(max, zero) + } + + @Test + fun convertToString() { + fun testToString(expected: String, u: ULong) { + assertEquals(expected, u.toString()) + assertEquals(expected, (u as Any).toString(), "Boxed toString") + assertEquals(expected, "$u", "String template") + } + + repeat(100) { + val v = Random.nextLong() ushr 1 + testToString(v.toString(), v.toULong()) + } + + repeat(100) { + val v = Random.nextLong(8446744073709551615L + 1) + testToString("1${v.toString().padStart(19, '0')}", (5000000000000000000.toULong() * 2.toULong() + v.toULong())) + } + + testToString("18446744073709551615", ULong.MAX_VALUE) + } + + @Test + fun comparisons() { + fun compare(op1: Comparable, op2: T) = op1.compareTo(op2) + + fun testComparison(uv1: ULong, uv2: ULong, expected: Int) { + val desc = "${uv1.toString()}, ${uv2.toString()}" + assertEquals(expected, uv1.compareTo(uv2).sign, "compareTo: $desc") + assertEquals(expected, (uv1 as Comparable).compareTo(uv2).sign, "Comparable.compareTo: $desc") + assertEquals(expected, compare(uv1, uv2).sign, "Generic compareTo: $desc") + + assertEquals(expected < 0, uv1 < uv2) + assertEquals(expected <= 0, uv1 <= uv2) + assertEquals(expected > 0, uv1 > uv2) + assertEquals(expected >= 0, uv1 >= uv2) + } + + fun testEquals(uv1: ULong, uv2: ULong) = testComparison(uv1, uv2, 0) + fun testCompare(uv1: ULong, uv2: ULong, expected12: Int) { + testComparison(uv1, uv2, expected12) + testComparison(uv2, uv1, -expected12) + } + + testEquals(one, identity(one)) + testEquals(max, identity(max)) + + testCompare(zero, one, -1) + testCompare(Long.MAX_VALUE.toULong(), zero, 1) + + testCompare(zero, ULong.MAX_VALUE, -1) + testCompare((Long.MAX_VALUE).toULong() + one, ULong.MAX_VALUE, -1) + } + + + + +} \ No newline at end of file