Releveant Code
package roll
import "math/rand"
func Roll(dice int) int {
return rand.Intn(dice) + 1
}
package dice
import (
"crabot/dice/roll"
)
var (
result int
)
func D2(count int) int {
for count > 0 {
result += roll.Roll(2)
count--
}
return result
}
func D4(count int) int {
for count > 0 {
result += roll.Roll(4)
count--
}
return result
}
package dice_test
import (
"testing"
"crabot/dice"
)
var (
result int
)
func TestD2(t *testing.T) {
result = dice.D2(1)
if result < 0 {
t.Fatal("dice.D2(1) was negative")
}
if result == 0 {
t.Fatal("dice.D2(1) was 0")
}
if result > 2 {
t.Fatal("dice.D2(1) was greater than 2")
}
result = dice.D2(2)
if result < 0 {
t.Fatal("dice.D2(2) was negative")
}
if result == 0 {
t.Fatal("dice.D2(2) was 0")
}
if result < 2 {
t.Fatal("dice.D2(2) was less than 2")
}
}
func TestD4(t *testing.T) {
result = dice.D4(1)
if result < 0 {
t.Fatal("dice.D4(1) was negative")
}
if result == 0 {
t.Fatal("dice.D4(1) was 0")
}
if result > 4 {
t.Fatal("dice.D4(1) was greater than 4")
}
result = dice.D4(2)
if result < 0 {
t.Fatal("dice.D4(2) was negative")
}
if result == 0 {
t.Fatal("dice.D4(2) was 0")
}
if result < 2 {
t.Fatal("dice.D4(2) was less than 2")
}
}