import random
import os
import shutil


if os.path.exists('../tests'):
    print('Directory "tests" already exists')
    shutil.rmtree('../tests', ignore_errors=False, onerror=None)

try:
    os.makedirs('../tests')
except OSError:
    pass

random.seed(239)

test_number = 1
def write_test(n, m):
    global test_number
    f = "../tests/" + "0" * (2 - len(str(test_number))) + str(test_number)

    outp = open(f, "w")

    outp.write(str(n) + " " + str(m) + "\n")

    outp.close()
    
    print("Test #" + str(test_number) + " has been generated")   

    test_number += 1

maxn = 10 ** 11

def gen_almost_dividing_test(delta, maxm):
    m = random.randint(int(maxm * 0.9), maxm)
    n = m * random.randint(1, maxn // m)
    n = list(str(n))
    for i in range(delta):
        n[random.randint(0, len(n) - 1)] = chr(ord('0') + random.randint(0, 9))
    write_test(int("".join(n)), m)


def gen_test_with_lengths(ln, lm):
    n = random.randint(1, 9)
    for i in range(ln - 1):
        n = n * 10 + random.randint(0, 9)
    m = random.randint(1, 9)
    for i in range(lm - 1):
        m = m * 10 + random.randint(0, 9)

    write_test(n, m)


write_test(123, 10)
write_test(123, 141)
write_test(9, 123)
write_test(12, 123)
write_test(123, 11)
write_test(100, 21)
write_test(52, 70)
write_test(40, 67)
write_test(35, 97)

write_test(11, 121)
write_test(10 ** 11 - 1, 10 ** 11)
write_test(random.randint(10 ** 5, 10 ** 6 - 1), random.randint(10 ** 6, 10 ** 7 - 1))
write_test(random.randint(10 ** 4, 10 ** 5 - 1), random.randint(10 ** 5, 10 ** 6 - 1))

write_test(76008015591, 54118079487)
write_test(42174026399, 48059235100)
write_test(70021982631, 95982437988)
write_test(4, 51)
write_test(4, 29)
write_test(7, 21)
write_test(9, 71)
write_test(3, 93)
write_test(61836938773, 25145)
write_test(97023009940, 97419)
write_test(57249316692, 42852)
write_test(89907621581, 234061)
write_test(73801319401, 22747)
write_test(11190155370, 58035)
write_test(76889978500, 160917)

for i in range(6):
    gen_almost_dividing_test(i // 2, 100)

for i in range(6):
    gen_almost_dividing_test(i // 2, 100000)

for i in range(6):
    gen_almost_dividing_test(i // 2, maxn)


for i in range(1, 12):
    gen_test_with_lengths(11, i)
