Codegen tests: move stdlib tests to boxWithStdlib
This commit is contained in:
committed by
Alexander Udalov
parent
83b93071d3
commit
8a605b3884
@@ -0,0 +1,150 @@
|
||||
package collections
|
||||
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
import java.security.KeyPair
|
||||
|
||||
|
||||
fun <T> testCollectionSize(c: Collection<T>) = assertEquals(0, c.size())
|
||||
fun <T> testCollectionIsEmpty(c: Collection<T>) = assertTrue(c.isEmpty())
|
||||
fun <T> testCollectionContains(c: Collection<T>) = assertTrue(c.contains(1))
|
||||
fun <T> testCollectionIterator(c: Collection<T>) {
|
||||
val it = c.iterator()
|
||||
while (it.hasNext()) {
|
||||
assertEquals(1, it.next())
|
||||
}
|
||||
}
|
||||
fun <T> testCollectionToArray(c: Collection<T>) = assertEquals(1, c.toArray()[0])
|
||||
fun <T> testCollectionContainsAll(c: Collection<T>) = assertTrue(c.containsAll(c))
|
||||
fun <T> testMutableCollectionAdd(c: MutableCollection<T>, t: T) {
|
||||
c.add(t)
|
||||
assertEquals(1, c.size())
|
||||
assertTrue(c.contains(t))
|
||||
}
|
||||
fun <T> testMutableCollectionRemove(c: MutableCollection<T>, t: T) {
|
||||
c.remove(t)
|
||||
assertEquals(0, c.size())
|
||||
assertFalse(c.contains(t))
|
||||
}
|
||||
fun <T> testMutableCollectionIterator(c: MutableCollection<T>, t: T) {
|
||||
c.add(t)
|
||||
val it = c.iterator()
|
||||
while (it.hasNext()) {
|
||||
it.next()
|
||||
it.remove()
|
||||
}
|
||||
assertEquals(0, c.size())
|
||||
}
|
||||
fun <T> testMutableCollectionAddAll(c: MutableCollection<T>, t1: T, t2: T) {
|
||||
c.addAll(arrayList(t1, t2))
|
||||
assertEquals(arrayList(t1, t2), c)
|
||||
}
|
||||
fun <T> testMutableCollectionRemoveAll(c: MutableCollection<T>, t1: T, t2: T) {
|
||||
c.addAll(arrayList(t1, t2))
|
||||
c.removeAll(arrayList(t1))
|
||||
assertEquals(arrayList(t2), c)
|
||||
}
|
||||
fun <T> testMutableCollectionRetainAll(c: MutableCollection<T>, t1: T, t2: T) {
|
||||
c.addAll(arrayList(t1, t2))
|
||||
c.retainAll(arrayList(t1))
|
||||
assertEquals(arrayList(t1), c)
|
||||
}
|
||||
fun <T> testMutableCollectionClear(c: MutableCollection<T>) {
|
||||
c.clear()
|
||||
assertTrue(c.isEmpty())
|
||||
}
|
||||
|
||||
fun testCollection() {
|
||||
testCollectionSize(arrayList<Int>())
|
||||
testCollectionIsEmpty(arrayList<String>())
|
||||
testCollectionContains(arrayList(1))
|
||||
testCollectionIterator(arrayList(1))
|
||||
testCollectionToArray(arrayList(1))
|
||||
testCollectionContainsAll(arrayList("a", "b"))
|
||||
|
||||
testMutableCollectionAdd(arrayList(), "")
|
||||
testMutableCollectionRemove(arrayList("a"), "a")
|
||||
testMutableCollectionIterator(arrayList(), 1)
|
||||
testMutableCollectionAddAll(arrayList(), 1, 2)
|
||||
testMutableCollectionRemoveAll(arrayList(), 1, 2)
|
||||
testMutableCollectionRetainAll(arrayList(), 1, 2)
|
||||
testMutableCollectionClear(arrayList(1, 2))
|
||||
}
|
||||
|
||||
|
||||
fun <T> testListGet(l: List<T>, t: T) = assertEquals(t, l.get(0))
|
||||
fun <T> testListIndexOf(l: List<T>, t: T) = assertEquals(0, l.indexOf(t))
|
||||
fun <T> testListIterator(l: List<T>, t1 : T, t2 : T) {
|
||||
val indexes = arrayList<Int>()
|
||||
val result = arrayList<T>()
|
||||
val it = l.listIterator()
|
||||
while (it.hasNext()) {
|
||||
indexes.add(it.nextIndex())
|
||||
result.add(it.next())
|
||||
}
|
||||
while (it.hasPrevious()) {
|
||||
indexes.add(it.previousIndex())
|
||||
result.add(it.previous())
|
||||
}
|
||||
assertEquals(arrayList(0, 1, 1, 0), indexes)
|
||||
assertEquals(arrayList(t1, t2, t2, t1), result)
|
||||
}
|
||||
fun <T> testListSublist(l: List<T>, t: T) = assertEquals(arrayList(t), l.subList(0, 1))
|
||||
|
||||
fun <T> testMutableListSet(l: MutableList<T>, t: T) {
|
||||
l.set(0, t)
|
||||
assertEquals(arrayList(t), l)
|
||||
}
|
||||
fun <T> testMutableListIterator(l: MutableList<T>, t1: T, t2: T, t3: T) {
|
||||
val it = l.listIterator()
|
||||
while (it.hasNext()) {
|
||||
it.next()
|
||||
it.add(t3)
|
||||
}
|
||||
assertEquals(arrayList(t1, t3, t2, t3), l)
|
||||
}
|
||||
|
||||
fun testList() {
|
||||
testListGet(arrayList(1), 1)
|
||||
testListIndexOf(arrayList(1), 1)
|
||||
testListIterator(arrayList("a", "b"), "a", "b")
|
||||
testListSublist(arrayList(1, 2), 1)
|
||||
|
||||
testMutableListSet(arrayList(2), 4)
|
||||
testMutableListIterator(arrayList(1, 2), 1, 2, 3)
|
||||
}
|
||||
|
||||
fun <K, V> testMapContainsKey(map: Map<K, V>, k: K) = assertTrue(map.containsKey(k))
|
||||
fun <K, V> testMapKeys(map: Map<K, V>, k1: K, k2: K) = assertEqualCollections(hashSet(k1, k2), map.keySet())
|
||||
fun <K, V> testMapValues(map: Map<K, V>, v1: V, v2: V) = assertEqualCollections(hashSet(v1, v2), map.values())
|
||||
fun <K, V> testMapEntrySet(map: Map<K, V>, k : K, v: V) {
|
||||
for (entry in map.entrySet()) {
|
||||
assertEquals(k, entry.key)
|
||||
assertEquals(v, entry.value)
|
||||
}
|
||||
}
|
||||
fun <K, V> testMutableMapEntry(map: MutableMap<K, V>, k1 : K, v: V) {
|
||||
for (entry in map.entrySet()) {
|
||||
entry.setValue(v)
|
||||
}
|
||||
assertEquals(hashMap(k1 to v), map)
|
||||
}
|
||||
|
||||
|
||||
fun testMap() {
|
||||
testMapContainsKey(hashMap(1 to 'a', 2 to 'b'), 2)
|
||||
testMapKeys(hashMap(1 to 'a', 2 to 'b'), 1, 2)
|
||||
testMapValues(hashMap(1 to 'a', 2 to 'b'), 'a', 'b')
|
||||
testMapEntrySet(hashMap(1 to 'a'), 1, 'a')
|
||||
testMutableMapEntry(hashMap(1 to 'a'), 1, 'b')
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
testCollection()
|
||||
testList()
|
||||
testMap()
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun <T> assertEqualCollections(c1: Collection<T>, c2: Collection<T>) = assertEquals(c1.toCollection(hashSet<T>()), c2.toCollection(hashSet<T>()))
|
||||
@@ -0,0 +1,21 @@
|
||||
import java.util.*
|
||||
|
||||
fun <T> ArrayList<T>.findAll(predicate: (T) -> Boolean): ArrayList<T> {
|
||||
val result = ArrayList<T>()
|
||||
for(val t in this) {
|
||||
if (predicate(t)) result.add(t)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
val list: ArrayList<String> = ArrayList<String>()
|
||||
list.add("Prague")
|
||||
list.add("St.Petersburg")
|
||||
list.add("Moscow")
|
||||
list.add("Munich")
|
||||
|
||||
val m: ArrayList<String> = list.findAll<String>({(name: String) -> name.startsWith("M")})
|
||||
return if (m.size() == 2) "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package pack
|
||||
|
||||
import java.util.ArrayList
|
||||
import java.util.regex.Pattern
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
class C{
|
||||
public fun foo(){
|
||||
val items : Collection<Item> = java.util.Collections.singleton(Item())!!
|
||||
val result = ArrayList<Item>()
|
||||
val pattern: Pattern? = Pattern.compile("...")
|
||||
items.filterTo(result) {
|
||||
pattern!!.matcher(it.name())!!.matches()
|
||||
}
|
||||
}
|
||||
|
||||
private fun Item.name() : String = ""
|
||||
}
|
||||
|
||||
class Item{
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
C().foo()
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun box() : String {
|
||||
val i = 1
|
||||
return if(i.javaClass.getSimpleName() == "int") "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fun box() : String {
|
||||
val map = java.util.TreeMap<String, String>()
|
||||
map["a"] = "1"
|
||||
map["b"] = "2"
|
||||
|
||||
var list = arrayList<String>()
|
||||
for (e in map) {
|
||||
list += e.getKey()
|
||||
}
|
||||
|
||||
return if(list.size==2) "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import kotlin.support.AbstractIterator
|
||||
|
||||
class MyIterator : AbstractIterator<String>() {
|
||||
var i = 0
|
||||
public override fun computeNext() {
|
||||
if(i < 5)
|
||||
setNext((i++).toString())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
var k = ""
|
||||
for (x in MyIterator()) {
|
||||
k+=x
|
||||
}
|
||||
return if(k=="01234") "OK" else k
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
//KT-1800 error/NonExistentClass generated on runtime
|
||||
package i
|
||||
|
||||
import java.util.ArrayList
|
||||
|
||||
public class User(val firstName: String,
|
||||
val lastName: String,
|
||||
val age: Int) {
|
||||
fun toString() = "$firstName $lastName, age $age"
|
||||
}
|
||||
|
||||
public fun <T: Comparable<T>> Collection<T>.min(): T? {
|
||||
var minValue: T? = null
|
||||
for(value in this) {
|
||||
if (minValue == null || value.compareTo(minValue!!) < 0) {
|
||||
minValue = value
|
||||
}
|
||||
}
|
||||
return minValue
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
val users = arrayList(
|
||||
User("John", "Doe", 30),
|
||||
User("Jane", "Doe", 27))
|
||||
|
||||
val ages = users.map { it.age }
|
||||
|
||||
val minAge = ages.min()
|
||||
return if (minAge == 27) "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import java.lang.annotation.Retention
|
||||
import java.lang.annotation.RetentionPolicy
|
||||
import java.lang.annotation.Annotation
|
||||
|
||||
Retention(RetentionPolicy.RUNTIME) annotation class foo(val name : String)
|
||||
|
||||
class Test() {
|
||||
foo("OK") fun hello(input : String) {
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val test = Test()
|
||||
for (method in javaClass<Test>().getMethods()!!) {
|
||||
val anns = method?.getAnnotations() as Array<Annotation>
|
||||
if (!anns.isEmpty()) {
|
||||
for (ann in anns) {
|
||||
val fooAnn = ann as foo
|
||||
return fooAnn.name
|
||||
}
|
||||
}
|
||||
}
|
||||
return "fail"
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class A<T>(t: Array<Array<T>>) {
|
||||
val a:Array<Array<T>> = t
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
A<Int>(array()) // <- java.lang.VerifyError: (class: A, method: getA signature: ()[[Ljava/lang/Object;) Wrong return type in function
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun box(): String {
|
||||
kotlin.assert(true)
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fun box(): String {
|
||||
javaClass<Boolean>()
|
||||
javaClass<Byte>()
|
||||
javaClass<Short>()
|
||||
javaClass<Char>()
|
||||
javaClass<Int>()
|
||||
javaClass<Long>()
|
||||
javaClass<Float>()
|
||||
javaClass<Double>()
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import kotlin.test.*
|
||||
import org.junit.Test as test
|
||||
|
||||
public class Test {
|
||||
test fun f(): Unit {
|
||||
assertEquals(true, !false)
|
||||
}
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
Test().f()
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun foo() {
|
||||
if (1==1) {
|
||||
1.javaClass
|
||||
} else {
|
||||
}
|
||||
}
|
||||
|
||||
fun box() = "OK"
|
||||
@@ -0,0 +1,14 @@
|
||||
import kotlin.*
|
||||
|
||||
fun box() : String {
|
||||
val vector = java.util.Vector<Int>()
|
||||
vector.add(1)
|
||||
vector.add(2)
|
||||
vector.add(3)
|
||||
|
||||
var sum = 0
|
||||
for(e in vector.elements()) {
|
||||
sum += e
|
||||
}
|
||||
return if(sum == 6) "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
fun box(): String {
|
||||
val bool = true
|
||||
if (bool.javaClass != javaClass<Boolean>()) return "javaClass function on boolean fails"
|
||||
val b = 1.toByte()
|
||||
if (b.javaClass != javaClass<Byte>()) return "javaClass function on byte fails"
|
||||
val s = 1.toShort()
|
||||
if (s.javaClass != javaClass<Short>()) return "javaClass function on short fails"
|
||||
val c = 'c'
|
||||
if (c.javaClass != javaClass<Char>()) return "javaClass function on char fails"
|
||||
val i = 1
|
||||
if (i.javaClass != javaClass<Int>()) return "javaClass function on int fails"
|
||||
val l = 1.toLong()
|
||||
if (l.javaClass != javaClass<Long>()) return "javaClass function on long fails"
|
||||
val f = 1.toFloat()
|
||||
if (f.javaClass != javaClass<Float>()) return "javaClass function on float fails"
|
||||
val d = 1.0
|
||||
if (d.javaClass != javaClass<Double>()) return "javaClass function on double fails"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
fun s0() : Boolean {
|
||||
val y = "222"
|
||||
val foo = {
|
||||
val bar = { y }
|
||||
bar ()
|
||||
}
|
||||
return foo() == "222"
|
||||
}
|
||||
|
||||
fun s1() : Boolean {
|
||||
var x = "222"
|
||||
val foo = {
|
||||
val bar = {
|
||||
x = "aaa"
|
||||
}
|
||||
bar ()
|
||||
}
|
||||
foo()
|
||||
return x == "aaa"
|
||||
}
|
||||
|
||||
fun t1() : Boolean {
|
||||
var x = "111"
|
||||
|
||||
val y = x + "22"
|
||||
val foo = {
|
||||
x = x + "45" + y
|
||||
x = x.substring(3)
|
||||
x += "aaa"
|
||||
Unit.VALUE
|
||||
}
|
||||
foo()
|
||||
|
||||
x += "bbb"
|
||||
System.out?.println(x)
|
||||
return x == "4511122aaabbb"
|
||||
}
|
||||
|
||||
fun t2() : Boolean {
|
||||
var x = 111
|
||||
val y = x + 22
|
||||
val foo = {
|
||||
x = x + 5 + y
|
||||
x += 5
|
||||
x++
|
||||
Unit.VALUE
|
||||
}
|
||||
foo()
|
||||
x -= 55
|
||||
System.out?.println(x)
|
||||
return x == 200
|
||||
}
|
||||
|
||||
fun t3() : Boolean {
|
||||
var x = true
|
||||
val foo = {
|
||||
x = false
|
||||
Unit.VALUE
|
||||
}
|
||||
foo()
|
||||
return !x
|
||||
}
|
||||
|
||||
fun t4() : Boolean {
|
||||
var x = 100.toFloat()
|
||||
val y = x + 22
|
||||
val foo = {
|
||||
x = x + 200.toFloat() + y
|
||||
x += 18
|
||||
Unit.VALUE
|
||||
}
|
||||
foo()
|
||||
System.out?.println(x)
|
||||
return x == 440.toFloat()
|
||||
}
|
||||
|
||||
fun t5() : Boolean {
|
||||
var x = 100.toDouble()
|
||||
val y = x + 22
|
||||
val foo = {
|
||||
x = x + 200.toDouble() + y
|
||||
x -= 22
|
||||
Unit.VALUE
|
||||
}
|
||||
foo()
|
||||
System.out?.println(x)
|
||||
return x == 400.toDouble()
|
||||
}
|
||||
|
||||
fun t6() : Boolean {
|
||||
var x = 20.toByte()
|
||||
val y = x + 22
|
||||
val foo = {
|
||||
x = (x + 20.toByte() + y).toByte()
|
||||
x += 2
|
||||
x--
|
||||
Unit.VALUE
|
||||
}
|
||||
foo()
|
||||
System.out?.println(x)
|
||||
return x == 83.toByte()
|
||||
}
|
||||
|
||||
fun t7() : Boolean {
|
||||
var x : Char = 'a'
|
||||
val foo = {
|
||||
x = 'b'
|
||||
Unit.VALUE
|
||||
}
|
||||
foo()
|
||||
System.out?.println(x)
|
||||
return x == 'b'
|
||||
}
|
||||
|
||||
fun t8() : Boolean {
|
||||
var x = 20.toShort()
|
||||
val foo = {
|
||||
val bar = {
|
||||
x = 30.toShort()
|
||||
Unit.VALUE
|
||||
}
|
||||
bar()
|
||||
Unit.VALUE
|
||||
}
|
||||
foo()
|
||||
return x == 30.toShort()
|
||||
}
|
||||
|
||||
fun t9(var x: Int) : Boolean {
|
||||
while(x < 100) {
|
||||
x++
|
||||
}
|
||||
return x == 100
|
||||
}
|
||||
|
||||
fun t10() : Boolean {
|
||||
var y = 1
|
||||
val foo = {
|
||||
val bar = {
|
||||
y = y + 1
|
||||
}
|
||||
bar()
|
||||
}
|
||||
foo()
|
||||
return y == 2
|
||||
}
|
||||
|
||||
fun t11(var x: Int) : Int {
|
||||
val foo = {
|
||||
x = x + 1
|
||||
val bar = {
|
||||
x = x + 1
|
||||
x += 3
|
||||
}
|
||||
bar()
|
||||
}
|
||||
while(x < 100) {
|
||||
foo()
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
fun t12(x: Int) : Int {
|
||||
var y = x
|
||||
val runnable = object : Runnable {
|
||||
override fun run () {
|
||||
y = y + 1
|
||||
}
|
||||
}
|
||||
while(y < 100) {
|
||||
runnable.run()
|
||||
}
|
||||
return y
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (!s0()) return "s0 fail"
|
||||
if (!s1()) return "s1 fail"
|
||||
if (!t1()) return "t1 fail"
|
||||
if (!t2()) return "t2 fail"
|
||||
if (!t3()) return "t3 fail"
|
||||
if (!t4()) return "t4 fail"
|
||||
if (!t5()) return "t5 fail"
|
||||
if (!t6()) return "t6 fail"
|
||||
if (!t7()) return "t7 fail"
|
||||
if (!t8()) return "t8 fail"
|
||||
if (!t9(0)) return "t9 fail"
|
||||
if (!t10()) return "t10 fail"
|
||||
if (t11(1) != 101) return "t11 fail"
|
||||
if (t12(0) != 100) return "t12 fail"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package mask
|
||||
|
||||
import java.io.*
|
||||
import java.util.*
|
||||
|
||||
fun box() : String {
|
||||
val input = StringReader("/Users/abreslav/work/jet/docs/luhnybin/src/test")
|
||||
|
||||
val luhny = Luhny()
|
||||
input.forEachChar {
|
||||
luhny.charIn(it)
|
||||
}
|
||||
luhny.printAll()
|
||||
return "OK"
|
||||
}
|
||||
|
||||
class Luhny() {
|
||||
val buffer = LinkedList<Char>()
|
||||
val digits = LinkedList<Int>()
|
||||
|
||||
var toBeMasked = 0
|
||||
|
||||
fun charIn(it : Char) {
|
||||
buffer.addLast(it)
|
||||
|
||||
// Commented for KT-621
|
||||
// when (it) {
|
||||
// .isDigit() => digits.addLast(it.toInt() - '0'.toInt())
|
||||
// ' ', '-' => {}
|
||||
// else => {
|
||||
// printAll()
|
||||
// digits.clear()
|
||||
// }
|
||||
// }
|
||||
|
||||
if (it.isDigit()) {
|
||||
digits.addLast(it.toInt() - '0'.toInt())
|
||||
} else if (it == ' ' || it == '-') {
|
||||
} else {
|
||||
printAll()
|
||||
digits.clear()
|
||||
}
|
||||
|
||||
if (digits.size() > 16)
|
||||
printOneDigit()
|
||||
check()
|
||||
}
|
||||
|
||||
fun check() {
|
||||
if (digits.size() < 14) return
|
||||
print("check")
|
||||
val sum = digits.sum { i, d ->
|
||||
// println("$i -> $d")
|
||||
if (i % 2 == digits.size()) {
|
||||
val f = d * 2 / 10
|
||||
val s = d * 2 % 10
|
||||
// println("$d: f = $f, s = $s")
|
||||
(f + s).pr {
|
||||
// println("to be doubled: $i -> $d : $it")
|
||||
}
|
||||
} else d
|
||||
}
|
||||
// println(sum)
|
||||
if (sum % 10 == 0) {print("s"); toBeMasked = digits.size()}
|
||||
}
|
||||
|
||||
fun printOneDigit() {
|
||||
while (!buffer.isEmpty()) {
|
||||
val c = buffer.removeFirst()
|
||||
out(c)
|
||||
if (c.isDigit()) {
|
||||
digits.removeFirst()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun printAll() {
|
||||
while (!buffer.isEmpty())
|
||||
out(buffer.removeFirst())
|
||||
}
|
||||
|
||||
fun out(c : Char) {
|
||||
if (toBeMasked > 0) {
|
||||
print('X')
|
||||
toBeMasked--
|
||||
}
|
||||
else {
|
||||
print(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> T.pr(f : (T) -> Unit) : T {
|
||||
f(this)
|
||||
return this
|
||||
}
|
||||
|
||||
fun LinkedList<Int>.sum(f : (Int, Int )-> Int): Int {
|
||||
var sum = 0
|
||||
for (i in 1..size()) {
|
||||
val j = size() - i
|
||||
sum += f(j, get(j))
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
//fun <T> List<T>.backwards() : Itl<T> = object : Itl<T> {
|
||||
// override fun iterator() : It<T> =
|
||||
// object : It<T> {
|
||||
// var current = size()
|
||||
// override fun next() : T = get(--current)
|
||||
// override fun hasNext() : Boolean = current > 0
|
||||
// override fun remove() {throw UnsupportedOperationException()}
|
||||
// }
|
||||
//}
|
||||
|
||||
// fun Char.isDigit() = Character.isDigit(this)
|
||||
|
||||
fun Reader.forEachChar(body : (Char) -> Unit) {
|
||||
do {
|
||||
var i = read();
|
||||
if (i == -1) break
|
||||
body(i.toChar())
|
||||
} while(true)
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package mask
|
||||
|
||||
import java.io.*
|
||||
import java.util.*
|
||||
|
||||
fun box() : String {
|
||||
val input = StringReader("/Users/abreslav/work/jet/docs/luhnybin/src/test")
|
||||
|
||||
val luhny = Luhny()
|
||||
input.forEachChar {
|
||||
luhny.process(it)
|
||||
}
|
||||
luhny.printAll()
|
||||
return "OK"
|
||||
}
|
||||
|
||||
class Luhny() {
|
||||
private val buffer = ArrayDeque<Char>()
|
||||
private val digits = ArrayDeque<Int>(16)
|
||||
|
||||
private var toBeMasked = 0
|
||||
|
||||
fun process(it : Char) {
|
||||
buffer.addLast(it)
|
||||
|
||||
// Commented for KT-621
|
||||
// when (it) {
|
||||
// .isDigit() => digits.addLast(it.toInt() - '0'.toInt())
|
||||
// ' ', '-' => {}
|
||||
// else => printAll()
|
||||
// }
|
||||
|
||||
if (it.isDigit()) {
|
||||
digits.addLast(it.toInt() - '0'.toInt())
|
||||
} else if (it == ' ' || it == '-') {
|
||||
} else {
|
||||
printAll()
|
||||
}
|
||||
|
||||
if (digits.size() > 16)
|
||||
printOneDigit()
|
||||
check()
|
||||
}
|
||||
|
||||
private fun check() {
|
||||
val size = digits.size()
|
||||
if (size < 14) return
|
||||
val sum = digits.sum {i, d ->
|
||||
if (i % 2 == size % 2) double(d) else d
|
||||
}
|
||||
// var sum = 0
|
||||
// var i = 0
|
||||
// for (d in digits) {
|
||||
// sum += if (i % 2 == size % 2) double(d) else d
|
||||
// i++
|
||||
// }
|
||||
if (sum % 10 == 0) toBeMasked = digits.size()
|
||||
}
|
||||
|
||||
private fun double(d : Int) = d * 2 / 10 + d * 2 % 10
|
||||
|
||||
private fun printOneDigit() {
|
||||
while (!buffer.isEmpty()) {
|
||||
val c = buffer.removeFirst()!!
|
||||
print(c)
|
||||
if (c.isDigit()) {
|
||||
digits.removeFirst()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun printAll() {
|
||||
while (!buffer.isEmpty())
|
||||
print(buffer.removeFirst())
|
||||
digits.clear()
|
||||
}
|
||||
|
||||
private fun print(c : Char) {
|
||||
if (c.isDigit() && toBeMasked > 0) {
|
||||
kotlin.io.print('X')
|
||||
toBeMasked--
|
||||
} else {
|
||||
kotlin.io.print(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fun Char.isDigit() = Character.isDigit(this)
|
||||
|
||||
fun Iterable<Int>.sum(f : (index : Int, value : Int) -> Int) : Int {
|
||||
var sum = 0
|
||||
var i = 0
|
||||
for (d in this) {
|
||||
sum += f(i, d)
|
||||
i++
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
fun Reader.forEachChar(body : (Char) -> Unit) {
|
||||
do {
|
||||
var i = read();
|
||||
if (i == -1) break
|
||||
body(i.toChar())
|
||||
} while(true)
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package mask
|
||||
|
||||
import java.io.*
|
||||
import java.util.*
|
||||
|
||||
fun box() : String {
|
||||
val input = StringReader("/Users/abreslav/work/jet/docs/luhnybin/src/test")
|
||||
|
||||
val luhny = Luhny()
|
||||
input.forEachChar {
|
||||
luhny.charIn(it)
|
||||
}
|
||||
luhny.printAll()
|
||||
return "OK"
|
||||
|
||||
}
|
||||
|
||||
class Luhny() {
|
||||
val buffer = LinkedList<Char>()
|
||||
val digits = LinkedList<Int>()
|
||||
|
||||
var toBeMasked = 0
|
||||
|
||||
fun charIn(it : Char) {
|
||||
buffer.push(it)
|
||||
|
||||
// Commented for KT-621
|
||||
// when (it) {
|
||||
// .isDigit() => digits.push(it.toInt() - '0'.toInt())
|
||||
// ' ', '-' => {}
|
||||
// else => {
|
||||
// printAll()
|
||||
// digits.clear()
|
||||
// }
|
||||
// }
|
||||
|
||||
if (it.isDigit()) {
|
||||
digits.push(it.toInt() - '0'.toInt())
|
||||
} else if (it == ' ' || it == '-') {
|
||||
} else {
|
||||
printAll()
|
||||
digits.clear()
|
||||
}
|
||||
|
||||
if (digits.size() > 16)
|
||||
printOneDigit()
|
||||
check()
|
||||
}
|
||||
|
||||
fun check() {
|
||||
if (digits.size() < 14) return
|
||||
val sum = digits.sum { i, d ->
|
||||
if (i % 2 != 0)
|
||||
d * 2 / 10 + d * 2 % 10
|
||||
else d
|
||||
}
|
||||
if (sum % 10 == 0) toBeMasked = digits.size()
|
||||
}
|
||||
|
||||
fun printOneDigit() {
|
||||
while (!buffer.isEmpty()) {
|
||||
val c = buffer.pop()
|
||||
out(c)
|
||||
if (c.isDigit()) {
|
||||
digits.pop()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun printAll() {
|
||||
while (!buffer.isEmpty())
|
||||
out(buffer.pop())
|
||||
}
|
||||
|
||||
fun out(c : Char) {
|
||||
if (toBeMasked > 0) {
|
||||
print('X')
|
||||
toBeMasked--
|
||||
}
|
||||
else {
|
||||
print(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun LinkedList<Int>.sum(f : (Int, Int) -> Int) : Int {
|
||||
var sum = 0
|
||||
var i = 0
|
||||
for (d in backwards()) {
|
||||
sum += f(i, d)
|
||||
i++
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
fun <T> List<T>.backwards() : Iterable<T> = object : Iterable<T> {
|
||||
override fun iterator() : jet.Iterator<T> =
|
||||
object : jet.Iterator<T> {
|
||||
var current = size()
|
||||
override fun next() : T = get(--current)
|
||||
override fun hasNext() : Boolean = current > 0
|
||||
}
|
||||
}
|
||||
|
||||
// fun Char.isDigit() = Character.isDigit(this)
|
||||
|
||||
//class Queue<T>(initialBufSize : Int) {
|
||||
//
|
||||
// private var bufSize = initialBufSize
|
||||
// private val buf = Array<T>(initialBufSize)
|
||||
// private var head = 0
|
||||
// private var tail = 0
|
||||
// private var size = 0
|
||||
//
|
||||
// val empty : Boolean get() = size == 0
|
||||
//
|
||||
// private fun prev(i : Int) = (bufSize + i - 1) % bufSize
|
||||
// private fun next(i : Int) = (i + 1) % bufSize
|
||||
//
|
||||
// fun push(c : T) {
|
||||
// buf[tail] = c
|
||||
// tail = prev(tail)
|
||||
// size++
|
||||
// }
|
||||
//
|
||||
// fun pop() : T {
|
||||
// if (size == 0) throw IllegalStateException()
|
||||
// size--
|
||||
// val result = buf[head]
|
||||
// head = prev(head)
|
||||
// return result
|
||||
// }
|
||||
//
|
||||
// fun clear() {}
|
||||
//}
|
||||
|
||||
fun Reader.forEachChar(body : (Char) -> Unit) {
|
||||
do {
|
||||
var i = read();
|
||||
if (i == -1) break
|
||||
body(i.toChar())
|
||||
} while(true)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import kotlin.*
|
||||
|
||||
val test = "lala".javaClass
|
||||
|
||||
val test2 = javaClass<Iterator<Int>> ()
|
||||
|
||||
fun box(): String {
|
||||
if(test.getCanonicalName() != "java.lang.String") return "fail"
|
||||
if(test2.getCanonicalName() != "java.util.Iterator") return "fail"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
fun box() : String {
|
||||
val a = ArrayList<Int>();
|
||||
a.add(1)
|
||||
a.add(2)
|
||||
return if((a.size == 2) && (a.get(1) == 2) && (a.get(0) == 1)) "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import kotlin.util.*
|
||||
import java.io.*
|
||||
import java.util.*
|
||||
|
||||
fun sample() : Reader {
|
||||
return StringReader("""Hello
|
||||
World""");
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
// TODO compiler error
|
||||
// both these expressions causes java.lang.NoClassDefFoundError: collections/CollectionPackage
|
||||
val list1 = sample().useLines{it.toArrayList()}
|
||||
val list2 = sample().useLines<ArrayList<String>>{it.toArrayList()}
|
||||
|
||||
if(arrayList("Hello", "World") != list1) return "fail"
|
||||
if(arrayList("Hello", "World") != list2) return "fail"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// KT-2823 TypeCastException has no message
|
||||
|
||||
fun box(): String {
|
||||
try {
|
||||
val a: Any? = null
|
||||
a as Array<String>
|
||||
}
|
||||
catch(e: TypeCastException) {
|
||||
if (e.getMessage() == "jet.Any? cannot be cast to jet.Array<jet.String>") {
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
return "fail"
|
||||
}
|
||||
Reference in New Issue
Block a user