#!/usr/bin/env python3

lcg_seed = 301703
def lcg_rand( a, b ):
    global lcg_seed
    lcg_seed = lcg_seed * 239017 + 666
    lcg_seed &= 0xffffffffffffffff
    lcg_middle = (lcg_seed >> 16) & 0xffffffff
    return a + lcg_middle % (b - a + 1)

test = 0
def output_test( field, n ):
    global test
    test += 1
    with open("../tests/%02d" % test, 'w') as f:
        h = len(field)
        assert h >= 1
        w = len(field[0])
        print("%d %d %d" % (w, h, n), file=f)
        for line in field:
            assert len(line) == w
            print(line, file=f)

manual = [
    (["AAATA",
      "VAATA",
      "AAAAT"], 6),
    (["TVT"], 2),
    (["VT"], 1),
    (["VT"], 10),
    (["VAT"], 1),
    (["VAT"], 10),
    (["VTT"], 1),
    (["VTT"], 10)
]

def test_random( w, h ):
    n = lcg_rand(1, 1000)
    field = [
       ["A" for j in range(w)] for i in range(h)
    ]
    for x in range((w + h) * 5):
        i = lcg_rand(0, w - 1)
        j = lcg_rand(0, h - 1)
        if field[j][i] == 'A':
            field[j][i] = 'T' if x else 'V'
    return (["".join(line) for line in field], n)

def test_snake( w, h ):
    n = 1000
    field = [
       ["A" for j in range(w)] for i in range(h)
    ]
    assert h % 2 == 1 and w % 4 == 1
    j = 0
    d = 0
    while j < w:
        for i in range(0, h, 2):
            field[i][j] = 'T'
        j += 2
        d = h - 1 - d
        if j < w:
            field[d][j] = 'T'
        j += 2
    field[0][0] = 'V';
    if d == 0:
        field[1][w - 1] = 'T'
    else:
        field[h - 2][w - 1] = 'T'
    return (["".join(line) for line in field], n)

def test_coomb( w, h ):
    n = 1000
    field = [
       ["A" for j in range(w)] for i in range(h)
    ]
    field[0][0] = 'V'
    uw, uh, cnt = 0, 1, 1
    while uh < h - 1:
        uh += 1
        cnt += 1
        if cnt % 2 == 0:
            field[uh][uw] = 'T'
    while uw < w - 2:
        uw += 1
        cnt += 1
        if cnt % 2 == 0:
            field[uh][uw] = 'T'
    while uh > 0:
        uh -= 1
        cnt += 1
        if cnt % 2 == 0:
            field[uh][uw] = 'T'
    field[0][w - 2] = field[0][w - 1] = 'T'
    return (["".join(line) for line in field], n)
    

for x in manual:
    output_test(*x)
for x in sorted([
        (lcg_rand(1, 500), lcg_rand(1, 500)) for i in range(20)
    ], key=lambda x: sum(x)):
        output_test(*test_random(*x))
for x in [(9, 11), (13, 19), (29, 31), (61, 21), (17, 41), (61, 59), (33, 117)]:
    output_test(*test_snake(*x))
for x in sorted([
        (lcg_rand(1, 500), lcg_rand(1, 300)) for i in range(10)
    ], key=lambda x: sum(x)):
        output_test(*test_coomb(*x))


