Regenerated samples
This commit is contained in:
parent
db7d99c254
commit
b237b9016f
|
@ -5,14 +5,14 @@ import kotlin.reflect.KClass
|
|||
|
||||
const val ERROR_TAG = "[Error]: "
|
||||
|
||||
private fun <L, R> runTest(
|
||||
private fun <L, R> test(
|
||||
actual: L,
|
||||
expected: R,
|
||||
checkEquals: Boolean = true,
|
||||
test: () -> Boolean
|
||||
predicate: () -> Boolean
|
||||
) {
|
||||
println(actual)
|
||||
if (!test()) {
|
||||
if (!predicate()) {
|
||||
print(ERROR_TAG)
|
||||
println("$actual " +
|
||||
(if (checkEquals) "!=" else "==") +
|
||||
|
@ -24,9 +24,9 @@ private fun <L, R> runTest(
|
|||
* Compares the string representation
|
||||
* of this object with the string `rval`.
|
||||
*/
|
||||
infix fun <T : Any> T.eq(rval: String) {
|
||||
runTest(this, rval) {
|
||||
this.toString().trim() == rval.trimIndent()
|
||||
infix fun Any.eq(rval: String) {
|
||||
test(this, rval) {
|
||||
toString().trim() == rval.trimIndent()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,7 +34,7 @@ infix fun <T : Any> T.eq(rval: String) {
|
|||
* Verifies this object is equal to `rval`.
|
||||
*/
|
||||
infix fun <T> T.eq(rval: T) {
|
||||
runTest(this, rval) {
|
||||
test(this, rval) {
|
||||
this == rval
|
||||
}
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ infix fun <T> T.eq(rval: T) {
|
|||
* Verifies this object is != `rval`.
|
||||
*/
|
||||
infix fun <T> T.neq(rval: T) {
|
||||
runTest(this, rval, checkEquals = false) {
|
||||
test(this, rval, checkEquals = false) {
|
||||
this != rval
|
||||
}
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ infix fun <T> T.neq(rval: T) {
|
|||
* to `rval` within a positive delta.
|
||||
*/
|
||||
infix fun Double.eq(rval: Double) {
|
||||
runTest(this, rval) {
|
||||
test(this, rval) {
|
||||
abs(this - rval) < 0.0000001
|
||||
}
|
||||
}
|
||||
|
@ -101,6 +101,13 @@ fun capture(f:() -> Unit): CapturedException =
|
|||
(e.message?.let { ": $it" } ?: ""))
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates output when called as in:
|
||||
* trace("info")
|
||||
* trace(object)
|
||||
* Later compares accumulated to expected:
|
||||
* trace eq "expected output"
|
||||
*/
|
||||
object trace {
|
||||
private val trc = mutableListOf<String>()
|
||||
operator fun invoke(obj: Any?) {
|
||||
|
@ -114,7 +121,7 @@ object trace {
|
|||
val trace = trc.joinToString("\n")
|
||||
val expected = multiline.trimIndent()
|
||||
.replace("\n", " ")
|
||||
runTest(trace, multiline) {
|
||||
test(trace, multiline) {
|
||||
trace.replace("\n", " ") == expected
|
||||
}
|
||||
trc.clear()
|
||||
|
|
|
@ -13,7 +13,7 @@ fun fibonacci(n: Int): BigInteger {
|
|||
): BigInteger {
|
||||
if (n == 0) return current
|
||||
return fibonacci(
|
||||
n - 1, next, current + next) // [1]
|
||||
n - 1, next, current + next) // [1]
|
||||
}
|
||||
return fibonacci(n, ZERO, ONE)
|
||||
}
|
||||
|
|
|
@ -3,11 +3,11 @@ package biginteger
|
|||
import java.math.BigInteger
|
||||
|
||||
fun Int.toBigInteger(): BigInteger =
|
||||
BigInteger.valueOf(this.toLong())
|
||||
BigInteger.valueOf(toLong())
|
||||
|
||||
fun String.toBigInteger(): BigInteger =
|
||||
BigInteger(this)
|
||||
|
||||
operator fun BigInteger.plus(
|
||||
other: BigInteger
|
||||
): BigInteger = this.add(other)
|
||||
): BigInteger = add(other)
|
|
@ -1,15 +1,6 @@
|
|||
Trace: debugLevel(Trace)
|
||||
Debug: debugLevel(Trace)
|
||||
Info: debugLevel(Trace)
|
||||
Warn: debugLevel(Trace)
|
||||
Error: debugLevel(Trace)
|
||||
Debug: debugLevel(Debug)
|
||||
Info: debugLevel(Debug)
|
||||
Warn: debugLevel(Debug)
|
||||
Error: debugLevel(Debug)
|
||||
Info: debugLevel(Info)
|
||||
Warn: debugLevel(Info)
|
||||
Error: debugLevel(Info)
|
||||
Warn: debugLevel(Warn)
|
||||
Error: debugLevel(Warn)
|
||||
Error: debugLevel(Error)
|
||||
Error: DBOpenFail
|
||||
Error: NetworkOpenFail
|
||||
Error: DBWriteFail
|
||||
Error: NetworkCloseFail
|
||||
Error: DBCloseFail
|
||||
Error: main(): DBCloseFail
|
||||
|
|
|
@ -6,7 +6,7 @@ fun <T, R : Any> Iterable<T>.mapIndexedNotNull(
|
|||
transform: (Int, T) -> R?
|
||||
): List<R> {
|
||||
val result = mutableListOf<R>()
|
||||
for ((index, e) in this.withIndex()) {
|
||||
for ((index, e) in withIndex()) {
|
||||
val transformed = transform(index, e)
|
||||
if (transformed != null) {
|
||||
result += transformed
|
||||
|
|
|
@ -4,7 +4,7 @@ files:
|
|||
visible: true
|
||||
placeholders:
|
||||
- offset: 218
|
||||
length: 149
|
||||
length: 144
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -13,7 +13,7 @@ fun Int.square(): Int {
|
|||
}
|
||||
|
||||
fun Int.lessThanTen(): Boolean {
|
||||
trace("${this}.lessThanTen()")
|
||||
trace("$this.lessThanTen()")
|
||||
return this < 10
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// Visibility/Task4.kt
|
||||
package constrainingVisibilityExercise4
|
||||
|
||||
fun printSum(x: Int) {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// Visibility/Task5.kt
|
||||
package constrainingVisibilityExercise5
|
||||
|
||||
fun printSum(x: Int) {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// Constructors/Task4.kt
|
||||
package constructorsExercise4
|
||||
|
||||
class Human(
|
||||
|
|
|
@ -3,7 +3,7 @@ files:
|
|||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 85
|
||||
- offset: 110
|
||||
length: 80
|
||||
placeholder_text: /*TODO*/
|
||||
- name: test/Tests.kt
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// CreatingClasses/Task4.kt
|
||||
package creatingClassesExercise4
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// CreatingClasses/Task5.kt
|
||||
package creatingClassesExercise5
|
||||
|
||||
fun cap(s: String) = s.capitalize()
|
||||
|
|
|
@ -3,7 +3,7 @@ files:
|
|||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 56
|
||||
- offset: 84
|
||||
length: 13
|
||||
placeholder_text: ' '
|
||||
- name: test/Tests.kt
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// Exceptions/Task4.kt
|
||||
package exceptionsExercise4
|
||||
|
||||
fun foo() {
|
||||
|
@ -9,5 +10,5 @@ fun bar() {
|
|||
}
|
||||
|
||||
fun main() {
|
||||
bar()
|
||||
// bar() // Uncomment this to see exception
|
||||
}
|
|
@ -3,15 +3,15 @@ import atomictest.eq
|
|||
|
||||
fun main() {
|
||||
val ints = listOf(99, 3, 5, 7, 11, 13)
|
||||
ints eq "[99, 3, 5, 7, 11, 13]" // [1]
|
||||
ints eq "[99, 3, 5, 7, 11, 13]" // [1]
|
||||
|
||||
// Select each element in the List:
|
||||
var result = ""
|
||||
for (i in ints) { // [2]
|
||||
for (i in ints) { // [2]
|
||||
result += "$i "
|
||||
}
|
||||
result eq "99 3 5 7 11 13"
|
||||
|
||||
// "Indexing" into the List:
|
||||
ints[4] eq 11 // [3]
|
||||
ints[4] eq 11 // [3]
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
// Lists/Task4.kt
|
||||
package listsExercise4
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// Maps/Task4.kt
|
||||
package mapsExercise4
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// ObjectsEverywhere/Task5.kt
|
||||
package objectsEverywhereExercise5
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// Properties/Task4.kt
|
||||
package propertiesExercise4
|
||||
|
||||
class Counter {
|
||||
|
|
|
@ -6,11 +6,11 @@ class Default {
|
|||
var i: Int = 0
|
||||
get() {
|
||||
trace("get()")
|
||||
return field // [1]
|
||||
return field // [1]
|
||||
}
|
||||
set(value) {
|
||||
trace("set($value)")
|
||||
field = value // [2]
|
||||
field = value // [2]
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// PropertyAccessors/Task5.kt
|
||||
package propertyAccessorsExercise5
|
||||
import atomictest.eq
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// Sets/Task4.kt
|
||||
package setsExercise4
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -3,18 +3,18 @@ package summary2
|
|||
import atomictest.eq
|
||||
|
||||
class JetPack(
|
||||
private var fuel: Double // [1]
|
||||
private var fuel: Double // [1]
|
||||
) {
|
||||
private var warning = false
|
||||
private fun burn() = // [2]
|
||||
private fun burn() = // [2]
|
||||
if (fuel - 1 <= 0) {
|
||||
fuel = 0.0
|
||||
warning = true
|
||||
} else
|
||||
fuel -= 1
|
||||
public fun fly() = burn() // [3]
|
||||
fun check() = // [4]
|
||||
if (warning) // [5]
|
||||
public fun fly() = burn() // [3]
|
||||
fun check() = // [4]
|
||||
if (warning) // [5]
|
||||
"Warning"
|
||||
else
|
||||
"OK"
|
||||
|
|
|
@ -26,7 +26,7 @@ class Temperature {
|
|||
}
|
||||
|
||||
fun main() {
|
||||
val temp = Temperature() // [1]
|
||||
val temp = Temperature() // [1]
|
||||
temp.setFahrenheit(98.6)
|
||||
temp.getFahrenheit() eq 98.6
|
||||
temp.getCelsius() eq 37.0
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// Testing/Task4.kt
|
||||
package testingExercise4
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// Varargs/Task5.kt
|
||||
package variableArgumentListsExercise5
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
|
|
@ -22,7 +22,7 @@ class VacationHouse(
|
|||
zip: String,
|
||||
val startMonth: String,
|
||||
val endMonth: String
|
||||
): House(address, state, zip) {
|
||||
) : House(address, state, zip) {
|
||||
override fun toString() =
|
||||
"Vacation house at $fullAddress " +
|
||||
"from $startMonth to $endMonth"
|
||||
|
@ -30,7 +30,7 @@ class VacationHouse(
|
|||
|
||||
class TreeHouse(
|
||||
val name: String
|
||||
): House("Tree Street, TR 00000") {
|
||||
) : House("Tree Street, TR 00000") {
|
||||
override fun toString() =
|
||||
"$name tree house at $fullAddress"
|
||||
}
|
||||
|
|
|
@ -2,5 +2,5 @@
|
|||
package baseClassInitializationExercise1
|
||||
|
||||
open class A(open val s: String)
|
||||
open class B(override val s: String): A(s)
|
||||
class C(override val s: String): B(s)
|
||||
open class B(override val s: String) : A(s)
|
||||
class C(override val s: String) : B(s)
|
|
@ -4,7 +4,7 @@ files:
|
|||
visible: true
|
||||
placeholders:
|
||||
- offset: 68
|
||||
length: 113
|
||||
length: 115
|
||||
placeholder_text: // TODO
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -8,6 +8,6 @@ val group: List<Creature> = listOf(
|
|||
|
||||
fun main() {
|
||||
val dog = group
|
||||
.find { it is Dog } as Dog? // [1]
|
||||
dog?.bark() eq "Yip!" // [2]
|
||||
.find { it is Dog } as Dog? // [1]
|
||||
dog?.bark() eq "Yip!" // [2]
|
||||
}
|
|
@ -13,8 +13,8 @@ class SmartCast1(val c: Creature) {
|
|||
|
||||
class SmartCast2(var c: Creature) {
|
||||
fun contact() {
|
||||
when (val c = c) { // [1]
|
||||
is Human -> c.greeting() // [2]
|
||||
when (val c = c) { // [1]
|
||||
is Human -> c.greeting() // [2]
|
||||
is Dog -> c.bark()
|
||||
is Alien -> c.mobility()
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ fun interface Counter {
|
|||
fun next(): Int
|
||||
}
|
||||
|
||||
class CounterFactory {
|
||||
object CounterFactory {
|
||||
private var count = 0
|
||||
fun new(name: String): Counter {
|
||||
// Local inner class:
|
||||
|
@ -43,10 +43,9 @@ fun main() {
|
|||
fun test(counter: Counter) {
|
||||
(0..3).forEach { counter.next() }
|
||||
}
|
||||
val cf = CounterFactory()
|
||||
test(cf.new("Local"))
|
||||
test(cf.new2("Anon"))
|
||||
test(cf.new3("SAM"))
|
||||
test(CounterFactory.new("Local"))
|
||||
test(CounterFactory.new2("Anon"))
|
||||
test(CounterFactory.new3("SAM"))
|
||||
trace eq """
|
||||
Local() Local 0 Local 1 Local 2 Local 3
|
||||
Counter() Anon 4 Anon 5 Anon 6 Anon 7
|
||||
|
|
|
@ -6,7 +6,7 @@ fun interface Pet {
|
|||
fun speak(): String
|
||||
}
|
||||
|
||||
class PetCreator {
|
||||
object CreatePet {
|
||||
fun home() = " home!"
|
||||
fun dog(): Pet {
|
||||
val say = "Bark"
|
||||
|
@ -31,8 +31,7 @@ class PetCreator {
|
|||
}
|
||||
|
||||
fun main() {
|
||||
val create = PetCreator()
|
||||
create.dog().speak() eq "Bark home!"
|
||||
create.cat().speak() eq "Meow home!"
|
||||
create.hamster().speak() eq "Squeak home!"
|
||||
CreatePet.dog().speak() eq "Bark home!"
|
||||
CreatePet.cat().speak() eq "Meow home!"
|
||||
CreatePet.hamster().speak() eq "Squeak home!"
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
// Interfaces/PlayerInterface.kt
|
||||
package propertiesininterfaces
|
||||
package interfaces
|
||||
import atomictest.eq
|
||||
|
||||
interface Player {
|
||||
|
|
|
@ -17,6 +17,6 @@ class Cat : Pet() {
|
|||
fun talk(pet: Pet) = pet.speak()
|
||||
|
||||
fun main() {
|
||||
talk(Dog()) eq "Bark!" // [1]
|
||||
talk(Cat()) eq "Meow" // [2]
|
||||
talk(Dog()) eq "Bark!" // [1]
|
||||
talk(Cat()) eq "Meow" // [2]
|
||||
}
|
|
@ -10,12 +10,12 @@ class WithSecondary(i: Int) {
|
|||
trace("Secondary: '$c'")
|
||||
}
|
||||
constructor(s: String) :
|
||||
this(s.first()) { // [1]
|
||||
this(s.first()) { // [1]
|
||||
trace("Secondary: \"$s\"")
|
||||
}
|
||||
/* Doesn't compile without a call
|
||||
to the primary constructor:
|
||||
constructor(f: Float) { // [2]
|
||||
constructor(f: Float) { // [2]
|
||||
trace("Secondary: $f")
|
||||
}
|
||||
*/
|
||||
|
|
|
@ -5,7 +5,7 @@ import typechecking.name
|
|||
|
||||
interface BeverageContainer {
|
||||
fun open(): String
|
||||
fun pour() = "${this.name}: Pour"
|
||||
fun pour() = "$name: Pour"
|
||||
fun recycle(): String
|
||||
}
|
||||
|
||||
|
|
|
@ -3,8 +3,8 @@ package typechecking
|
|||
import atomictest.eq
|
||||
|
||||
interface Insect {
|
||||
fun walk() = "${this.name}: walk"
|
||||
fun fly() = "${this.name}: fly"
|
||||
fun walk() = "$name: walk"
|
||||
fun fly() = "$name: fly"
|
||||
}
|
||||
|
||||
class HouseFly : Insect
|
||||
|
@ -16,19 +16,19 @@ class Flea : Insect {
|
|||
}
|
||||
|
||||
fun Insect.basic() =
|
||||
this.walk() + " " +
|
||||
walk() + " " +
|
||||
if (this is Flea)
|
||||
this.crawl()
|
||||
crawl()
|
||||
else
|
||||
this.fly()
|
||||
fly()
|
||||
|
||||
interface SwimmingInsect: Insect {
|
||||
fun swim() = "${this.name}: swim"
|
||||
fun swim() = "$name: swim"
|
||||
}
|
||||
|
||||
interface WaterWalker: Insect {
|
||||
fun walkWater() =
|
||||
"${this.name}: walk on water"
|
||||
"$name: walk on water"
|
||||
}
|
||||
|
||||
class WaterBeetle : SwimmingInsect
|
||||
|
@ -38,9 +38,9 @@ class WhirligigBeetle :
|
|||
|
||||
fun Insect.water() =
|
||||
when(this) {
|
||||
is SwimmingInsect -> this.swim()
|
||||
is WaterWalker -> this.walkWater()
|
||||
else -> "${this.name}: drown"
|
||||
is SwimmingInsect -> swim()
|
||||
is WaterWalker -> walkWater()
|
||||
else -> "$name: drown"
|
||||
}
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -4,7 +4,7 @@ import atomictest.eq
|
|||
import typechecking.name
|
||||
|
||||
sealed class Shape {
|
||||
fun draw() = "${this.name}: Draw"
|
||||
fun draw() = "$name: Draw"
|
||||
}
|
||||
|
||||
class Circle : Shape()
|
||||
|
|
|
@ -4,7 +4,7 @@ import atomictest.eq
|
|||
import typechecking.name
|
||||
|
||||
interface Shape {
|
||||
fun draw() = "${this.name}: Draw"
|
||||
fun draw() = "$name: Draw"
|
||||
fun rotate() = ""
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ files:
|
|||
visible: true
|
||||
placeholders:
|
||||
- offset: 114
|
||||
length: 449
|
||||
length: 442
|
||||
placeholder_text: // TODO
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -4,8 +4,8 @@ import atomictest.eq
|
|||
import typechecking.name
|
||||
|
||||
sealed class Insect {
|
||||
open fun walk() = "${this.name}: walk"
|
||||
open fun fly() = "${this.name}: fly"
|
||||
open fun walk() = "$name: walk"
|
||||
open fun fly() = "$name: fly"
|
||||
}
|
||||
|
||||
class HouseFly : Insect()
|
||||
|
@ -17,19 +17,19 @@ class Flea : Insect() {
|
|||
}
|
||||
|
||||
fun Insect.basic() =
|
||||
this.walk() + " " +
|
||||
walk() + " " +
|
||||
when(this) {
|
||||
is Flea -> this.crawl()
|
||||
else -> this.fly()
|
||||
is Flea -> crawl()
|
||||
else -> fly()
|
||||
}
|
||||
|
||||
interface SwimmingInsect {
|
||||
fun swim() = "${this.name}: swim"
|
||||
fun swim() = "$name: swim"
|
||||
}
|
||||
|
||||
interface WaterWalker {
|
||||
fun walkWater() =
|
||||
"${this.name}: walk on water"
|
||||
"$name: walk on water"
|
||||
}
|
||||
|
||||
class WaterBeetle : Insect(), SwimmingInsect
|
||||
|
@ -39,9 +39,9 @@ class WhirligigBeetle : Insect(),
|
|||
|
||||
fun Insect.water() =
|
||||
when(this) {
|
||||
is SwimmingInsect -> this.swim()
|
||||
is WaterWalker -> this.walkWater()
|
||||
else -> "${this.name}: drown"
|
||||
is SwimmingInsect -> swim()
|
||||
is WaterWalker -> walkWater()
|
||||
else -> "$name: drown"
|
||||
}
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -4,7 +4,7 @@ files:
|
|||
visible: true
|
||||
placeholders:
|
||||
- offset: 114
|
||||
length: 1366
|
||||
length: 1306
|
||||
placeholder_text: // TODO
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -3,7 +3,7 @@ package creatinggenerics
|
|||
import atomictest.eq
|
||||
|
||||
inline fun <reified T> check(t: Any) = t is T
|
||||
// fun <T> check1(t: Any) = t is T // [1]
|
||||
// fun <T> check1(t: Any) = t is T // [1]
|
||||
|
||||
fun main() {
|
||||
check<String>("1") eq true
|
||||
|
|
|
@ -9,5 +9,5 @@ fun main() {
|
|||
mutablePetList.add(Dog())
|
||||
// Type mismatch:
|
||||
// mutablePetList =
|
||||
// mutableListOf<Cat>(Cat()) // [1]
|
||||
// mutableListOf<Cat>(Cat()) // [1]
|
||||
}
|
|
@ -32,17 +32,17 @@ class ConcreteImplementation
|
|||
fun basicGenerics() {
|
||||
gFunction("Yellow")
|
||||
gFunction(1)
|
||||
gFunction(Dog()).bark() // [1]
|
||||
gFunction(Dog()).bark() // [1]
|
||||
gFunction<Dog>(Dog()).bark()
|
||||
|
||||
GClass("Cyan").f()
|
||||
GClass(11).f()
|
||||
GClass(Dog()).f().bark() // [2]
|
||||
GClass(Dog()).f().bark() // [2]
|
||||
GClass<Dog>(Dog()).f().bark()
|
||||
|
||||
GMemberFunction().f("Amber")
|
||||
GMemberFunction().f(111)
|
||||
GMemberFunction().f(Dog()).bark() // [3]
|
||||
GMemberFunction().f(Dog()).bark() // [3]
|
||||
GMemberFunction().f<Dog>(Dog()).bark()
|
||||
|
||||
GImplementation("Cyan").f()
|
||||
|
|
|
@ -9,5 +9,5 @@ fun main() {
|
|||
}
|
||||
|
||||
fun useList(list: List<Any>) {
|
||||
// if (list is List<String>) {} // [1]
|
||||
// if (list is List<String>) {} // [1]
|
||||
}
|
|
@ -17,6 +17,6 @@ files:
|
|||
placeholder_text: TODO()
|
||||
- offset: 702
|
||||
length: 47
|
||||
placeholder_text: TODO()
|
||||
placeholder_text: '"TODO"'
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -7,6 +7,6 @@ fun main() {
|
|||
val y: Int = 2
|
||||
val sum = x ?: 0 + y
|
||||
sum eq 1
|
||||
(x ?: 0) + y eq 3 // [1]
|
||||
x ?: (0 + y) eq 1 // [2]
|
||||
(x ?: 0) + y eq 3 // [1]
|
||||
x ?: (0 + y) eq 1 // [2]
|
||||
}
|
|
@ -17,7 +17,7 @@ data class Molecule(
|
|||
fun main() {
|
||||
val m1 = Molecule()
|
||||
val m2 = Molecule()
|
||||
m1 + m2 // [1]
|
||||
m1 + m2 // [1]
|
||||
m1 eq "Molecule(id=0, attached=" +
|
||||
"Molecule(id=1, attached=null))"
|
||||
}
|
|
@ -11,14 +11,14 @@ enum class Count {
|
|||
|
||||
operator fun Count.inc(): Count =
|
||||
Count.values()[
|
||||
(this.ordinal + 1) % Count.values().size
|
||||
(ordinal + 1) % Count.values().size
|
||||
]
|
||||
|
||||
operator fun Count.dec(): Count =
|
||||
if (this.ordinal - 1 < 0)
|
||||
if (ordinal - 1 < 0)
|
||||
Count.max
|
||||
else
|
||||
Count.values()[this.ordinal - 1]
|
||||
Count.values()[ordinal - 1]
|
||||
|
||||
fun main() {
|
||||
var c = Count.Eeny
|
||||
|
|
|
@ -4,10 +4,10 @@ files:
|
|||
visible: true
|
||||
placeholders:
|
||||
- offset: 235
|
||||
length: 64
|
||||
length: 59
|
||||
placeholder_text: TODO()
|
||||
- offset: 337
|
||||
length: 83
|
||||
- offset: 332
|
||||
length: 73
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -21,7 +21,7 @@ class DataFile(val fileName: String) :
|
|||
}
|
||||
|
||||
fun main() {
|
||||
DataFile("Test.txt") eq
|
||||
DataFile("Test.txt").reset() eq
|
||||
Paths.get("DataFiles", "Test.txt")
|
||||
.toString()
|
||||
}
|
|
@ -3,10 +3,10 @@ package checkinstructions
|
|||
import atomictest.*
|
||||
|
||||
fun notNull(n: Int?): Int {
|
||||
requireNotNull(n) { // [1]
|
||||
requireNotNull(n) { // [1]
|
||||
"notNull() argument cannot be null"
|
||||
}
|
||||
return n * 9 // [2]
|
||||
return n * 9 // [2]
|
||||
}
|
||||
|
||||
fun main() {
|
||||
|
@ -16,7 +16,7 @@ fun main() {
|
|||
} eq "IllegalArgumentException: " +
|
||||
"notNull() argument cannot be null"
|
||||
capture {
|
||||
requireNotNull(n) // [3]
|
||||
requireNotNull(n) // [3]
|
||||
} eq "IllegalArgumentException: " +
|
||||
"Required value was null."
|
||||
notNull(11) eq 99
|
||||
|
|
|
@ -11,7 +11,7 @@ files:
|
|||
placeholder_text: '"TODO"'
|
||||
- offset: 228
|
||||
length: 17
|
||||
placeholder_text: '"TODO"'
|
||||
placeholder_text: TODO()
|
||||
- offset: 249
|
||||
length: 43
|
||||
placeholder_text: class Failure
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// Booleans/Task4.kt
|
||||
package booleansExercise4
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// DataTypes/Task4.kt
|
||||
package dataTypesExercise4
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// DataTypes/Task5.kt
|
||||
package dataTypesExercise5
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// ExpressionsStatements/Task4.kt
|
||||
package expressionsAndStatementsExercise4
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// Functions/Task4.kt
|
||||
package functionsExercise4
|
||||
|
||||
fun foo(): String {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// HelloWorld/Task4.kt
|
||||
package helloWorldExercise4
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// HelloWorld/Task5.kt
|
||||
package helloWorldExercise5
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
// LoopingAndRanges/DefiningRanges.kt
|
||||
|
||||
fun main() {
|
||||
val range1 = 1..10 // [1]
|
||||
val range2 = 0 until 10 // [2]
|
||||
val range1 = 1..10 // [1]
|
||||
val range2 = 0 until 10 // [2]
|
||||
println(range1)
|
||||
println(range2)
|
||||
}
|
||||
|
|
|
@ -11,9 +11,9 @@ fun showRange(r: IntProgression) {
|
|||
fun main() {
|
||||
showRange(1..5)
|
||||
showRange(0 until 5)
|
||||
showRange(5 downTo 1) // [1]
|
||||
showRange(0..9 step 2) // [2]
|
||||
showRange(0 until 10 step 3) // [3]
|
||||
showRange(5 downTo 1) // [1]
|
||||
showRange(0..9 step 2) // [2]
|
||||
showRange(0 until 10 step 3) // [3]
|
||||
showRange(9 downTo 2 step 3)
|
||||
}
|
||||
/* Output:
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// LoopingAndRanges/Task5.kt
|
||||
package loopingAndRangesExercise5
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// NumberTypes/Task5.kt
|
||||
package numberTypesExercise5
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// RepetitionWithWhile/Task4.kt
|
||||
package repetitionWithWhileExercise4
|
||||
|
||||
fun getFooResult(): Int = 736
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// RepetitionWithWhile/Task5.kt
|
||||
package repetitionWithWhileExercise5
|
||||
|
||||
fun getFooResult(): Int = 736
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// StringTemplates/Task4.kt
|
||||
package stringTemplatesExercise4
|
||||
|
||||
// foo
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// StringTemplates/Task5.kt
|
||||
package stringTemplatesExercise5
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -2,11 +2,11 @@
|
|||
|
||||
fun main() {
|
||||
val answer = 42
|
||||
println("Found $answer!") // [1]
|
||||
println("Found $answer!") // [1]
|
||||
val condition = true
|
||||
println(
|
||||
"${if (condition) 'a' else 'b'}") // [2]
|
||||
println("printing a $1") // [3]
|
||||
"${if (condition) 'a' else 'b'}") // [2]
|
||||
println("printing a $1") // [3]
|
||||
}
|
||||
/* Output:
|
||||
Found 42!
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
fun isDigit(ch: Char) = ch in '0'..'9'
|
||||
|
||||
fun notDigit(ch: Char) =
|
||||
ch !in '0'..'9' // [1]
|
||||
ch !in '0'..'9' // [1]
|
||||
|
||||
fun main() {
|
||||
println(isDigit('a'))
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// InKeyword/Task5.kt
|
||||
package theInKeywordExercise5
|
||||
|
||||
fun foo() {
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
fun trueOrFalse(exp: Boolean): String {
|
||||
if (exp)
|
||||
return "It's true!" // [1]
|
||||
return "It's false"
|
||||
return "It's false" // [2]
|
||||
}
|
||||
|
||||
fun main() {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// IfExpressions/Task4.kt
|
||||
package ifExpressionsExercise4
|
||||
|
||||
fun oneOrTheOther(exp: Boolean) =
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
fun main() {
|
||||
val whole = 11
|
||||
// whole = 15 // Error // [1]
|
||||
// whole = 15 // Error // [1]
|
||||
val fractional = 1.4
|
||||
val words = "Twas Brillig"
|
||||
println(whole)
|
||||
|
|
|
@ -13,7 +13,7 @@ fun AirlineTicket.transferTicket(
|
|||
otherFirstName: String,
|
||||
otherLastName: String
|
||||
): AirlineTicket {
|
||||
return this.copy(firstName = otherFirstName,
|
||||
return copy(firstName = otherFirstName,
|
||||
lastName = otherLastName)
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ files:
|
|||
visible: true
|
||||
placeholders:
|
||||
- offset: 304
|
||||
length: 74
|
||||
length: 69
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// Enumerations/EnumImport.kt
|
||||
import atomictest.eq
|
||||
import enumerations.Level.* // [1]
|
||||
import enumerations.Level.* // [1]
|
||||
|
||||
fun main() {
|
||||
Overflow eq "Overflow"
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
// Enumerations/RecursiveEnumImport.kt
|
||||
package enumerations
|
||||
import atomictest.eq
|
||||
import enumerations.Size.* // [1]
|
||||
import enumerations.Size.* // [1]
|
||||
|
||||
enum class Size {
|
||||
Tiny, Small, Medium, Large, Huge, Gigantic
|
||||
}
|
||||
|
||||
fun main() {
|
||||
Gigantic eq "Gigantic" // [2]
|
||||
Size.values().toList() eq // [3]
|
||||
Gigantic eq "Gigantic" // [2]
|
||||
Size.values().toList() eq // [3]
|
||||
listOf(Tiny, Small, Medium,
|
||||
Large, Huge, Gigantic)
|
||||
Tiny.ordinal eq 0 // [4]
|
||||
Tiny.ordinal eq 0 // [4]
|
||||
Huge.ordinal eq 4
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
// ExtensionFunctions/Task4.kt
|
||||
package extensionFunctionsExercise4
|
||||
import atomictest.eq
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
package introgenerics
|
||||
import atomictest.eq
|
||||
|
||||
class GenericHolder<T>( // [1]
|
||||
class GenericHolder<T>( // [1]
|
||||
private val value: T
|
||||
) {
|
||||
fun getValue(): T = value
|
||||
|
@ -10,14 +10,14 @@ class GenericHolder<T>( // [1]
|
|||
|
||||
fun main() {
|
||||
val h1 = GenericHolder(Automobile("Ford"))
|
||||
val a: Automobile = h1.getValue() // [2]
|
||||
val a: Automobile = h1.getValue() // [2]
|
||||
a eq "Automobile(brand=Ford)"
|
||||
|
||||
val h2 = GenericHolder(1)
|
||||
val i: Int = h2.getValue() // [3]
|
||||
val i: Int = h2.getValue() // [3]
|
||||
i eq 1
|
||||
|
||||
val h3 = GenericHolder("Chartreuse")
|
||||
val s: String = h3.getValue() // [4]
|
||||
val s: String = h3.getValue() // [4]
|
||||
s eq "Chartreuse"
|
||||
}
|
|
@ -6,12 +6,12 @@ fun color(red: Int, green: Int, blue: Int) =
|
|||
"($red, $green, $blue)"
|
||||
|
||||
fun main() {
|
||||
color(1, 2, 3) eq "(1, 2, 3)" // [1]
|
||||
color(1, 2, 3) eq "(1, 2, 3)" // [1]
|
||||
color(
|
||||
red = 76, // [2]
|
||||
red = 76, // [2]
|
||||
green = 89,
|
||||
blue = 0
|
||||
) eq "(76, 89, 0)"
|
||||
color(52, 34, blue = 0) eq // [3]
|
||||
color(52, 34, blue = 0) eq // [3]
|
||||
"(52, 34, 0)"
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
// NamedAndDefaultArgs/Task4.kt
|
||||
package namedAndDefaultArgumentsExercise4
|
||||
import atomictest.eq
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ fun main() {
|
|||
val s1: String = "abc"
|
||||
val s2: String? = s1
|
||||
|
||||
s1.length eq 3 // [1]
|
||||
s1.length eq 3 // [1]
|
||||
// Doesn't compile:
|
||||
// s2.length // [2]
|
||||
// s2.length // [2]
|
||||
}
|
|
@ -2,18 +2,18 @@
|
|||
import atomictest.eq
|
||||
|
||||
fun main() {
|
||||
val s1 = "abc" // [1]
|
||||
val s1 = "abc" // [1]
|
||||
|
||||
// Compile-time error:
|
||||
// val s2: String = null // [2]
|
||||
// val s2: String = null // [2]
|
||||
|
||||
// Nullable definitions:
|
||||
val s3: String? = null // [3]
|
||||
val s4: String? = s1 // [4]
|
||||
val s3: String? = null // [3]
|
||||
val s4: String? = s1 // [4]
|
||||
|
||||
// Compile-time error:
|
||||
// val s5: String = s4 // [5]
|
||||
val s6 = s4 // [6]
|
||||
// val s5: String = s4 // [5]
|
||||
val s6 = s4 // [6]
|
||||
|
||||
s1 eq "abc"
|
||||
s3 eq null
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// Overloading/Task4.kt
|
||||
package overloadingExercise4
|
||||
import atomictest.eq
|
||||
|
||||
|
|
|
@ -9,13 +9,13 @@ class Person(
|
|||
|
||||
fun main() {
|
||||
val alice = Person("Alice")
|
||||
alice.friend?.friend?.name eq null // [1]
|
||||
alice.friend?.friend?.name eq null // [1]
|
||||
|
||||
val bob = Person("Bob")
|
||||
val charlie = Person("Charlie", bob)
|
||||
bob.friend = charlie
|
||||
bob.friend?.friend?.name eq "Bob" // [2]
|
||||
bob.friend?.friend?.name eq "Bob" // [2]
|
||||
|
||||
(alice.friend?.friend?.name
|
||||
?: "Unknown") eq "Unknown" // [3]
|
||||
?: "Unknown") eq "Unknown" // [3]
|
||||
}
|
|
@ -4,8 +4,8 @@ import atomictest.eq
|
|||
|
||||
fun checkLength(s: String?, expected: Int) {
|
||||
val length1 =
|
||||
if (s != null) s.length else 0 // [1]
|
||||
val length2 = s?.length ?: 0 // [2]
|
||||
if (s != null) s.length else 0 // [1]
|
||||
val length2 = s?.length ?: 0 // [2]
|
||||
length1 eq expected
|
||||
length2 eq expected
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@ import atomictest.eq
|
|||
|
||||
fun checkLength(s: String?, expected: Int?) {
|
||||
val length1 =
|
||||
if (s != null) s.length else null // [1]
|
||||
val length2 = s?.length // [2]
|
||||
if (s != null) s.length else null // [1]
|
||||
val length2 = s?.length // [2]
|
||||
length1 eq expected
|
||||
length2 eq expected
|
||||
}
|
||||
|
|
|
@ -3,16 +3,16 @@ package safecalls
|
|||
import atomictest.*
|
||||
|
||||
fun String.echo() {
|
||||
trace(this.toUpperCase())
|
||||
trace(toUpperCase())
|
||||
trace(this)
|
||||
trace(this.toLowerCase())
|
||||
trace(toLowerCase())
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val s1: String? = "Howdy!"
|
||||
s1?.echo() // [1]
|
||||
s1?.echo() // [1]
|
||||
val s2: String? = null
|
||||
s2?.echo() // [2]
|
||||
s2?.echo() // [2]
|
||||
trace eq """
|
||||
HOWDY!
|
||||
Howdy!
|
||||
|
|
|
@ -19,16 +19,16 @@ class Coordinates {
|
|||
fun processInputs(inputs: List<String>) {
|
||||
val coordinates = Coordinates()
|
||||
for (input in inputs) {
|
||||
when (input) { // [1]
|
||||
"up", "u" -> coordinates.y-- // [2]
|
||||
when (input) { // [1]
|
||||
"up", "u" -> coordinates.y-- // [2]
|
||||
"down", "d" -> coordinates.y++
|
||||
"left", "l" -> coordinates.x--
|
||||
"right", "r" -> { // [3]
|
||||
"right", "r" -> { // [3]
|
||||
trace("Moving right")
|
||||
coordinates.x++
|
||||
}
|
||||
"nowhere" -> {} // [4]
|
||||
"exit" -> return // [5]
|
||||
"nowhere" -> {} // [4]
|
||||
"exit" -> return // [5]
|
||||
else -> trace("bad input: $input")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// WhenExpressions/Task4.kt
|
||||
package whenExpressionsExercise4
|
||||
import atomictest.eq
|
||||
|
||||
|
|
|
@ -1673,7 +1673,7 @@ public class TestAllExamples extends AbstractTestExamples {
|
|||
|
||||
@Test
|
||||
public void testPlayerInterface() {
|
||||
testExample("Object-Oriented Programming/Interfaces/Examples/src/PlayerInterface.kt", propertiesininterfaces.PlayerInterfaceKt::main);
|
||||
testExample("Object-Oriented Programming/Interfaces/Examples/src/PlayerInterface.kt", interfaces.PlayerInterfaceKt::main);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue