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
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)
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, )}
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
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
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)
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
def test_element_roundtrip(n): f = finite.Field(n) for m in int_sampler(n): el = f.element(m) assert int(el) == m
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)
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
def test_sampler_heterodim(dims, size): it = int_sampler(dims, limit=size) assert sum(1 for _ in it) == size