Test framework for inline
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
import builders.*
|
||||
|
||||
fun testAllInline() : String {
|
||||
val args = array("1", "2", "3")
|
||||
val result =
|
||||
html {
|
||||
val htmlVal = 0
|
||||
head {
|
||||
title { +"XML encoding with Kotlin" }
|
||||
}
|
||||
body {
|
||||
var bodyVar = 1
|
||||
h1 { +"XML encoding with Kotlin" }
|
||||
p { +"this format can be used as an alternative markup to XML" }
|
||||
|
||||
// an element with attributes and text content
|
||||
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
|
||||
|
||||
// mixed content
|
||||
p {
|
||||
+"This is some"
|
||||
b { +"mixed" }
|
||||
+"text. For more see the"
|
||||
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
|
||||
+"project"
|
||||
}
|
||||
p { +"some text" }
|
||||
|
||||
// content generated from command-line arguments
|
||||
p {
|
||||
+"Command line arguments were:"
|
||||
ul {
|
||||
for (arg in args)
|
||||
li { +arg; +"$htmlVal"; +"$bodyVar" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result.toString()!!
|
||||
}
|
||||
|
||||
fun testHtmlNoInline() : String {
|
||||
val args = array("1", "2", "3")
|
||||
val result =
|
||||
htmlNoInline() {
|
||||
val htmlVal = 0
|
||||
head {
|
||||
title { +"XML encoding with Kotlin" }
|
||||
}
|
||||
body {
|
||||
var bodyVar = 1
|
||||
h1 { +"XML encoding with Kotlin" }
|
||||
p { +"this format can be used as an alternative markup to XML" }
|
||||
|
||||
// an element with attributes and text content
|
||||
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
|
||||
|
||||
// mixed content
|
||||
p {
|
||||
+"This is some"
|
||||
b { +"mixed" }
|
||||
+"text. For more see the"
|
||||
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
|
||||
+"project"
|
||||
}
|
||||
p { +"some text" }
|
||||
|
||||
// content generated from command-line arguments
|
||||
p {
|
||||
+"Command line arguments were:"
|
||||
ul {
|
||||
for (arg in args)
|
||||
li { +arg; +"$htmlVal"; +"$bodyVar" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result.toString()!!
|
||||
}
|
||||
|
||||
fun testBodyNoInline() : String {
|
||||
val args = array("1", "2", "3")
|
||||
val result =
|
||||
html {
|
||||
val htmlVal = 0
|
||||
head {
|
||||
title { +"XML encoding with Kotlin" }
|
||||
}
|
||||
bodyNoInline {
|
||||
var bodyVar = 1
|
||||
h1 { +"XML encoding with Kotlin" }
|
||||
p { +"this format can be used as an alternative markup to XML" }
|
||||
|
||||
// an element with attributes and text content
|
||||
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
|
||||
|
||||
// mixed content
|
||||
p {
|
||||
+"This is some"
|
||||
b { +"mixed" }
|
||||
+"text. For more see the"
|
||||
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
|
||||
+"project"
|
||||
}
|
||||
p { +"some text" }
|
||||
|
||||
// content generated from command-line arguments
|
||||
p {
|
||||
+"Command line arguments were:"
|
||||
ul {
|
||||
for (arg in args)
|
||||
li { +arg; +"$htmlVal"; +"$bodyVar" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result.toString()!!
|
||||
}
|
||||
|
||||
fun testBodyHtmlNoInline() : String {
|
||||
val args = array("1", "2", "3")
|
||||
val result =
|
||||
htmlNoInline {
|
||||
val htmlVal = 0
|
||||
head {
|
||||
title { +"XML encoding with Kotlin" }
|
||||
}
|
||||
bodyNoInline {
|
||||
var bodyVar = 1
|
||||
h1 { +"XML encoding with Kotlin" }
|
||||
p { +"this format can be used as an alternative markup to XML" }
|
||||
|
||||
// an element with attributes and text content
|
||||
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
|
||||
|
||||
// mixed content
|
||||
p {
|
||||
+"This is some"
|
||||
b { +"mixed" }
|
||||
+"text. For more see the"
|
||||
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
|
||||
+"project"
|
||||
}
|
||||
p { +"some text" }
|
||||
|
||||
// content generated from command-line arguments
|
||||
p {
|
||||
+"Command line arguments were:"
|
||||
ul {
|
||||
for (arg in args)
|
||||
li { +arg; +"$htmlVal"; +"$bodyVar" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result.toString()!!
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var expected = testAllInline();
|
||||
|
||||
if (expected != testHtmlNoInline()) return "fail 1: ${testHtmlNoInline()}\nbut expected\n${expected} "
|
||||
|
||||
if (expected != testBodyNoInline()) return "fail 2: ${testBodyNoInline()}\nbut expected\n${expected} "
|
||||
|
||||
if (expected != testBodyHtmlNoInline()) return "fail 3: ${testBodyHtmlNoInline()}\nbut expected\n${expected} "
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package builders
|
||||
|
||||
import java.util.ArrayList
|
||||
import java.util.HashMap
|
||||
|
||||
trait Element {
|
||||
fun render(builder: StringBuilder, indent: String)
|
||||
|
||||
override fun toString(): String {
|
||||
val builder = StringBuilder()
|
||||
render(builder, "")
|
||||
return builder.toString()
|
||||
}
|
||||
}
|
||||
|
||||
class TextElement(val text: String) : Element {
|
||||
override fun render(builder: StringBuilder, indent: String) {
|
||||
builder.append("$indent$text\n")
|
||||
}
|
||||
}
|
||||
|
||||
abstract class Tag(val name: String) : Element {
|
||||
val children = ArrayList<Element>()
|
||||
val attributes = HashMap<String, String>()
|
||||
|
||||
inline protected fun initTag<T : Element>(tag: T, init: T.() -> Unit): T {
|
||||
tag.init()
|
||||
children.add(tag)
|
||||
return tag
|
||||
}
|
||||
|
||||
override fun render(builder: StringBuilder, indent: String) {
|
||||
builder.append("$indent<$name${renderAttributes()}>\n")
|
||||
for (c in children) {
|
||||
c.render(builder, indent + " ")
|
||||
}
|
||||
builder.append("$indent</$name>\n")
|
||||
}
|
||||
|
||||
private fun renderAttributes(): String? {
|
||||
val builder = StringBuilder()
|
||||
for (a in attributes.keySet()) {
|
||||
builder.append(" $a=\"${attributes[a]}\"")
|
||||
}
|
||||
return builder.toString()
|
||||
}
|
||||
}
|
||||
|
||||
abstract class TagWithText(name: String) : Tag(name) {
|
||||
fun String.plus() {
|
||||
children.add(TextElement(this))
|
||||
}
|
||||
}
|
||||
|
||||
class HTML() : TagWithText("html") {
|
||||
inline fun head(init: Head.() -> Unit) = initTag(Head(), init)
|
||||
|
||||
inline fun body(init: Body.() -> Unit) = initTag(Body(), init)
|
||||
|
||||
fun bodyNoInline(init: Body.() -> Unit) = initTag(Body(), init)
|
||||
}
|
||||
|
||||
class Head() : TagWithText("head") {
|
||||
inline fun title(init: Title.() -> Unit) = initTag(Title(), init)
|
||||
}
|
||||
|
||||
class Title() : TagWithText("title")
|
||||
|
||||
abstract class BodyTag(name: String) : TagWithText(name) {
|
||||
inline fun b(init: B.() -> Unit) = initTag(B(), init)
|
||||
inline fun p(init: P.() -> Unit) = initTag(P(), init)
|
||||
inline fun pNoInline(init: P.() -> Unit) = initTag(P(), init)
|
||||
inline fun h1(init: H1.() -> Unit) = initTag(H1(), init)
|
||||
inline fun ul(init: UL.() -> Unit) = initTag(UL(), init)
|
||||
inline fun a(href: String, init: A.() -> Unit) {
|
||||
val a = initTag(A(), init)
|
||||
a.href = href
|
||||
}
|
||||
}
|
||||
|
||||
class Body() : BodyTag("body")
|
||||
class UL() : BodyTag("ul") {
|
||||
inline fun li(init: LI.() -> Unit) = initTag(LI(), init)
|
||||
}
|
||||
|
||||
class B() : BodyTag("b")
|
||||
class LI() : BodyTag("li")
|
||||
class P() : BodyTag("p")
|
||||
class H1() : BodyTag("h1")
|
||||
class A() : BodyTag("a") {
|
||||
public var href: String
|
||||
get() = attributes["href"]!!
|
||||
set(value) {
|
||||
attributes["href"] = value
|
||||
}
|
||||
}
|
||||
|
||||
inline fun html(init: HTML.() -> Unit): HTML {
|
||||
val html = HTML()
|
||||
html.init()
|
||||
return html
|
||||
}
|
||||
|
||||
fun htmlNoInline(init: HTML.() -> Unit): HTML {
|
||||
val html = HTML()
|
||||
html.init()
|
||||
return html
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import test.*
|
||||
|
||||
fun box(): String {
|
||||
val result = doWork({11})
|
||||
if (result != 11) return "test1: ${result}"
|
||||
|
||||
val result2 = doWork({12; result+1})
|
||||
if (result2 != 12) return "test2: ${result2}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package test
|
||||
|
||||
inline fun <R> doWork(job: ()-> R) : R {
|
||||
return notInline({job()})
|
||||
}
|
||||
|
||||
fun <R> notInline(job: ()-> R) : R {
|
||||
return job()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import test.*
|
||||
|
||||
fun box(): String {
|
||||
val result = doWork({11})
|
||||
if (result != 11) return "test1: ${result}"
|
||||
|
||||
val result2 = doWork({12; result+1})
|
||||
if (result2 != 12) return "test2: ${result2}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package test
|
||||
|
||||
inline fun <R> doWork(job: ()-> R) : R {
|
||||
val k = 10;
|
||||
return notInline({k; job()})
|
||||
}
|
||||
|
||||
fun <R> notInline(job: ()-> R) : R {
|
||||
return job()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
fun test1() : Int {
|
||||
val inlineX = My(111)
|
||||
|
||||
return inlineX.perform<My, Int>{
|
||||
|
||||
val outX = My(1111111)
|
||||
outX.perform<My, Int>(
|
||||
{inlineX.value}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun My.execute(): Int {
|
||||
return perform { this.value }
|
||||
}
|
||||
|
||||
fun test2(): Int {
|
||||
val inlineX = My(11)
|
||||
|
||||
return inlineX.execute()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (test1() != 111) return "test1: ${test1()}"
|
||||
if (test2() != 11) return "test2: ${test2()}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class My(val value: Int)
|
||||
|
||||
inline fun <T, R> T.perform(job: (T)-> R) : R {
|
||||
return job(this)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import test.*
|
||||
|
||||
fun testClassObjectCall(): String {
|
||||
return InlineAll.inline({"classobject"})
|
||||
}
|
||||
|
||||
fun testInstanceCall(): String {
|
||||
val inlineX = InlineAll()
|
||||
return inlineX.inline({"instance"})
|
||||
}
|
||||
|
||||
fun testPackageCall(): String {
|
||||
return inline({"package"})
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (testClassObjectCall() != "classobject") return "test1: ${testClassObjectCall()}"
|
||||
if (testInstanceCall() != "instance") return "test2: ${testInstanceCall()}"
|
||||
if (testPackageCall() != "package") return "test3: ${testPackageCall()}"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package test
|
||||
|
||||
inline fun inline(s: () -> String): String {
|
||||
return s()
|
||||
}
|
||||
|
||||
class InlineAll {
|
||||
|
||||
inline fun inline(s: () -> String): String {
|
||||
return s()
|
||||
}
|
||||
|
||||
class object {
|
||||
inline fun inline(s: () -> String): String {
|
||||
return s()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
fun test1(): Int {
|
||||
val inlineX = Inline()
|
||||
return inlineX.foo({(z: Int) -> "" + z}, 25, {String.() -> this.length})
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (test1() != 2) return "test1: ${test1()}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class Inline() {
|
||||
|
||||
inline fun foo(closure1 : (l: Int) -> String, param: Int, closure2: String.() -> Int) : Int {
|
||||
return closure1(param).closure2()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
fun test1(): Int {
|
||||
val inlineX = Inline(9)
|
||||
return inlineX.calcExt({(z: Int) -> z}, 25)
|
||||
}
|
||||
|
||||
fun test2(): Int {
|
||||
val inlineX = Inline(9)
|
||||
return inlineX.calcExt2({Int.() -> this}, 25)
|
||||
}
|
||||
|
||||
fun test3(): Int {
|
||||
val inlineX = Inline(9)
|
||||
return inlineX.doWork(InlineX(11))
|
||||
}
|
||||
|
||||
fun test4(): Double {
|
||||
val inlineX = Inline(9)
|
||||
return inlineX.doWorkWithDouble(11.0)
|
||||
}
|
||||
|
||||
fun test5(): Double {
|
||||
val inlineX = Inline(9)
|
||||
with(inlineX) {
|
||||
11.0.calcDouble{(a: Int, b: Double) -> a + b}
|
||||
}
|
||||
return inlineX.doWorkWithDouble(11.0)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (test1() != 25) return "test1: ${test1()}"
|
||||
if (test2() != 25) return "test2: ${test2()}"
|
||||
if (test3() != 20) return "test3: ${test3()}"
|
||||
if (test4() != 20.0) return "test4: ${test4()}"
|
||||
if (test5() != 20.0) return "test5: ${test5()}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
inline fun Inline.calcExt(s: (Int) -> Int, p: Int) : Int {
|
||||
return s(p)
|
||||
}
|
||||
|
||||
inline fun Inline.calcExt2(s: Int.() -> Int, p: Int) : Int {
|
||||
return p.s()
|
||||
}
|
||||
|
||||
class InlineX(val value : Int) {}
|
||||
|
||||
class Inline(val res: Int) {
|
||||
|
||||
inline fun InlineX.calcInt(s: (Int, Int) -> Int) : Int {
|
||||
return s(res, this.value)
|
||||
}
|
||||
|
||||
inline fun Double.calcDouble(s: (Int, Double) -> Double) : Double {
|
||||
return s(res, this)
|
||||
}
|
||||
|
||||
fun doWork(l : InlineX) : Int {
|
||||
return l.calcInt({(a: Int, b: Int) -> a + b})
|
||||
}
|
||||
|
||||
fun doWorkWithDouble(s : Double) : Double {
|
||||
return s.calcDouble({(a: Int, b: Double) -> a + b})
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import test.*
|
||||
import java.util.*
|
||||
|
||||
|
||||
fun sample(): Input {
|
||||
return Input("Hello", "World");
|
||||
}
|
||||
|
||||
test fun testForEachLine() {
|
||||
val list = ArrayList<String>()
|
||||
val reader = sample()
|
||||
|
||||
reader.forEachLine{
|
||||
list.add(it)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
testForEachLine()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package test
|
||||
|
||||
public class Input(val s1: String, val s2: String) {
|
||||
public fun iterator() : Iterator<String> {
|
||||
return arrayListOf(s1, s2).iterator()
|
||||
}
|
||||
}
|
||||
|
||||
public inline fun <T, R> T.use(block: (T)-> R) : R {
|
||||
return block(this)
|
||||
}
|
||||
|
||||
public inline fun Input.forEachLine(block: (String) -> Unit): Unit {
|
||||
useLines { lines -> lines.forEach(block) }
|
||||
}
|
||||
|
||||
public inline fun Input.useLines(block2: (Iterator<String>) -> Unit): Unit {
|
||||
this.use{ block2(it.iterator()) }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import test.*
|
||||
|
||||
fun test1(s: Long): String {
|
||||
var result = "OK"
|
||||
result = mfun(s) { a ->
|
||||
result + doSmth(s) + doSmth(a)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val result = test1(11.toLong())
|
||||
if (result != "OK1111") return "fail1: ${result}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package test
|
||||
|
||||
inline fun <T, R> mfun(arg: T, f: (T) -> R) : R {
|
||||
return f(arg)
|
||||
}
|
||||
|
||||
inline fun <T> doSmth(a: T): String {
|
||||
return a.toString()
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import test.*
|
||||
|
||||
fun test1(s: Long): Boolean {
|
||||
return doSmth(s)
|
||||
}
|
||||
|
||||
fun test2(s: Int): Boolean {
|
||||
return doSmth(s)
|
||||
}
|
||||
|
||||
inline fun <T> test3(s: T): Boolean {
|
||||
return doSmth(s)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (!test1(11111.toLong())) return "fail 1"
|
||||
if (!test2(11111)) return "fail 2"
|
||||
if (!test3(11111)) return "fail 3.1"
|
||||
if (!test3("11111")) return "fail 3.2"
|
||||
if (!test3(11111.3)) return "fail 3.3"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package test
|
||||
|
||||
inline fun <T> doSmth(a: T) : Boolean {
|
||||
return a.identityEquals(a)
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import test.*
|
||||
|
||||
fun testIf(): String {
|
||||
if (runIf({it}, 11, 11, 12) != 11) return "testIf 1 test fail"
|
||||
if (runIf({it}, 11, 1, 12) != 12) return "testIf 2 test fail"
|
||||
|
||||
if (runIf({if (it == 11) it else 12}, 11, 11, 0) != 11) return "testIf 3 test fail"
|
||||
if (runIf({if (it == 11) it else 12}, 11, 1, 0) != 12) return "testIf 4 test fail"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun testIf2(): String {
|
||||
if (runIf2({it}, 11, 11, 12) != 11) return "testIf2 1 test fail"
|
||||
if (runIf2({it}, 11, 1, 12) != 12) return "testIf2 2 test fail"
|
||||
|
||||
if (runIf2({if (it == 11) it else 12}, 11, 11, 0) != 11) return "testIf2 3 test fail"
|
||||
if (runIf2({if (it == 11) it else 12}, 11, 1, 0) != 12) return "testIf2 4 test fail"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun testIfElse(): String {
|
||||
if (runIfElse({it}, 11, 11, 12) != 11) return "testIfElse 1 test fail"
|
||||
if (runIfElse({it}, 11, 1, 12) != 12) return "testIfElse 2 test fail"
|
||||
|
||||
if (runIfElse({if (it == 11) it else 12}, 11, 11, 0) != 11) return "testIfElse 3 test fail"
|
||||
if (runIfElse({if (it == 11) it else 12}, 11, 1, 0) != 12) return "testIfElse 4 test fail"
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var result = testIf()
|
||||
if (result != "OK") return "fail1: ${result}"
|
||||
|
||||
result = testIf2()
|
||||
if (result != "OK") return "fail2: ${result}"
|
||||
|
||||
result = testIfElse()
|
||||
if (result != "OK") return "fail2: ${result}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package test
|
||||
|
||||
inline fun <T> runIf(f: (T) -> T, start: T, stop: T, secondStart: T) : T {
|
||||
if (f(start) == stop) {
|
||||
return f(start)
|
||||
}
|
||||
return f(secondStart)
|
||||
}
|
||||
|
||||
|
||||
inline fun <T> runIf2(f: (T) -> T, start: T, stop: T, secondStart: T) : T {
|
||||
val result = f(start)
|
||||
if (result == stop) {
|
||||
return result
|
||||
}
|
||||
return f(secondStart)
|
||||
}
|
||||
|
||||
inline fun <T> runIfElse(f: (T) -> T, start: T, stop: T, secondStart: T) : T {
|
||||
if (f(start) == stop) {
|
||||
return f(start)
|
||||
} else {
|
||||
return f(secondStart)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fun test1(): Int {
|
||||
var s = 0;
|
||||
2.times2 {
|
||||
s++
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (test1() != 2) return "test1: ${test1()}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
public inline fun Int.times2(body : () -> Unit) {
|
||||
var count = this;
|
||||
while (count > 0) {
|
||||
body()
|
||||
count--
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fun test1(): String {
|
||||
val inlineX = My()
|
||||
var d = "";
|
||||
inlineX.doWork({(z: String) -> d = z; z})
|
||||
return d
|
||||
}
|
||||
|
||||
fun test2(): Int {
|
||||
val inlineX = My()
|
||||
return inlineX.perform({(z: My) -> 11})
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (test1() != "OK") return "test1: ${test1()}"
|
||||
if (test2() != 11) return "test1: ${test2()}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
class My
|
||||
|
||||
inline fun <T, R> T.perform(job: (T)-> R) : R {
|
||||
return job(this)
|
||||
}
|
||||
|
||||
|
||||
inline fun My.someWork(job: (String) -> Any): Unit {
|
||||
this.perform {
|
||||
job("OK")
|
||||
}
|
||||
}
|
||||
|
||||
inline fun My.doWork (closure : (param : String) -> Unit) : Unit {
|
||||
this.someWork(closure)
|
||||
}
|
||||
|
||||
inline fun My.doPerform (closure : (param : My) -> Int) : Int {
|
||||
return perform(closure)
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import zzz.*
|
||||
|
||||
fun box(): String {
|
||||
|
||||
val p = { calc { 11 }} ()
|
||||
|
||||
val z = { calc { 12 }}()
|
||||
|
||||
if (p == z) return "fail"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package zzz
|
||||
|
||||
inline fun calc(lambda: () -> Int): Int {
|
||||
return doCalc { lambda() }
|
||||
}
|
||||
|
||||
fun doCalc(lambda2: () -> Int): Int {
|
||||
return lambda2()
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import test.*
|
||||
|
||||
fun test1(s: Long): String {
|
||||
return doSmth(s)
|
||||
}
|
||||
|
||||
fun test2(s: Int): String {
|
||||
return doSmth2(s)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var result = test1(11.toLong())
|
||||
if (result != "11") return "fail1: ${result}"
|
||||
|
||||
result = test2(11)
|
||||
if (result != "11") return "fail2: ${result}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package test
|
||||
|
||||
inline fun <T> doSmth(a: T) : String {
|
||||
return {a.toString()}()
|
||||
}
|
||||
|
||||
inline fun <T> doSmth2(a: T) : String {
|
||||
return {{a.toString()}()}()
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import test.*
|
||||
import java.util.*
|
||||
|
||||
|
||||
fun sample(): Input {
|
||||
return Input("Hello", "World");
|
||||
}
|
||||
|
||||
test fun testForEachLine() {
|
||||
val list = ArrayList<String>()
|
||||
val reader = sample()
|
||||
|
||||
reader.forEachLine{
|
||||
list.add("111")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
testForEachLine()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package test
|
||||
|
||||
public class Input(val s1: String, val s2: String) {
|
||||
public fun iterator() : Iterator<String> {
|
||||
return arrayListOf(s1, s2).iterator()
|
||||
}
|
||||
}
|
||||
|
||||
public inline fun <T, R> T.use(block: ()-> R) : R {
|
||||
return block()
|
||||
}
|
||||
|
||||
public inline fun <T, R> T.use2(block: ()-> R) : R {
|
||||
return block()
|
||||
}
|
||||
|
||||
public inline fun Input.forEachLine(block: () -> Unit): Unit {
|
||||
use { use2 (block) }
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import test.*
|
||||
import java.util.*
|
||||
|
||||
fun test1(prefix: String): String {
|
||||
var result = "fail"
|
||||
mfun {
|
||||
concat("start") {
|
||||
if (it.startsWith(prefix)) {
|
||||
result = "OK"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (test1("start") != "OK") return "fail1"
|
||||
if (test1("nostart") != "fail") return "fail2"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package test
|
||||
|
||||
inline fun <R> mfun(f: () -> R) {
|
||||
f()
|
||||
}
|
||||
|
||||
fun concat(suffix: String, l: (s: String) -> Unit) {
|
||||
l(suffix)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import test.*
|
||||
|
||||
fun test1(param: String): String {
|
||||
var result = "fail1"
|
||||
mfun(param) { a ->
|
||||
concat("start") {
|
||||
result = doSmth(a).toString()
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (test1("start") != "start") return "fail1"
|
||||
if (test1("nostart") != "nostart") return "fail2"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package test
|
||||
|
||||
fun concat(suffix: String, l: (s: String) -> Unit) {
|
||||
l(suffix)
|
||||
}
|
||||
|
||||
fun <T> mfun(arg: T, f: (T) -> Unit) {
|
||||
f(arg)
|
||||
}
|
||||
|
||||
inline fun doSmth(a: String): String {
|
||||
return a.toString()
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import test.*
|
||||
|
||||
fun test1(d: Data): Int {
|
||||
val input = Input(d)
|
||||
var result = 10
|
||||
with(input) {
|
||||
fun localFun() {
|
||||
result = input.d.value
|
||||
}
|
||||
localFun()
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
val result = test1(Data(11))
|
||||
if (result != 11) return "test1: ${result}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package test
|
||||
|
||||
public class Data(val value: Int)
|
||||
|
||||
public class Input(val d: Data) {
|
||||
public fun data() : Int = 100
|
||||
}
|
||||
|
||||
public inline fun <R> use(block: ()-> R) : R {
|
||||
return block()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
fun test1(): Int {
|
||||
return calc( {(l : Int) -> 2*l}, {(l : Int) -> 4*l})
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
if (test1() != 110) return "test1: ${test1()}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
inline fun calc(s: (Int) -> Int, noinline p: (Int) -> Int) : Int {
|
||||
val z = p
|
||||
return s(11) + z(11) + p(11)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
fun test1(): Int {
|
||||
return 1.inlineMethod()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val result = test1()
|
||||
if (result != 2) return "test1: ${result}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
var s: Int = 1;
|
||||
|
||||
inline fun Int.inlineMethod() : Int {
|
||||
noInlineLambda()
|
||||
return noInlineLambda()
|
||||
}
|
||||
|
||||
inline fun Int.noInlineLambda() = { s++ } ()
|
||||
@@ -0,0 +1,32 @@
|
||||
|
||||
fun test1(): Int {
|
||||
val inlineX = Inline()
|
||||
return inlineX.foo1Int({(z: Int) -> z}, 25)
|
||||
}
|
||||
|
||||
fun test2(): Double {
|
||||
val inlineX = Inline()
|
||||
return inlineX.foo1Double(25.0, {(z: Double) -> z})
|
||||
}
|
||||
|
||||
fun test3(): Double {
|
||||
val inlineX = Inline()
|
||||
return inlineX.foo2Param(15.0, {(z1: Int, z2: Double) -> z1 + z2}, 10)
|
||||
}
|
||||
|
||||
fun test3WithCaptured(): Double {
|
||||
val inlineX = Inline()
|
||||
var c = 11.0;
|
||||
return inlineX.foo2Param(15.0, {(z1: Int, z2: Double) -> z1 + z2 + c}, 10)
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
if (test1() != 25) return "test1: ${test1()}"
|
||||
if (test2() != 25.0) return "test2: ${test2()}"
|
||||
if (test3() != 25.0) return "test3: ${test3()}"
|
||||
if (test3WithCaptured() != 36.0) return "test3WithCaptured: ${test3WithCaptured()}"
|
||||
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
class Inline() {
|
||||
|
||||
inline fun foo1Int(s : (l: Int) -> Int, param: Int) : Int {
|
||||
return s(param)
|
||||
}
|
||||
|
||||
inline fun foo1Double(param: Double, s : (l: Double) -> Double) : Double {
|
||||
return s(param)
|
||||
}
|
||||
|
||||
inline fun foo2Param(param1: Double, s : (i: Int, l: Double) -> Double, param2: Int) : Double {
|
||||
return s(param2, param1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import test.*
|
||||
|
||||
fun test1(s: Int): Int {
|
||||
val z = Z(s)
|
||||
z += {s}
|
||||
return z.s
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val result = test1(11)
|
||||
if (result != 22) return "fail1: ${result}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package test
|
||||
|
||||
public class Z(public var s: Int)
|
||||
|
||||
inline fun Z.plusAssign(lambda: () -> Int) {
|
||||
this.s += lambda()
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import test.*
|
||||
|
||||
fun sameName(s: Long): Long {
|
||||
return call {
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
fun sameName(s: Int): Int {
|
||||
return call {
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val result = sameName(1.toLong())
|
||||
if (result != 1.toLong()) return "fail1: ${result}"
|
||||
|
||||
val result2 = sameName(2)
|
||||
if (result2 != 2) return "fail2: ${result2}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package test
|
||||
|
||||
inline fun <R> call(f: () -> R) : R {
|
||||
return {f()} ()
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import test.*
|
||||
|
||||
val s = doWork({11})
|
||||
|
||||
fun box(): String {
|
||||
if (s != 11) return "test1: ${s}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package test
|
||||
|
||||
inline fun <R> doWork(job: ()-> R) : R {
|
||||
return notInline({job()})
|
||||
}
|
||||
|
||||
fun <R> notInline(job: ()-> R) : R {
|
||||
return job()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
fun test1(): Double {
|
||||
val inlineX = Inline()
|
||||
return inlineX.foo1({(z: Int) -> z}, 25, {(z: Double) -> z}, 11.5)
|
||||
}
|
||||
|
||||
fun test1WithCaptured(): Double {
|
||||
val inlineX = Inline()
|
||||
var d = 0.0;
|
||||
return inlineX.foo1({(z: Int) -> d = 1.0; z}, 25, {(z: Double) -> z + d}, 11.5)
|
||||
}
|
||||
|
||||
fun test2(): Double {
|
||||
val inlineX = Inline()
|
||||
return inlineX.foo2({(z: Int, p: Int) -> z + p}, 25, {(x: Double, y: Int, z: Int) -> z + x + y}, 11.5, 2)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (test1() != 36.5) return "test1: ${test1()}"
|
||||
if (test1WithCaptured() != 37.5) return "test1WithCaptured: ${test1WithCaptured()}"
|
||||
if (test2() != 65.5) return "test2: ${test2()}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
class Inline() {
|
||||
|
||||
inline fun foo1(closure1 : (l: Int) -> Int, param1: Int, closure2 : (l: Double) -> Double, param2: Double) : Double {
|
||||
return closure1(param1) + closure2(param2)
|
||||
}
|
||||
|
||||
inline fun foo2(closure1 : (Int, Int) -> Int, param1: Int, closure2 : (Double, Int, Int) -> Double, param2: Double, param3: Int) : Double {
|
||||
return closure1(param1, param3) + closure2(param2, param1, param3)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
fun box(): String {
|
||||
val result = runTest{minByTest<Int> { it }}
|
||||
|
||||
if (result != 1) return "test1: ${result}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
public inline fun <R> runTest(f: () -> R): R {
|
||||
return f()
|
||||
}
|
||||
|
||||
public inline fun <R> minByTest(f: (Int) -> R): R {
|
||||
var minValue = f(1)
|
||||
val v = f(1)
|
||||
return v
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
fun testAll(): String {
|
||||
val inlineX = InlineAll()
|
||||
|
||||
return inlineX.inline({(a1: Int, a2: Double, a3: Double, a4: String, a5: Long) ->
|
||||
"" + a1 + a2 + a3 + a4 + a5},
|
||||
1, 12.0, 13.0, "14", 15)
|
||||
}
|
||||
|
||||
fun testAllWithCapturedVal(): String {
|
||||
val inlineX = InlineAll()
|
||||
|
||||
val c1 = 21
|
||||
val c2 = 22.0
|
||||
val c3 = 23.0
|
||||
val c4 = "24"
|
||||
val c5 = 25.toLong()
|
||||
val c6 = 'H'
|
||||
val c7 = 26.toByte()
|
||||
val c8 = 27.toShort()
|
||||
val c9 = 28.toFloat()
|
||||
|
||||
return inlineX.inline({(a1: Int, a2: Double, a3: Double, a4: String, a5: Long) ->
|
||||
"" + a1 + a2 + a3 + a4 + a5 + c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9},
|
||||
1, 12.0, 13.0, "14", 15)
|
||||
}
|
||||
|
||||
fun testAllWithCapturedVar(): String {
|
||||
val inlineX = InlineAll()
|
||||
|
||||
var c1 = 21
|
||||
var c2 = 22.0
|
||||
var c3 = 23.0
|
||||
var c4 = "24"
|
||||
var c5 = 25.toLong()
|
||||
var c6 = 'H'
|
||||
var c7 = 26.toByte()
|
||||
var c8 = 27.toShort()
|
||||
val c9 = 28.toFloat()
|
||||
|
||||
return inlineX.inline({(a1: Int, a2: Double, a3: Double, a4: String, a5: Long) ->
|
||||
"" + a1 + a2 + a3 + a4 + a5 + c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9},
|
||||
1, 12.0, 13.0, "14", 15)
|
||||
}
|
||||
|
||||
fun testAllWithCapturedValAndVar(): String {
|
||||
val inlineX = InlineAll()
|
||||
|
||||
var c1 = 21
|
||||
var c2 = 22.0
|
||||
val c3 = 23.0
|
||||
val c4 = "24"
|
||||
var c5 = 25.toLong()
|
||||
val c6 = 'H'
|
||||
var c7 = 26.toByte()
|
||||
var c8 = 27.toShort()
|
||||
val c9 = 28.toFloat()
|
||||
|
||||
return inlineX.inline({(a1: Int, a2: Double, a3: Double, a4: String, a5: Long) ->
|
||||
"" + a1 + a2 + a3 + a4 + a5 + c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9},
|
||||
1, 12.0, 13.0, "14", 15)
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
if (testAll() != "112.013.01415") return "testAll: ${testAll()}"
|
||||
if (testAllWithCapturedVal() != "112.013.014152122.023.02425H262728.0") return "testAllWithCapturedVal: ${testAllWithCapturedVal()}"
|
||||
if (testAllWithCapturedVar() != "112.013.014152122.023.02425H262728.0") return "testAllWithCapturedVar: ${testAllWithCapturedVar()}"
|
||||
if (testAllWithCapturedValAndVar() != "112.013.014152122.023.02425H262728.0") return "testAllWithCapturedVal: ${testAllWithCapturedValAndVar()}"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class InlineAll {
|
||||
|
||||
inline fun inline(s: (Int, Double, Double, String, Long) -> String,
|
||||
a1: Int, a2: Double, a3: Double, a4: String, a5: Long): String {
|
||||
return s(a1, a2, a3, a4, a5)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
fun testAll(): String {
|
||||
return inline({(a1: Int, a2: Double, a3: Double, a4: String, a5: Long) ->
|
||||
"" + a1 + a2 + a3 + a4 + a5},
|
||||
1, 12.0, 13.0, "14", 15)
|
||||
}
|
||||
|
||||
fun testAllWithCapturedVal(): String {
|
||||
val c1 = 21
|
||||
val c2 = 22.0
|
||||
val c3 = 23.0
|
||||
val c4 = "24"
|
||||
val c5 = 25.toLong()
|
||||
val c6 = 'H'
|
||||
val c7 = 26.toByte()
|
||||
val c8 = 27.toShort()
|
||||
val c9 = 28.toFloat()
|
||||
|
||||
return inline({(a1: Int, a2: Double, a3: Double, a4: String, a5: Long) ->
|
||||
"" + a1 + a2 + a3 + a4 + a5 + c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9},
|
||||
1, 12.0, 13.0, "14", 15)
|
||||
}
|
||||
|
||||
fun testAllWithCapturedVar(): String {
|
||||
var c1 = 21
|
||||
var c2 = 22.0
|
||||
var c3 = 23.0
|
||||
var c4 = "24"
|
||||
var c5 = 25.toLong()
|
||||
var c6 = 'H'
|
||||
var c7 = 26.toByte()
|
||||
var c8 = 27.toShort()
|
||||
val c9 = 28.toFloat()
|
||||
|
||||
return inline({(a1: Int, a2: Double, a3: Double, a4: String, a5: Long) ->
|
||||
"" + a1 + a2 + a3 + a4 + a5 + c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9},
|
||||
1, 12.0, 13.0, "14", 15)
|
||||
}
|
||||
|
||||
fun testAllWithCapturedValAndVar(): String {
|
||||
var c1 = 21
|
||||
var c2 = 22.0
|
||||
val c3 = 23.0
|
||||
val c4 = "24"
|
||||
var c5 = 25.toLong()
|
||||
val c6 = 'H'
|
||||
var c7 = 26.toByte()
|
||||
var c8 = 27.toShort()
|
||||
val c9 = 28.toFloat()
|
||||
|
||||
return inline({(a1: Int, a2: Double, a3: Double, a4: String, a5: Long) ->
|
||||
"" + a1 + a2 + a3 + a4 + a5 + c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9},
|
||||
1, 12.0, 13.0, "14", 15)
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
if (testAll() != "112.013.01415") return "testAll: ${testAll()}"
|
||||
if (testAllWithCapturedVal() != "112.013.014152122.023.02425H262728.0") return "testAllWithCapturedVal: ${testAllWithCapturedVal()}"
|
||||
if (testAllWithCapturedVar() != "112.013.014152122.023.02425H262728.0") return "testAllWithCapturedVar: ${testAllWithCapturedVar()}"
|
||||
if (testAllWithCapturedValAndVar() != "112.013.014152122.023.02425H262728.0") return "testAllWithCapturedVal: ${testAllWithCapturedValAndVar()}"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
inline fun inline(s: (Int, Double, Double, String, Long) -> String,
|
||||
a1: Int, a2: Double, a3: Double, a4: String, a5: Long): String {
|
||||
return s(a1, a2, a3, a4, a5)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
fun test0Param(): Double {
|
||||
val inlineX = InlineDouble(10.0)
|
||||
return inlineX.foo({() -> 1.0})
|
||||
}
|
||||
|
||||
fun test1Param(): Double {
|
||||
val inlineX = InlineDouble(10.0)
|
||||
return inlineX.foo11({(z: Double) -> z})
|
||||
}
|
||||
|
||||
fun test1ParamCaptured(): Double {
|
||||
val s = 100.0
|
||||
val inlineX = InlineDouble(10.0)
|
||||
return inlineX.foo11({(z: Double) -> s})
|
||||
}
|
||||
|
||||
fun test1ParamMissed() : Double {
|
||||
val inlineX = InlineDouble(10.0)
|
||||
return inlineX.foo11({(z: Double) -> 111.0})
|
||||
}
|
||||
|
||||
fun test1ParamFromCallContext() : Double {
|
||||
val inlineX = InlineDouble(1000.0)
|
||||
return inlineX.fooRes({(z: Double) -> z})
|
||||
}
|
||||
|
||||
fun test2Params() : Double {
|
||||
val inlineX = InlineDouble(1000.0)
|
||||
return inlineX.fooRes2({(y: Double, z: Double) -> 2.0 * y + 3.0 * z})
|
||||
}
|
||||
|
||||
fun test2ParamsWithCaptured() : Double {
|
||||
val inlineX = InlineDouble(1000.0)
|
||||
val s = 9.0
|
||||
var t = 1.0
|
||||
return inlineX.fooRes2({(y: Double, z: Double) -> 2.0 * s + t})
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (test0Param() != 1.0) return "test0Param"
|
||||
if (test1Param() != 11.0) return "test1Param()"
|
||||
if (test1ParamCaptured() != 100.0) return "testtest1ParamCaptured()"
|
||||
if (test1ParamMissed() != 111.0) return "test1ParamMissed()"
|
||||
if (test1ParamFromCallContext() != 1000.0) return "test1ParamFromCallContext()"
|
||||
if (test2Params() != 35.0) return "test2Params()"
|
||||
if (test2ParamsWithCaptured() != 19.0) return "test2ParamsWithCaptured()"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
class InlineDouble(val res : Double) {
|
||||
|
||||
inline fun foo(s : () -> Double) : Double {
|
||||
val f = "fooStart"
|
||||
val z = s()
|
||||
return z
|
||||
}
|
||||
|
||||
inline fun foo11(s : (l: Double) -> Double) : Double {
|
||||
return s(11.0)
|
||||
}
|
||||
|
||||
inline fun fooRes(s : (l: Double) -> Double) : Double {
|
||||
val z = s(res)
|
||||
return z
|
||||
}
|
||||
|
||||
inline fun fooRes2(s : (l: Double, t: Double) -> Double) : Double {
|
||||
val f = "fooRes2Start"
|
||||
val z = s(1.0, 11.0)
|
||||
return z
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import test.*
|
||||
|
||||
fun test1(): String {
|
||||
return MyEnum.K.doSmth("O")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val result = test1()
|
||||
if (result != "OK") return "fail1: ${result}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package test
|
||||
|
||||
enum class MyEnum {
|
||||
K;
|
||||
|
||||
inline fun <T> doSmth(a: T) : String {
|
||||
return a.toString() + K.name()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import test.*
|
||||
|
||||
fun test1(s: Long): String {
|
||||
return doSmth(s)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val result = test1(11.toLong())
|
||||
if (result != "11") return "fail1: ${result}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package test
|
||||
|
||||
inline fun <T> doSmth(a: T) : String {
|
||||
return a.toString()
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
fun test0Param(): Int {
|
||||
val inlineX = Inline(10)
|
||||
return inlineX.foo({() -> 1})
|
||||
}
|
||||
|
||||
fun test1Param(): Int {
|
||||
val inlineX = Inline(10)
|
||||
return inlineX.foo11({(z: Int) -> z})
|
||||
}
|
||||
|
||||
fun test1ParamCaptured(): Int {
|
||||
val s = 100
|
||||
val inlineX = Inline(10)
|
||||
return inlineX.foo11({(z: Int) -> s})
|
||||
}
|
||||
|
||||
fun test1ParamMissed() : Int {
|
||||
val inlineX = Inline(10)
|
||||
return inlineX.foo11({(z: Int) -> 111})
|
||||
}
|
||||
|
||||
fun test1ParamFromCallContext() : Int {
|
||||
val inlineX = Inline(1000)
|
||||
return inlineX.fooRes({(z: Int) -> z})
|
||||
}
|
||||
|
||||
fun test2Params() : Int {
|
||||
val inlineX = Inline(1000)
|
||||
return inlineX.fooRes2({(y: Int, z: Int) -> 2 * y + 3 * z})
|
||||
}
|
||||
|
||||
fun test2ParamsWithCaptured() : Int {
|
||||
val inlineX = Inline(1000)
|
||||
val s = 9
|
||||
var t = 1
|
||||
return inlineX.fooRes2({(y: Int, z: Int) -> 2 * s + t})
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (test0Param() != 1) return "test0Param: ${test0Param()}"
|
||||
if (test1Param() != 11) return "test1Param: ${test1Param()}"
|
||||
if (test1ParamCaptured() != 100) return "test1ParamCaptured: ${test1ParamCaptured()}"
|
||||
if (test1ParamMissed() != 111) return "test1ParamMissed: ${test1ParamMissed()}"
|
||||
if (test1ParamFromCallContext() != 1000) return "test1ParamFromCallContext: ${test1ParamFromCallContext()}"
|
||||
if (test2Params() != 35) return "test2Params: ${test2Params()}"
|
||||
if (test2ParamsWithCaptured() != 19) return "test2ParamsWithCaptured: ${test2ParamsWithCaptured()}"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
class Inline(val res : Int) {
|
||||
|
||||
inline fun foo(s : () -> Int) : Int {
|
||||
val f = "fooStart"
|
||||
val z = s()
|
||||
return z
|
||||
}
|
||||
|
||||
inline fun foo11(s : (l: Int) -> Int) : Int {
|
||||
return s(11)
|
||||
}
|
||||
|
||||
inline fun fooRes(s : (l: Int) -> Int) : Int {
|
||||
val z = s(res)
|
||||
return z
|
||||
}
|
||||
|
||||
inline fun fooRes2(s : (l: Int, t: Int) -> Int) : Int {
|
||||
val f = "fooRes2Start"
|
||||
val z = s(1, 11)
|
||||
return z
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import test.*
|
||||
|
||||
class Z {}
|
||||
|
||||
fun test1() : Int {
|
||||
val input = Z()
|
||||
return input.use<Z, Int>{
|
||||
100
|
||||
}
|
||||
}
|
||||
|
||||
fun test2() : Int {
|
||||
val x = 1000
|
||||
return use2() + x
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
if (test1() != 100) return "test1: ${test1()}"
|
||||
if (test2() != 1100) return "test1: ${test2()}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package test
|
||||
|
||||
public class Data()
|
||||
|
||||
public inline fun <T, R> T.use(block: (T)-> R) : R {
|
||||
return block(this)
|
||||
}
|
||||
|
||||
public inline fun use2() : Int {
|
||||
val s = 100
|
||||
return s
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
fun test0Param(): String {
|
||||
val inlineX = InlineString("10")
|
||||
return inlineX.foo({() -> "1"})
|
||||
}
|
||||
|
||||
fun test1Param(): String {
|
||||
val inlineX = InlineString("10")
|
||||
return inlineX.foo11({(z: String) -> z})
|
||||
}
|
||||
|
||||
fun test1ParamCaptured(): String {
|
||||
val s = "100"
|
||||
val inlineX = InlineString("10")
|
||||
return inlineX.foo11({(z: String) -> s})
|
||||
}
|
||||
|
||||
fun test1ParamMissed() : String {
|
||||
val inlineX = InlineString("10")
|
||||
return inlineX.foo11({(z: String) -> "111"})
|
||||
}
|
||||
|
||||
fun test1ParamFromCallContext() : String {
|
||||
val inlineX = InlineString("1000")
|
||||
return inlineX.fooRes({(z: String) -> z})
|
||||
}
|
||||
|
||||
fun test2Params() : String {
|
||||
val inlineX = InlineString("1000")
|
||||
return inlineX.fooRes2({(y: String, z: String) -> y + "0" + z})
|
||||
}
|
||||
|
||||
fun test2ParamsWithCaptured() : String {
|
||||
val inlineX = InlineString("1000")
|
||||
val s = "9"
|
||||
var t = "1"
|
||||
return inlineX.fooRes2({(y: String, z: String) -> s + t})
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (test0Param() != "1") return "test0Param: ${test0Param()}"
|
||||
if (test1Param() != "11") return "test1Param: ${test1Param()}"
|
||||
if (test1ParamCaptured() != "100") return "test1ParamCaptured: ${test1ParamCaptured()}"
|
||||
if (test1ParamMissed() != "111") return "test1ParamMissed: ${test1ParamMissed()}"
|
||||
if (test1ParamFromCallContext() != "1000") return "test1ParamFromCallContext: ${test1ParamFromCallContext()}"
|
||||
if (test2Params() != "1011") return "test2Params: ${test2Params()}"
|
||||
if (test2ParamsWithCaptured() != "91") return "test2ParamsWithCaptured: ${test2ParamsWithCaptured()}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
class InlineString(val res : String) {
|
||||
|
||||
inline fun foo(s : () -> String) : String {
|
||||
val f = "fooStart"
|
||||
val z = s()
|
||||
return z
|
||||
}
|
||||
|
||||
inline fun foo11(s : (l: String) -> String) : String {
|
||||
return s("11")
|
||||
}
|
||||
|
||||
inline fun fooRes(s : (l: String) -> String) : String {
|
||||
val z = s(res)
|
||||
return z
|
||||
}
|
||||
|
||||
inline fun fooRes2(s : (l: String, t: String) -> String) : String {
|
||||
val f = "fooRes2Start"
|
||||
val z = s("1", "11")
|
||||
return z
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
import test.*
|
||||
|
||||
fun box(): String {
|
||||
mfun{ "".toLowerCase2() }
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package test
|
||||
|
||||
inline fun <R> mfun(f: () -> R) {
|
||||
f()
|
||||
f()
|
||||
}
|
||||
|
||||
public inline fun String.toLowerCase2() : String = ""
|
||||
@@ -0,0 +1,21 @@
|
||||
import test.*
|
||||
|
||||
fun testFinalInline(): String {
|
||||
return Z().finalInline({"final"})
|
||||
}
|
||||
|
||||
fun testFinalInline2(instance: InlineTrait): String {
|
||||
return instance.finalInline({"final2"})
|
||||
}
|
||||
|
||||
fun testClassObject(): String {
|
||||
return InlineTrait.finalInline({"classobject"})
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (testFinalInline() != "final") return "test1: ${testFinalInline()}"
|
||||
if (testFinalInline2(Z()) != "final2") return "test2: ${testFinalInline2(Z())}"
|
||||
if (testClassObject() != "classobject") return "test3: ${testClassObject()}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package test
|
||||
|
||||
trait InlineTrait {
|
||||
|
||||
inline final fun finalInline(s: () -> String): String {
|
||||
return s()
|
||||
}
|
||||
|
||||
class object {
|
||||
inline final fun finalInline(s: () -> String): String {
|
||||
return s()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Z: InlineTrait {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
fun test1() : Int {
|
||||
val inlineX = My(111)
|
||||
var result = 0
|
||||
val res = inlineX.perform<My, Int>{
|
||||
|
||||
try {
|
||||
throw RuntimeException()
|
||||
} catch (e: RuntimeException) {
|
||||
result = -1
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
fun test11() : Int {
|
||||
val inlineX = My(111)
|
||||
val res = inlineX.perform<My, Int>{
|
||||
try {
|
||||
throw RuntimeException()
|
||||
} catch (e: RuntimeException) {
|
||||
-1
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
fun test2() : Int {
|
||||
try {
|
||||
val inlineX = My(111)
|
||||
var result = 0
|
||||
val res = inlineX.perform<My, Int>{
|
||||
try {
|
||||
throw RuntimeException("-1")
|
||||
} catch (e: RuntimeException) {
|
||||
throw RuntimeException("-2")
|
||||
}
|
||||
}
|
||||
return result
|
||||
} catch (e: RuntimeException) {
|
||||
return e.getMessage()!!.toInt2()!!
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (test1() != -1) return "test1: ${test1()}"
|
||||
if (test11() != -1) return "test11: ${test11()}"
|
||||
if (test2() != -2) return "test2: ${test2()}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class My(val value: Int)
|
||||
|
||||
inline fun <T, R> T.perform(job: (T)-> R) : R {
|
||||
return job(this)
|
||||
}
|
||||
|
||||
public inline fun String.toInt2() : Int = java.lang.Integer.parseInt(this)
|
||||
@@ -0,0 +1,107 @@
|
||||
fun test1(): Int {
|
||||
val res = My(111).performWithFail<My, Int>(
|
||||
{
|
||||
throw RuntimeException()
|
||||
}, {
|
||||
it.value
|
||||
})
|
||||
return res
|
||||
}
|
||||
|
||||
fun test11(): Int {
|
||||
val res = My(111).performWithFail2<My, Int>(
|
||||
{
|
||||
try {
|
||||
throw RuntimeException("1")
|
||||
} catch (e: RuntimeException) {
|
||||
throw RuntimeException("2")
|
||||
}
|
||||
},
|
||||
{(ex, thizz) ->
|
||||
if (ex.getMessage() == "2") {
|
||||
thizz.value
|
||||
} else {
|
||||
-11111
|
||||
}
|
||||
})
|
||||
return res
|
||||
}
|
||||
|
||||
fun test2(): Int {
|
||||
val res = My(111).performWithFail<My, Int>(
|
||||
{
|
||||
it.value
|
||||
},
|
||||
{
|
||||
it.value + 1
|
||||
})
|
||||
return res
|
||||
}
|
||||
|
||||
fun test22(): Int {
|
||||
val res = My(111).performWithFail2<My, Int>(
|
||||
{
|
||||
try {
|
||||
throw RuntimeException("1")
|
||||
} catch (e: RuntimeException) {
|
||||
it.value
|
||||
111
|
||||
}
|
||||
},
|
||||
{(ex, thizz) ->
|
||||
-11111
|
||||
})
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
|
||||
fun test3(): Int {
|
||||
try {
|
||||
val res = My(111).performWithFail<My, Int>(
|
||||
{
|
||||
throw RuntimeException("-1")
|
||||
}, {
|
||||
throw RuntimeException("-2")
|
||||
})
|
||||
return res
|
||||
} catch (e: RuntimeException) {
|
||||
return e.getMessage()?.toInt2()!!
|
||||
}
|
||||
}
|
||||
|
||||
fun test33(): Int {
|
||||
try {
|
||||
val res = My(111).performWithFail2<My, Int>(
|
||||
{
|
||||
try {
|
||||
throw RuntimeException("-1")
|
||||
} catch (e: RuntimeException) {
|
||||
throw RuntimeException("-2")
|
||||
}
|
||||
},
|
||||
{(ex, thizz) ->
|
||||
if (ex.getMessage() == "-2") {
|
||||
throw RuntimeException("-3")
|
||||
} else {
|
||||
-11111
|
||||
}
|
||||
})
|
||||
return res
|
||||
} catch (e: RuntimeException) {
|
||||
return e.getMessage()!!.toInt2()!!
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (test1() != 111) return "test1: ${test1()}"
|
||||
if (test11() != 111) return "test11: ${test11()}"
|
||||
|
||||
if (test2() != 111) return "test2: ${test2()}"
|
||||
if (test22() != 111) return "test22: ${test22()}"
|
||||
|
||||
if (test3() != -2) return "test3: ${test3()}"
|
||||
if (test33() != -3) return "test33: ${test33()}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
class My(val value: Int)
|
||||
|
||||
inline fun <T, R> T.performWithFail(job: (T)-> R, failJob : (T) -> R) : R {
|
||||
try {
|
||||
return job(this)
|
||||
} catch (e: RuntimeException) {
|
||||
return failJob(this)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T, R> T.performWithFail2(job: (T)-> R, failJob : (e: RuntimeException, T) -> R) : R {
|
||||
try {
|
||||
return job(this)
|
||||
} catch (e: RuntimeException) {
|
||||
return failJob(e, this)
|
||||
}
|
||||
}
|
||||
|
||||
public inline fun String.toInt2() : Int = java.lang.Integer.parseInt(this)
|
||||
@@ -0,0 +1,73 @@
|
||||
fun test1(): Int {
|
||||
|
||||
var res = My(111).performWithFinally<My, Int>(
|
||||
{
|
||||
1
|
||||
}, {
|
||||
it.value
|
||||
})
|
||||
return res
|
||||
}
|
||||
|
||||
fun test11(): Int {
|
||||
var result = -1;
|
||||
val res = My(111).performWithFinally<My, Int>(
|
||||
{
|
||||
try {
|
||||
result = it.value
|
||||
throw RuntimeException("1")
|
||||
} catch (e: RuntimeException) {
|
||||
++result
|
||||
throw RuntimeException("2")
|
||||
}
|
||||
},
|
||||
{
|
||||
++result
|
||||
})
|
||||
return res
|
||||
}
|
||||
|
||||
fun test2(): Int {
|
||||
var res = My(111).performWithFinally<My, Int>(
|
||||
{
|
||||
throw RuntimeException("1")
|
||||
},
|
||||
{
|
||||
it.value
|
||||
})
|
||||
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
fun test3(): Int {
|
||||
try {
|
||||
var result = -1;
|
||||
val res = My(111).performWithFailFinally<My, Int>(
|
||||
{
|
||||
result = it.value;
|
||||
throw RuntimeException("-1")
|
||||
},
|
||||
{ (e, z)->
|
||||
++result
|
||||
throw RuntimeException("-2")
|
||||
},
|
||||
{
|
||||
++result
|
||||
})
|
||||
return res
|
||||
} catch (e: RuntimeException) {
|
||||
return e.getMessage()?.toInt()!!
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (test1() != 111) return "test1: ${test1()}"
|
||||
if (test11() != 113) return "test11: ${test11()}"
|
||||
|
||||
if (test2() != 111) return "test2: ${test2()}"
|
||||
|
||||
if (test3() != 113) return "test3: ${test3()}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
class My(val value: Int)
|
||||
|
||||
inline fun <T, R> T.performWithFinally(job: (T)-> R, finally: (T) -> R) : R {
|
||||
try {
|
||||
return job(this)
|
||||
} finally {
|
||||
return finally(this)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T, R> T.performWithFailFinally(job: (T)-> R, failJob : (e: RuntimeException, T) -> R, finally: (T) -> R) : R {
|
||||
try {
|
||||
return job(this)
|
||||
} catch (e: RuntimeException) {
|
||||
return failJob(e, this)
|
||||
} finally {
|
||||
return finally(this)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun String.toInt2() : Int = java.lang.Integer.parseInt(this)
|
||||
@@ -0,0 +1,22 @@
|
||||
import test.*
|
||||
|
||||
|
||||
fun Data.test1(d: Data) : Long {
|
||||
val input2 = Input(this)
|
||||
val input = Input(this)
|
||||
return input.use<Input, Long>{
|
||||
val output = Output(d)
|
||||
output.use<Output,Long>{
|
||||
input.copyTo(output, 10)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
|
||||
val result = Data().test1(Data())
|
||||
if (result != 100.toLong()) return "test1: ${result}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package test
|
||||
|
||||
public class Data()
|
||||
|
||||
public class Input(val d: Data) : Closeable {
|
||||
public fun data() : Int = 100
|
||||
}
|
||||
public class Output(val d: Data) : Closeable {
|
||||
public fun doOutput(data: Int): Int = data
|
||||
}
|
||||
|
||||
public open trait Closeable {
|
||||
open public fun close() {}
|
||||
}
|
||||
|
||||
public inline fun <T: Closeable, R> T.use(block: (T)-> R) : R {
|
||||
var closed = false
|
||||
try {
|
||||
return block(this)
|
||||
} catch (e: Exception) {
|
||||
closed = true
|
||||
try {
|
||||
this.close()
|
||||
} catch (closeException: Exception) {
|
||||
|
||||
}
|
||||
throw e
|
||||
} finally {
|
||||
if (!closed) {
|
||||
this.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public fun Input.copyTo(output: Output, size: Int): Long {
|
||||
return output.doOutput(this.data()).toLong()
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import test.*
|
||||
|
||||
fun test1(): String {
|
||||
return doSmth("O", "K")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val result = test1()
|
||||
if (result != "OK") return "fail1: ${result}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package test
|
||||
|
||||
inline fun doSmth(vararg a: String) : String {
|
||||
return a.foldRight("", {(a, b) -> a + b})
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import test.*
|
||||
|
||||
fun Data.test1(d: Data) : Long {
|
||||
val input = Input(this)
|
||||
var result = 10.toLong()
|
||||
with(input) {
|
||||
result = use<Long>{
|
||||
val output = Output(d)
|
||||
useNoInline<Long>{
|
||||
data()
|
||||
copyTo(output, 10)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
|
||||
val result = Data().test1(Data())
|
||||
if (result != 100.toLong()) return "test1: ${result}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package test
|
||||
|
||||
public class Data()
|
||||
|
||||
public class Input(val d: Data) : Closeable {
|
||||
public fun data() : Int = 100
|
||||
}
|
||||
public class Output(val d: Data) : Closeable {
|
||||
public fun doOutput(data: Int): Int = data
|
||||
}
|
||||
|
||||
public open trait Closeable {
|
||||
open public fun close() {}
|
||||
}
|
||||
|
||||
public inline fun <R> use(block: ()-> R) : R {
|
||||
return block()
|
||||
}
|
||||
|
||||
public fun <R> useNoInline(block: ()-> R) : R {
|
||||
return block()
|
||||
}
|
||||
|
||||
|
||||
public fun Input.copyTo(output: Output, size: Int): Long {
|
||||
return output.doOutput(this.data()).toLong()
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fun test1(): Int {
|
||||
val inlineX = Inline()
|
||||
var p = {(l : Int) -> l};
|
||||
return inlineX.calc(p, 25)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (test1() != 25) return "test1: ${test1()}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class Inline {
|
||||
|
||||
inline fun calc(s: (Int) -> Int, p: Int) : Int {
|
||||
return s(p)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user