diff --git a/tests/CSM15.blt b/tests/data/CSM15.blt similarity index 100% rename from tests/CSM15.blt rename to tests/data/CSM15.blt diff --git a/tests/aec-senate-formalpreferences-24310-TAS.blt.gz b/tests/data/aec-senate-formalpreferences-24310-TAS.blt.gz similarity index 100% rename from tests/aec-senate-formalpreferences-24310-TAS.blt.gz rename to tests/data/aec-senate-formalpreferences-24310-TAS.blt.gz diff --git a/tests/aec-senate-formalpreferences-24310-TAS.csv b/tests/data/aec-senate-formalpreferences-24310-TAS.csv similarity index 100% rename from tests/aec-senate-formalpreferences-24310-TAS.csv rename to tests/data/aec-senate-formalpreferences-24310-TAS.csv diff --git a/tests/aec-senate-formalpreferences-24310-TAS.ods b/tests/data/aec-senate-formalpreferences-24310-TAS.ods similarity index 100% rename from tests/aec-senate-formalpreferences-24310-TAS.ods rename to tests/data/aec-senate-formalpreferences-24310-TAS.ods diff --git a/tests/prsa1.blt b/tests/data/prsa1.blt similarity index 100% rename from tests/prsa1.blt rename to tests/data/prsa1.blt diff --git a/tests/test_aec.py b/tests/test_aec.py index 8daecb0..16063b3 100644 --- a/tests/test_aec.py +++ b/tests/test_aec.py @@ -26,7 +26,7 @@ import gzip # 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) data = [x for x in reader] @@ -38,7 +38,7 @@ def test_aec_tas19(): 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()) assert len(election.candidates) == len(candidates) diff --git a/tests/test_combinations.py b/tests/test_combinations.py index 6e535cf..7091001 100644 --- a/tests/test_combinations.py +++ b/tests/test_combinations.py @@ -28,7 +28,7 @@ def maketst(numbers, counter_cls, options): pyRCV2.numbers.set_numclass(getattr(pyRCV2.numbers, numbers)) 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()) counter = eval(counter_cls)(election, options) @@ -49,7 +49,7 @@ def maketst(numbers, counter_cls, options): with open('html/bundle.js', 'r') as f: ctx.eval(f.read()) - with open('tests/prsa1.blt', 'r') as f: + 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);') diff --git a/tests/test_csm.py b/tests/test_csm.py index 580f1b0..5b49381 100644 --- a/tests/test_csm.py +++ b/tests/test_csm.py @@ -36,7 +36,7 @@ def test_csm15(): 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()) assert len(election.candidates) == 40 diff --git a/tests/test_numbers.py b/tests/test_numbers.py new file mode 100644 index 0000000..0021efe --- /dev/null +++ b/tests/test_numbers.py @@ -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 . + +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') diff --git a/tests/test_prsa.py b/tests/test_prsa.py index b9e02ce..96ab359 100644 --- a/tests/test_prsa.py +++ b/tests/test_prsa.py @@ -29,7 +29,7 @@ def test_prsa1(): pyRCV2.numbers.set_numclass(pyRCV2.numbers.Fixed) 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()) assert len(election.candidates) == 7