001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.synth.sql;
007:
008: import java.sql.Date;
009: import java.sql.Time;
010: import java.sql.Timestamp;
011: import java.util.Random;
012:
013: /**
014: * A random data generator class.
015: */
016: public class RandomGen {
017: private Random random = new Random();
018:
019: // private TestSynth config;
020:
021: public RandomGen(TestSynth config) {
022: // this.config = config;
023: random.setSeed(12);
024: }
025:
026: public int getInt(int max) {
027: return max == 0 ? 0 : random.nextInt(max);
028: }
029:
030: public double nextGaussian() {
031: return random.nextGaussian();
032: }
033:
034: public int getLog(int max) {
035: if (max == 0) {
036: return 0;
037: }
038: while (true) {
039: int d = Math.abs((int) (random.nextGaussian() / 2. * max));
040: if (d < max) {
041: return d;
042: }
043: }
044: }
045:
046: public void getBytes(byte[] data) {
047: random.nextBytes(data);
048: }
049:
050: public boolean getBoolean(int percent) {
051: return random.nextInt(100) <= percent;
052: }
053:
054: public String randomString(int len) {
055: StringBuffer buff = new StringBuffer();
056: for (int i = 0; i < len; i++) {
057: String from = (i % 2 == 0) ? "bdfghklmnpqrst" : "aeiou";
058: buff.append(from.charAt(getInt(from.length())));
059: }
060: return buff.toString();
061: }
062:
063: public int getRandomInt() {
064: switch (random.nextInt(10)) {
065: case 0:
066: return Integer.MAX_VALUE;
067: case 1:
068: return Integer.MIN_VALUE;
069: case 2:
070: return random.nextInt();
071: case 3:
072: case 4:
073: return 0;
074: case 5:
075: return (int) (random.nextGaussian() * 2000) - 200;
076: default:
077: return (int) (random.nextGaussian() * 20) - 5;
078: }
079: }
080:
081: public long getRandomLong() {
082: switch (random.nextInt(10)) {
083: case 0:
084: return Long.MAX_VALUE;
085: case 1:
086: return Long.MIN_VALUE;
087: case 2:
088: return random.nextLong();
089: case 3:
090: case 4:
091: return 0;
092: case 5:
093: return (int) (random.nextGaussian() * 20000) - 2000;
094: default:
095: return (int) (random.nextGaussian() * 200) - 50;
096: }
097: }
098:
099: public double getRandomDouble() {
100: switch (random.nextInt(10)) {
101: case 0:
102: return Double.MIN_VALUE;
103: case 1:
104: return Double.MAX_VALUE;
105: case 2:
106: return Float.MIN_VALUE;
107: case 3:
108: return Float.MAX_VALUE;
109: case 4:
110: return random.nextDouble();
111: case 5:
112: case 6:
113: return 0;
114: case 7:
115: return random.nextGaussian() * 20000. - 2000.;
116: default:
117: return random.nextGaussian() * 200. - 50.;
118: }
119: }
120:
121: public boolean nextBoolean() {
122: return random.nextBoolean();
123: }
124:
125: public int[] getIntArray() {
126: switch (random.nextInt(10)) {
127: case 0:
128: return null;
129: default:
130: int len = getInt(100);
131: int[] list = new int[len];
132: for (int i = 0; i < len; i++) {
133: list[i] = getRandomInt();
134: }
135: return list;
136: }
137: }
138:
139: public Object getByteArray() {
140: switch (random.nextInt(10)) {
141: case 0:
142: return null;
143: default:
144: int len = getInt(100);
145: byte[] list = new byte[len];
146: random.nextBytes(list);
147: return list;
148: }
149: }
150:
151: public Time randomTime() {
152: if (random.nextInt(10) == 0) {
153: return null;
154: }
155: StringBuffer buff = new StringBuffer();
156: buff.append(getInt(24));
157: buff.append(':');
158: buff.append(getInt(24));
159: buff.append(':');
160: buff.append(getInt(24));
161: return Time.valueOf(buff.toString());
162:
163: }
164:
165: public Timestamp randomTimestamp() {
166: if (random.nextInt(10) == 0) {
167: return null;
168: }
169: StringBuffer buff = new StringBuffer();
170: buff.append(getInt(10) + 2000);
171: buff.append('-');
172: int month = getInt(12) + 1;
173: if (month < 10) {
174: buff.append('0');
175: }
176: buff.append(month);
177: buff.append('-');
178: int day = getInt(28) + 1;
179: if (day < 10) {
180: buff.append('0');
181: }
182: buff.append(day);
183: buff.append(' ');
184: int hour = getInt(24);
185: if (hour < 10) {
186: buff.append('0');
187: }
188: buff.append(hour);
189: buff.append(':');
190: int minute = getInt(60);
191: if (minute < 10) {
192: buff.append('0');
193: }
194: buff.append(minute);
195: buff.append(':');
196: int second = getInt(60);
197: if (second < 10) {
198: buff.append('0');
199: }
200: buff.append(second);
201: // TODO test timestamp nanos
202: return Timestamp.valueOf(buff.toString());
203: }
204:
205: public Date randomDate() {
206: if (random.nextInt(10) == 0) {
207: return null;
208: }
209: StringBuffer buff = new StringBuffer();
210: buff.append(getInt(10) + 2000);
211: buff.append('-');
212: buff.append(getInt(11) + 1);
213: buff.append('-');
214: buff.append(getInt(29) + 1);
215: return Date.valueOf(buff.toString());
216: }
217:
218: public String modify(String sql) {
219: int len = getLog(10);
220: for (int i = 0; i < len; i++) {
221: int pos = getInt(sql.length());
222: if (getBoolean(50)) {
223: String badChars = "abcABCDEF\u00ef\u00f6\u00fcC1230=<>+\"\\*%&/()=?$_-.:,;{}[]";
224: // auml, ouml, uuml
225: char bad = badChars.charAt(getInt(badChars.length()));
226: sql = sql.substring(0, pos) + bad + sql.substring(pos);
227: } else {
228: if (pos >= sql.length()) {
229: sql = sql.substring(0, pos);
230: } else {
231: sql = sql.substring(0, pos)
232: + sql.substring(pos + 1);
233: }
234: }
235: }
236: return sql;
237: }
238:
239: public void setSeed(int seed) {
240: random.setSeed(seed);
241: }
242:
243: }
|