-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.jl
68 lines (57 loc) · 2.23 KB
/
testing.jl
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module testing
#=
Unit tests require Test and InteractiveUtils to be installed. The latter doesn't
need to be explicitly using'ed if you're running the tests the Jupyter notebook
1. Move elections.jl and testing.jl to the same folder
2. Inside Julia (REPL), type push!(LOAD_PATH, "path_of_your_folder"). You may
want to add it to your startup.jl as well
3. Type (in Julia REPL), include("path_of_your_folder/testing.jl"). The tests
will immediately run
=#
using elections
using Test
using InteractiveUtils
const candidate_x_coord = [0.93 0.79 0.27]
const candidate_y_coord = [0.49 0.42 0.45]
const voter_grid = Iterators.product(-2:0.1:2, -2:0.1:2)
# Note: columns are candidates and rows are each voter
# The number is the distance between them
# So [1 2 3] means candidate 3 is furthest from this voter
# d_sorted
test_distance_list = [[1 2 3]; # 1 2 3
[2 1 3]; # 2 1 3
[2 3 1]; # 3 1 2
[3 2 1];] # 3 2 1
@testset "sorter" begin
@test sorter(test_distance_list) == [1 2 3; 2 1 3; 3 1 2; 3 2 1]
@inferred sorter(test_distance_list)
end
@testset "FPTP" begin
@test fptp(test_distance_list) ≈ 3
@inferred fptp(test_distance_list)
end
@testset "Approval" begin
@inferred approval(test_distance_list)
end
@testset "Borda" begin
@test borda(test_distance_list, candidate_x_coord) ≈ 1 #|| 2 || 3
@inferred borda(test_distance_list, candidate_x_coord)
end
@testset "IRV" begin
@test irv(test_distance_list) ≈ 3
@inferred irv(test_distance_list)
end
@testset "Score" begin
@test score(test_distance_list) ≈ 1 #|| 2
@inferred score(test_distance_list)
end
@testset "election functions" begin
@test typeof(election(1, 1, 1, 100, [1 2 3], [3 2 1])) == NTuple{5,Int64}
@inferred election(1, 1, 1, 100, [1 2 3], [3 2 1])
@test size(loop_elections(Iterators.product(-2:1:2, -2:1:2), [1 2 3], [3 2 1])) == (25, 7)
@inferred loop_elections(Iterators.product(-2:1:2, -2:1:2), [1 2 3], [3 2 1])
end
@code_warntype borda(test_distance_list)
# this, irv(), and sorted() shows yellow on Union{Nothing, ...}
# This is just the loop over the iterator, which returns the next value, or nothing if at end
end