Skip to content

Commit

Permalink
Multiplatform "assert"
Browse files Browse the repository at this point in the history
It's missing on K/JS, which we currently don't use, but just to ensure we *can*...
  • Loading branch information
ephemient committed Dec 23, 2022
1 parent 1c2ce4d commit 9fc638f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Day21(lines: List<String>) {
private data class Rational(val numerator: Long, val denominator: Long = 1) {
init {
require(denominator > 0)
assert(gcd(numerator, denominator) == 1L)
assert { gcd(numerator, denominator) == 1L }
}

operator fun plus(other: Rational): Rational {
Expand Down
4 changes: 4 additions & 0 deletions kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ package com.github.ephemient.aoc2022

expect fun getInput(day: Int): List<String>

expect fun assert(condition: () -> Boolean)

expect fun assert(condition: () -> Boolean, lazyMessage: () -> Any)

expect fun trace(message: String)
8 changes: 8 additions & 0 deletions kt/src/jvmMain/kotlin/com/github/ephemient/aoc2022/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ private val trace by lazy(LazyThreadSafetyMode.NONE) {
System.getenv("TRACE")?.startsWith('0') != true
}

actual inline fun assert(condition: () -> Boolean) {
assert(condition())
}

actual inline fun assert(condition: () -> Boolean, lazyMessage: () -> Any) {
assert(condition(), lazyMessage)
}

actual fun trace(message: String) {
if (trace) System.err.println(message)
}
8 changes: 8 additions & 0 deletions kt/src/nativeMain/kotlin/com/github/ephemient/aoc2022/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ actual fun getInput(day: Int): List<String> {
}
}

actual inline fun assert(condition: () -> Boolean) {
assert(condition())
}

actual inline fun assert(condition: () -> Boolean, lazyMessage: () -> Any) {
assert(condition(), lazyMessage)
}

actual fun trace(message: String) {
if (getenv("TRACE")?.get(0) != '0'.code.toByte()) fputs("$message\n", stderr)
}

0 comments on commit 9fc638f

Please sign in to comment.