import java.io.*;
import java.util.*;

public class TestGen {

        final static Random rnd = new Random(213);

        public static void main(String[] args) throws Exception {
                new TestGen().generate();
        }
        
        int MAX = 100000;
        int MAX_LEN = 1000000;

        PrintWriter out;
        int countTests = 0;
        
        char genNextChar() {
                String can = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm.,:?1234567890 ";
                return can.charAt(rnd.nextInt(can.length()));
        }
        
        char genChar() {
                String can = ".,:? ";
                return can.charAt(rnd.nextInt(can.length()));
        }
        
        String gen(int[] a, int maxLen) {
                int need = -1;
                int n = a.length;
                for (int i = 0; i < n; i++) {
                        need += 2 + Integer.toString(a[i]).length();
                }
                if (need > maxLen) {
                        throw new AssertionError("Need more length");
                }
                int free = maxLen - need;
                char[] res = new char[maxLen];
                int it= 0;
                for (int i = 0; i < n; i++) {
                        while (free > 0 && rnd.nextBoolean()) {
                                free--;
                                res[it++] = genNextChar();
                        }
                        res[it++] = '^';
                        String number = Integer.toString(a[i]);
                        for (int j=0; j < number.length(); j++) {
                                res[it++] = number.charAt(j);
                        }
                        res[it++] = genChar();
                }
                char[] tmp = new char[it];
                for (int i = 0; i < tmp.length; i++)
                    tmp[i] = res[i];
                return new String(tmp);
        }

        int[] genRandomArray(int n) {
                int[] res = new int[n];
                for (int i = 0; i < n; i++) {
                        res[i] = 1 + rnd.nextInt(10000);
                }
                return res;
        }
        
        int[] merge(int[] a, int [] b) {
                int[] res = new int[a.length + b.length];
                int it1 =0, it2 = 0;
                int it = 0;
                while (it1 < a.length && it2 < b.length) {
                        if (rnd.nextBoolean()) {
                                res[it++] = a[it1++];
                        } else {
                                res[it++] = b[it2++];
                        }
                }
                while (it1 < a.length) {
                        res[it++]=a[it1++];
                }
                while (it2 < b.length ) {
                        res[it++] = b[it2++];
                }
                return res;
        }
        
        int[] genNaturalumbers(int n) {
                int[]res = new int[n];
                for (int i = 0; i < n; i ++) {
                        res[i] = i + 1;
                }
                return res;
        }
        
        int[] genSmartTest(int n) {
                if (n < 2)
                        throw new AssertionError("n must be > 1");
                int left = rnd.nextInt(n - 1);
                int right = n - left;
                int[] first = genRandomArray(left);
                int[] second = genNaturalumbers(right);
                return merge(first, second);
        }
        
        String genSpecial(int maxLen) {
                char[] res = new char[maxLen];
                int it = 0;
                while (it < maxLen - 10) {
                        if (rnd.nextBoolean()) {
                                res[it++] = genNextChar();
                        } else {
                                res[it++] = '^';
                                int len = 1 + rnd.nextInt(Math.min(50, maxLen - it - 5));
                                for (int j = 0; j < len; j++) 
                                        res[it++] = (char)('1'+rnd.nextInt(9));
                                res[it++] = genChar();
                        }
                }
				char[] tmp = new char[it];
                for (int i = 0; i < tmp.length; i++)
                    tmp[i] = res[i];
                return new String(tmp);
        }

        void generate() {
                printTest(sample());

                for (int i = 0; i < 10; i++) {
                        printTest(gen(genSmartTest(100), MAX_LEN));
                }
                
                for (int i = 0; i < 10; i++) {
                        printTest(gen(genSmartTest(MAX), MAX_LEN));
                }
                
                for (int i = 0; i < 10; i++) {
                        printTest(gen(genRandomArray(MAX), MAX_LEN));
                }
                
                for (int i = 0; i < 5; i++) {
                        printTest(genSpecial(MAX_LEN));
                }
        }

        String sample() {
                return "sample: 2 plus 2 equals 2^2.^1 easy,^2 isnt it? 1^1 equals 1.";
        }

        void printTest(String s) {
                try {
                        countTests++;
                        String fileName = "../tests/";
                        fileName += String.format("%02d", countTests);
                        out = new PrintWriter(fileName);

			if (s.charAt(s.length() - 1) == '.') {
                        	out.println(s);
			} else {
				out.print(s);
				out.println(".");
			}

                        System.out.println("Done printing test #" + countTests);
                        out.close();
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                }
        }
}
