Skip to content

test_brief.py

The file math/tests/test_brief.py. 310 lines, 25 definitions.

One test for each ([DOĞRULANDI], verified) number in the brief. Section numbers follow docs/PROJE-BRIFI.md.

Code, comments and printed output are in Turkish. The explanations are translated.

Top of file: imports

"""Brifteki ([DOĞRULANDI]) her sayı için bir test. Bölüm numaraları docs/PROJE-BRIFI.md'ye göre."""from math import gcdimport networkx as nximport pytestfrom tonnetz import (    MAKAMS_53,    TETS,    Chord,    L,    P,    R,    Tonnetz,    apply,    chain,    chain_chords,    chain_count_table,    chain_index,    chain_span,    cycle,    cycle_length,    diameter,    distances_from,    formula_range,    kernel_basis,    moving_step,    parse_steps,    pitch_at,    representatives,    scale,    translation,)

TABLE

constant
# Brif 4.2 tablosu: N -> ((a, b, a+b), (P, L, R adımı), (PL, PR, LR çevrimi))TABLE = {    12: ((3, 4, 7), (1, 1, 2), (6, 8, 24)),    19: ((5, 6, 11), (1, 2, 3), (38, 38, 38)),    31: ((8, 10, 18), (2, 3, 5), (62, 62, 62)),    41: ((11, 13, 24), (2, 4, 6), (82, 82, 82)),    53: ((14, 17, 31), (3, 5, 8), (106, 106, 106)),}

T53

constant
T53 = Tonnetz.tet(53)

test_tets_match_table

function
# --- 3.1, 3.2 -------------------------------------------------------------def test_tets_match_table():    assert set(TETS) == set(TABLE)

test_intervals

function
@pytest.mark.parametrize("N", TETS)def test_intervals(N):    t = Tonnetz.tet(N)    assert (t.a, t.b, t.fifth) == TABLE[N][0]

test_53_counts

function
def test_53_counts():    assert len(T53.nodes) == 53    assert len(T53.edges) == 159    assert len(T53.triangles) == 106    assert T53.pitches(T53.major(0)) == {0, 17, 31}    assert T53.pitches(T53.minor(0)) == {0, 14, 31}

test_euler_characteristic

function
@pytest.mark.parametrize("N", TETS)def test_euler_characteristic(N):    t = Tonnetz.tet(N)    assert len(t.edges) == 3 * N    assert len(t.triangles) == 2 * N    assert t.euler_characteristic == 0

test_surface_is_closed

function
@pytest.mark.parametrize("N", TETS)def test_surface_is_closed(N):    """Her kenar tam iki üçgene, her düğüm tam altı üçgene aittir."""    t = Tonnetz.tet(N)    for e in t.edges:        assert sum(e <= tri for tri in t.triangles) == 2    for x in t.nodes:        assert sum(x in tri for tri in t.triangles) == 6        assert len(t.neighbors(x)) == 6

test_degenerate_parameters_rejected

function
def test_degenerate_parameters_rejected():    with pytest.raises(ValueError):        Tonnetz(12, 4, 4)  # a = b    with pytest.raises(ValueError):        Tonnetz(12, 2, 4)  # a + b = N/2    with pytest.raises(ValueError):        Tonnetz(12, 2, 8)  # ebob(a, b, N) > 1

test_plr_involutions_share_an_edge

function
# --- 3.3 ------------------------------------------------------------------@pytest.mark.parametrize("N", TETS)def test_plr_involutions_share_an_edge(N):    t = Tonnetz.tet(N)    for c in t.chords:        for op in (P, L, R):            d = op(t, c)            assert d.kind != c.kind            assert op(t, d) == c            assert len(t.pitches(c) & t.pitches(d)) == 2        assert len({P(t, c), L(t, c), R(t, c)}) == 3

test_plr_53_definitions

function
def test_plr_53_definitions():    for x in range(53):        assert P(T53, Chord("M", x)) == Chord("m", x)        assert L(T53, Chord("M", x)) == Chord("m", (x + 17) % 53)        assert R(T53, Chord("M", x)) == Chord("m", (x - 14) % 53)        assert R(T53, Chord("M", x)) == Chord("m", (x + 39) % 53)

