1
1
Fork 0

Added tests for 'truth table' task

This commit is contained in:
Svetlana Isakova 2019-10-09 17:17:28 +02:00
parent 5147999720
commit a0cef9878f
10 changed files with 110 additions and 5 deletions

View File

@ -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()
}

View File

@ -0,0 +1,6 @@
type: edu
files:
- name: src/Task.kt
visible: true
- name: test/Tests.kt
visible: false

View File

@ -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.

View File

@ -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)
}
}
}

View File

@ -1,4 +1,4 @@
package booleans2
package booleans3
fun and(b1: Boolean, b2: Boolean): Boolean =
if (b1) b2 else false

View File

@ -1,4 +1,4 @@
package booleans2
package booleans3
import org.junit.Assert
import org.junit.FixMethodOrder

View File

@ -1,3 +1,3 @@
package booleans3
package booleans4
fun foo(): Boolean = true

View File

@ -1,4 +1,4 @@
package booleans3
package booleans4
fun main() {
println(foo())

View File

@ -1 +1 @@
package booleans3
package booleans4

View File

@ -1,5 +1,6 @@
content:
- Examples
- Exercise 1
- Exercise 2
- Exercise 3
- Exercise 4