Add test cases for numbers

This commit is contained in:
RunasSudo 2021-01-02 23:34:09 +11:00
parent 8a304aedcf
commit 1b74e41695
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
10 changed files with 64 additions and 6 deletions

View File

@ -26,7 +26,7 @@ import gzip
# Read model CSV # Read model CSV
with open('tests/aec-senate-formalpreferences-24310-TAS.csv', 'r', newline='') as f: with open('tests/data/aec-senate-formalpreferences-24310-TAS.csv', 'r', newline='') as f:
reader = csv.reader(f) reader = csv.reader(f)
data = [x for x in reader] data = [x for x in reader]
@ -38,7 +38,7 @@ def test_aec_tas19():
pyRCV2.numbers.set_numclass(pyRCV2.numbers.NativeInt) pyRCV2.numbers.set_numclass(pyRCV2.numbers.NativeInt)
with gzip.open('tests/aec-senate-formalpreferences-24310-TAS.blt.gz', 'rt') as f: with gzip.open('tests/data/aec-senate-formalpreferences-24310-TAS.blt.gz', 'rt') as f:
election = pyRCV2.blt.readBLT(f.read()) election = pyRCV2.blt.readBLT(f.read())
assert len(election.candidates) == len(candidates) assert len(election.candidates) == len(candidates)

View File

@ -28,7 +28,7 @@ def maketst(numbers, counter_cls, options):
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)
with open('tests/prsa1.blt', 'r') as f: with open('tests/data/prsa1.blt', 'r') as f:
election = pyRCV2.blt.readBLT(f.read()) election = pyRCV2.blt.readBLT(f.read())
counter = eval(counter_cls)(election, options) counter = eval(counter_cls)(election, options)
@ -49,7 +49,7 @@ 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())
with open('tests/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);')

View File

@ -36,7 +36,7 @@ def test_csm15():
pyRCV2.numbers.set_numclass(pyRCV2.numbers.Native) pyRCV2.numbers.set_numclass(pyRCV2.numbers.Native)
with open('tests/CSM15.blt', 'r') as f: with open('tests/data/CSM15.blt', 'r') as f:
election = pyRCV2.blt.readBLT(f.read()) election = pyRCV2.blt.readBLT(f.read())
assert len(election.candidates) == 40 assert len(election.candidates) == 40

58
tests/test_numbers.py Normal file
View File

@ -0,0 +1,58 @@
# 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.numbers
from pyRCV2.numbers import Num
from py_mini_racer import py_mini_racer
def maketst(numbers, dps, method, result):
def t_py():
pyRCV2.numbers.set_numclass(getattr(pyRCV2.numbers, numbers))
pyRCV2.numbers.set_dps(dps)
num1 = Num('314.15')
num2 = Num('42.42')
assert getattr(num1, method)(num2) == Num(result)
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())
ctx.eval('py.pyRCV2.numbers.set_numclass(py.pyRCV2.numbers.{});'.format(numbers))
ctx.eval('py.pyRCV2.numbers.set_dps({});'.format(dps))
ctx.eval('num1 = py.pyRCV2.numbers.Num("314.15"); void(0);')
ctx.eval('num2 = py.pyRCV2.numbers.Num("42.42"); void(0);')
assert ctx.eval('num1.{}(num2).__eq__(py.pyRCV2.numbers.Num("{}"))'.format(method, result))
return t_py, t_js
test_fixed2_add_py, test_fixed2_add_js = maketst('Fixed', 2, '__add__', '356.57')
test_fixed1_add_py, test_fixed1_add_js = maketst('Fixed', 1, '__add__', '356.6')
test_fixed0_add_py, test_fixed0_add_js = maketst('Fixed', 0, '__add__', '356')
test_rational_add_py, test_rational_add_js = maketst('Rational', 0, '__add__', '356.57')

View File

@ -29,7 +29,7 @@ def test_prsa1():
pyRCV2.numbers.set_numclass(pyRCV2.numbers.Fixed) pyRCV2.numbers.set_numclass(pyRCV2.numbers.Fixed)
pyRCV2.numbers.set_dps(5) pyRCV2.numbers.set_dps(5)
with open('tests/prsa1.blt', 'r') as f: with open('tests/data/prsa1.blt', 'r') as f:
election = pyRCV2.blt.readBLT(f.read()) election = pyRCV2.blt.readBLT(f.read())
assert len(election.candidates) == 7 assert len(election.candidates) == 7