2020-12-31 16:44:17 +11:00
|
|
|
# pyRCV2: Preferential vote counting
|
2021-01-03 18:57:56 +11:00
|
|
|
# Copyright © 2020–2021 Lee Yingtong Li (RunasSudo)
|
2020-12-31 16:44:17 +11:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import pyRCV2.blt
|
|
|
|
import pyRCV2.numbers
|
2021-01-12 03:51:57 +11:00
|
|
|
import pyRCV2.method.gregory, pyRCV2.method.meek
|
2020-12-31 16:44:17 +11:00
|
|
|
from pyRCV2.model import CountCompleted
|
2021-01-12 23:54:56 +11:00
|
|
|
import tests.util
|
2020-12-31 16:44:17 +11:00
|
|
|
|
|
|
|
import json
|
|
|
|
|
2021-01-12 03:51:57 +11:00
|
|
|
def maketst(numbers, counter_cls, options, options_description):
|
2020-12-31 16:44:17 +11:00
|
|
|
def t_py():
|
|
|
|
pyRCV2.numbers.set_numclass(getattr(pyRCV2.numbers, numbers))
|
|
|
|
pyRCV2.numbers.set_dps(5)
|
|
|
|
|
2021-01-02 23:34:09 +11:00
|
|
|
with open('tests/data/prsa1.blt', 'r') as f:
|
2020-12-31 16:44:17 +11:00
|
|
|
election = pyRCV2.blt.readBLT(f.read())
|
|
|
|
|
|
|
|
counter = eval(counter_cls)(election, options)
|
2021-01-12 03:51:57 +11:00
|
|
|
|
|
|
|
assert counter.describe_options() == options_description
|
|
|
|
|
2020-12-31 16:44:17 +11:00
|
|
|
result = counter.reset()
|
|
|
|
while not isinstance(result, CountCompleted):
|
|
|
|
result = counter.step()
|
2021-01-12 03:51:57 +11:00
|
|
|
|
|
|
|
# TODO: Do some sanity checks
|
2020-12-31 16:44:17 +11:00
|
|
|
|
|
|
|
def t_js():
|
2021-01-12 23:54:56 +11:00
|
|
|
ctx = tests.util.init_context()
|
2020-12-31 16:44:17 +11:00
|
|
|
|
2021-01-12 03:51:57 +11:00
|
|
|
ctx.eval('py.pyRCV2.numbers.set_numclass(py.pyRCV2.numbers.Fixed);')
|
|
|
|
ctx.eval('py.pyRCV2.numbers.set_dps(5);')
|
|
|
|
|
2021-01-02 23:34:09 +11:00
|
|
|
with open('tests/data/prsa1.blt', 'r') as f:
|
2020-12-31 16:44:17 +11:00
|
|
|
election = f.read()
|
|
|
|
ctx.eval('let electionData = {};'.format(json.dumps(election)))
|
|
|
|
ctx.eval('let election = py.pyRCV2.blt.readBLT(electionData);')
|
|
|
|
ctx.eval('let options = {};'.format(json.dumps(options)))
|
|
|
|
ctx.eval('let counter = py.{}(election, options);'.format(counter_cls))
|
2021-01-12 03:51:57 +11:00
|
|
|
assert ctx.eval('counter.describe_options()') == options_description
|
2020-12-31 16:44:17 +11:00
|
|
|
ctx.eval('let result = counter.reset();')
|
|
|
|
ctx.eval('while (!py.isinstance(result, py.pyRCV2.model.CountCompleted)) { result = counter.step(); }')
|
|
|
|
|
|
|
|
return t_py, t_js
|
|
|
|
|
2021-01-12 03:51:57 +11:00
|
|
|
test_prsa1_scottish_py, test_prsa1_scottish_js = maketst('Fixed', 'pyRCV2.method.gregory.WIGSTVCounter', {'round_quota': 0}, '--round-quota 0')
|
2021-01-15 16:00:22 +11:00
|
|
|
test_prsa1_senate_py, test_prsa1_senate_js = maketst('Fixed', 'pyRCV2.method.gregory.UIGSTVCounter', {'surplus_order': 'order', 'exclusion': 'by_value', 'bulk_exclude': True, 'round_quota': 0, 'round_votes': 0}, '--method uig --bulk-exclude --round-quota 0 --round-votes 0 --surplus-order order --exclusion by_value')
|
2021-01-12 03:51:57 +11:00
|
|
|
test_prsa1_meek_py, test_prsa1_meek_js = maketst('Fixed', 'pyRCV2.method.meek.MeekSTVCounter', {'quota_criterion': 'gt', 'quota': 'droop_exact', 'quota_mode': 'progressive'}, '--method meek --quota droop_exact --quota-criterion gt --quota-mode progressive')
|
|
|
|
test_prsa1_wright_py, test_prsa1_wright_js = maketst('Fixed', 'pyRCV2.method.gregory.WIGSTVCounter', {'exclusion': 'wright', 'round_quota': 0, 'bulk_exclude': True}, '--bulk-exclude --round-quota 0 --exclusion wright')
|
2021-01-15 16:00:22 +11:00
|
|
|
|
|
|
|
# Regression test: Withdrawn candidates interfering with bulk exclusion
|
|
|
|
test_meekm_senate_py, test_meekm_senate_js = maketst('Fixed', 'pyRCV2.method.gregory.UIGSTVCounter', {'surplus_order': 'order', 'exclusion': 'by_value', 'bulk_exclude': True, 'round_quota': 0, 'round_votes': 0}, '--method uig --bulk-exclude --round-quota 0 --round-votes 0 --surplus-order order --exclusion by_value')
|