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

66 lines
2.7 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
from pyRCV2.model import CountCompleted
import json
from py_mini_racer import py_mini_racer
def maketst(numbers, counter_cls, options):
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)
result = counter.reset()
while not isinstance(result, CountCompleted):
result = counter.step()
def t_js():
ctx = py_mini_racer.MiniRacer()
# Imports
with open('html/vendor/BigInt_BigRat-a5f89e2.min.js', 'r') as f:
ctx.eval(f.read())
with open('html/vendor/big-6.0.0.min.js', 'r') as f:
ctx.eval(f.read())
with open('html/vendor/sjcl-1.0.8.min.js', 'r') as f:
ctx.eval(f.read())
with open('html/bundle.js', 'r') as f:
ctx.eval(f.read())
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))
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', {})
test_prsa1_stvc_py, test_prsa1_stvc_js = maketst('Rational', 'pyRCV2.method.gregory.WIGSTVCounter', {'quota': 'droop_exact', 'quota_criterion': 'gt', 'prog_quota': True})
test_prsa1_senate_py, test_prsa1_senate_js = maketst('Fixed', 'pyRCV2.method.gregory.UIGSTVCounter', {'surplus_order': 'order', 'exclusion': 'by_value'})
test_prsa1_wright_py, test_prsa1_wright_js = maketst('Fixed', 'pyRCV2.method.gregory.WIGSTVCounter', {'exclusion': 'wright'})