Added tests for 'truth table' task
This commit is contained in:
parent
5147999720
commit
a0cef9878f
|
@ -0,0 +1,25 @@
|
|||
package booleans2
|
||||
|
||||
fun showAnd(first: Boolean, second: Boolean) {
|
||||
println("$first && $second == ${first && second}")
|
||||
}
|
||||
|
||||
fun showOr(first: Boolean, second: Boolean) {
|
||||
println("$first || $second == ${first || second}")
|
||||
}
|
||||
|
||||
fun showTruthTable() {
|
||||
showAnd(true, true)
|
||||
showAnd(true, false)
|
||||
showAnd(false, true)
|
||||
showAnd(false, false)
|
||||
|
||||
showOr(true, true)
|
||||
showOr(true, false)
|
||||
showOr(false, true)
|
||||
showOr(false, false)
|
||||
}
|
||||
|
||||
fun main() {
|
||||
showTruthTable()
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
type: edu
|
||||
files:
|
||||
- name: src/Task.kt
|
||||
visible: true
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
|
@ -0,0 +1,16 @@
|
|||
## Booleans (#2)
|
||||
|
||||
Create a "truth table" for `&&` and `||`. Write a function `showAnd(first:
|
||||
Boolean, second: Boolean)` that uses a `String` template to show `first`, the
|
||||
`&&` symbol, `second`, `==` symbol, and then the result of `first && second`.
|
||||
Write a similar function for `showOr()`.
|
||||
|
||||
The sample output for `showAnd(true, true)` should be:
|
||||
|
||||
```
|
||||
true && true == true
|
||||
```
|
||||
|
||||
Then write a function `showTruthTable` that should display a table produced by
|
||||
all possible combinations of `true` and `false` for both `showAnd()` and
|
||||
`showOr()`. Make sure you include all possible ordering.
|
|
@ -0,0 +1,57 @@
|
|||
package booleans2
|
||||
|
||||
import org.junit.Assert
|
||||
import org.junit.FixMethodOrder
|
||||
import org.junit.Test
|
||||
import org.junit.runners.MethodSorters
|
||||
import util.runAndCheckSystemOutput
|
||||
import util.runAndGetSystemOutput
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
class TestBooleans {
|
||||
@Test
|
||||
fun test1ShowFunctions() {
|
||||
testShowAnd(true, true)
|
||||
testShowAnd(true, false)
|
||||
testShowAnd(false, true)
|
||||
testShowAnd(false, false)
|
||||
|
||||
testShowOr(true, true)
|
||||
testShowOr(true, false)
|
||||
testShowOr(false, true)
|
||||
testShowOr(false, false)
|
||||
}
|
||||
|
||||
private fun testShowAnd(first: Boolean, second: Boolean) {
|
||||
runAndCheckSystemOutput("Wrong output for 'showAnd($first, $second)",
|
||||
"$first && $second == ${first && second}") {
|
||||
showAnd(first, second)
|
||||
}
|
||||
}
|
||||
|
||||
private fun testShowOr(first: Boolean, second: Boolean) {
|
||||
runAndCheckSystemOutput("Wrong output for 'showOr($first, $second)",
|
||||
"$first || $second == ${first || second}") {
|
||||
showOr(first, second)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test2Table() {
|
||||
val table = runAndGetSystemOutput { showTruthTable() }
|
||||
val expectedLines = """
|
||||
true && true == true
|
||||
true && false == false
|
||||
false && true == false
|
||||
false && false == false
|
||||
true || true == true
|
||||
true || false == true
|
||||
false || true == true
|
||||
false || false == false
|
||||
""".trimIndent().lines()
|
||||
expectedLines.forEach {
|
||||
Assert.assertTrue("""Not found the line "$it" in the table""",
|
||||
it in table)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package booleans2
|
||||
package booleans3
|
||||
|
||||
fun and(b1: Boolean, b2: Boolean): Boolean =
|
||||
if (b1) b2 else false
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package booleans2
|
||||
package booleans3
|
||||
|
||||
import org.junit.Assert
|
||||
import org.junit.FixMethodOrder
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
package booleans3
|
||||
package booleans4
|
||||
|
||||
fun foo(): Boolean = true
|
|
@ -1,4 +1,4 @@
|
|||
package booleans3
|
||||
package booleans4
|
||||
|
||||
fun main() {
|
||||
println(foo())
|
||||
|
|
|
@ -1 +1 @@
|
|||
package booleans3
|
||||
package booleans4
|
|
@ -1,5 +1,6 @@
|
|||
content:
|
||||
- Examples
|
||||
- Exercise 1
|
||||
- Exercise 2
|
||||
- Exercise 3
|
||||
- Exercise 4
|
||||
|
|
Loading…
Reference in New Issue