3b81d63334
Move test-related code to org.jetbrains.kotlin.test, also move some tests to packages with better names
190 lines
6.7 KiB
Java
190 lines
6.7 KiB
Java
/*
|
|
* Copyright 2010-2015 JetBrains s.r.o.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package org.jetbrains.kotlin.codegen;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.jetbrains.kotlin.test.ConfigurationKind;
|
|
|
|
import java.lang.reflect.Method;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
import static org.jetbrains.kotlin.codegen.CodegenTestUtil.assertThrows;
|
|
|
|
public class ControlStructuresTest extends CodegenTestCase {
|
|
@Override
|
|
protected void setUp() throws Exception {
|
|
super.setUp();
|
|
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
|
|
}
|
|
|
|
@NotNull
|
|
@Override
|
|
protected String getPrefix() {
|
|
return "controlStructures";
|
|
}
|
|
|
|
public void testIf() throws Exception {
|
|
loadFile();
|
|
Method main = generateFunction();
|
|
assertEquals(15, main.invoke(null, true));
|
|
assertEquals(20, main.invoke(null, false));
|
|
}
|
|
|
|
public void testSingleBranchIf() throws Exception {
|
|
loadFile();
|
|
Method main = generateFunction();
|
|
assertEquals(15, main.invoke(null, true));
|
|
assertEquals(20, main.invoke(null, false));
|
|
}
|
|
|
|
public void testWhile() throws Exception {
|
|
factorialTest("controlStructures/while.kt");
|
|
}
|
|
|
|
public void testDoWhile() throws Exception {
|
|
factorialTest("controlStructures/doWhile.kt");
|
|
}
|
|
|
|
public void testBreak() throws Exception {
|
|
factorialTest("controlStructures/break.kt");
|
|
}
|
|
|
|
private void factorialTest(String name) throws Exception {
|
|
loadFile(name);
|
|
Method main = generateFunction();
|
|
assertEquals(6, main.invoke(null, 3));
|
|
assertEquals(120, main.invoke(null, 5));
|
|
}
|
|
|
|
public void testContinue() throws Exception {
|
|
loadFile();
|
|
Method main = generateFunction();
|
|
assertEquals(3, main.invoke(null, 4));
|
|
assertEquals(7, main.invoke(null, 5));
|
|
}
|
|
|
|
public void testIfNoElse() throws Exception {
|
|
loadFile();
|
|
Method main = generateFunction();
|
|
assertEquals(5, main.invoke(null, 5, true));
|
|
assertEquals(10, main.invoke(null, 5, false));
|
|
}
|
|
|
|
public void testCondJumpOnStack() throws Exception {
|
|
loadText("import java.lang.Boolean as jlBoolean; fun foo(a: String): Int = if (jlBoolean.parseBoolean(a)) 5 else 10");
|
|
Method main = generateFunction();
|
|
assertEquals(5, main.invoke(null, "true"));
|
|
assertEquals(10, main.invoke(null, "false"));
|
|
}
|
|
|
|
public void testFor() throws Exception {
|
|
loadFile();
|
|
Method main = generateFunction();
|
|
List<String> args = Arrays.asList("IntelliJ", " ", "IDEA");
|
|
assertEquals("IntelliJ IDEA", main.invoke(null, args));
|
|
}
|
|
|
|
public void testIfBlock() throws Exception {
|
|
loadFile();
|
|
Method main = generateFunction();
|
|
List<String> args = Arrays.asList("IntelliJ", " ", "IDEA");
|
|
assertEquals("TTT", main.invoke(null, args));
|
|
args = Arrays.asList("JetBrains");
|
|
assertEquals("F", main.invoke(null, args));
|
|
}
|
|
|
|
public void testForInArray() throws Exception {
|
|
loadFile();
|
|
Method main = generateFunction();
|
|
String[] args = new String[] { "IntelliJ", " ", "IDEA" };
|
|
assertEquals("IntelliJ IDEA", main.invoke(null, new Object[] { args }));
|
|
}
|
|
|
|
public void testForInRange() throws Exception {
|
|
loadText("fun foo(sb: StringBuilder) { for(x in 1..4) sb.append(x) }");
|
|
Method main = generateFunction();
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
main.invoke(null, stringBuilder);
|
|
assertEquals("1234", stringBuilder.toString());
|
|
}
|
|
|
|
public void testThrowCheckedException() throws Exception {
|
|
loadText("fun foo() { throw Exception(); }");
|
|
Method main = generateFunction();
|
|
assertThrows(main, Exception.class, null);
|
|
}
|
|
|
|
public void testTryCatch() throws Exception {
|
|
loadFile();
|
|
Method main = generateFunction();
|
|
assertEquals("no message", main.invoke(null, "0"));
|
|
assertEquals("For input string: \"a\"", main.invoke(null, "a"));
|
|
}
|
|
|
|
public void testTryFinally() throws Exception {
|
|
loadFile();
|
|
Method main = generateFunction();
|
|
StringBuilder sb = new StringBuilder();
|
|
main.invoke(null, sb, "9");
|
|
assertEquals("foo9bar", sb.toString());
|
|
sb = new StringBuilder();
|
|
assertThrows(main, NumberFormatException.class, null, sb, "x");
|
|
assertEquals("foobar", sb.toString());
|
|
}
|
|
|
|
public void testCompareToZero() throws Exception {
|
|
loadText("fun foo(a: Int, b: Int): Boolean = a == 0 && b != 0 && 0 == a && 0 != b");
|
|
String text = generateToText();
|
|
/*
|
|
* Check that the we generate optimized byte-code!
|
|
*/
|
|
assertTrue(text.contains("IFEQ"));
|
|
assertTrue(text.contains("IFNE"));
|
|
assertFalse(text.contains("IF_ICMPEQ"));
|
|
assertFalse(text.contains("IF_ICMPNE"));
|
|
Method main = generateFunction();
|
|
assertEquals(true, main.invoke(null, 0, 1));
|
|
assertEquals(false, main.invoke(null, 1, 0));
|
|
}
|
|
|
|
public void testCompareToNull() throws Exception {
|
|
loadText("fun foo(a: String?, b: String?): Boolean = a == null && b !== null && null == a && null !== b");
|
|
String text = generateToText();
|
|
assertTrue(!text.contains("java/lang/Object.equals"));
|
|
Method main = generateFunction();
|
|
assertEquals(true, main.invoke(null, null, "lala"));
|
|
assertEquals(false, main.invoke(null, null, null));
|
|
}
|
|
|
|
public void testCompareToNonnullableEq() throws Exception {
|
|
loadText("fun foo(a: String?, b: String): Boolean = a == b || b == a");
|
|
Method main = generateFunction();
|
|
assertEquals(false, main.invoke(null, null, "lala"));
|
|
assertEquals(true, main.invoke(null, "papa", "papa"));
|
|
}
|
|
|
|
public void testCompareToNonnullableNotEq() throws Exception {
|
|
loadText("fun foo(a: String?, b: String): Boolean = a != b");
|
|
String text = generateToText();
|
|
assertTrue(text.contains("IXOR"));
|
|
Method main = generateFunction();
|
|
assertEquals(true, main.invoke(null, null, "lala"));
|
|
assertEquals(false, main.invoke(null, "papa", "papa"));
|
|
}
|
|
}
|