Rename directory for back-end tests
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
tailRecursive fun test(x : Int = 0, e : Any = "a") {
|
||||
if (!e.equals("a")) {
|
||||
throw IllegalArgumentException()
|
||||
}
|
||||
if (x > 0) {
|
||||
test(x - 1)
|
||||
}
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
test(100000)
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
tailRecursive fun test(counter : Int, a : Any) : Int? {
|
||||
if (counter < 0) return null
|
||||
if (counter == 0) return 777
|
||||
|
||||
return test(-1, "no tail") ?: test(-2, "no tail") ?: test(counter - 1, "tail")
|
||||
}
|
||||
|
||||
fun box() : String =
|
||||
if (test(100000, "test") == 777) "OK"
|
||||
else "FAIL"
|
||||
@@ -0,0 +1,25 @@
|
||||
class B {
|
||||
inner class C {
|
||||
tailRecursive fun h(counter : Int, x : Any) {
|
||||
if (counter > 0) {
|
||||
this@C.h(counter - 1, "tail")
|
||||
}
|
||||
}
|
||||
|
||||
tailRecursive fun h2(x : Any) {
|
||||
this@B.h2("no recursion") // keep vigilance
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun makeC() : C = C()
|
||||
|
||||
fun h2(x : Any) {
|
||||
}
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
B().makeC().h(1000000, "test")
|
||||
B().makeC().h2(0)
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
tailRecursive fun test(x : Int, a : Any) : Int {
|
||||
var z = if (x > 3) 3 else x
|
||||
while (z > 0) {
|
||||
if (z > 10) {
|
||||
return test(x - 1, "tail")
|
||||
}
|
||||
test(0, "no tail")
|
||||
z = z - 1
|
||||
}
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
fun box() : String = if (test(100000, "test") == 1) "OK" else "FAIL"
|
||||
@@ -0,0 +1,15 @@
|
||||
tailRecursive fun test(x : Int, a : Any) : Int {
|
||||
if (x == 1) {
|
||||
if (x != 1) {
|
||||
test(0, "no tail")
|
||||
return test(0, "tail")
|
||||
} else {
|
||||
return test(x + test(0, "no tail"), "tail")
|
||||
}
|
||||
} else if (x > 0) {
|
||||
return test(x - 1, "tail")
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
fun box() : String = if (test(1000000, "test") == -1) "OK" else "FAIL"
|
||||
@@ -0,0 +1,11 @@
|
||||
tailRecursive fun <T, A> Iterator<T>.foldl(acc : A, foldFunction : (e : T, acc : A) -> A) : A =
|
||||
if (!hasNext()) acc
|
||||
else foldl(foldFunction(next(), acc), foldFunction)
|
||||
|
||||
fun box() : String {
|
||||
val sum = (1..1000000).iterator().foldl(0) { (e : Int, acc : Long) ->
|
||||
acc + e
|
||||
}
|
||||
|
||||
return if (sum == 500000500000) "OK" else "FAIL: $sum"
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
fun escapeChar(c : Char) : String? = when (c) {
|
||||
'\\' -> "\\\\"
|
||||
'\n' -> "\\n"
|
||||
'"' -> "\\\""
|
||||
else -> "" + c
|
||||
}
|
||||
|
||||
tailRecursive fun String.escape(i : Int = 0, result : StringBuilder = StringBuilder()) : String =
|
||||
if (i == length) result.toString()
|
||||
else escape(i + 1, result.append(escapeChar(get(i))))
|
||||
|
||||
fun box() : String {
|
||||
"test me not \\".escape()
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
tailRecursive fun String.repeat(num : Int, acc : StringBuilder = StringBuilder()) : String =
|
||||
if (num == 0) acc.toString()
|
||||
else repeat(num - 1, acc.append(this)!!)
|
||||
|
||||
fun box() : String {
|
||||
val s = "a".repeat(10000)
|
||||
return if (s.length == 10000) "OK" else "FAIL: ${s.length}"
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fun test() {
|
||||
[tailRecursive] fun g3(counter : Int, x : Any) {
|
||||
if (counter > 0) { g3(counter - 1, "tail") }
|
||||
}
|
||||
g3(1000000, "test")
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
test()
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
tailRecursive fun test(counter : Int, a : Any) : Int {
|
||||
if (counter == 0) return 0
|
||||
|
||||
try {
|
||||
throw Exception()
|
||||
} catch (e : Exception) {
|
||||
return test(counter - 1, "no tail")
|
||||
}
|
||||
}
|
||||
|
||||
fun box() : String = if (test(3, "test") == 0) "OK" else "FAIL"
|
||||
@@ -0,0 +1,11 @@
|
||||
tailRecursive fun test(counter : Int, a : Any) : Int {
|
||||
if (counter == 0) return 0
|
||||
|
||||
try {
|
||||
// do nothing
|
||||
} finally {
|
||||
return test(counter - 1, "no tail")
|
||||
}
|
||||
}
|
||||
|
||||
fun box() : String = if (test(3, "test") == 0) "OK" else "FAIL"
|
||||
@@ -0,0 +1,15 @@
|
||||
tailRecursive fun test(counter : Int, a : Any) : Int {
|
||||
if (counter == 0) return 0
|
||||
|
||||
try {
|
||||
// do nothing
|
||||
} finally {
|
||||
if (counter > 0) {
|
||||
return test(counter - 1, "no tail")
|
||||
}
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
fun box() : String = if (test(3, "test") == 0) "OK" else "FAIL"
|
||||
@@ -0,0 +1,11 @@
|
||||
tailRecursive fun test(counter : Int, a : Any) : Int {
|
||||
if (counter == 0) return 0
|
||||
|
||||
try {
|
||||
return test(counter - 1, "no tail")
|
||||
} catch (any : Throwable) {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
fun box() : String = if (test(3, "test") == 0) "OK" else "FAIL"
|
||||
@@ -0,0 +1,11 @@
|
||||
tailRecursive fun test(x : Int, a : Any) : Int =
|
||||
if (x == 1) {
|
||||
test(x - 1, "no tail")
|
||||
1 + test(x - 1, "no tail")
|
||||
} else if (x > 0) {
|
||||
test(x - 1, "tail")
|
||||
} else {
|
||||
0
|
||||
}
|
||||
|
||||
fun box() : String = if (test(1000000, "test") == 1) "OK" else "FAIL"
|
||||
@@ -0,0 +1,11 @@
|
||||
tailRecursive fun test(x : Int, z : Any) : Int {
|
||||
if (x == 10) {
|
||||
return 1 + test(x - 1, "no tail")
|
||||
}
|
||||
if (x > 0) {
|
||||
return test(x - 1, "tail")
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
fun box() : String = if (test(1000000, "test") == 1) "OK" else "FAIL"
|
||||
@@ -0,0 +1,12 @@
|
||||
tailRecursive fun test(x : Int, a : Any) : Int {
|
||||
if (x == 0) {
|
||||
return 0
|
||||
} else if (x == 10) {
|
||||
test(0, "no tail")
|
||||
return 1 + test(x - 1, "no tail")
|
||||
} else {
|
||||
return test(x - 1, "tail")
|
||||
}
|
||||
}
|
||||
|
||||
fun box() : String = if (test(1000000, "test") == 1) "OK" else "FAIL"
|
||||
@@ -0,0 +1,23 @@
|
||||
class A {
|
||||
tailRecursive fun f1(c : Int, x : Any) {
|
||||
if (c > 0) {
|
||||
this.f1(c - 1, "tail")
|
||||
}
|
||||
}
|
||||
|
||||
tailRecursive fun f2(c : Int, x : Any) {
|
||||
if (c > 0) {
|
||||
f2(c - 1, "tail 108")
|
||||
}
|
||||
}
|
||||
|
||||
tailRecursive fun f3(a : A, x : Any) {
|
||||
a.f3(a, "no tail") // non-tail recursion, could be potentially resolved by condition if (a == this) f3() else a.f3()
|
||||
}
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
A().f1(1000000, "test")
|
||||
A().f2(1000000, "test")
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
tailRecursive fun test(x : Int, e : Any) : Unit {
|
||||
if (x == 1) {
|
||||
test(x - 1, "tail")
|
||||
} else if (x == 2) {
|
||||
test(x - 1, "tail")
|
||||
return
|
||||
} else if (x == 3) {
|
||||
test(x - 1, "no tail")
|
||||
if (x == 3) {
|
||||
test(x - 1, "tail")
|
||||
}
|
||||
return
|
||||
} else if (x > 0) {
|
||||
test(x - 1, "tail")
|
||||
}
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
test(1000000, "test")
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
tailRecursive fun withWhen(counter : Int, x : Any) : Int =
|
||||
when (counter) {
|
||||
0 -> counter
|
||||
50 -> 1 + withWhen(counter - 1, "no tail")
|
||||
else -> withWhen(counter - 1, "tail")
|
||||
}
|
||||
|
||||
fun box() : String = if (withWhen(100000, "test") == 1) "OK" else "FAIL"
|
||||
@@ -0,0 +1,11 @@
|
||||
tailRecursive fun withWhen(counter : Int, d : Any, x : Any) : Int =
|
||||
when (counter) {
|
||||
0 -> counter
|
||||
1, 2 -> withWhen(counter - 1, "1,2", "tail")
|
||||
in 3..49 -> withWhen(counter - 1, "3..49", "tail")
|
||||
50 -> 1 + withWhen(counter - 1, "50", "no tail")
|
||||
!in 0..50 -> withWhen(counter - 1, "!0..50", "tail")
|
||||
else -> withWhen(counter - 1, "else", "tail")
|
||||
}
|
||||
|
||||
fun box() : String = if (withWhen(100000, "test", "test") == 1) "OK" else "FAIL"
|
||||
@@ -0,0 +1,15 @@
|
||||
tailRecursive fun withWhen(counter : Int, d : Any, x : Any) : Int =
|
||||
if (counter == 0) {
|
||||
0
|
||||
}
|
||||
else if (counter == 5) {
|
||||
withWhen(counter - 1, 999, "tail")
|
||||
}
|
||||
else
|
||||
when (d) {
|
||||
is String -> withWhen(counter - 1, "is String", "tail")
|
||||
is Number -> withWhen(counter, "is Number", "tail")
|
||||
else -> throw IllegalStateException()
|
||||
}
|
||||
|
||||
fun box() : String = if (withWhen(100000, "test", "test") == 0) "OK" else "FAIL"
|
||||
@@ -0,0 +1,9 @@
|
||||
tailRecursive fun withWhen2(counter : Int, x : Any) : Int =
|
||||
when {
|
||||
counter == 0 -> counter
|
||||
counter == 50 -> 1 + withWhen2(counter - 1, "no tail")
|
||||
withWhen2(0, "no tail") == 0 -> withWhen2(counter - 1, "tail")
|
||||
else -> 1
|
||||
}
|
||||
|
||||
fun box() : String = if (withWhen2(100000, "test") == 1) "OK" else "FAIL"
|
||||
Reference in New Issue
Block a user