test_moving_steps

function
@pytest.mark.parametrize("N", TETS)def test_moving_steps(N):    t = Tonnetz.tet(N)    assert tuple(moving_step(t, op) for op in "PLR") == TABLE[N][1]

test_r_step_is_p_plus_l

function
@pytest.mark.parametrize("N", range(5, 80))def test_r_step_is_p_plus_l(N):    try:        t = Tonnetz.tet(N)    except ValueError:        pytest.skip("N için geçerli Tonnetz yok")    assert moving_step(t, "R") == moving_step(t, "P") + moving_step(t, "L")

test_compositions_53

function
def test_compositions_53():    # Önce soldaki uygulanır: PL = önce P, sonra L.    assert apply(T53, "PL", Chord("M", 0)) == L(T53, P(T53, Chord("M", 0)))    assert translation(T53, "PL") == (-17) % 53    assert translation(T53, "PR") == 14    assert translation(T53, "LR") == 31

test_chain_coordinate_53

function
# --- 3.4 ------------------------------------------------------------------def test_chain_coordinate_53():    assert gcd(31, 53) == 1    assert chain_index(T53, 31) == 1    assert chain_index(T53, 17) == (-8) % 53    assert chain_index(T53, 14) == 9    for k in range(53):        x = (31 * k) % 53        assert {chain_index(T53, p) for p in T53.pitches(T53.major(x))} == {            k,            (k - 8) % 53,            (k + 1) % 53,        }        assert {chain_index(T53, p) for p in T53.pitches(T53.minor(x))} == {            k,            (k + 9) % 53,            (k + 1) % 53,        }    assert chain_span(T53) == 9

test_aeu_chain_has_30_chords_for_every_k0

function
# --- 4.1 ------------------------------------------------------------------def test_aeu_chain_has_30_chords_for_every_k0():    for k0 in range(53):        cs = chain_chords(T53, 24, k0)        assert len(chain(T53, 24, k0)) == 24        assert sum(c.kind == "M" for c in cs) == 15        assert sum(c.kind == "m" for c in cs) == 15

test_t1_formula_range_53

function
def test_t1_formula_range_53():    table = chain_count_table(T53)    assert all(r["k0_independent"] for r in table)    for r in table:        n = r["n"]        if n < 9:            assert r["total"] == 0        elif n <= 45:            assert r["total"] == 2 * (n - 9)            assert r["major"] == r["minor"] == n - 9        else:            assert r["total"] > 2 * (n - 9)    assert table[53]["total"] == 106    assert formula_range(T53) == (9, 45)

test_cycle_lengths_table

function
# --- 4.2 ------------------------------------------------------------------@pytest.mark.parametrize("N", TETS)def test_cycle_lengths_table(N):    t = Tonnetz.tet(N)    assert tuple(cycle_length(t, w) for w in ("PL", "PR", "LR")) == TABLE[N][2]

test_cycle_length_formula

function
@pytest.mark.parametrize("N", range(5, 80))def test_cycle_length_formula(N):    try:        t = Tonnetz.tet(N)    except ValueError:        pytest.skip("N için geçerli Tonnetz yok")    assert cycle_length(t, "PL") == 2 * N // gcd(N, t.b)    assert cycle_length(t, "PR") == 2 * N // gcd(N, t.a)    assert cycle_length(t, "LR") == 2 * N // gcd(N, t.fifth)

test_prime_n_gives_hamiltonian_cycles

function
@pytest.mark.parametrize("N", [19, 31, 41, 53])def test_prime_n_gives_hamiltonian_cycles(N):    t = Tonnetz.tet(N)    for w in ("PL", "PR", "LR"):        cyc = cycle(t, w, Chord("M", 0))        assert len(cyc) == len(set(cyc)) == 2 * N        assert set(cyc) == set(t.chords)

test_cycle_does_not_depend_on_start

function
def test_cycle_does_not_depend_on_start():    t = Tonnetz.tet(12)    for w in ("PL", "PR", "LR"):        assert {len(cycle(t, w, c)) for c in t.chords} == {cycle_length(t, w)}

test_diameter

function
# --- 4.3 ------------------------------------------------------------------@pytest.mark.parametrize("N,expected", [(12, 2), (19, 3), (31, 4), (41, 4), (53, 5)])def test_diameter(N, expected):    t = Tonnetz.tet(N)    assert diameter(t) == expected    g = nx.Graph(tuple(e) for e in t.edges)    assert nx.diameter(g) == expected    assert distances_from(t, 0) == [nx.shortest_path_length(g, 0, x) for x in range(N)]

test_hexagonal_bound

function
@pytest.mark.parametrize("N", TETS)def test_hexagonal_bound(N):    d = diameter(Tonnetz.tet(N))    assert N <= 3 * d * d + 3 * d + 1

test_makam_steps_close_the_octave

function
# --- 4.4 ------------------------------------------------------------------@pytest.mark.parametrize("key", MAKAMS_53)def test_makam_steps_close_the_octave(key):    steps = parse_steps(MAKAMS_53[key]["steps"])    assert sum(steps) == 53    assert len(set(scale(T53, 0, steps))) == 7

test_makam_steps_match_brief

function
def test_makam_steps_match_brief():    assert parse_steps(MAKAMS_53["cargah"]["steps"]) == [9, 9, 4, 9, 9, 9, 4]    assert parse_steps(MAKAMS_53["buselik"]["steps"]) == [9, 4, 9, 9, 4, 9, 9]    assert parse_steps(MAKAMS_53["kurdi"]["steps"]) == [4, 9, 9, 9, 4, 9, 9]    assert parse_steps(MAKAMS_53["rast"]["steps"]) == [9, 8, 5, 9, 9, 8, 5]    assert parse_steps(MAKAMS_53["ussak"]["steps"]) == [8, 5, 9, 9, 4, 9, 9]    assert parse_steps(MAKAMS_53["hicaz"]["steps"]) == [5, 12, 5, 9, 4, 9, 9]

test_lattice

function
# --- kafes ----------------------------------------------------------------@pytest.mark.parametrize("N", TETS)def test_lattice(N):    t = Tonnetz.tet(N)    v1, v2 = kernel_basis(t)    assert pitch_at(t, *v1) == 0 and pitch_at(t, *v2) == 0    assert v1[0] * v2[1] - v1[1] * v2[0] == N    reps = representatives(t)    assert [pitch_at(t, i, j) for i, j in reps] == list(range(N))    # Kafeste (i, j), (i+1, j), (i, j+1) majör, (i, j), (i+1, j), (i+1, j-1) minör üçgendir.    x = pitch_at(t, 2, 3)    assert {x, pitch_at(t, 3, 3), pitch_at(t, 2, 4)} == t.pitches(t.major(x))    assert {x, pitch_at(t, 3, 3), pitch_at(t, 3, 2)} == t.pitches(t.minor(x))

test_aeu_region_is_connected

function
# --- 4.1 ve 4.4: bölge ve makam bulguları (V1 ve V2 altında) ---------------def test_aeu_region_is_connected():    for k0 in range(53):        pitches = chain(T53, 24, k0)        assert len(T53.node_components(pitches)) == 1        assert len(T53.chord_components(chain_chords(T53, 24, k0))) == 1

test_makam_chord_counts_and_shapes

function
def test_makam_chord_counts_and_shapes():    from tonnetz import analyze, canonical_shape    info = {k: analyze(T53, 0, parse_steps(m["steps"])) for k, m in MAKAMS_53.items()}    counts = {k: (v["major_count"], v["minor_count"]) for k, v in info.items()}    assert counts == {        "cargah": (0, 0),        "buselik": (0, 0),        "kurdi": (0, 0),        "rast": (2, 1),        "ussak": (1, 0),        "hicaz": (1, 1),    }    for k in ("cargah", "buselik", "kurdi"):        assert info[k]["chain_arc_length"] == 7    assert all(v["chain_arc_length"] <= 24 for v in info.values())    shapes = {k: canonical_shape(T53, v["pitches"]) for k, v in info.items()}    assert shapes["cargah"] == shapes["buselik"] == shapes["kurdi"]    assert len({shapes[k] for k in ("cargah", "rast", "ussak", "hicaz")}) == 4

Every number on this site comes from data produced by the Python core and is checked by tests.

TÜBİTAK 2204-A mathematics project