diff --git a/compiler/testData/codegen/boxWithStdlib/ranges/expression/simpleRangeWithNonConstantEnds.kt b/compiler/testData/codegen/boxWithStdlib/ranges/expression/simpleRangeWithNonConstantEnds.kt new file mode 100644 index 00000000000..9c09e865392 --- /dev/null +++ b/compiler/testData/codegen/boxWithStdlib/ranges/expression/simpleRangeWithNonConstantEnds.kt @@ -0,0 +1,69 @@ +// Auto-generated by org.jetbrains.jet.generators.tests.GenerateRangesCodegenTestData. DO NOT EDIT! +import java.util.ArrayList + +fun box(): String { + val list1 = ArrayList() + val range1 = (1 + 2)..(10 - 1) + for (i in range1) { + list1.add(i) + } + if (list1 != listOf(3, 4, 5, 6, 7, 8, 9)) { + return "Wrong elements for (1 + 2)..(10 - 1): $list1" + } + + val list2 = ArrayList() + val range2 = (1.toByte() + 2.toByte()).toByte()..(10.toByte() - 1.toByte()).toByte() + for (i in range2) { + list2.add(i) + } + if (list2 != listOf(3, 4, 5, 6, 7, 8, 9)) { + return "Wrong elements for (1.toByte() + 2.toByte()).toByte()..(10.toByte() - 1.toByte()).toByte(): $list2" + } + + val list3 = ArrayList() + val range3 = (1.toShort() + 2.toShort()).toShort()..(10.toShort() - 1.toShort()).toShort() + for (i in range3) { + list3.add(i) + } + if (list3 != listOf(3, 4, 5, 6, 7, 8, 9)) { + return "Wrong elements for (1.toShort() + 2.toShort()).toShort()..(10.toShort() - 1.toShort()).toShort(): $list3" + } + + val list4 = ArrayList() + val range4 = (1.toLong() + 2.toLong())..(10.toLong() - 1.toLong()) + for (i in range4) { + list4.add(i) + } + if (list4 != listOf(3, 4, 5, 6, 7, 8, 9)) { + return "Wrong elements for (1.toLong() + 2.toLong())..(10.toLong() - 1.toLong()): $list4" + } + + val list5 = ArrayList() + val range5 = ("ace"[1])..("age"[1]) + for (i in range5) { + list5.add(i) + } + if (list5 != listOf('c', 'd', 'e', 'f', 'g')) { + return "Wrong elements for (\"ace\"[1])..(\"age\"[1]): $list5" + } + + val list6 = ArrayList() + val range6 = (1.5 * 2)..(3.0 * 3.0) + for (i in range6) { + list6.add(i) + } + if (list6 != listOf(3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)) { + return "Wrong elements for (1.5 * 2)..(3.0 * 3.0): $list6" + } + + val list7 = ArrayList() + val range7 = (1.5.toFloat() * 2.toFloat())..(3.0.toFloat() * 3.0.toFloat()) + for (i in range7) { + list7.add(i) + } + if (list7 != listOf(3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)) { + return "Wrong elements for (1.5.toFloat() * 2.toFloat())..(3.0.toFloat() * 3.0.toFloat()): $list7" + } + + return "OK" +} diff --git a/compiler/testData/codegen/boxWithStdlib/ranges/literal/simpleRangeWithNonConstantEnds.kt b/compiler/testData/codegen/boxWithStdlib/ranges/literal/simpleRangeWithNonConstantEnds.kt new file mode 100644 index 00000000000..f5215f7af66 --- /dev/null +++ b/compiler/testData/codegen/boxWithStdlib/ranges/literal/simpleRangeWithNonConstantEnds.kt @@ -0,0 +1,62 @@ +// Auto-generated by org.jetbrains.jet.generators.tests.GenerateRangesCodegenTestData. DO NOT EDIT! +import java.util.ArrayList + +fun box(): String { + val list1 = ArrayList() + for (i in (1 + 2)..(10 - 1)) { + list1.add(i) + } + if (list1 != listOf(3, 4, 5, 6, 7, 8, 9)) { + return "Wrong elements for (1 + 2)..(10 - 1): $list1" + } + + val list2 = ArrayList() + for (i in (1.toByte() + 2.toByte()).toByte()..(10.toByte() - 1.toByte()).toByte()) { + list2.add(i) + } + if (list2 != listOf(3, 4, 5, 6, 7, 8, 9)) { + return "Wrong elements for (1.toByte() + 2.toByte()).toByte()..(10.toByte() - 1.toByte()).toByte(): $list2" + } + + val list3 = ArrayList() + for (i in (1.toShort() + 2.toShort()).toShort()..(10.toShort() - 1.toShort()).toShort()) { + list3.add(i) + } + if (list3 != listOf(3, 4, 5, 6, 7, 8, 9)) { + return "Wrong elements for (1.toShort() + 2.toShort()).toShort()..(10.toShort() - 1.toShort()).toShort(): $list3" + } + + val list4 = ArrayList() + for (i in (1.toLong() + 2.toLong())..(10.toLong() - 1.toLong())) { + list4.add(i) + } + if (list4 != listOf(3, 4, 5, 6, 7, 8, 9)) { + return "Wrong elements for (1.toLong() + 2.toLong())..(10.toLong() - 1.toLong()): $list4" + } + + val list5 = ArrayList() + for (i in ("ace"[1])..("age"[1])) { + list5.add(i) + } + if (list5 != listOf('c', 'd', 'e', 'f', 'g')) { + return "Wrong elements for (\"ace\"[1])..(\"age\"[1]): $list5" + } + + val list6 = ArrayList() + for (i in (1.5 * 2)..(3.0 * 3.0)) { + list6.add(i) + } + if (list6 != listOf(3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)) { + return "Wrong elements for (1.5 * 2)..(3.0 * 3.0): $list6" + } + + val list7 = ArrayList() + for (i in (1.5.toFloat() * 2.toFloat())..(3.0.toFloat() * 3.0.toFloat())) { + list7.add(i) + } + if (list7 != listOf(3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)) { + return "Wrong elements for (1.5.toFloat() * 2.toFloat())..(3.0.toFloat() * 3.0.toFloat()): $list7" + } + + return "OK" +} diff --git a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java index 51f36efebca..6b1ad8221e5 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java @@ -466,6 +466,11 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/ranges/expression/simpleRange.kt"); } + @TestMetadata("simpleRangeWithNonConstantEnds.kt") + public void testSimpleRangeWithNonConstantEnds() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/ranges/expression/simpleRangeWithNonConstantEnds.kt"); + } + @TestMetadata("simpleSteppedDownTo.kt") public void testSimpleSteppedDownTo() throws Exception { doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/ranges/expression/simpleSteppedDownTo.kt"); @@ -554,6 +559,11 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/ranges/literal/simpleRange.kt"); } + @TestMetadata("simpleRangeWithNonConstantEnds.kt") + public void testSimpleRangeWithNonConstantEnds() throws Exception { + doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/ranges/literal/simpleRangeWithNonConstantEnds.kt"); + } + @TestMetadata("simpleSteppedDownTo.kt") public void testSimpleSteppedDownTo() throws Exception { doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/ranges/literal/simpleSteppedDownTo.kt"); diff --git a/generators/org/jetbrains/jet/generators/tests/GenerateRangesCodegenTestData.java b/generators/org/jetbrains/jet/generators/tests/GenerateRangesCodegenTestData.java index 47ca5ee03af..4678b42d353 100644 --- a/generators/org/jetbrains/jet/generators/tests/GenerateRangesCodegenTestData.java +++ b/generators/org/jetbrains/jet/generators/tests/GenerateRangesCodegenTestData.java @@ -17,6 +17,7 @@ package org.jetbrains.jet.generators.tests; import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.util.text.StringUtil; import com.intellij.util.LineSeparator; import java.io.File; @@ -40,7 +41,7 @@ public class GenerateRangesCodegenTestData { " $LIST.add(i)\n" + " }\n" + " if ($LIST != listOf<$TYPE>($LIST_ELEMENTS)) {\n" + - " return \"Wrong elements for $RANGE_EXPR: $$LIST\"\n" + + " return \"Wrong elements for $RANGE_EXPR_ESCAPED: $$LIST\"\n" + " }\n" + "\n"; @@ -50,7 +51,7 @@ public class GenerateRangesCodegenTestData { " $LIST.add(i)\n" + " }\n" + " if ($LIST != listOf<$TYPE>($LIST_ELEMENTS)) {\n" + - " return \"Wrong elements for $RANGE_EXPR: $$LIST\"\n" + + " return \"Wrong elements for $RANGE_EXPR_ESCAPED: $$LIST\"\n" + " }\n" + "\n"; @@ -59,7 +60,7 @@ public class GenerateRangesCodegenTestData { if (matcher.find()) { return matcher.group(1); } - if (rangeExpression.contains("'")) { + if (rangeExpression.contains("'") || rangeExpression.contains("\"")) { return "Char"; } if (Pattern.compile("\\d\\.\\d").matcher(rangeExpression).find()) { @@ -70,6 +71,7 @@ public class GenerateRangesCodegenTestData { private static String renderTemplate(String template, int number, String elementType, String rangeExpression, String expectedListElements) { return template + .replace("$RANGE_EXPR_ESCAPED", StringUtil.escapeStringCharacters(rangeExpression)) .replace("$RANGE_EXPR", rangeExpression) .replace("$LIST_ELEMENTS", expectedListElements) .replace("$LIST", "list" + number) diff --git a/libraries/stdlib/test/language/RangeIterationTest.kt b/libraries/stdlib/test/language/RangeIterationTest.kt index 59b2c6574d1..2677c350abd 100644 --- a/libraries/stdlib/test/language/RangeIterationTest.kt +++ b/libraries/stdlib/test/language/RangeIterationTest.kt @@ -69,6 +69,20 @@ public class RangeIterationTest { } + test fun simpleRangeWithNonConstantEnds() { + doTest((1 + 2)..(10 - 1), 3, 9, 1, listOf(3, 4, 5, 6, 7, 8, 9)) + doTest((1.toByte() + 2.toByte()).toByte()..(10.toByte() - 1.toByte()).toByte(), 3.toByte(), 9.toByte(), 1, listOf(3, 4, 5, 6, 7, 8, 9)) + doTest((1.toShort() + 2.toShort()).toShort()..(10.toShort() - 1.toShort()).toShort(), 3.toShort(), 9.toShort(), 1, listOf(3, 4, 5, 6, 7, 8, 9)) + doTest((1.toLong() + 2.toLong())..(10.toLong() - 1.toLong()), 3.toLong(), 9.toLong(), 1.toLong(), listOf(3, 4, 5, 6, 7, 8, 9)) + + doTest(("ace"[1])..("age"[1]), 'c', 'g', 1, listOf('c', 'd', 'e', 'f', 'g')) + + doTest((1.5 * 2)..(3.0 * 3.0), 3.0, 9.0, 1.0, listOf(3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)) + doTest((1.5.toFloat() * 2.toFloat())..(3.0.toFloat() * 3.0.toFloat()), 3.0.toFloat(), 9.0.toFloat(), 1.toFloat(), + listOf(3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)) + } + + test fun emptyDownto() { doTest(5 downTo 10, 5, 10, -1, listOf()) doTest(5.toByte() downTo 10.toByte(), 5.toByte(), 10.toByte(), -1, listOf())