import random
random.seed("grievous")

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

    def generate_samples(self):
        #2 tests
        self.print_test(2, [[1, 2], [3, 4]])
        self.print_test(4, [[1, 4], [2, 5], [3, 6], [4, 7]])

    def generate_min_tests(self):
        #5 tests
        self.print_test(1, [[1, 2]])
        self.print_test(1, [[4, 5]])
        self.print_test(2, [[5, 6], [1, 5]])
        self.print_test(2, [[1, 4], [3, 5]]) 
        self.print_test(2, [[3, 4], [1, 4]])

    def generate_hand_tests(self):
        #5 tests
        self.print_test(3, [[2, 4], [1, 3], [3, 4]])
        self.print_test(3, [[3, 4], [2, 5], [4, 6]])
        self.print_test(3, [[10**9 - 3, 10**9 - 2], [10**9 - 4, 10**9 - 1], [10**9 - 2, 10**9]])
        self.print_test(4, [[3, 6], [1, 5], [4, 8], [7, 8]])
        self.print_test(4, [[2, 4], [6, 9], [8, 10], [5, 7]])

    def generate_max_tests(self, maxn, maxvalue):
        #13 tests
        a = []
        for i in range(1, maxn + 1):
            a.append([i * 2, i * 2 + 1])
        self.print_test(maxn, a)
        random.shuffle(a)
        self.print_test(maxn, a)

        a = []
        for i in range(maxn):
            a.append([maxvalue - i * 2 - 1, maxvalue - i * 2])
        self.print_test(maxn, a)
        random.shuffle(a)
        self.print_test(maxn, a)

        a = []
        for i in range(maxn):
            a.append([i + 1, maxvalue])
        self.print_test(maxn, a)
        random.shuffle(a)
        self.print_test(maxn, a)

        a = []
        for i in range(maxn):
            a.append([maxvalue - i - 1, maxvalue])
        self.print_test(maxn, a)
        random.shuffle(a)
        self.print_test(maxn, a)

        for it in range(5):
            a = []
            for i in range(maxn):
                l = random.randint(1, maxvalue - 1)
                r = l + random.randint(1, maxvalue - l)
                a.append([l, r])
            self.print_test(maxn, a)

        
    def generate_random_test(self, maxn, maxvalue):
        n = random.randint(1, maxn)
        a = []
        for i in range(n):
            l = random.randint(1, maxvalue - 1)
            r = l + random.randint(1, maxvalue - l)
            a.append([l, r])
        self.print_test(n, a)
        
    def generate_small_test(self):
        self.generate_random_test(random.randint(5, 10), random.randint(5, 20))

    def generate_medium_test(self):
        self.generate_random_test(random.randint(500, 1000), random.randint(500, 2000))

    def print_test(self, n, a):
        assert(len(a) == n)
        self.test_number += 1
        test_name = "../tests/" + "{0:0=2d}".format(self.test_number)
        test_file = open(test_name, "w")
        print(n, file=test_file)
        print('\n'.join(' '.join([str(element[0]), str(element[1])]) for element in a), file=test_file)
        test_file.close()

    def generate_all_tests(self):
        maxn = 10**5
        maxvalue = 10**9
        self.generate_samples()                      #2 tests
        self.generate_min_tests()                    #5 tests
        self.generate_hand_tests()                   #5 tests
        for i in range(10):                          #10 tests
          self.generate_small_test()
        for i in range(10):                          #10 tests
          self.generate_medium_test()
        for i in range(10):                          #10 tests
          self.generate_random_test(maxn, maxvalue)
        self.generate_max_tests(maxn, maxvalue)      #13 tests

import os, shutil
if os.path.exists('../tests'):
    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)
