import random
import math
random.seed("regional-2016/day02/division")

class TestGen:
    def __init__(self):
        self.test_number = 0
        self.subtask = ""
        self.desc_file = open("../tests.desc", "w")

    def generate_samples(self):             
        self.print_test(6, "n = 6", "preliminary")

    def generate_first_group_of_tests(self, folder):
        self.subtask = folder
        for n in range(7, 16):
            self.print_test(n, "n = %d" % n)
        for n in range(50, 39, -1):
            self.print_test(n, "n = %d" % n)

    def generate_second_group_of_tests(self, folder):
        self.subtask = folder
        for it in range(10):
            x = random.randint(1000, 2000)
            self.print_test(x, "n = %d" % x)
        for it in range(5):
            x = 2000 - it
            self.print_test(x, "n = %d" % x)
                       
    def generate_third_group_of_tests(self, folder):
        self.subtask = folder
        for it in range(10):
            x = random.randint(30000, 40000)
            self.print_test(x, "n = %d" % x)
        for it in range(5):
            x = 40000 - it
            self.print_test(x, "n = %d" % x)

    def generate_fourth_group_of_tests(self, folder):
        self.subtask = folder
        self.subtask = folder
        for it in range(10):
            x = random.randint(9 * 10**8, 10 * 10**8)
            self.print_test(x, "n = %d" % x)
        for it in range(5):
            x = 10**9 - it
            self.print_test(x, "n = %d" % x)
    
    def print_test(self, n, description, dirname="tests"):
        self.test_number += 1
        print("Generating test %d" % self.test_number)
        
        folder = "../" + dirname + self.subtask
        test_name = folder + "/{0:0=2d}".format(self.test_number)
        if not os.path.exists(folder):
            os.makedirs(folder)
        test_file = open(test_name, "w")
        print(n, file=test_file)
        print(test_name + " " + description, file=self.desc_file)
        test_file.close()

    def generate_all_tests(self):
        self.generate_samples() 
        self.test_number = 0
        
        #First group of tests
        self.generate_first_group_of_tests("/subtask1")
        
        #Second group of tests
        self.generate_second_group_of_tests("/subtask2")
        
        #Third group of tests
        self.generate_third_group_of_tests("/subtask3")

        #Fourth group of tests
        self.generate_fourth_group_of_tests("/subtask4")


import os, shutil
if os.path.exists('../tests'):
    shutil.rmtree('../tests', ignore_errors=False, onerror=None)
if os.path.exists('../preliminary'):
    shutil.rmtree('../preliminary', ignore_errors=False, onerror=None)
try:
    os.makedirs('../tests')
    os.makedirs('../preliminary')
except OSError:
    pass

import time
start = time.time()
writer = TestGen()
writer.generate_all_tests()
finish = time.time()
print('All tests were generated. Elapsed time: ', finish - start)
