Correct some bugs and add additional test cases
Fix describing election with only --ties prompt Return JS number from SHARandom
This commit is contained in:
parent
fccb36ffbd
commit
5812b252d8
@ -572,7 +572,7 @@ class BaseSTVCounter:
|
|||||||
result.append('--transferable-only')
|
result.append('--transferable-only')
|
||||||
if self.options['exclusion'] != 'one_round':
|
if self.options['exclusion'] != 'one_round':
|
||||||
result.append('--exclusion ' + self.options['exclusion'])
|
result.append('--exclusion ' + self.options['exclusion'])
|
||||||
if len(self.options['ties']) == 0 and isinstance(self.options['ties'][0], pyRCV2.ties.TiesPrompt):
|
if len(self.options['ties']) == 1 and isinstance(self.options['ties'][0], pyRCV2.ties.TiesPrompt):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
for t in self.options['ties']:
|
for t in self.options['ties']:
|
||||||
|
@ -29,4 +29,6 @@ class SHARandom:
|
|||||||
# Discard this value to avoid bias
|
# Discard this value to avoid bias
|
||||||
return self.next(modulus)
|
return self.next(modulus)
|
||||||
|
|
||||||
return val.mod(modulus)
|
# Result is used to index arrays, etc. so result must be a JS number
|
||||||
|
# TODO: Check for overflow
|
||||||
|
return val.mod(modulus).toJSNumber()
|
||||||
|
@ -16,13 +16,13 @@
|
|||||||
|
|
||||||
import pyRCV2.blt
|
import pyRCV2.blt
|
||||||
import pyRCV2.numbers
|
import pyRCV2.numbers
|
||||||
import pyRCV2.method.gregory
|
import pyRCV2.method.gregory, pyRCV2.method.meek
|
||||||
from pyRCV2.model import CountCompleted
|
from pyRCV2.model import CountCompleted
|
||||||
|
|
||||||
import json
|
import json
|
||||||
from py_mini_racer import py_mini_racer
|
from py_mini_racer import py_mini_racer
|
||||||
|
|
||||||
def maketst(numbers, counter_cls, options):
|
def maketst(numbers, counter_cls, options, options_description):
|
||||||
def t_py():
|
def t_py():
|
||||||
pyRCV2.numbers.set_numclass(getattr(pyRCV2.numbers, numbers))
|
pyRCV2.numbers.set_numclass(getattr(pyRCV2.numbers, numbers))
|
||||||
pyRCV2.numbers.set_dps(5)
|
pyRCV2.numbers.set_dps(5)
|
||||||
@ -31,10 +31,15 @@ def maketst(numbers, counter_cls, options):
|
|||||||
election = pyRCV2.blt.readBLT(f.read())
|
election = pyRCV2.blt.readBLT(f.read())
|
||||||
|
|
||||||
counter = eval(counter_cls)(election, options)
|
counter = eval(counter_cls)(election, options)
|
||||||
|
|
||||||
|
assert counter.describe_options() == options_description
|
||||||
|
|
||||||
result = counter.reset()
|
result = counter.reset()
|
||||||
while not isinstance(result, CountCompleted):
|
while not isinstance(result, CountCompleted):
|
||||||
result = counter.step()
|
result = counter.step()
|
||||||
|
|
||||||
|
# TODO: Do some sanity checks
|
||||||
|
|
||||||
def t_js():
|
def t_js():
|
||||||
ctx = py_mini_racer.MiniRacer()
|
ctx = py_mini_racer.MiniRacer()
|
||||||
|
|
||||||
@ -48,18 +53,22 @@ def maketst(numbers, counter_cls, options):
|
|||||||
with open('html/bundle.js', 'r') as f:
|
with open('html/bundle.js', 'r') as f:
|
||||||
ctx.eval(f.read())
|
ctx.eval(f.read())
|
||||||
|
|
||||||
|
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:
|
with open('tests/data/prsa1.blt', 'r') as f:
|
||||||
election = f.read()
|
election = f.read()
|
||||||
ctx.eval('let electionData = {};'.format(json.dumps(election)))
|
ctx.eval('let electionData = {};'.format(json.dumps(election)))
|
||||||
ctx.eval('let election = py.pyRCV2.blt.readBLT(electionData);')
|
ctx.eval('let election = py.pyRCV2.blt.readBLT(electionData);')
|
||||||
ctx.eval('let options = {};'.format(json.dumps(options)))
|
ctx.eval('let options = {};'.format(json.dumps(options)))
|
||||||
ctx.eval('let counter = py.{}(election, options);'.format(counter_cls))
|
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('let result = counter.reset();')
|
||||||
ctx.eval('while (!py.isinstance(result, py.pyRCV2.model.CountCompleted)) { result = counter.step(); }')
|
ctx.eval('while (!py.isinstance(result, py.pyRCV2.model.CountCompleted)) { result = counter.step(); }')
|
||||||
|
|
||||||
return t_py, t_js
|
return t_py, t_js
|
||||||
|
|
||||||
test_prsa1_scottish_py, test_prsa1_scottish_js = maketst('Fixed', 'pyRCV2.method.gregory.WIGSTVCounter', {})
|
test_prsa1_scottish_py, test_prsa1_scottish_js = maketst('Fixed', 'pyRCV2.method.gregory.WIGSTVCounter', {'round_quota': 0}, '--round-quota 0')
|
||||||
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', 'round_quota': 3, 'round_votes': 3, 'round_tvs': 3, 'round_weights': 3}, '--method uig --round-quota 3 --round-votes 3 --round-tvs 3 --round-weights 3 --surplus-order order --exclusion by_value')
|
||||||
test_prsa1_senate_py, test_prsa1_senate_js = maketst('Fixed', 'pyRCV2.method.gregory.UIGSTVCounter', {'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'})
|
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')
|
||||||
|
43
tests/test_random.py
Normal file
43
tests/test_random.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
# 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/>.
|
||||||
|
|
||||||
|
from pyRCV2.random import SHARandom
|
||||||
|
|
||||||
|
from py_mini_racer import py_mini_racer
|
||||||
|
|
||||||
|
def test_sharandom_py():
|
||||||
|
random = SHARandom('foobar')
|
||||||
|
assert random.next(10) == 0
|
||||||
|
assert random.next(42) == 30
|
||||||
|
assert random.next(64) == 13
|
||||||
|
|
||||||
|
def test_sharandom_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())
|
||||||
|
|
||||||
|
ctx.eval('let random = py.pyRCV2.random.SHARandom("foobar");')
|
||||||
|
assert ctx.eval('random.py_next(10)') == 0
|
||||||
|
assert ctx.eval('random.py_next(42)') == 30
|
||||||
|
assert ctx.eval('random.py_next(64)') == 13
|
Reference in New Issue
Block a user