Commit 7dce3d78 authored by Anders Jensen Løvig's avatar Anders Jensen Løvig
Browse files

Test Int2Bools

parent 639191ef
......@@ -52,12 +52,13 @@ func mod(a, n int) int {
return (a%n + n) % n
}
// Int2Bools return x as a slice containing the binary representation.
func Int2Bools(x, bits int) []bool {
var output = make([]bool,0)
var output = make([]bool, 0)
var tmp = 0
for i := bits-1; i >= 0; i-- {
for i := bits - 1; i >= 0; i-- {
tmp = int(math.Exp2(float64(i)))
output = append(output,(x & tmp) / tmp == 1)
output = append(output, (x&tmp)/tmp == 1)
}
return output
}
......@@ -286,7 +287,7 @@ func (P *Protocol) RunProtocol(x, y []bool) bool {
func main() {
var p = InitProtocol(1)
var bloodA, BloodB = BloodType_ABn, BloodType_ABp
x, y := Int2Bools(bloodA,3), Int2Bools(BloodB,3)//[]bool{false, false, false}, []bool{true, true, true}
x, y := Int2Bools(bloodA, 3), Int2Bools(BloodB, 3) //[]bool{false, false, false}, []bool{true, true, true}
z := p.RunProtocol(x, y)
if z == BloodTable[bloodA][BloodB] {
fmt.Println("Protocol succeded")
......
package main
import (
"testing"
)
func CheckBloodType(t *testing.T, bloodType int, expected []bool) {
actual := Int2Bools(bloodType, 3)
if len(actual) != len(expected) {
t.Fatalf(" Expected length to be %d, was %d", len(expected), len(actual))
}
for i := 0; i < 3; i++ {
if actual[i] != expected[i] {
t.Errorf("Expected %t at index %d, got %t", expected[i], i, actual[i])
}
}
}
func TestInt2Bools(t *testing.T) {
t.Run("O-", func(t *testing.T) { CheckBloodType(t, BloodType_On, []bool{false, false, false}) })
t.Run("O+", func(t *testing.T) { CheckBloodType(t, BloodType_Op, []bool{false, false, true}) })
t.Run("A-", func(t *testing.T) { CheckBloodType(t, BloodType_An, []bool{false, true, false}) })
t.Run("A+", func(t *testing.T) { CheckBloodType(t, BloodType_Ap, []bool{false, true, true}) })
t.Run("B-", func(t *testing.T) { CheckBloodType(t, BloodType_Bn, []bool{true, false, false}) })
t.Run("B+", func(t *testing.T) { CheckBloodType(t, BloodType_Bp, []bool{true, false, true}) })
t.Run("AB-", func(t *testing.T) { CheckBloodType(t, BloodType_ABn, []bool{true, true, false}) })
t.Run("AB+", func(t *testing.T) { CheckBloodType(t, BloodType_ABp, []bool{true, true, true}) })
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment