~jasper/type_stack_calc

12fd0b2cfdbee2a5909b06f1389090961e8330bd — Jasper den Ouden 8 months ago 338e7ab
Apparently i incorrectly flipped input order before. I added case tests to keep track of it better
M script/show.sh => script/show.sh +1 -1
@@ 4,7 4,7 @@ export PYTHONPATH="./::/usr/lib/python39.zip:/usr/lib/python3.9:/usr/lib/python3

# find test/ -type f | grep -G '.*[.]py' | \
ls test/{integermath,sc_parser}.py \
    test/sets/{one_num,general,range,stepping,intersection}.py \
    test/sets/{cases,one_num,general,range,stepping,intersection}.py \
    | while read FILE; do
    if ! python $FILE ; then
        echo ❌ ERROR IN TEST $FILE

M test/integermath.py => test/integermath.py +3 -2
@@ 1,4 1,4 @@
# Tests with constants from parsing to type output. (not C output etc)
"""Tests with constants from parsing to type output. (not C output etc)"""

from random import randint, random



@@ 10,6 10,7 @@ def_funs = dict(sqr=(1, lambda x: x*x))
#   But then probably going to replace the numbers type code.
def add_n_stuff(p_d=0.9, n_max=4, min=-10, max=10,
                p={'*':0.2, '+':0.3, '-':0.4, 'sqr':0.1}):
    """NOTE: only generates subset..."""
    val = randint(min, max)
    s = str(val)



@@ 27,7 28,7 @@ def add_n_stuff(p_d=0.9, n_max=4, min=-10, max=10,
            if r < 0:
                try:  # Should automatically skip it if not possible.
                    args = [randint(min, max) for i in range(n - 1)]
                    val = fun(*args, val)
                    val = fun(val, *args)
                    s += ' ' + ' '.join(map(str, args)) + ' ' + k
                    break
                except:

A test/sets/cases.py => test/sets/cases.py +15 -0
@@ 0,0 1,15 @@

from type_stack_calc.sets.one_num import OneInt

# Some manual cases.
assert OneInt(4).param_subtract(2) == 2  # 4 - 2
assert OneInt(4).param_gt(2) == True     # 4 > 2
assert OneInt(4).param_lt(2) == False    # 4 < 2

from type_stack_calc.sets.range import SetRange

assert SetRange(2, 4).param_subtract(2) == SetRange(0, 2)
assert SetRange(2, 4).param_gt(0) == True
assert SetRange(2, 4).param_ge(2) == True
assert SetRange(2, 4).param_le(4) == True
print(SetRange(2, 4).param_le(3))  # TODO None? should be bool.

M test/sets/one_num.py => test/sets/one_num.py +8 -6
@@ 6,19 6,21 @@ from type_stack_calc.sets.one_num import OneFloat, OneInt

from random import random

# Tests calculations with `OneFloat` and `OneInt`.
for _n in range(50):
    for k, info in fun_info.items():
    for k, info in fun_info.items():  # TODO inputs are all flipped around!
        n, call = info.n, info.call
        assert n is not None, k

        args = [random() for _i in range(n)]
        try:
            ret = call(*reversed(args))
            ret = call(*args)
        except:
            ret = None
        if ret is not None:  # Compare plain with OneFloat approach.
            x, *rest = map(OneFloat, args)
            assert getattr(x, f"param_{k}")(*rest) == ret
            complain = (k, ret, (x, *rest))
            assert getattr(x, f"param_{k}")(*rest) == ret, complain

            # TODO non-working:
            if k in {'%', 'modulo', '**', 'pow', 'cos', 'sin'}:


@@ 28,8 30,8 @@ for _n in range(50):
                   getattr(SetRange(x,x), f"param_{k}")(*(SetRange(y,y) for y in rest))]
            for j, rg in enumerate(got):
                if getattr(info, 'bool', None):
                    assert rg == ret
                    assert rg == ret, (rg, *complain)
                elif isinstance(rg, (int, float)):
                    assert rg == ret, (k, rg, ret)
                    assert rg == ret, (rg, *complain)
                else:
                    assert rg == SetRange(float(ret), float(ret)), (j, k, rg, ret)
                    assert rg == SetRange(float(ret), float(ret)), (rg, *complain)

M type_stack_calc/ib/ib_fun.py => type_stack_calc/ib/ib_fun.py +3 -1
@@ 1,5 1,7 @@

"""Simple inbuild function, but additionally permits a `.code_type`, which is code that may determine the type instead of the function."""
"""Changes simple inbuilt function, to additionally permits a `.code_type`, which is code that may determine the type instead of the function.

Had to be separate because of circular dependencies regarding typecalc applies number definitions which need inbuilt functions, and function variants needs typecalc."""

from type_stack_calc.base.function import BaseFunction
from type_stack_calc.ib.simple_ib_fun import SimpleInbuildFunction

M type_stack_calc/sets/one_num.py => type_stack_calc/sets/one_num.py +2 -2
@@ 85,7 85,7 @@ def param_2(name, commute):
    setattr(BaseOneNum, num_name, num_set)

    stepping_name, range_name = f"stepping_{name}", f"range_{name}"
    def plain_ret(y, self):
    def plain_ret(self, y):
        ret = num_set(y, self)
        if ret is not None:
            return ret


@@ 95,7 95,7 @@ def param_2(name, commute):
            return getattr(self, range_name)(y)

    param_name = f"param_{name}"
    def commute_ret(y, self):
    def commute_ret(self, y):
        """If not num, flip em around."""
        ret = num_set(y, self)
        if ret is None: