from os import mkdir
from random import *
from collections import namedtuple

seed('amusement')

logger = lambda s : print (s)

rand = lambda n, p, c: [(randint(1, p), randint(1, c)) for i in range(n)]

Test = namedtuple('Test', ['n', 'm', 'pcap', 'scap', 'slen', 'sdist', 'speed'])

sample1 = Test(n=2, m=1, pcap=[], scap=[1], slen=[30], sdist=[2], speed=[[2]])
sample2 = Test(n=3, m=2, pcap=[1], scap=[2,2], slen=[10,10], sdist=[5,5], speed=[[2,2],[1,2]])

samples = [sample1, sample2]

# stress finds this tests worth noting
stress = [
    Test(n=4, m=4, pcap=[5, 3], scap=[2, 2, 5], slen=[10, 15, 13], sdist=[4, 11, 9], speed=[[9, 3, 10, 2], [8, 1, 7, 2], [4, 5, 1, 7]])
    , Test(n=3, m=2, pcap=[1], scap=[2, 2], slen=[10, 10], sdist=[5, 5], speed=[[2, 2], [1, 2]])
    , Test(n=2, m=1, pcap=[], scap=[1], slen=[30], sdist=[2], speed=[[2]])
    , Test(n=5, m=3, pcap=[1, 3, 4], scap=[2, 4, 3, 2], slen=[10, 10, 10, 10], sdist=[2, 2, 2, 2], speed=[[5, 1, 2], [6, 10, 3], [7, 3, 9], [8, 8, 4]])
    , Test(n=3, m=3, pcap=[1], scap=[1, 1], slen=[10, 10], sdist=[2, 2], speed=[[2, 1, 2], [1, 2, 2]])
    , Test(n=3, m=3, pcap=[1], scap=[4, 1], slen=[10, 10], sdist=[2, 2], speed=[[2, 5, 8], [7, 7, 5]])
    , Test(n=3, m=3, pcap=[1], scap=[4, 1], slen=[10, 10], sdist=[2, 2], speed=[[5, 7, 5], [3, 4, 2]])
    , Test(n=2, m=2, pcap=[], scap=[2], slen=[10], sdist=[2], speed=[[1, 10]])
    , Test(n=2, m=3, pcap=[], scap=[2], slen=[10], sdist=[2], speed=[[1, 10, 10]])
    , Test(n=3, m=2, pcap=[1], scap=[2, 1], slen=[10, 10], sdist=[2, 2], speed=[[10, 10], [1, 2]])
    , Test(n=2, m=2, pcap=[], scap=[1], slen=[10], sdist=[2], speed=[[5, 10]])
    , Test(n=2, m=1, pcap=[], scap=[1], slen=[14], sdist=[11], speed=[[3]])
    , Test(n=3, m=2, pcap=[1], scap=[2, 2], slen=[10, 10], sdist=[5, 5], speed=[[2, 2], [2, 2]])
    , Test(n=3, m=1, pcap=[1], scap=[1, 1], slen=[10, 10], sdist=[5, 5], speed=[[2], [2]])
][::-1]

ltos = lambda xs : " ".join(str(x) for x in xs)

class Printer:
    def __init__(self, path):
        self.path = "../" + path
        try:
            mkdir(self.path)
            logger("Creating directory %s"%self.path)
        except:
            pass
        self.tests = 0

    def print(self, t):
        self.tests += 1
        d = self.path
        try:
            mkdir(d)
            logger("Creating directory %s"%d)
        except:
            pass
        p = d + "/" + "%02d"%self.tests
        logger("Printing test %s"%p)
        out = open(p, "w")
        print(t.n, t.m, file=out)
        print(ltos(t.pcap), file=out)
        print(ltos(t.scap), file=out)
        print(ltos(t.slen), file=out)
        print(ltos(t.sdist), file=out)
        for sx in t.speed:
            print(ltos(sx), file=out)

tests = Printer("tests")

for t in samples:
    tests.print(t)

for t in stress:
    tests.print(t)

def rand_test(n, m, w=100):
    pcap = [randint(1, m) for i in range(n - 2)]
    scap = [randint(1, n) for i in range(n - 1)]
    slen = [randint(1, w) for i in range(n - 1)]
    sdist = [randint(1, slen[i]) for i in range(n - 1)]
    speed = [[randint(1, w) for j in range(m)] for i in range(n - 1)]
    return Test(n=n, m=m, pcap=pcap, scap=scap, slen=slen, sdist=sdist, speed=speed)

for n in [2, 5, 10, 20, 50, 100]:
    for m in [2, 5, 10, 20, 50, 100]:
        tests.print(rand_test(n, m))
