Skip to content

Instantly share code, notes, and snippets.

@Mahoney
Created September 24, 2012 21:58
Show Gist options
  • Save Mahoney/3778705 to your computer and use it in GitHub Desktop.
Save Mahoney/3778705 to your computer and use it in GitHub Desktop.
Tests for Boolean Logic
package booleanlogictests
import booleanlogic.Conditional.if1
import booleanlogic.Boolean
import booleanlogic.false1
import booleanlogic.true1
import org.junit.Test
import scala.collection.mutable.MutableList
class BooleanTest {
@Test def `equality`() {
assert(true1 == true1 )
assert(true1 != false1)
assert(false1 != true1 )
assert(false1 == false1)
}
@Test def `simple or`() {
assert((true1 | true1) == true1 )
assert((true1 | false1) == true1 )
assert((false1 | true1) == true1 )
assert((false1 | false1) == false1)
}
@Test def `shortcut or`() {
assert((true1 || true1) == true1 )
assert((true1 || false1) == true1 )
assert((false1 || true1) == true1 )
assert((false1 || false1) == false1)
}
@Test def `simple or left side true`() {
var evaluated = "not evaluated"
true1 | {evaluated = "evaluated"; true1}
assert(evaluated == "evaluated")
}
@Test def `shortcut or left side true`() {
var evaluated = "not evaluated"
true1 || {evaluated = "evaluated"; true1}
assert(evaluated == "not evaluated")
}
@Test def `simple and`() {
assert((true1 & true1) == true1 )
assert((true1 & false1) == false1)
assert((false1 & true1) == false1)
assert((false1 & false1) == false1)
}
@Test def `shortcut and`() {
assert((true1 && true1) == true1 )
assert((true1 && false1) == false1)
assert((false1 && true1) == false1)
assert((false1 && false1) == false1)
}
@Test def `simple and left side false`() {
var evaluated = "not evaluated"
false1 & {evaluated = "evaluated"; true1}
assert(evaluated == "evaluated")
}
@Test def `shortcut and left side false`() {
var evaluated = "not evaluated"
false1 && {evaluated = "evaluated"; true1}
assert(evaluated == "not evaluated")
}
@Test def xor() {
assert((true1 ^ true1) == false1)
assert((true1 ^ false1) == true1 )
assert((false1 ^ true1) == true1 )
assert((false1 ^ false1) == false1)
}
@Test def not() {
assert(!true1 == false1);
assert(!false1 == true1 );
}
@Test def `if else if true`() {
val result = if1 (true1) {
"trueresult"
} else1 {
throw new IllegalStateException
}
assert(result == "trueresult")
}
@Test def `if else if false`() {
val result = if1 (false1) {
throw new IllegalStateException
} else1 {
"falseresult"
}
assert(result == "falseresult")
}
@Test def `if true no else`() {
var evaluated = "not evaluated"
if1 (true1) {
evaluated = "evaluated"
}
assert(evaluated == "evaluated")
}
@Test def `if elseif else when if and elseif both true`() {
val evaluated: MutableList[String] = MutableList()
val result =
if1 (true1) {
evaluated += "branch1"
"branch1"
}.elseif (true1) {
evaluated += "branch2"
"branch2"
} else1 {
evaluated += "branch3"
"branch3"
}
assert(result == "branch1")
assert(evaluated == MutableList("branch1"))
}
@Test def `if elseif else when if false and elseif true`() {
val evaluated: MutableList[String] = MutableList()
val result =
if1 (false1) {
evaluated += "branch1"
"branch1"
}.elseif (true1) {
evaluated += "branch2"
"branch2"
} else1 {
evaluated += "branch3"
"branch3"
}
assert(result == "branch2")
assert(evaluated == MutableList("branch2"))
}
@Test def `if elseif else when if and elseif both false`() {
val evaluated: MutableList[String] = MutableList()
val result =
if1 (false1) {
evaluated += "branch1"
"branch1"
}.elseif (false1) {
evaluated += "branch2"
"branch2"
} else1 {
evaluated += "branch3"
"branch3"
}
assert(result == "branch3")
assert(evaluated == MutableList("branch3"))
}
@Test def `if elseif when if and elseif both true`() {
val evaluated: MutableList[String] = MutableList()
if1 (true1) {
evaluated += "branch1"
}.elseif (true1) {
evaluated += "branch2"
}
assert(evaluated == MutableList("branch1"))
}
@Test def `if elseif when if true and elseif false`() {
val evaluated: MutableList[String] = MutableList()
if1 (true1) {
evaluated += "branch1"
}.elseif (false1) {
evaluated += "branch2"
}
assert(evaluated == MutableList("branch1"))
}
@Test def `if elseif when if false and elseif true`() {
val evaluated: MutableList[String] = MutableList()
if1 (false1) {
evaluated += "branch1"
}.elseif (true1) {
evaluated += "branch2"
}
assert(evaluated == MutableList("branch2"))
}
@Test def `if elseif when if and elseif both false`() {
val evaluated: MutableList[String] = MutableList()
if1 (false1) {
evaluated += "branch1"
}.elseif (false1) {
evaluated += "branch2"
}
assert(evaluated == MutableList())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment