import numpy as np import pytest from theory.code.structured_sources import checked_quadruple, compose, controls, starter, mixed_product_controls def test_retained_starters_compose_before_using_fixed_design(): report = controls() assert report['input_widths'] == [3, 7] assert report['output_width'] == 42 assert report['conditional_fixed_T631_endpoint_order'] == 106008 assert report['endpoint_order_improvement_factor'] == 631 literal = np.array([[[1 if x == '+' else -1 for x in row] for row in matrix] for matrix in report['literal_output_matrices_pm']]) checked_quadruple(literal) def test_three_sources_compose_and_symmetric_inputs_stay_commuting(): base = starter(5) result = compose(compose(base, base), base) assert result.shape == (4, 108, 108) for i, a in enumerate(result): assert np.array_equal(a, a.T) for b in result[:i]: assert np.array_equal(a @ b, b @ a) def test_nonsymmetric_amicable_inputs_produce_full_sign_endpoint(): left, right = starter(5), starter(13) left = left[:, [1, 2, 0], :] assert any(not np.array_equal(x, x.T) for x in left) result = compose(left, right) # Quaternion coefficient design of type(1,1,1,1), independently assembled. a,b,c,d = result endpoint = np.block([[a,b,c,d],[-b,a,-d,c],[-c,d,a,-b],[-d,-c,b,a]]) assert np.all(abs(endpoint) == 1) assert np.array_equal(endpoint @ endpoint.T, 168 * np.eye(168, dtype=np.int64)) def test_corrupt_source_and_replay_budget_are_rejected_before_product_allocation(monkeypatch): base = starter(5) bad = base.copy(); bad[0,0,0] *= -1 with pytest.raises(ValueError, match='Gram|amicability'): compose(bad, base) def forbidden(*args): raise AssertionError('budget gate came after allocation') monkeypatch.setattr(np, 'kron', forbidden) monkeypatch.setattr('theory.code.structured_sources.checked_quadruple', forbidden) with pytest.raises(ValueError, match='budget'): compose(base, base, maximum_order=17) with pytest.raises(ValueError, match='positive integer'): compose(base, base, maximum_order=True) def test_float_arrays_are_not_exact_source_certificates(): with pytest.raises(ValueError, match='exact integers'): checked_quadruple(starter(5).astype(float)) def test_mixed_input_choices_avoid_repeating_t_and_compare_known_baseline(): reports = mixed_product_controls() assert [r['mixed_product_order'] for r in reports[:2]] == [272592, 817776] assert [r['improvement_factor_over_that_classical_expression'] for r in reports] == [1, 2, 2] assert reports[-1]['mixed_product_order'] == 631 * 6**12 // 8 for record in reports: k = len(record['input_orders']) assert record['all_T_scaled_inputs_order'] == record['mixed_product_order'] * 631**(k-1)