add test files

This commit is contained in:
pTalanov
2012-02-27 21:55:57 +04:00
parent 645a3a9f2f
commit c21ea8aa86
240 changed files with 16998 additions and 0 deletions
@@ -0,0 +1,163 @@
/*
* Copyright 2000-2012 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.k2js.translate.utils;
import com.intellij.psi.PsiElement;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lexer.JetToken;
import org.jetbrains.jet.lexer.JetTokens;
import org.jetbrains.k2js.translate.context.Namer;
import java.util.Collections;
import java.util.List;
/**
* @author Pavel Talanov
*/
public final class PsiUtils {
private PsiUtils() {
}
@Nullable
public static JetSimpleNameExpression getSelectorAsSimpleName(@NotNull JetQualifiedExpression expression) {
JetExpression selectorExpression = getSelector(expression);
if (!(selectorExpression instanceof JetSimpleNameExpression)) {
return null;
}
return (JetSimpleNameExpression) selectorExpression;
}
@NotNull
public static JetExpression getSelector(@NotNull JetQualifiedExpression expression) {
JetExpression selectorExpression = expression.getSelectorExpression();
assert selectorExpression != null : "Selector should not be null.";
return selectorExpression;
}
@NotNull
public static JetSimpleNameExpression getNotNullSimpleNameSelector(@NotNull JetQualifiedExpression expression) {
JetSimpleNameExpression selectorAsSimpleName = getSelectorAsSimpleName(expression);
assert selectorAsSimpleName != null;
return selectorAsSimpleName;
}
@NotNull
public static JetToken getOperationToken(@NotNull JetOperationExpression expression) {
JetSimpleNameExpression operationExpression = expression.getOperationReference();
IElementType elementType = operationExpression.getReferencedNameElementType();
assert elementType instanceof JetToken : "Unary expression should have operation token of type JetToken";
return (JetToken) elementType;
}
@NotNull
public static JetExpression getBaseExpression(@NotNull JetUnaryExpression expression) {
JetExpression baseExpression = expression.getBaseExpression();
assert baseExpression != null;
return baseExpression;
}
public static boolean isPrefix(@NotNull JetUnaryExpression expression) {
return (expression instanceof JetPrefixExpression);
}
public static boolean isAssignment(JetToken token) {
return (token == JetTokens.EQ);
}
public static boolean isBackingFieldReference(@NotNull JetSimpleNameExpression expression) {
return expression.getReferencedNameElementType() == JetTokens.FIELD_IDENTIFIER;
}
public static boolean isInOrNotInOperation(@NotNull JetBinaryExpression binaryExpression) {
return isInOperation(binaryExpression) || isNotInOperation(binaryExpression);
}
public static boolean isNotInOperation(@NotNull JetBinaryExpression binaryExpression) {
return (binaryExpression.getOperationToken() == JetTokens.NOT_IN);
}
private static boolean isInOperation(@NotNull JetBinaryExpression binaryExpression) {
return (binaryExpression.getOperationToken() == JetTokens.IN_KEYWORD);
}
@NotNull
public static JetExpression getCallee(@NotNull JetCallExpression expression) {
JetExpression calleeExpression = expression.getCalleeExpression();
assert calleeExpression != null;
return calleeExpression;
}
@NotNull
public static JetExpression getLoopBody(@NotNull JetLoopExpression expression) {
JetExpression body = expression.getBody();
assert body != null : "Loops cannot have null bodies.";
return body;
}
@NotNull
public static JetParameter getLoopParameter(@NotNull JetForExpression expression) {
JetParameter loopParameter = expression.getLoopParameter();
assert loopParameter != null;
return loopParameter;
}
@NotNull
public static List<JetParameter> getPrimaryConstructorParameters(@NotNull JetClassOrObject classDeclaration) {
if (classDeclaration instanceof JetClass) {
return ((JetClass) classDeclaration).getPrimaryConstructorParameters();
}
return Collections.emptyList();
}
@NotNull
public static JetObjectDeclaration getObjectDeclarationForName(@NotNull JetObjectDeclarationName name) {
PsiElement parent = name.getParent();
assert parent instanceof JetObjectDeclaration :
"ObjectDeclarationName should have a parent of type ObjectDeclaration.";
return (JetObjectDeclaration) parent;
}
@NotNull
public static JetObjectDeclarationName getObjectDeclarationName(@NotNull JetObjectDeclaration objectDeclaration) {
//TODO: util
JetObjectDeclarationName nameAsDeclaration = objectDeclaration.getNameAsDeclaration();
assert nameAsDeclaration != null;
return nameAsDeclaration;
}
@NotNull
public static String getNamespaceName(@NotNull JetFile psiFile) {
JetNamespaceHeader namespaceHeader = psiFile.getNamespaceHeader();
String name = namespaceHeader.getName();
assert name != null : "NamespaceHeader must have a name";
if (name.equals("")) {
return Namer.getAnonymousNamespaceName();
}
return name;
}
@NotNull
public static JetExpression getLoopRange(@NotNull JetForExpression expression) {
JetExpression rangeExpression = expression.getLoopRange();
assert rangeExpression != null;
return rangeExpression;
}
}
@@ -0,0 +1,230 @@
/*
* Copyright 2000-2012 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.k2js.translate.utils;
import com.google.dart.compiler.backend.js.ast.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.k2js.translate.context.TemporaryVariable;
import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.general.Translation;
import java.util.ArrayList;
import java.util.List;
import static org.jetbrains.k2js.translate.utils.BindingUtils.*;
import static org.jetbrains.k2js.translate.utils.DescriptorUtils.getExpectedReceiverDescriptor;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.*;
/**
* @author Pavel Talanov
*/
public final class TranslationUtils {
private TranslationUtils() {
}
@NotNull
public static JsBinaryOperation notNullCheck(@NotNull TranslationContext context,
@NotNull JsExpression expressionToCheck) {
JsNullLiteral nullLiteral = context.program().getNullLiteral();
return notEqual(expressionToCheck, nullLiteral);
}
@NotNull
public static JsBinaryOperation isNullCheck(@NotNull TranslationContext context,
@NotNull JsExpression expressionToCheck) {
JsNullLiteral nullLiteral = context.program().getNullLiteral();
return JsAstUtils.equals(expressionToCheck, nullLiteral);
}
@NotNull
public static List<JsExpression> translateArgumentList(@NotNull TranslationContext context,
@NotNull List<? extends ValueArgument> jetArguments) {
List<JsExpression> jsArguments = new ArrayList<JsExpression>();
for (ValueArgument argument : jetArguments) {
jsArguments.add(translateArgument(context, argument));
}
return jsArguments;
}
@NotNull
private static JsExpression translateArgument(@NotNull TranslationContext context, @NotNull ValueArgument argument) {
JetExpression jetExpression = argument.getArgumentExpression();
assert jetExpression != null : "Argument with no expression";
return Translation.translateAsExpression(jetExpression, context);
}
//TODO: refactor backing field reference generation to use the generic way
@NotNull
public static JsNameRef backingFieldReference(@NotNull TranslationContext context,
@NotNull JetProperty expression) {
PropertyDescriptor propertyDescriptor = getPropertyDescriptor(context.bindingContext(), expression);
return backingFieldReference(context, propertyDescriptor);
}
@NotNull
public static JsNameRef backingFieldReference(@NotNull TranslationContext context,
@NotNull PropertyDescriptor descriptor) {
JsName backingFieldName = context.getNameForDescriptor(descriptor);
if (isOwnedByClass(descriptor)) {
return qualified(backingFieldName, new JsThisRef());
}
assert isOwnedByNamespace(descriptor)
: "Only classes and namespaces may own backing fields.";
JsNameRef qualifier = context.getQualifierForDescriptor(descriptor);
return qualified(backingFieldName, qualifier);
}
@NotNull
public static JsStatement assignmentToBackingField(@NotNull TranslationContext context,
@NotNull PropertyDescriptor descriptor,
@NotNull JsExpression assignTo) {
JsNameRef backingFieldReference = backingFieldReference(context, descriptor);
return newAssignmentStatement(backingFieldReference, assignTo);
}
@Nullable
public static JsExpression translateInitializerForProperty(@NotNull JetProperty declaration,
@NotNull TranslationContext context) {
JsExpression jsInitExpression = null;
JetExpression initializer = declaration.getInitializer();
if (initializer != null) {
jsInitExpression = Translation.translateAsExpression(initializer, context);
}
return jsInitExpression;
}
@NotNull
public static JsNameRef getQualifiedReference(@NotNull TranslationContext context,
@NotNull DeclarationDescriptor descriptor) {
JsName name = context.getNameForDescriptor(descriptor);
JsNameRef reference = name.makeRef();
JsNameRef qualifier = context.getQualifierForDescriptor(descriptor);
if (qualifier != null) {
setQualifier(reference, qualifier);
}
return reference;
}
//TODO: refactor
@NotNull
public static JsExpression getThisObject(@NotNull TranslationContext context,
@NotNull DeclarationDescriptor correspondingDeclaration) {
JsExpression thisRef = null;
if (correspondingDeclaration instanceof ClassDescriptor) {
if (context.aliaser().hasAliasForThis(correspondingDeclaration)) {
thisRef = context.aliaser().getAliasForThis(correspondingDeclaration);
}
}
if (correspondingDeclaration instanceof CallableDescriptor) {
DeclarationDescriptor receiverDescriptor =
getExpectedReceiverDescriptor((CallableDescriptor) correspondingDeclaration);
assert receiverDescriptor != null;
if (context.aliaser().hasAliasForThis(receiverDescriptor)) {
thisRef = context.aliaser().getAliasForThis(receiverDescriptor);
}
}
if (thisRef != null) {
return thisRef;
}
return new JsThisRef();
}
@NotNull
public static List<JsExpression> translateExpressionList(@NotNull TranslationContext context,
@NotNull List<JetExpression> expressions) {
List<JsExpression> result = new ArrayList<JsExpression>();
for (JetExpression expression : expressions) {
result.add(Translation.translateAsExpression(expression, context));
}
return result;
}
@NotNull
public static JsExpression translateBaseExpression(@NotNull TranslationContext context,
@NotNull JetUnaryExpression expression) {
JetExpression baseExpression = PsiUtils.getBaseExpression(expression);
return Translation.translateAsExpression(baseExpression, context);
}
//TODO:
@NotNull
public static JsExpression translateReceiver(@NotNull TranslationContext context,
@NotNull JetDotQualifiedExpression expression) {
return Translation.translateAsExpression(expression.getReceiverExpression(), context);
}
@NotNull
public static JsExpression translateLeftExpression(@NotNull TranslationContext context,
@NotNull JetBinaryExpression expression) {
return Translation.translateAsExpression(expression.getLeft(), context);
}
@NotNull
public static JsExpression translateRightExpression(@NotNull TranslationContext context,
@NotNull JetBinaryExpression expression) {
JetExpression rightExpression = expression.getRight();
assert rightExpression != null : "Binary expression should have a right expression";
return Translation.translateAsExpression(rightExpression, context);
}
public static boolean isIntrinsicOperation(@NotNull TranslationContext context,
@NotNull JetOperationExpression expression) {
FunctionDescriptor operationDescriptor =
BindingUtils.getFunctionDescriptorForOperationExpression(context.bindingContext(), expression);
if (operationDescriptor == null) return true;
if (context.intrinsics().isIntrinsic(operationDescriptor)) return true;
return false;
}
@NotNull
public static JsNameRef getMethodReferenceForOverloadedOperation(@NotNull TranslationContext context,
@NotNull JetOperationExpression expression) {
FunctionDescriptor overloadedOperationDescriptor = getFunctionDescriptorForOperationExpression
(context.bindingContext(), expression);
assert overloadedOperationDescriptor != null;
JsNameRef overloadedOperationReference = context.getNameForDescriptor(overloadedOperationDescriptor).makeRef();
assert overloadedOperationReference != null;
return overloadedOperationReference;
}
@NotNull
public static JsNumberLiteral zeroLiteral(@NotNull TranslationContext context) {
return context.program().getNumberLiteral(0);
}
@NotNull
public static TemporaryVariable newAliasForThis(@NotNull TranslationContext context,
@NotNull DeclarationDescriptor descriptor) {
JsExpression thisQualifier = getThisObject(context, descriptor);
TemporaryVariable aliasForThis = context.declareTemporary(thisQualifier);
context.aliaser().setAliasForThis(descriptor, aliasForThis.name());
return aliasForThis;
}
public static void removeAliasForThis(@NotNull TranslationContext context,
@NotNull DeclarationDescriptor descriptor) {
context.aliaser().removeAliasForThis(descriptor);
}
}
@@ -0,0 +1,43 @@
/*
* Copyright 2000-2012 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.k2js.utils;
import org.jetbrains.annotations.NotNull;
import java.util.List;
//TODO: very thin class
/**
* @author Pavel Talanov
*/
public final class GenerationUtils {
@NotNull
public static String generateCallToMain(@NotNull String namespaceName, @NotNull List<String> arguments) {
String constructArguments = "var args = [];\n";
int index = 0;
for (String argument : arguments) {
constructArguments = constructArguments + "args[" + index + "]= \"" + argument + "\";\n";
index++;
}
String callMain = namespaceName + ".main(args);\n";
return constructArguments + callMain;
}
}
@@ -0,0 +1,89 @@
/*
* Copyright 2000-2012 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.k2js.utils;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.impl.PsiFileFactoryImpl;
import com.intellij.testFramework.LightVirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.plugin.JetLanguage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author Pavel Talanov
*/
public final class JetFileUtils {
@NotNull
public static String loadFile(@NotNull String path) throws IOException {
return doLoadFile(path);
}
@NotNull
private static String doLoadFile(@NotNull String path) throws IOException {
String text = FileUtil.loadFile(new File(path), CharsetToolkit.UTF8).trim();
text = StringUtil.convertLineSeparators(text);
return text;
}
@NotNull
public static JetFile createPsiFile(@NotNull String name,
@NotNull String text,
@NotNull Project project) {
return (JetFile) createFile(name + ".jet", text, project);
}
@NotNull
public static JetFile loadPsiFile(@NotNull String name, @NotNull Project project) {
try {
return createPsiFile(name, loadFile(name), project);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@NotNull
private static PsiFile createFile(@NotNull String name, @NotNull String text, @NotNull Project project) {
LightVirtualFile virtualFile = new LightVirtualFile(name, JetLanguage.INSTANCE, text);
virtualFile.setCharset(CharsetToolkit.UTF8_CHARSET);
PsiFile result = ((PsiFileFactoryImpl) PsiFileFactory.getInstance(project))
.trySetupPsiForFile(virtualFile, JetLanguage.INSTANCE, true, false);
assert result != null;
return result;
}
@NotNull
public static List<JetFile> createPsiFileList(@NotNull List<String> inputFiles,
@NotNull Project project) {
List<JetFile> psiFiles = new ArrayList<JetFile>();
for (String inputFile : inputFiles) {
psiFiles.add(JetFileUtils.loadPsiFile(inputFile, project));
}
return psiFiles;
}
}
@@ -0,0 +1,180 @@
/*
* Copyright 2000-2012 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.k2js.utils;
import com.google.common.base.Predicates;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.StandardConfiguration;
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.diagnostics.Severity;
import org.jetbrains.jet.lang.diagnostics.UnresolvedReferenceDiagnostic;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.resolve.BindingTraceContext;
import org.jetbrains.jet.util.slicedmap.ReadOnlySlice;
import org.jetbrains.jet.util.slicedmap.SlicedMap;
import org.jetbrains.jet.util.slicedmap.WritableSlice;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
/**
* @author abreslav
*/
public class JetTestUtils {
public static final BindingTrace DUMMY_TRACE = new BindingTrace() {
@Override
public BindingContext getBindingContext() {
return new BindingContext() {
@Override
public Collection<Diagnostic> getDiagnostics() {
throw new UnsupportedOperationException(); // TODO
}
@Override
public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
return DUMMY_TRACE.get(slice, key);
}
@NotNull
@Override
public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
return DUMMY_TRACE.getKeys(slice);
}
};
}
@Override
public <K, V> void record(WritableSlice<K, V> slice, K key, V value) {
}
@Override
public <K> void record(WritableSlice<K, Boolean> slice, K key) {
}
@Override
public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
if (slice == BindingContext.PROCESSED) return (V) Boolean.FALSE;
return SlicedMap.DO_NOTHING.get(slice, key);
}
@NotNull
@Override
public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
assert slice.isCollective();
return Collections.emptySet();
}
@Override
public void report(@NotNull Diagnostic diagnostic) {
if (diagnostic instanceof UnresolvedReferenceDiagnostic) {
UnresolvedReferenceDiagnostic unresolvedReferenceDiagnostic = (UnresolvedReferenceDiagnostic) diagnostic;
throw new IllegalStateException("Unresolved: " + unresolvedReferenceDiagnostic.getPsiElement().getText());
}
}
};
public static BindingTrace DUMMY_EXCEPTION_ON_ERROR_TRACE = new BindingTrace() {
@Override
public BindingContext getBindingContext() {
return new BindingContext() {
@Override
public Collection<Diagnostic> getDiagnostics() {
throw new UnsupportedOperationException();
}
@Override
public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
return DUMMY_EXCEPTION_ON_ERROR_TRACE.get(slice, key);
}
@NotNull
@Override
public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
return DUMMY_EXCEPTION_ON_ERROR_TRACE.getKeys(slice);
}
};
}
@Override
public <K, V> void record(WritableSlice<K, V> slice, K key, V value) {
}
@Override
public <K> void record(WritableSlice<K, Boolean> slice, K key) {
}
@Override
public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
return null;
}
@NotNull
@Override
public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
assert slice.isCollective();
return Collections.emptySet();
}
@Override
public void report(@NotNull Diagnostic diagnostic) {
if (diagnostic.getSeverity() == Severity.ERROR) {
throw new IllegalStateException(diagnostic.getMessage());
}
}
};
public static void mkdirs(File file) throws IOException {
if (file.isDirectory()) {
return;
}
if (!file.mkdirs()) {
throw new IOException();
}
}
public static void rmrf(File file) {
if (file != null) {
File[] children = file.listFiles();
if (children != null) {
for (File child : children) {
rmrf(child);
}
}
file.delete();
}
}
public static void recreateDirectory(File file) throws IOException {
rmrf(file);
mkdirs(file);
}
}
@@ -0,0 +1,27 @@
/*
* Copyright 2000-2012 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.
*/
function getContext() {
return getCanvas().getContext('2d');
}
function getCanvas() {
return document.getElementsByTagName('canvas')[0];
}
function getKotlinLogo() {
return document.getElementsByTagName('img')[0];
}
@@ -0,0 +1,7 @@
class A(var a : Int) {
{
$a=3
}
}
fun box() = (A(1).a == 3)
@@ -0,0 +1,7 @@
fun box() : String {
return apply( "OK", {(arg: String) -> arg } )
}
fun apply(arg : String, f : (p:String) -> String) : String {
return f(arg)
}
@@ -0,0 +1,7 @@
fun box() : String {
return if (apply( 5, {(arg: Int) -> arg + 13 } ) == 18) "OK" else "fail"
}
fun apply(arg : Int, f : (p:Int) -> Int) : Int {
return f(arg)
}
@@ -0,0 +1,24 @@
// Changed when traits were introduced. May not make sense any more
open class Base() {
public var v : Int = 0
}
open class Left() : Base() {}
trait Right : Base {}
class D() : Left(), Right
fun vl(l : Left) : Int = l.v
fun vr(r : Right) : Int = r.v
fun box() : String {
val d = D()
d.v = 42
if (d.v != 42) return "Fail #1"
if (vl(d) != 42) return "Fail #2"
if (vr(d) != 42) return "Fail #3"
return "OK"
}
@@ -0,0 +1,8 @@
fun box() : String {
val cl = 39
return if (sum(200, { val ff = {cl}; ff() }) == 239) "OK" else "FAIL"
}
fun sum(arg:Int, f : () -> Int) : Int {
return arg + f()
}
@@ -0,0 +1,8 @@
fun box() : String {
val cl = 39
return if (sum(200, { val m = { val r = { cl }; r() }; m() }) == 239) "OK" else "FAIL"
}
fun sum(arg:Int, f : () -> Int) : Int {
return arg + f()
}
@@ -0,0 +1,13 @@
class Point(val x : Int, val y : Int)
fun box() : String {
val answer = apply(Point(3, 5), { Point.(scalar : Int) : Point ->
Point(x * scalar, y * scalar)
})
return if (answer.x == 6 && answer.y == 10) "OK" else "FAIL"
}
fun apply(arg:Point, f : Point.(scalar : Int) -> Point) : Point {
return arg.f(2)
}
@@ -0,0 +1,17 @@
open class Base() {
fun n(n : Int) : Int = n + 1
}
trait Abstract {}
class Derived1() : Base(), Abstract {}
class Derived2() : Abstract, Base() {}
fun test(s : Base) : Boolean = s.n(238) == 239
fun box() : String {
if (!test(Base())) return "Fail #1"
if (!test(Derived1())) return "Fail #2"
if (!test(Derived2())) return "Fail #3"
return "OK"
}
@@ -0,0 +1,14 @@
class Slot() {
var vitality: Int = 10000
fun increaseVitality(delta: Int) {
vitality = vitality + delta
if (vitality > 65535) vitality = 65535;
}
}
fun box(): String {
val s = Slot()
s.increaseVitality(1000)
if (s.vitality == 11000) return "OK" else return "fail"
}
@@ -0,0 +1,13 @@
open class Foo() {
fun xyzzy(): String = "xyzzy"
}
class Bar(): Foo() {
fun test(): String = xyzzy()
}
fun box() : String {
val bar = Bar()
val f = bar.test()
return if (f == "xyzzy") "OK" else "fail"
}
@@ -0,0 +1,13 @@
class C() {
public var f: Int
{
$f = 610
}
}
fun box(): String {
val c = C()
if (c.f != 610) return "fail"
return "OK"
}
@@ -0,0 +1,19 @@
fun box() : String {
val i: Int? = 7
val j: Int? = null
val k = 7
//verify errors
if (i == 7) {}
if (7 == i) {}
if (j == 7) {}
if (7 == j) {}
if (i == k) {}
if (k == i) {}
if (j == k) {}
if (k == j) {}
return "OK"
}
@@ -0,0 +1,11 @@
class SimpleClass() {
fun foo() = 610
}
fun box() : String {
val c = SimpleClass()
if (c.foo() == 610) {
return "OK"
}
return "FAIL"
}
@@ -0,0 +1,25 @@
import java.util.*
class ArrayWrapper<T>() {
val contents = ArrayList<T>()
fun add(item: T) {
contents.add(item)
}
fun plus(b: ArrayWrapper<T>): ArrayWrapper<T> {
val result = ArrayWrapper<T>()
result.contents.addAll(contents)
result.contents.addAll(b.contents)
return result
}
}
fun box(): String {
val v1 = ArrayWrapper<String>()
val v2 = ArrayWrapper<String>()
v1.add("foo")
v2.add("bar")
val v3 = v1 + v2
return if (v3.contents.size() == 2) "OK" else "fail"
}
@@ -0,0 +1,27 @@
import java.util.*
class ArrayWrapper<T>() {
val contents = ArrayList<T>()
fun add(item: T) {
contents.add(item)
}
fun plusAssign(rhs: ArrayWrapper<T>) {
contents.addAll(rhs.contents)
}
fun get(index: Int): T {
return contents.get(index)
}
}
fun box(): String {
var v1 = ArrayWrapper<String>()
val v2 = ArrayWrapper<String>()
v1.add("foo")
val v3 = v1
v2.add("bar")
v1 += v2
return if (v1.contents.size() == 2 && v3.contents.size() == 2) "OK" else "fail"
}
@@ -0,0 +1,30 @@
import java.util.*
class ArrayWrapper<T>() {
val contents = ArrayList<T>()
fun add(item: T) {
contents.add(item)
}
fun plus(rhs: ArrayWrapper<T>): ArrayWrapper<T> {
val result = ArrayWrapper<T>()
result.contents.addAll(contents)
result.contents.addAll(rhs.contents)
return result
}
fun get(index: Int): T {
return contents.get(index)
}
}
fun box(): String {
var v1 = ArrayWrapper<String>()
val v2 = ArrayWrapper<String>()
v1.add("foo")
val v3 = v1
v2.add("bar")
v1 += v2
return if (v1.contents.size() == 2 && v3.contents.size() == 1) "OK" else "fail"
}
@@ -0,0 +1,31 @@
import java.util.*
class ArrayWrapper<T>() {
val contents = ArrayList<T>()
fun add(item: T) {
contents.add(item)
}
fun minus(): ArrayWrapper<T> {
val result = ArrayWrapper<T>()
result.contents.addAll(contents)
var i = contents.size();
for (a in contents) {
result.contents[--i] = a;
}
return result
}
fun get(index: Int): T {
return contents.get(index)
}
}
fun box(): String {
val v1 = ArrayWrapper<String>()
v1.add("foo")
v1.add("bar")
val v2 = -v1
return if (v2[0] == "bar" && v2[1] == "foo") "OK" else "fail"
}
@@ -0,0 +1,33 @@
open class Base() {
val plain = 239
public val read : Int
get() = 239
public var readwrite : Int = 0
get() = $readwrite + 1
set(n : Int) {
$readwrite = n
}
}
trait Abstract {}
class Derived1() : Base(), Abstract {}
class Derived2() : Abstract, Base() {}
fun code(s : Base) : Int {
if (s.plain != 239) return 1
if (s.read != 239) return 2
s.readwrite = 238
if (s.readwrite != 239) return 3
return 0
}
fun test(s : Base) : Boolean = code(s) == 0
fun box() : String {
if (!test(Base())) return "Fail #1"
if (!test(Derived1())) return "Fail #2"
if (!test(Derived2())) return "Fail #3"
return "OK"
}
@@ -0,0 +1,20 @@
// Changed when traits were introduced. May not make sense any more
trait Left {}
open class Right() {
open fun f() = 42
}
class D() : Left, Right() {
override fun f() = 239
}
fun box() : String {
val r : Right = Right()
val d : D = D()
if (r.f() != 42) return "Fail #1"
if (d.f() != 239) return "Fail #2"
return "OK"
}
@@ -0,0 +1,7 @@
fun box() : String {
return invoker( {"OK"} )
}
fun invoker(gen : () -> String) : String {
return gen()
}
@@ -0,0 +1,7 @@
fun box() : String {
return if (int_invoker( { 7 } ) == 7) "OK" else "fail"
}
fun int_invoker(gen : () -> Int) : Int {
return gen()
}
@@ -0,0 +1,21 @@
import java.util.ArrayList
trait Tr {
fun extra() : String = "_"
}
class N() : ArrayList<Any>(), Tr {
override fun add(el: Any) : Boolean {
super<ArrayList>.add(el)
return super<ArrayList>.add(el.toString() + super<Tr>.extra() + el + extra())
}
override fun extra() : String = super<Tr>.extra() + super<Tr>.extra()
}
fun box(): String {
val n = N()
n.add("239")
if (n.get(0) == "239" && n.get(1) == "239_239__") return "OK";
return "fail";
}
@@ -0,0 +1,22 @@
open class M() {
open var b: Int = 0
}
class N() : M() {
val a : Int
get() {
super.b = super.b + 1
return super.b + 1
}
override var b: Int = a + 1
val superb : Int
get() = super.b
}
fun box(): String {
val n = N()
println("a: " + n.a + " b: " + n.b + " superb: " + n.superb)
if (n.b == 3 && n.a == 4 && n.superb == 3) return "OK";
return "fail";
}
@@ -0,0 +1,11 @@
class C() {
class object {
fun create() = C()
}
}
fun box(): String {
val c = C.create()
return if (c is C) "OK" else "fail"
}
@@ -0,0 +1,9 @@
class C() {
fun getInstance(): Runnable = C
class object: Runnable {
override fun run(): Unit { }
}
}
fun foo() = C().getInstance()
@@ -0,0 +1,13 @@
abstract open class Default {
abstract fun defaultValue(): Int
}
class MyInt() {
class object : Default {
override fun defaultValue(): Int = 610
}
}
fun toDefault<T: Any>(t: T) where class object T: Default = T.defaultValue()
fun box(): String = if (toDefault<MyInt>(MyInt()) == 610) "OK" else "fail"
@@ -0,0 +1,42 @@
trait BK {
fun x() : Int = 50
}
trait K : BK {
override fun x() : Int = super.x() * 2
}
open class M() {
open fun x() : Int = 10
open var y = 500
}
open class N() : M(), K {
override fun x() : Int = 20
override var y = 200
open class C() : K {
fun test1() = x()
fun test2() = super<M>@N.x()
fun test3() = super<K>@N.x()
fun test4() = super<K>.x()
fun test5() = y
fun test6() : Int {
super<M>@N.y += 200
return super<M>@N.y
}
}
}
fun box(): String {
if (N().C().test1() != 100) return "test1 fail";
if (N().C().test2() != 10) return "test2 fail";
if (N().C().test3() != 100) return "test3 fail";
if (N().C().test4() != 100) return "test4 fail";
if (N().C().test5() != 200) return "test5 fail";
if (N().C().test6() != 700) return "test6 fail";
return "OK";
}
@@ -0,0 +1,12 @@
class Foo() { }
class Bar() { }
fun isInstance<T>(obj: Any?) = obj is T
fun isInstance2<T>(obj: Any?) = isInstance<T>(obj)
fun box(): String {
if (! isInstance2<Foo>(Foo())) return "fail 1"
if (isInstance2<Bar>(Foo())) return "fail 2"
return "OK"
}
@@ -0,0 +1,41 @@
// Changed when traits were introduced. May not make sense any more
open class X(val x : Int) {}
trait Y {
abstract val y : Int
}
class YImpl(override val y : Int) : Y {}
class Point(x : Int, yy : Int) : X(x) , Y {
override val y : Int = yy
}
trait Abstract {}
class P1(x : Int, yy : Y) : Abstract, X(x), Y by yy {}
class P2(x : Int, yy : Y) : X(x), Abstract, Y by yy {}
class P3(x : Int, yy : Y) : X(x), Y by yy, Abstract {}
class P4(x : Int, yy : Y) : Y by yy, Abstract, X(x) {}
fun box() : String {
if (X(239).x != 239) return "FAIL #1"
if (YImpl(239).y != 239) return "FAIL #2"
val p = Point(240, -1)
if (p.x + p.y != 239) return "FAIL #3"
val y = YImpl(-1)
val p1 = P1(240, y)
if (p1.x + p1.y != 239) return "FAIL #4"
val p2 = P2(240, y)
if (p2.x + p2.y != 239) return "FAIL #5"
val p3 = P3(240, y)
if (p3.x + p3.y != 239) return "FAIL #6"
val p4 = P4(240, y)
if (p4.x + p4.y != 239) return "FAIL #7"
return "OK"
}
@@ -0,0 +1,14 @@
class Outer() {
open class InnerBase() {
}
class InnerDerived(): InnerBase() {
}
public val foo: InnerBase? = InnerDerived()
}
fun box() : String {
val o = Outer()
return if (o.foo === null) "fail" else "OK"
}
@@ -0,0 +1,20 @@
import java.util.*
import java.io.*
class World() {
public val items: ArrayList<Item> = ArrayList<Item>
class Item() {
{
items.add(this)
}
}
val foo = Item()
}
fun box() : String {
val w = World()
if (w.items.size() != 1) return "fail"
return "OK"
}
@@ -0,0 +1,19 @@
class Outer(val foo: StringBuilder) {
class Inner() {
fun len() : Int {
return foo.length()
}
}
fun test() : Inner {
return Inner()
}
}
fun box() : String {
val sb = StringBuilder("xyzzy")
val o = Outer(sb)
val i = o.test()
val l = i.len()
return if (l != 5) "fail" else "OK"
}
@@ -0,0 +1,16 @@
class Outer() {
val s = "xyzzy"
open class InnerBase(public val name: String) {
}
class InnerDerived(): InnerBase(s) {
}
val x = InnerDerived()
}
fun box() : String {
val o = Outer()
return if (o.x.name != "xyzzy") "fail" else "OK"
}
@@ -0,0 +1,8 @@
class Box<T>(t: T) {
var value = t
}
fun box(): String {
val box: Box<Int> = Box<Int>(1)
return if (box.value == 1) "OK" else "fail"
}
@@ -0,0 +1,29 @@
trait M {
var backingB : Int
var b : Int
get() = backingB
set(value: Int) {
backingB = value
}
}
class N() : M {
override var backingB : Int = 0
val a : Int
get() {
super.b = super.b + 1
return super.b + 1
}
override var b: Int = a + 1
val superb : Int
get() = super.b
}
fun box(): String {
val n = N()
println("a: " + n.a + " b: " + n.b + " superb: " + n.superb)
if (n.b == 3 && n.a == 4 && n.superb == 3) return "OK";
return "fail";
}
@@ -0,0 +1,7 @@
class GameError(msg: String): Exception(msg) {
}
fun box(): String {
val e = GameError("foo")
return if (e.getMessage() == "foo") "OK" else "fail"
}
@@ -0,0 +1,3 @@
package foo
fun box() = if (true) throw Exception() else false
@@ -0,0 +1,19 @@
package foo
val a1 = Array<Int>(10)
fun box() : Boolean {
var c = 0
var d = 0
a1[3] = 3
a1[5] = 5
for (var a : Int? in a1) {
if (a != null) {
c += 1;
} else {
d += 1
}
}
return (c == 2) && (d == 8)
}
@@ -0,0 +1,10 @@
package foo
val a1 = Array<Int>(0)
fun box() : Boolean {
for (a in a1) {
return false;
}
return true;
}
@@ -0,0 +1,67 @@
import java.util.*
class Lifetime() {
val attached = ArrayList< Function0<Unit> >()
public fun attach(action : ()->Unit)
{
attached.add(action)
}
fun close()
{
for(x in attached) x()
attached.clear()
}
}
public class Viewable<T>()
{
val items = ArrayList<T>()
fun add(item:T)
{
items.add(item)
}
fun remove(item:T)
{
items.remove(item)
}
fun view(lifetime: Lifetime, viewer: (itemLifetime:Lifetime, item:T) -> Unit)
{
for(item in items)
viewer(lifetime, item)
}
}
fun lifetime(body: (Lifetime)->Unit)
{
val l = Lifetime()
body(l)
l.close()
}
fun<T> Dump(items:ArrayList<T>)
{
for(item in items)
print(item.toString() + ", ")
println()
}
fun main(args:Array<String>)
{
val v = Viewable<Int>()
val x = ArrayList<Int>()
v.add(1)
v.add(2)
lifetime(
{
v.view(it, {(itemLifetime, item)->
x.add(item)
Dump(x)
itemLifetime.attach { x.remove(item as Any); Dump(x) }
})
})
}
@@ -0,0 +1,9 @@
package foo
fun box() : Boolean {
var sum = 0
val adder = {(a: Int) -> sum += a }
adder(3)
adder(2)
return sum == 5
}
@@ -0,0 +1,9 @@
package foo
fun box() : String {
return apply( "OK", {(arg: String) -> arg } )
}
fun apply(arg : String, f : (p:String) -> String) : String {
return f(arg)
}
@@ -0,0 +1,9 @@
package foo
fun box() : String {
return if (apply( 5, {(arg: Int) -> arg + 13 } ) == 18) "OK" else "fail"
}
fun apply(arg : Int, f : (p:Int) -> Int) : Int {
return f(arg)
}
@@ -0,0 +1,15 @@
package foo
fun f(a : Int = 2, b : Int = 3) = a + b
fun box() : Boolean
{
if (f(1,2) != 3) return false;
if (f(1,3) != 4) return false;
if (f(3) != 6) return false;
if (f() != 5) return false;
return true;
}
@@ -0,0 +1,12 @@
class Point(val x:Int, val y:Int) {
fun mul() : (scalar:Int)->Point {
return { (scalar:Int):Point -> Point(x * scalar, y * scalar) }
}
}
val m = Point(2, 3).mul() : (scalar:Int)->Point
fun box() : String {
val answer = m(5)
return if (answer.x == 10 && answer.y == 15) "OK" else "FAIL"
}
@@ -0,0 +1,42 @@
package foo
import java.util.*;
val d = {(a : Int) -> a + 1}
val p = {(a : Int) -> a * 3}
val list = ArrayList<Function1<Int, Int>>();
fun chain(start : Int) : Int {
var res = start;
for (func in list) {
res = (func)(res);
}
return res;
}
fun box() : Boolean {
if (chain(0) != 0) {
return false;
}
list.add(d);
if (list.get(0)(0) != 1) {
return false;
}
list.add(p);
if (list.get(1)(10) != 30) {
return false;
}
if (chain(0) != 3) {
return false;
}
list.add({it * it});
list.add({it - 100});
if (chain(2) != -19) {
return false;
}
if (({(a : Int) -> a * a}(3)) != 9) {
return false;
}
return true;
}
@@ -0,0 +1,19 @@
package foo
fun box() : Boolean{
fun f() = 3
return (((f() + f()) == 6) && (b() == 24))
}
fun b() : Int {
fun a() : Int {
fun c() = 4
return c() * 3
}
val a = 2
return a() * a
}
@@ -0,0 +1,8 @@
package foo
fun box() : Boolean {
var sum = 0
val addFive = {(a: Int) -> a + 5 }
sum = addFive(sum)
return sum == 5
}
@@ -0,0 +1,14 @@
package foo
fun f(a: (Int) -> Int) = a(1)
fun box() : Boolean {
if (f() {
it + 2
} != 3) return false
if (f() {(a : Int) -> a * 300} != 300) return false;
return true
}
@@ -0,0 +1,10 @@
package foo
fun apply(f : (Int) -> Int, t : Int) : Int {
return f(t)
}
fun box() : Boolean {
return apply({(a: Int) -> a + 5 }, 3) == 8
}
@@ -0,0 +1,13 @@
package foo
fun box() : Boolean
{
return f()
}
fun f() : Boolean {
return true
}
@@ -0,0 +1,9 @@
package foo
fun sum(param1 : Int, param2 : Int) : Int {
return param1 + param2;
}
fun box() : Boolean {
return (sum(1, 5) == 6)
}
@@ -0,0 +1,12 @@
package foo
fun test(f : (Int) -> Boolean, p : Int) = f(p)
fun box() : Boolean {
if (!test({it + 1 == 2}, 1)) return false;
if (!test({it > 1}, 3)) return false;
return (test({((it < 1) == false)}, 1))
}
@@ -0,0 +1,17 @@
package foo
var b = 0
fun loop(var times : Int) {
while(times > 0) {
val u = {(value : Int) ->
b = b + 1
}
u(times--)
}
}
fun box() : Boolean {
loop(5)
return b == 5
}
@@ -0,0 +1,16 @@
package foo
fun test(x: Int, y: Int) = y - x
fun box() : Boolean {
if (test(1,2) != 1) {
return false;
}
if (test(x=1, y=2) != 1) {
return false;
}
if (test(y=2, x=1) != 1) {
return false;
}
return true
}
@@ -0,0 +1,17 @@
package foo
fun testSize(expectedSize : Int, vararg i : Int) : Boolean {
return (i.size == expectedSize)
}
fun testSum(expectedSum : Int, vararg i : Int) : Boolean {
var sum = 0
for (j in i) {
sum += j
}
return (expectedSum == sum)
}
fun box() = testSize(0) && testSum(0) && testSize(3, 1, 1, 1) && testSum(3, 1, 1, 1) && testSize(6, 1, 1, 1, 2, 3, 4) &&
testSum(30, 10, 20, 0)
@@ -0,0 +1,112 @@
package foo
import js.*
class RangeIterator(val start : Int, var count : Int, val reversed : Boolean) {
var i = start
fun next() : Int {
--count
if (reversed) {
i--
return i + 1
} else {
i++
return i - 1
}
}
fun hasNext() = (count > 0);
}
class NumberRange(val start : Int, val size : Int, val reversed : Boolean) {
val end : Int
get() = if (reversed) start - size + 1 else start + size - 1
fun contains(number : Int) : Boolean {
if (reversed) {
return (number <= start) && (number > start - size);
} else {
return (number >= start) && (number < start + size);
}
}
fun iterator() = RangeIterator(start, size, reversed);
}
fun box() = testRange() && testReversedRange();
fun testRange() : Boolean {
val oneToFive = NumberRange(1, 4, false);
if (oneToFive.contains(5)) return false;
if (oneToFive.contains(0)) return false;
if (oneToFive.contains(-100)) return false;
if (oneToFive.contains(10)) return false;
if (!oneToFive.contains(1)) return false;
if (!oneToFive.contains(2)) return false;
if (!oneToFive.contains(3)) return false;
if (!oneToFive.contains(4)) return false;
if (!(oneToFive.start == 1)) return false;
if (!(oneToFive.size == 4)) return false;
if (!(oneToFive.end == 4)) return false;
var sum = 0;
for (i in oneToFive) {
sum += i;
}
for (i in oneToFive) {
print(i)
}
if (sum != 10) return false;
return true;
}
fun testReversedRange() : Boolean {
println("Testing reversed range.");
val tenToFive = NumberRange(10, 5, true);
if (tenToFive.contains(5)) return false;
if (tenToFive.contains(11)) return false;
if (tenToFive.contains(-100)) return false;
if (tenToFive.contains(1000)) return false;
if (!tenToFive.contains(6)) return false;
if (!tenToFive.contains(7)) return false;
if (!tenToFive.contains(8)) return false;
if (!tenToFive.contains(9)) return false;
if (!tenToFive.contains(10)) return false;
if (!(tenToFive.start == 10)) return false;
if (!(tenToFive.size == 5)) return false;
if (!(tenToFive.end == 6)) return false;
for (i in tenToFive) {
println(i)
}
var sum = 0;
for (i in tenToFive) {
sum += i;
}
if (sum != 40) {
return false;
}
return true;
}
fun main(args : Array<String>) {
println(box())
}
@@ -0,0 +1,8 @@
package foo
import jquery.*;
fun box() : String {
val aa = jq("a").attr("a");
return "OK"
}
@@ -0,0 +1,15 @@
package foo
val y = 3
fun f(a:Int) : Int {
val x = 42
val y = 50
return y
}
fun box() : Int
{
return f(y)
}
@@ -0,0 +1,14 @@
package foo
var i = 0
fun test() : Int? = i++
fun box() : Boolean {
if (i != 0) return false
test()?.plus(1)
if (i != 1) return false
test()?.minus(2)
if (i != 2) return false
return true
}
@@ -0,0 +1,7 @@
package foo
fun box() : String {
var number = 3
return ("my age is $number");
}
@@ -0,0 +1,8 @@
package foo
fun box() : String {
var right = 2
var left = 3
return ("left = $left\nright = $right\nsum = ${left + right}\n");
}
@@ -0,0 +1,18 @@
package foo
class A(var i : Int) {
fun toString() = "a$i"
}
fun box() : Boolean {
var p = A(2);
var n = A(1);
if ("$p$n" != "a2a1") {
return false;
}
if ("${A(10)}" != "a10") {
return false;
}
return true;
}
@@ -0,0 +1,10 @@
package foo
fun box() : Boolean {
val a = "bar";
var b = "foo";
b = a;
return (b == "bar");
}
@@ -0,0 +1,8 @@
package foo
fun box() : Boolean {
val a = "String";
return true;
}
@@ -0,0 +1,7 @@
package foo
fun box() : String {
var name = "Hello"
return ("o${name}o");
}
@@ -0,0 +1,13 @@
package foo
fun Int.same() : Int {
return this
}
fun Int.quadruple() : Int {
return same() * 4;
}
fun box(): Boolean {
return ((3 + 4).quadruple() == 28)
}
@@ -0,0 +1,22 @@
package foo
class M() {
var m = 0
fun eval() {
var d = {
var c = {Int.() -> this + 3}
m += 3.c()
}
d();
}
}
fun box() : Boolean {
var a = M()
if (a.m != 0) return false;
a.eval()
if (a.m != 6) return false;
return true;
}
@@ -0,0 +1,7 @@
package foo
fun apply(i : Int, f : Int.(Int) -> Int) = i.f(1);
fun box() : Boolean {
return (apply(1, {i -> i + this})) == 2
}
@@ -0,0 +1,15 @@
package foo
class A(var a : Int) {
fun eval() = f();
}
fun A.f() : Int {
a = 3
return 10
}
fun box(): Boolean {
val a = A(4)
return (a.eval() == 10) && (a.a == 3)
}
@@ -0,0 +1,13 @@
package foo
fun Int.same() : Int {
return this
}
fun Int.quadruple() : Int {
return same() * 4;
}
fun box(): Boolean {
return (3.quadruple() == 12)
}
@@ -0,0 +1,24 @@
package foo
import java.util.*
fun <T> ArrayList<T>.findAll(predicate: (T) -> Boolean): ArrayList<T> {
val result = ArrayList<T>()
for(val t in this) {
if (predicate(t)) result.add(t)
}
return result
}
fun box(): String {
val list: ArrayList<Int> = ArrayList<Int>()
list.add(2)
list.add(3)
list.add(5)
val m: ArrayList<Int> = list.findAll<Int>({(name: Int) -> name < 4})
return if (m.size() == 2) "OK" else "fail"
}
@@ -0,0 +1,9 @@
package foo
fun Int.quadruple() : Int {
return this * 4;
}
fun box(): Boolean {
return (3.quadruple() == 12)
}
@@ -0,0 +1,15 @@
package foo
class A(var a : Int) {
fun Int.modify() : Int {
return this * 3;
}
fun eval() = a.modify();
}
fun box(): Boolean {
val a = A(4)
return (a.eval() == 12)
}
@@ -0,0 +1,20 @@
package foo
open class A(var a : Int) {
open fun Int.modify() : Int {
return this * 3;
}
fun eval() = a.modify();
}
class B(a : Int) : A(a) {
override fun Int.modify() : Int {
return this - 2;
}
}
fun box(): Boolean {
return (A(4).eval() == 12) && (A(2).eval() == 6) && (B(3).eval() == 1)
}
@@ -0,0 +1,11 @@
package foo;
val Double.abs : Double
get() = if (this > 0) this else -this
fun box() : Boolean {
if (4.0.abs != 4.0) return false;
if ((-5.2).abs != 5.2) return false;
return true;
}
@@ -0,0 +1,23 @@
package foo
class Test() {
var a = 0
}
var Test.b : Int
get() = a * 3
set(c : Int) {
a = c - 1
}
fun box() : Boolean {
val c = Test()
if (c.a != 0) return false;
if (c.b != 0) return false;
c.a = 3;
if (c.b != 9) return false;
c.b = 10;
if (c.a != 9) return false;
if (c.b != 27) return false;
return true;
}
@@ -0,0 +1,17 @@
package foo
val String.size : Int
get() = length
val Int.quadruple : Int
get() = this * 4
fun box() : Boolean
{
if ("1".size != 1) return false;
if ("11".size != 2) return false;
if (("121" + "123").size != 6) return false;
if (1.quadruple != 4) return false;
if (0.quadruple != 0) return false;
return true;
}
@@ -0,0 +1,13 @@
package foo
class A() : B() {
}
open class B() {
val a = 3
}
fun box() = (A().a == 3)
@@ -0,0 +1,37 @@
package foo
open class A() {
var order = ""
{
order = order + "A"
}
}
open class B() : A() {
{
order = order + "B"
}
}
class C() : B() {
{
order = order + "C"
}
}
class D() : B() {
{
order = order + "D"
}
}
class E() : A() {
{
order = order + "E"
}
}
fun box() : Boolean {
return (C().order == "ABC") && (D().order == "ABD") && (E().order == "AE")
}
@@ -0,0 +1,37 @@
package foo
class C() : B() {
{
order = order + "C"
}
}
class D() : B() {
{
order = order + "D"
}
}
class E() : A() {
{
order = order + "E"
}
}
open class B() : A() {
{
order = order + "B"
}
}
open class A() {
var order = ""
{
order = order + "A"
}
}
fun box() : Boolean {
return (C().order == "ABC") && (D().order == "ABD") && (E().order == "AE")
}
@@ -0,0 +1,26 @@
package foo
open class A() {
var order = ""
{
order = order + "A"
}
}
open class B() : A() {
{
order = order + "B"
}
}
class C() : B() {
{
order = order + "C"
}
}
fun box() : Boolean {
return (C().order == "ABC") && (B().order == "AB") && (A().order == "A")
}
@@ -0,0 +1,13 @@
package foo
open class A() {
var a = 3;
}
class B() : A() {
}
fun box() : Int {
return (B().a)
}
@@ -0,0 +1,15 @@
package foo
open class C() {
open fun f(): Any = "C f"
}
class D() : C() {
override fun f(): String = "D f"
}
fun box(): Boolean {
val d : C = D()
if(d.f() != "D f") return false
return true
}
@@ -0,0 +1,12 @@
package foo
open class C(a : Int) {
val b = a
}
class D(c : Int) : C(c + 2) {
}
fun box(): Boolean {
return (D(0).b == 2)
}
@@ -0,0 +1,10 @@
package foo
import java.util.ArrayList;
fun box() : Boolean {
val a = ArrayList<Int>();
a.add(1)
a.add(2)
return (a.size() == 2) && (a.get(1) == 2) && (a.get(0) == 1);
}
@@ -0,0 +1,11 @@
package foo
import java.util.ArrayList;
fun box() : Boolean {
val a = ArrayList<Int>();
a.add(1)
a.add(2)
a[1] = 100
return (a.size() == 2) && (a[1] == 100) && (a[0] == 1);
}
@@ -0,0 +1,8 @@
package foo
import java.util.ArrayList;
fun box() : Boolean {
val a = ArrayList<Int>();
return a.size() == 0;
}
@@ -0,0 +1,12 @@
package foo
import java.util.ArrayList;
fun box() : Boolean {
val arr = ArrayList<Int>();
var i = 0;
while (i++ < 10) {
arr.add(i);
}
return (arr[10] == 11)
}
@@ -0,0 +1,9 @@
package foo
import java.util.ArrayList;
fun box() : Boolean {
val a = ArrayList<Int>();
a.add(3)
return !(a.isEmpty()) && (ArrayList<Int>().isEmpty());
}
@@ -0,0 +1,16 @@
package foo
import java.util.ArrayList;
fun box() : Boolean {
var i = 0
val arr = ArrayList<Int>();
while (i++ < 10) {
arr.add(i);
}
var sum = 0
for (a in arr) {
sum += a;
}
return (sum == 55) && (arr.size() == 10)
}
@@ -0,0 +1,8 @@
package foo
import java.util.Collection;
import java.util.Comparator.
fun max<T>(col :Collection<T>, ) {
}
@@ -0,0 +1,17 @@
package foo
import java.util.ArrayList;
fun box() : Boolean {
var i = 0
val arr = ArrayList<Int>();
while (i++ < 10) {
arr.add(i);
}
arr.remove(2)
var sum = 0
for (a in arr) {
sum += a;
}
return ((sum == 52) && (arr[1] == 2) && (arr[2] == 4) && (arr[3] == 5) && (arr[4] == 6) && (arr[8] == 10))
}
@@ -0,0 +1,22 @@
/*
* Copyright 2000-2012 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.
*/
function test() {
var a = Kotlin.arrayFromFun(3, function () {
return 3
});
return (a[0] == 3) && (a[2] == 3) && (a[1] == 3);
}

Some files were not shown because too many files have changed in this diff Show More