|
| 1 | +# Copyright 2025 The Elekto Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# Author(s): Carson Weeks <mail@carsonweeks.com> |
| 16 | + |
| 17 | +import os |
| 18 | +import sys |
| 19 | +import pytest |
| 20 | + |
| 21 | +from elekto.core import schulze_d, schulze_p, schulze_rank # noqa |
| 22 | + |
| 23 | +def test_schulze_d(): |
| 24 | + candidates = ["A", "B", "C"] |
| 25 | + ballots = { |
| 26 | + "voter1": [("A", 3), ("B", 2), ("C", 1)], |
| 27 | + "voter2": [("B", 3), ("C", 2), ("A", 1)] |
| 28 | + } |
| 29 | + |
| 30 | + expected_d = { |
| 31 | + ("A", "B"): 1, |
| 32 | + ("A", "C"): 1, |
| 33 | + ("B", "A"): 1, |
| 34 | + ("B", "C"): 2, |
| 35 | + ("C", "A"): 1, |
| 36 | + ("C", "B"): 0, |
| 37 | + } |
| 38 | + |
| 39 | + result = schulze_d(candidates, ballots) |
| 40 | + print(result) |
| 41 | + assert result == expected_d |
| 42 | + |
| 43 | +def test_schulze_p(): |
| 44 | + candidates = ["A", "B", "C", "D"] |
| 45 | + d = { |
| 46 | + ("A", "B"): 12, ("B", "A"): 9, |
| 47 | + ("A", "C"): 7, ("C", "A"): 14, |
| 48 | + ("A", "D"): 16, ("D", "A"): 3, |
| 49 | + ("B", "C"): 5, ("C", "B"): 10, |
| 50 | + ("B", "D"): 18, ("D", "B"): 1, |
| 51 | + ("C", "D"): 2, ("D", "C"): 20, |
| 52 | + } |
| 53 | + |
| 54 | + expected_p = { |
| 55 | + ("A", "B"): 12, |
| 56 | + ("B", "A"): 14, |
| 57 | + ("A", "C"): 16, |
| 58 | + ("C", "A"): 14, |
| 59 | + ("A", "D"): 16, |
| 60 | + ("D", "A"): 14, |
| 61 | + ("B", "C"): 18, |
| 62 | + ("C", "B"): 12, |
| 63 | + ("B", "D"): 18, |
| 64 | + ("D", "B"): 12, |
| 65 | + ("C", "D"): 14, |
| 66 | + ("D", "C"): 20, |
| 67 | + } |
| 68 | + |
| 69 | + result = schulze_p(candidates, d) |
| 70 | + print(result) |
| 71 | + assert result == expected_p |
| 72 | + |
| 73 | +def test_schulze_rank_simple(): |
| 74 | + candidates = ["A", "B", "C"] |
| 75 | + p = { |
| 76 | + ("A", "B"): 10, ("B", "A"): 5, |
| 77 | + ("A", "C"): 15, ("C", "A"): 2, |
| 78 | + ("B", "C"): 8, ("C", "B"): 3, |
| 79 | + } |
| 80 | + |
| 81 | + expected = [ |
| 82 | + (0, ["C"]), |
| 83 | + (1, ["B"]), |
| 84 | + (2, ["A"]) |
| 85 | + ] |
| 86 | + |
| 87 | + result = schulze_rank(candidates, p) |
| 88 | + assert result == expected |
| 89 | + |
| 90 | +def test_schulze_rank_tie(): |
| 91 | + candidates = ["A", "B", "C"] |
| 92 | + p = { |
| 93 | + ("A", "B"): 10, ("B", "A"): 5, |
| 94 | + ("B", "C"): 10, ("C", "B"): 5, |
| 95 | + ("C", "A"): 10, ("A", "C"): 5, |
| 96 | + } |
| 97 | + expected = [ |
| 98 | + (1, ["A", "B", "C"]) |
| 99 | + ] |
| 100 | + |
| 101 | + result = schulze_rank(candidates, p) |
| 102 | + assert result == expected |
| 103 | + |
0 commit comments