Exemplo n.º 1
0
def test_characteristic(n):
    f = finite.Field(n)
    _sanity_run_check = False
    for m in int_sampler(n, limit=int(MAX_ITER_N / f.prime) + 1):
        _sanity_run_check = True
        el = f.element(m)
        assert sum(el for _ in range(f.prime)) == f.element(0)
    assert _sanity_run_check
Exemplo n.º 2
0
def test_sampler_basic():
    it = int_sampler([2, 2], limit=100)
    assert next(it) == (0, 0)
    assert next(it) == (0, 1)
    assert next(it) == (1, 0)
    assert next(it) == (1, 1)
    with pytest.raises(StopIteration):
        next(it)
Exemplo n.º 3
0
def test_sampler_sampling(dim):
    size = 2**dim
    gen = int_sampler([2] * dim, limit=size - 1)
    combos = list(gen)

    assert len(combos) == size - 1
    assert len(set(combos)) == size - 1
    assert min(combos) in {(0, ) * dim, (0, ) * (dim - 1) + (1, )}
    assert max(combos) in {(1, ) * dim, (1, ) * (dim - 1) + (0, )}
Exemplo n.º 4
0
def test_sampler_varying_dims(dim, limitpad, type_):
    size = 2**dim

    ranges = [2] * dim
    ranges = [type_(r) for r in ranges]

    gen = int_sampler(ranges, limit=size + limitpad)
    combos = list(gen)

    assert len(combos) == size
    assert len(set(combos)) == size
    assert min(combos) == (0, ) * dim
    assert max(combos) == (1, ) * dim
Exemplo n.º 5
0
def test_sampler_scalar2scalar(mode, type_):
    """When provided a scalar as the 'ranges', yield scalars."""
    size = 4
    if type_ == "sparse_range":
        notranges = range(0, size * 2, 2)
    else:
        notranges = type_(size)

    limit_tweak = -1 if mode == "sampling" else 0

    it = int_sampler(notranges, limit=size + limit_tweak)

    for x in it:
        assert type(x) is int
Exemplo n.º 6
0
def test_associative(n, func):
    f = finite.Field(n)
    _op = "*" if func is operator.mul else "+"

    for na, nb, nc in int_sampler([n, n, n], seed=n):
        a = f.element(na)
        b = f.element(nb)
        c = f.element(nc)
        if func(func(a, b), c) != func(a, func(b, c)):
            print(na, nb, nc)
            ab = func(a, b)
            bc = func(b, c)
            ab_c = func(ab, c)
            a_bc = func(a, bc)
            print(f)
            print(f.mod_poly)
            print(f"a: {na} -> {a}")
            print(f"b: {nb} -> {b}")
            print(f"c: {nc} -> {c}")
            print(f"{ab=}, {ab_c=}")
            print(f"{bc=}, {a_bc=}")
            _note = f"in {f}: ({na} {_op} {nb}) {_op} {nc} != {na} {_op} ({nb} {_op} {nc})"
            pytest.fail(_note)
Exemplo n.º 7
0
def test_add_identity(n, func):
    f = finite.Field(n)
    additive_identity = f.element(0)
    for m in int_sampler(n):
        el = f.element(m)
        assert func(el, additive_identity) == el
Exemplo n.º 8
0
def test_element_roundtrip(n):
    f = finite.Field(n)
    for m in int_sampler(n):
        el = f.element(m)
        assert int(el) == m
Exemplo n.º 9
0
def test_commutative(n, func):
    f = finite.Field(n)
    for na, nb in int_sampler([n, n]):
        a = f.element(na)
        b = f.element(nb)
        assert func(a, b) == func(b, a), (a, b)
Exemplo n.º 10
0
def test_add_inverse(n):
    f = finite.Field(n)
    additive_identity = f.element(0)
    for m in int_sampler(n):
        el = f.element(m)
        assert el + (-el) == additive_identity
Exemplo n.º 11
0
def test_sampler_heterodim(dims, size):
    it = int_sampler(dims, limit=size)

    assert sum(1 for _ in it) == size