KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > synth > RandomGen


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.synth;
6
7 import java.sql.Date JavaDoc;
8 import java.sql.Time JavaDoc;
9 import java.sql.Timestamp JavaDoc;
10 import java.util.Random JavaDoc;
11
12 public class RandomGen {
13     private Random JavaDoc random = new Random JavaDoc();
14 // private TestSynth config;
15

16     public RandomGen(TestSynth config) {
17 // this.config = config;
18
random.setSeed(12);
19     }
20     
21     public int getInt(int max) {
22         return max==0 ? 0 : random.nextInt(max);
23     }
24     
25     public double nextGaussian() {
26         return random.nextGaussian();
27     }
28     
29     public int getLog(int max) {
30         if(max==0) {
31             return 0;
32         }
33         while(true) {
34             int d = Math.abs((int)(random.nextGaussian()/2.*max));
35             if(d<max) {
36                 return d;
37             }
38         }
39     }
40     
41     public void getBytes(byte[] data) {
42         random.nextBytes(data);
43     }
44     
45     public boolean getBoolean(int percent) {
46         return random.nextInt(100) <= percent;
47     }
48     
49     public String JavaDoc randomString(int len) {
50         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
51         for(int i=0; i<len; i++) {
52             String JavaDoc from = (i%2==0)?"bdfghklmnpqrst":"aeiou";
53             buff.append(from.charAt(getInt(from.length())));
54         }
55         return buff.toString();
56     }
57
58     public int getRandomInt() {
59         switch(random.nextInt(10)) {
60         case 0:
61             return Integer.MAX_VALUE;
62         case 1:
63             return Integer.MIN_VALUE;
64         case 2:
65             return random.nextInt();
66         case 3:
67         case 4:
68             return 0;
69         case 5:
70             return (int)(random.nextGaussian()*2000)-200;
71         default:
72             return (int)(random.nextGaussian()*20)-5;
73         }
74     }
75
76     public long getRandomLong() {
77         switch(random.nextInt(10)) {
78         case 0:
79             return Long.MAX_VALUE;
80         case 1:
81             return Long.MIN_VALUE;
82         case 2:
83             return random.nextLong();
84         case 3:
85         case 4:
86             return 0;
87         case 5:
88             return (int)(random.nextGaussian()*20000)-2000;
89         default:
90             return (int)(random.nextGaussian()*200)-50;
91         }
92     }
93
94     public double getRandomDouble() {
95         switch(random.nextInt(10)) {
96         case 0:
97             return Double.MIN_VALUE;
98         case 1:
99             return Double.MAX_VALUE;
100         case 2:
101             return Float.MIN_VALUE;
102         case 3:
103             return Float.MAX_VALUE;
104         case 4:
105             return random.nextDouble();
106         case 5:
107         case 6:
108             return 0;
109         case 7:
110             return random.nextGaussian()*20000.-2000.;
111         default:
112             return random.nextGaussian()*200.-50.;
113         }
114     }
115
116     public boolean nextBoolean() {
117         return random.nextBoolean();
118     }
119
120     public int[] getIntArray() {
121         switch(random.nextInt(10)) {
122         case 0:
123             return null;
124         default:
125             int len = getInt(100);
126             int[] list = new int[len];
127             for(int i=0; i<len; i++) {
128                 list[i] = getRandomInt();
129             }
130             return list;
131         }
132     }
133
134     public Object JavaDoc getByteArray() {
135         switch(random.nextInt(10)) {
136         case 0:
137             return null;
138         default:
139             int len = getInt(100);
140             byte[] list = new byte[len];
141             random.nextBytes(list);
142             return list;
143         }
144     }
145
146     public Time JavaDoc randomTime() {
147         if(random.nextInt(10)==0) {
148             return null;
149         }
150         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
151         buff.append(getInt(24));
152         buff.append(':');
153         buff.append(getInt(24));
154         buff.append(':');
155         buff.append(getInt(24));
156         return Time.valueOf(buff.toString());
157
158     }
159
160     public Timestamp JavaDoc randomTimestamp() {
161         if(random.nextInt(10)==0) {
162             return null;
163         }
164         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
165         buff.append(getInt(10) + 2000);
166         buff.append('-');
167         buff.append(getInt(12) + 1);
168         buff.append('-');
169         buff.append(getInt(29) + 1);
170         buff.append(' ');
171         buff.append(getInt(24));
172         buff.append(':');
173         buff.append(getInt(60));
174         buff.append(':');
175         buff.append(getInt(60));
176         // TODO test timestamp nanos
177
return Timestamp.valueOf(buff.toString());
178     }
179
180     public Date JavaDoc randomDate() {
181         if(random.nextInt(10)==0) {
182             return null;
183         }
184         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
185         buff.append(getInt(10) + 2000);
186         buff.append('-');
187         buff.append(getInt(11) + 1);
188         buff.append('-');
189         buff.append(getInt(29) + 1);
190         return Date.valueOf(buff.toString());
191     }
192
193     public String JavaDoc modify(String JavaDoc sql) {
194         int len = getLog(10);
195         for(int i=0; i<len; i++) {
196             int pos = getInt(sql.length());
197             if(getBoolean(50)) {
198                 String JavaDoc badChars = "abcABCDEF\u00ef\u00f6\u00fcC1230=<>+\"\\*%&/()=?$_-.:,;{}[]"; // auml ouml uuml
199
char bad = badChars.charAt(getInt(badChars.length()));
200                 sql = sql.substring(0, pos) + bad + sql.substring(pos);
201             } else {
202                 if(pos >= sql.length()) {
203                     sql = sql.substring(0, pos);
204                 } else {
205                     sql = sql.substring(0, pos) + sql.substring(pos+1);
206                 }
207             }
208         }
209         return sql;
210     }
211
212     public void setSeed(int seed) {
213         random.setSeed(seed);
214     }
215     
216 }
217
Popular Tags