Skip to content

Commit

Permalink
Use Boolean Values (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwilhe2 authored Dec 27, 2020
1 parent 999fc6d commit 5dfa9ac
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/commonMain/kotlin/com/github/fwilhe/inzell/StandardLibrary.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@ package com.github.fwilhe.inzell

import kotlin.jvm.JvmName
import kotlin.math.pow
import kotlin.math.sqrt
import kotlin.random.*

fun isEven(x: Int): Double = if (x.rem(2) == 0) 1.0 else 0.0
fun count(x: Int): Double = x.toDouble()
fun isEven(x: Int): Boolean = x.rem(2) == 0

fun isPrime(x: Int): Boolean {
if (x < 2) {
return false
}

for (i in 2..sqrt(x.toDouble()).toInt()){
if (x.rem(i) == 0) {
return false
}
}
return true
}

fun count(x: Int): Int = x

fun sine(x: Int): Double = kotlin.math.sin(x.toDouble())
fun cosine(x: Int): Double = kotlin.math.cos(x.toDouble())
Expand Down
12 changes: 12 additions & 0 deletions src/commonTest/kotlin/SheetsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ class SheetsTest {
assertEquals(expected, sheet)
}

@Test
fun booleanValues() {
val sheet = spreadsheet {
column("i") { x -> count(x) }
column("is even") { x -> isEven(x) }
column("even and prime") { x -> isEven(x).and(isPrime(x)) }
}

MarkdownPrinter(sheet).printToStandardOut()

}

@Test
fun printTables() {
val numberOfCpus = Column("Number of CPUs") { x -> x * x }
Expand Down
28 changes: 28 additions & 0 deletions src/commonTest/kotlin/StandardLibraryTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.fwilhe.inzell

import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class StandardLibraryTest {
@Test
fun even() {
assertTrue(isEven(0))
assertFalse(isEven(1))
assertTrue(isEven(2))
assertFalse(isEven(3))
}

@Test
fun prime() {
assertFalse(isPrime(0))
assertFalse(isPrime(1))
assertTrue(isPrime(2))
assertTrue(isPrime(3))
assertFalse(isPrime(4))
assertTrue(isPrime(5))
assertFalse(isPrime(6))
assertTrue(isPrime(7))
assertFalse(isPrime(8))
}
}

0 comments on commit 5dfa9ac

Please sign in to comment.