more tests

This commit is contained in:
Sergey Chernov 2025-08-23 09:41:23 +03:00
parent cf79163802
commit a5e51a3f90
4 changed files with 62 additions and 12 deletions

View File

@ -16,9 +16,15 @@ fun naiveCountHappyNumbers() {
count count
} }
import lyng.time
// //
// After all optimizations it takes ~120ms. // After all optimizations it takes ~120ms.
// //
//for( r in 1..100 ) {
// val start = Instant.now()
val found = naiveCountHappyNumbers() val found = naiveCountHappyNumbers()
println("Found happy numbers: "+found) // println("Found happy numbers: %d time %s"(found, Instant.now() - start))
assert( found == 55252 ) assert( found == 55252 )
// delay(0.01)
//}

View File

@ -492,7 +492,8 @@ class Compiler(
// if it is an open end range, then the end of line could be here that we do not want // if it is an open end range, then the end of line could be here that we do not want
// to skip in parseExpression: // to skip in parseExpression:
val current = cc.current() val current = cc.current()
val right = if( current.type == Token.Type.NEWLINE || current.type == Token.Type.SINLGE_LINE_COMMENT) val right =
if (current.type == Token.Type.NEWLINE || current.type == Token.Type.SINLGE_LINE_COMMENT)
null null
else else
parseExpression() parseExpression()
@ -1383,7 +1384,13 @@ class Compiler(
loopIterable(forContext, sourceObj, loopSO, body, elseStatement, label, canBreak) loopIterable(forContext, sourceObj, loopSO, body, elseStatement, label, canBreak)
} else { } else {
val size = runCatching { sourceObj.invokeInstanceMethod(forContext, "size").toInt() } 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 result: Obj = ObjVoid
var breakCaught = false var breakCaught = false

View File

@ -77,9 +77,8 @@ fun Iterable.dropLast(n) {
} }
fun Iterable.takeLast(n) { fun Iterable.takeLast(n) {
val list = this
val buffer = RingBuffer(n) val buffer = RingBuffer(n)
for( item in list ) buffer += item for( item in this ) buffer += item
buffer buffer
} }

View File

@ -3123,9 +3123,47 @@ class ScriptTest {
println(e.toString()) println(e.toString())
println("-------------------- dee") println("-------------------- dee")
println(decoded.toString()) println(decoded.toString())
// assertEquals( e.toString(), decoded.toString() ) assertEquals( e.toString(), decoded.toString() )
} }
""".trimIndent() """.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())
}
} }