agreement_test.go 2.91 KB
Newer Older
Mikkel Wienberg Madsen's avatar
Mikkel Wienberg Madsen committed
1
2
3
package election

import (
4
5
	"math/big"
	"strconv"
Mikkel Wienberg Madsen's avatar
Mikkel Wienberg Madsen committed
6
7
8
9
10
11
12
13
14
15
16
	"testing"

	"github.com/google/uuid"
)

func TestIntersection(t *testing.T) {
	N := 7
	elem := make([]uuid.UUID, N)
	for i := 0; i < N; i++ {
		elem[i] = uuid.New()
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
17
18
19
20
21
	superset := []BallotList{
		{"0": elem[0], "1": elem[1], "2": elem[2], "3": elem[3]},
		{"0": elem[0], "1": elem[1], "2": elem[2], "4": elem[4]},
		{"0": elem[0], "1": elem[1], "2": elem[2], "5": elem[5]},
		{"0": elem[0], "1": elem[1], "2": elem[2], "6": elem[6]},
Mikkel Wienberg Madsen's avatar
Mikkel Wienberg Madsen committed
22
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
23

Mikkel Wienberg Madsen's avatar
Mikkel Wienberg Madsen committed
24
	subset := Intersection(superset)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
25
26
	if !subset["0"] {
		t.Error()
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
27
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
28
29
	if !subset["1"] {
		t.Error()
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
30
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
31
32
	if !subset["2"] {
		t.Error()
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
33
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
34
35
	if subset["3"] {
		t.Error()
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
36
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
37
38
	if subset["4"] {
		t.Error()
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
39
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
40
41
	if subset["5"] {
		t.Error()
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
42
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
43
44
	if subset["6"] {
		t.Error()
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
45
	}
Mikkel Wienberg Madsen's avatar
Mikkel Wienberg Madsen committed
46
}
Mikkel Wienberg Madsen's avatar
Mikkel Wienberg Madsen committed
47
48
49
50
51
52
53

func TestMaxSubset(t *testing.T) {
	N := 7
	elem := make([]uuid.UUID, N)
	for i := 0; i < N; i++ {
		elem[i] = uuid.New()
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
54
55
56
57
58
59
	superset := []BallotList{
		{"0": elem[0], "1": elem[1], "2": elem[2], "3": elem[3], "4": elem[4]},
		{"0": elem[0], "1": elem[1], "2": elem[2], "4": elem[4]},
		{"0": elem[0], "1": elem[1], "2": elem[2], "5": elem[5]},
		{"0": elem[0], "1": elem[1], "2": elem[2], "4": elem[4], "6": elem[6]},
		{"6": elem[6]},
Mikkel Wienberg Madsen's avatar
Mikkel Wienberg Madsen committed
60
61
	}
	subset := MaxSubset(4, superset)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
62
63
	if !subset["0"] {
		t.Error()
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
64
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
65
66
	if !subset["1"] {
		t.Error()
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
67
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
68
69
	if !subset["2"] {
		t.Error()
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
70
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
71
72
	if subset["3"] {
		t.Error()
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
73
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
74
75
	if subset["4"] {
		t.Error()
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
76
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
77
78
	if subset["5"] {
		t.Error()
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
79
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
80
81
	if subset["6"] {
		t.Error()
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
82
	}
Mikkel Wienberg Madsen's avatar
Mikkel Wienberg Madsen committed
83
}
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

func TestBallotIntersect(t *testing.T) {
	lists := []BallotList{
		map[string]uuid.UUID{},
		map[string]uuid.UUID{},
		map[string]uuid.UUID{},
	}

	xs := createXS(3)
	ballots := createAllBallots([]*big.Int{
		big.NewInt(0), big.NewInt(1), big.NewInt(0), big.NewInt(0),
		big.NewInt(1), big.NewInt(0), big.NewInt(1), big.NewInt(0),
		big.NewInt(0), big.NewInt(1),
	}, 3, 2, xs)

	for _, bmap := range ballots {
		for id, b := range bmap {
			i, _ := strconv.Atoi(string(id))
			lists[i-1][b.Hash()] = b.ID
		}
	}

	inter := Intersection(lists)
	if len(inter) != 10 {
		t.Error()
	}

	for i := 0; i < len(lists); i++ {
		count := 0
		for h := range lists[i] {
			if _, ok := inter[h]; ok {
				count++
			}
		}
		if count != 10 {
			t.Error(i)
		}
	}
}

func TestBallotMaxSubset(t *testing.T) {
	lists := []BallotList{
		map[string]uuid.UUID{},
		map[string]uuid.UUID{},
		map[string]uuid.UUID{},
	}

	xs := createXS(3)
	ballots := createAllBallots([]*big.Int{
		big.NewInt(0), big.NewInt(1), big.NewInt(0), big.NewInt(0),
		big.NewInt(1), big.NewInt(0), big.NewInt(1), big.NewInt(0),
		big.NewInt(0), big.NewInt(1),
	}, 3, 2, xs)

	for _, bmap := range ballots {
		for id, b := range bmap {
			i, _ := strconv.Atoi(string(id))
			lists[i-1][b.Hash()] = b.ID
		}
	}

	inter := MaxSubset(2, lists)
	if len(inter) != 10 {
		t.Error()
	}

	for i := 0; i < len(lists); i++ {
		count := 0
		for h := range lists[i] {
			if _, ok := inter[h]; ok {
				count++
			}
		}
		if count != 10 {
			t.Error(i)
		}
	}
}