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

public class TestGen {

	static int done = 0;

	static final Random rnd = new Random(556668949239L);

	static int rand(int n) {
		if (n == 0)
			return 1;
		return rnd.nextInt(n);
	}

	static final int MAXN2 = 1000;
	static final int MAXN1 = 100;

	static ArrayList<String> data = new ArrayList<String>();

	static void print() throws IOException {
		done++;
		System.err.println("Generation test #" + done);
		PrintWriter out;
		if (done < 10)
			out = new PrintWriter("../tests/0" + String.valueOf(done));
		else
			out = new PrintWriter("../tests/" + String.valueOf(done));
		for (int i = 0; i < data.size(); i++)
			out.println(data.get(i));
		data.clear();
		out.close();
	}

	static void gen_sample() throws IOException {
		PrintWriter out;
		out = new PrintWriter("../preliminary/01");
		out.println("4 3");
		out.println("+-+");
		out.println("??-");
		out.println("?-?");
		out.println("++?");
		out.close();
	}

	static char chars[] = { '+', '-', '?' };

	static char randc() {
		return chars[rand(3)];
	}

	static String rands(int n) {
		String s = "";
		for (int i = 0; i < n; i++)
			s += randc();
		return s;
	}

	static String consts(int n, char c) {
		String s = "";
		for (int i = 0; i < n; i++)
			s += c;
		return s;
	}

	static void gen_rand(int n, int m) throws IOException {
		data.add(String.valueOf(n) + " " + String.valueOf(m));
		for (int i = 0; i < n; i++)
			data.add(rands(m));
		print();
	}

	static void gen_const(int n, int m, char c) throws IOException {
		data.add(String.valueOf(n) + " " + String.valueOf(m));
		for (int i = 0; i < n; i++)
			data.add(consts(m, c));
		print();
	}

	static void gen_mid(int n, int m) throws IOException {
		data.add(String.valueOf(n) + " " + String.valueOf(m));
		for (int i = 0; i < n; i++)
			if (i % 2 == 0)
				data.add(rands(m));
			else
				data.add(consts(m, randc()));
		print();
	}

	static void gen_spec(int n, int m) throws IOException {
		data.add(String.valueOf(n) + " " + String.valueOf(m));
		int a = rand(n);
		int b = rand(m);
		for (int i = 0; i < n; i++) {
			if(i == a)
			{
				data.add(consts(m, '+'));
				continue;
			}
			String s = rands(m);
			s = s.substring(1, b) + "+-" + s.substring(b + 1);
			data.add(s);
		}
		print();
	}

	public static void main(String[] args) throws IOException {
		new File("../preliminary").mkdir();
		new File("../tests").mkdir();

		gen_sample();

		done = 0;
		
		gen_rand(1, 1);
		
		for(int i = 0; i < 4; i++)
			gen_rand(rand((i + 1) * 10) + 1, rand((i + 1) * 10) + 1);
		
		gen_rand(1, 100);
		gen_rand(100, 1);
		
		gen_rand(rand(10) + 10, rand(10) + 10);
		gen_mid(rand(10) + 10, rand(10) + 10);
		gen_const(rand(10) + 10, rand(10) + 10, '+');
		gen_const(rand(10) + 10, rand(10) + 10, '-');
		gen_const(rand(10) + 10, rand(10) + 10, '?');
		gen_spec(rand(10) + 10, rand(10) + 10);
		
		gen_rand(rand(50) + 50, rand(50) + 50);
		gen_mid(rand(50) + 50, rand(50) + 50);
		gen_const(rand(50) + 50, rand(50) + 50, '+');
		gen_const(rand(50) + 50, rand(50) + 50, '-');
		gen_const(rand(50) + 50, rand(50) + 50, '?');
		gen_spec(rand(50) + 50, rand(50) + 50);
		
		gen_rand(100, 100);
		gen_mid(100, 100);
		gen_const(100, 100, '+');
		gen_const(100, 100, '-');
		gen_const(100, 100, '?');
		gen_spec(100, 100);
		
		//big tests
		for(int i = 0; i < 5; i++)
			gen_rand(rand((i + 1) * 100) + 100, rand((i + 1) * 100) + 100);
		
		gen_rand(1, 1000);
		gen_rand(1000, 1);
		
		gen_rand(rand(100) + 100, rand(100) + 100);
		gen_mid(rand(100) + 100, rand(100) + 100);
		gen_const(rand(100) + 100, rand(100) + 100, '+');
		gen_const(rand(100) + 100, rand(100) + 100, '-');
		gen_const(rand(100) + 100, rand(100) + 100, '?');
		gen_spec(rand(100) + 100, rand(100) + 100);
		
		gen_rand(rand(500) + 500, rand(500) + 500);
		gen_mid(rand(500) + 500, rand(500) + 500);
		gen_const(rand(500) + 500, rand(500) + 500, '+');
		gen_const(rand(500) + 500, rand(500) + 500, '-');
		gen_const(rand(500) + 500, rand(500) + 500, '?');
		gen_spec(rand(500) + 500, rand(500) + 500);
		
		gen_rand(1000, 1000);
		gen_mid(1000, 1000);
		gen_const(1000, 1000, '+');
		gen_const(1000, 1000, '-');
		gen_const(1000, 1000, '?');
		gen_spec(1000, 1000);
		
		
		return;
	}
}
