This repository has been archived on 2021-05-25. You can view files and clone it, but cannot push or open issues or pull requests.
pyRCV2/tests/test_numbers.py

88 lines
4.3 KiB
Python
Raw Normal View History

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
2021-01-12 23:54:56 +11:00
import tests.util
2021-01-02 23:34:09 +11:00
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():
2021-01-12 23:54:56 +11:00
ctx = tests.util.init_context()
2021-01-02 23:34:09 +11:00
ctx.eval('py.pyRCV2.numbers.set_numclass(py.pyRCV2.numbers.{});'.format(numbers))
ctx.eval('py.pyRCV2.numbers.set_dps({});'.format(dps))
2021-01-12 23:54:56 +11:00
ctx.eval('let num1 = py.pyRCV2.numbers.Num("314.15");')
ctx.eval('let num2 = py.pyRCV2.numbers.Num("42.42");')
2021-01-02 23:34:09 +11:00
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_gfixed2_add_py, test_gfixed2_add_js = maketst('FixedGuarded', 2, '__add__', '356.57')
2021-01-02 23:34:09 +11:00
test_rational_add_py, test_rational_add_js = maketst('Rational', 0, '__add__', '356.57')
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():
2021-01-12 23:54:56 +11:00
ctx = tests.util.init_context()
ctx.eval('py.pyRCV2.numbers.set_numclass(py.pyRCV2.numbers.{});'.format(numbers))
ctx.eval('py.pyRCV2.numbers.set_dps({});'.format(dps))
2021-01-12 23:54:56 +11:00
ctx.eval('let num1 = py.pyRCV2.numbers.Num("{}");'.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_fixed_round3_py, test_fixed_round3_js = maketst_round('Fixed', 5, '3141.45', 1, 'ROUND_HALF_EVEN', '3141.4')
test_fixed_round4_py, test_fixed_round4_js = maketst_round('Fixed', 5, '3141.45', 1, 'ROUND_HALF_UP', '3141.5')
test_gfixed_round1_py, test_gfixed_round1_js = maketst_round('FixedGuarded', 5, '3141.59', 1, 'ROUND_DOWN', '3141.5')
test_gfixed_round2_py, test_gfixed_round2_js = maketst_round('FixedGuarded', 5, '3141.59', 1, 'ROUND_UP', '3141.6')
test_gfixed_round3_py, test_gfixed_round3_js = maketst_round('FixedGuarded', 5, '3141.45', 1, 'ROUND_HALF_EVEN', '3141.4')
test_gfixed_round4_py, test_gfixed_round4_js = maketst_round('FixedGuarded', 5, '3141.45', 1, 'ROUND_HALF_UP', '3141.5')
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_native_round3_py, _ = maketst_round('Native', 0, '3141.45', 1, 'ROUND_HALF_EVEN', '3141.4')
_, test_native_round4_js = maketst_round('Native', 0, '3141.45', 1, 'ROUND_HALF_UP', '3141.5')
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')
test_rational_round3_py, _ = maketst_round('Rational', 0, '3141.45', 1, 'ROUND_HALF_EVEN', '3141.4')
_, test_rational_round4_js = maketst_round('Rational', 0, '3141.45', 1, 'ROUND_HALF_UP', '3141.5')