main_test.go 936 Bytes
Newer Older
Thomas Hoffmann's avatar
Thomas Hoffmann committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main

import (
	"crycomp/internal/blood"
	"fmt"
	"testing"
)

func TestBloodTable(t *testing.T) {
	// Check the dimensions of BloodTable.

	if len(blood.Table) != 8 {
		t.Fatalf("Expected 8 rows, got %d", len(blood.Table))
	}

	for i := range blood.Table {
		if len(blood.Table[i]) != 8 {
			t.Fatalf("Expected columns in row %d, got %d", i, len(blood.Table))
		}
	}
}

func TestProtocol(t *testing.T) {
	// Runs the protocol for all combinations of recipient and donor blood types.
	n := len(blood.Table)

	for x := 0; x < n; x++ {
		for y := 0; y < n; y++ {
			testName := fmt.Sprintf("(x=%s,y=%s)", blood.Names[x], blood.Names[y])
			t.Run(testName, func(t *testing.T) {
				z, err := RunProtocol(x, y)
				if err != nil {
					t.Errorf("Protocol error: %s", err)
				} else if z != blood.Table[x][y] {
					t.Fatalf("Failed blood compatibility test for index [%d,%d]. Expected %t, got %t", x, y, !z, z)
				}
			})
		}
	}
}