import random
import math
random.seed("basic04")

class TestGen:
    def __init__(self):
        self.test_number = 0

    def generate_samples(self):
        self.print_test("abacaba")
        self.print_test("abab");

    def generate_min_tests(self):
        self.print_test("a")
        self.print_test("b")
        self.print_test("z")
        self.print_test("aa")
        self.print_test("ab")
        self.print_test("zz")
        self.print_test("zy")
        self.print_test("bb")
                                                                    
    def generate_hand_tests(self):
        pass

    def generate_random_string(self, maxn, alphabet):
        n = maxn
        s = ""
        for i in range(n):
            s += chr(ord('a') + random.randint(0, alphabet - 1))
        return s

    def generate_random_test(self, maxn):
        self.print_test(self.generate_random_string(maxn, 26))
        alphabet = "abcdefghijklmnopqrstuvwxyz"
        groups = random.randint(1, 5)
        strings = ["" for i in range(groups)]
        pos = 0
        for i in range(groups - 1):
            newpos = random.randint(pos, 26 - groups + i)
            strings[i] = alphabet[pos:(newpos + 1)]
            pos = newpos + 1
        strings[groups - 1] = alphabet[pos:25]
        s = ""
        for i in range(maxn // 26):
            random.shuffle(strings)
            for j in range(groups):
                s += str(strings[j])
        self.print_test(s)

    def generate_max_tests(self, maxn):
        self.print_test("a" * maxn)
        self.print_test("z" * maxn)
        
        alphabet = list("abcdefghijklmnopqrstuvwxyz")
        for t in range(10):
            length = random.randint(1, 20)
            random.shuffle(alphabet)
            s = ""
            for i in range(length):
                s += str(alphabet[i])
            ss = s * (maxn // len(s))
            for i in range(maxn - len(ss)):
                if random.randint(1, 2) == 1:
                    ss = alphabet[random.randint(20, 25)] + ss
                else:
                    ss = ss + alphabet[random.randint(20, 25)]
            self.print_test(ss)

    def print_test(self, s):
        self.test_number += 1
        print('Generating test %d' % self.test_number)
        test_name = "../tests/" + "{0:0=2d}".format(self.test_number)
        test_file = open(test_name, "w")
        anss = ""
        for c in s:
            anss += c
        print(anss, file=test_file)
        test_file.close()

    def generate_all_tests(self):
        maxn = 10**5

        self.generate_samples()
        self.generate_hand_tests()
        self.generate_min_tests()
        
        for i in range(20):
            self.generate_random_test(maxn)
        self.generate_max_tests(maxn)        

import os, shutil
if os.path.exists('../tests'):
    print('Directory tests is already exists')
    shutil.rmtree('../tests', ignore_errors=False, onerror=None)
try:
    os.makedirs('../tests')
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)