2021-01-02 23:34:09 +11:00
|
|
|
# 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')
|
2021-01-03 00:29:41 +11:00
|
|
|
|
|
|
|
def maketst_round(numbers, dps, num, dps_round, mode_round, result):
|
|
|
|
def t_py():
|
|
|
|
pyRCV2.numbers.set_numclass(getattr(pyRCV2.numbers, numbers))
|
|
|
|
pyRCV2.numbers.set_dps(dps)
|
|
|
|
|
|
|
|
num1 = Num(num)
|
|
|
|
assert num1.round(dps_round, getattr(num1, mode_round)) == 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("{}"); void(0);'.format(num))
|
|
|
|
assert ctx.eval('num1.round({}, num1.{}).__eq__(py.pyRCV2.numbers.Num("{}"))'.format(dps_round, mode_round, result))
|
|
|
|
|
|
|
|
return t_py, t_js
|
|
|
|
|
|
|
|
test_fixed_round1_py, test_fixed_round1_js = maketst_round('Fixed', 5, '3141.59', 1, 'ROUND_DOWN', '3141.5')
|
|
|
|
test_fixed_round2_py, test_fixed_round2_js = maketst_round('Fixed', 5, '3141.59', 1, 'ROUND_UP', '3141.6')
|
|
|
|
test_native_round1_py, test_native_round1_js = maketst_round('Native', 0, '3141.59', 1, 'ROUND_DOWN', '3141.5')
|
|
|
|
test_native_round2_py, test_native_round2_js = maketst_round('Native', 0, '3141.59', 1, 'ROUND_UP', '3141.6')
|
|
|
|
test_rational_round1_py, test_rational_round1_js = maketst_round('Rational', 0, '3141.59', 1, 'ROUND_DOWN', '3141.5')
|
|
|
|
test_rational_round2_py, test_rational_round2_js = maketst_round('Rational', 0, '3141.59', 1, 'ROUND_UP', '3141.6')
|