This repository has been archived on 2021-05-25. You can view files and clone it, but cannot push or open issues or pull requests.
pyRCV2/tests/test_combinations.py

68 lines
3.5 KiB
Python

# pyRCV2: Preferential vote counting
# Copyright © 2020–2021 Lee Yingtong Li (RunasSudo)
#
# 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
import pyRCV2.method.gregory, pyRCV2.method.meek
from pyRCV2.model import CountCompleted
import tests.util
import json
def maketst(numbers, counter_cls, options, options_description):
def t_py():
pyRCV2.numbers.set_numclass(getattr(pyRCV2.numbers, numbers))
pyRCV2.numbers.set_dps(5)
with open('tests/data/prsa1.blt', 'r') as f:
election = pyRCV2.blt.readBLT(f.read())
counter = eval(counter_cls)(election, options)
assert counter.describe_options() == options_description
result = counter.reset()
while not isinstance(result, CountCompleted):
result = counter.step()
# TODO: Do some sanity checks
def t_js():
ctx = tests.util.init_context()
ctx.eval('py.pyRCV2.numbers.set_numclass(py.pyRCV2.numbers.Fixed);')
ctx.eval('py.pyRCV2.numbers.set_dps(5);')
with open('tests/data/prsa1.blt', 'r') as f:
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))
assert ctx.eval('counter.describe_options()') == options_description
ctx.eval('let result = counter.reset();')
ctx.eval('while (!py.isinstance(result, py.pyRCV2.model.CountCompleted)) { result = counter.step(); }')
return t_py, t_js
test_prsa1_scottish_py, test_prsa1_scottish_js = maketst('Fixed', 'pyRCV2.method.gregory.WIGSTVCounter', {'round_quota': 0}, '--round-quota 0')
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')
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')
# 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')