59 lines
2.2 KiB
Python
59 lines
2.2 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.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')
|