From a5e51a3f9090604bae8f9d7b8d7ee854b08919f1 Mon Sep 17 00:00:00 2001 From: sergeych Date: Sat, 23 Aug 2025 09:41:23 +0300 Subject: [PATCH] more tests --- docs/samples/happy_numbers.lyng | 12 ++++-- .../kotlin/net/sergeych/lyng/Compiler.kt | 19 ++++++--- .../lyng/stdlib_included/root_lyng.kt | 3 +- lynglib/src/commonTest/kotlin/ScriptTest.kt | 40 ++++++++++++++++++- 4 files changed, 62 insertions(+), 12 deletions(-) diff --git a/docs/samples/happy_numbers.lyng b/docs/samples/happy_numbers.lyng index 2bada4b..c5810dc 100644 --- a/docs/samples/happy_numbers.lyng +++ b/docs/samples/happy_numbers.lyng @@ -16,9 +16,15 @@ fun naiveCountHappyNumbers() { count } +import lyng.time + // // After all optimizations it takes ~120ms. // -val found = naiveCountHappyNumbers() -println("Found happy numbers: "+found) -assert( found == 55252 ) \ No newline at end of file +//for( r in 1..100 ) { +// val start = Instant.now() + val found = naiveCountHappyNumbers() +// println("Found happy numbers: %d time %s"(found, Instant.now() - start)) + assert( found == 55252 ) +// delay(0.01) +//} \ No newline at end of file diff --git a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt index 88ba683..d5db1fd 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt @@ -225,7 +225,7 @@ class Compiler( // very special case chained calls: call().call2 {}.call3() Token.Type.NEWLINE -> { val saved = cc.savePos() - if( cc.peekNextNonWhitespace().type == Token.Type.DOT) { + if (cc.peekNextNonWhitespace().type == Token.Type.DOT) { // chained call continue from it continue } else { @@ -492,10 +492,11 @@ class Compiler( // if it is an open end range, then the end of line could be here that we do not want // to skip in parseExpression: val current = cc.current() - val right = if( current.type == Token.Type.NEWLINE || current.type == Token.Type.SINLGE_LINE_COMMENT) - null - else - parseExpression() + val right = + if (current.type == Token.Type.NEWLINE || current.type == Token.Type.SINLGE_LINE_COMMENT) + null + else + parseExpression() operand = Accessor { ObjRange( left?.getter?.invoke(it)?.value ?: ObjNull, @@ -1383,7 +1384,13 @@ class Compiler( loopIterable(forContext, sourceObj, loopSO, body, elseStatement, label, canBreak) } else { val size = runCatching { sourceObj.invokeInstanceMethod(forContext, "size").toInt() } - .getOrElse { throw ScriptError(tOp.pos, "object is not enumerable: no size", it) } + .getOrElse { + throw ScriptError( + tOp.pos, + "object is not enumerable: no size in $sourceObj", + it + ) + } var result: Obj = ObjVoid var breakCaught = false diff --git a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/stdlib_included/root_lyng.kt b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/stdlib_included/root_lyng.kt index 7ca2fd7..3370384 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/stdlib_included/root_lyng.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/stdlib_included/root_lyng.kt @@ -77,9 +77,8 @@ fun Iterable.dropLast(n) { } fun Iterable.takeLast(n) { - val list = this val buffer = RingBuffer(n) - for( item in list ) buffer += item + for( item in this ) buffer += item buffer } diff --git a/lynglib/src/commonTest/kotlin/ScriptTest.kt b/lynglib/src/commonTest/kotlin/ScriptTest.kt index 0f88a5b..2157a20 100644 --- a/lynglib/src/commonTest/kotlin/ScriptTest.kt +++ b/lynglib/src/commonTest/kotlin/ScriptTest.kt @@ -3123,9 +3123,47 @@ class ScriptTest { println(e.toString()) println("-------------------- dee") println(decoded.toString()) -// assertEquals( e.toString(), decoded.toString() ) + assertEquals( e.toString(), decoded.toString() ) } """.trimIndent() ) } + + @Test + fun testThisInClosure() = runTest { + eval(""" + fun Iterable.sum2by(f) { + var acc = null + for( x in this ) { + println(x) + println(f(x)) + acc = acc?.let { acc + f(x) } ?: f(x) + } + } + class T(val coll, val factor) { + fun sum() { + // here we use ths::T and it must be available: + coll.sum2by { it * factor } + } + } + assertEquals(60, T([1,2,3], 10).sum()) + """.trimIndent()) + } + + @Test + fun testThisInFlowClosure() = runTest { + eval(""" + class T(val coll, val factor) { + fun seq() { + flow { + for( x in coll ) { + emit(x*factor) + } + } + } + } + assertEquals([10,20,30], T([1,2,3], 10).seq().toList()) + """.trimIndent()) + } + } \ No newline at end of file