more tests
This commit is contained in:
		
							parent
							
								
									cf79163802
								
							
						
					
					
						commit
						a5e51a3f90
					
				@ -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 )
 | 
			
		||||
//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)
 | 
			
		||||
//}
 | 
			
		||||
@ -225,7 +225,7 @@ class Compiler(
 | 
			
		||||
                // very special case chained calls: call()<NL>.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,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
 | 
			
		||||
                    // to skip in parseExpression:
 | 
			
		||||
                    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
 | 
			
		||||
                        else
 | 
			
		||||
                            parseExpression()
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user