1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
|
# Copyright (c) 2008,2010 Oracle and/or its affiliates. All rights reserved.
# Use is subject to license terms.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA
package GenTest::Random;
require Exporter;
@ISA = qw(GenTest);
@EXPORT = qw(
FIELD_TYPE_NUMERIC
FIELD_TYPE_STRING
FIELD_TYPE_DATE
FIELD_TYPE_TIME
FIELD_TYPE_DATETIME
FIELD_TYPE_TIMESTAMP
FIELD_TYPE_ENUM
FIELD_TYPE_SET
FIELD_TYPE_YEAR
FIELD_TYPE_BLOB
FIELD_TYPE_DICT
FIELD_TYPE_DIGIT
FIELD_TYPE_LETTER
FIELD_TYPE_NULL
FIELD_TYPE_ASCII
FIELD_TYPE_EMPTY
FIELD_TYPE_HEX
FIELD_TYPE_QUID
);
use strict;
use GenTest;
use Cwd;
=pod
This module provides a clean interface to a pseudo-random number
generator.
There are quite a few of them on CPAN with various interfaces, so I
decided to create a uniform interface so that the underlying
pseudo-random function or module can be changed without affecting the
rest of the software.
The important thing to note is that several pseudo-random number
generators may be active at the same time, seeded with different
values. Therefore the underlying pseudo-random function must not rely
on perlfunc's srand() and rand() because those maintain a single
system-wide pseudo-random sequence.
This module is equipped with it's own Linear Congruential Random
Number Generator, see
http://en.wikipedia.org/wiki/Linear_congruential_generator For
efficiency, math is done in integer mode
=cut
use constant RANDOM_SEED => 0;
use constant RANDOM_GENERATOR => 1;
use constant RANDOM_VARCHAR_LENGTH => 2;
use constant RANDOM_STRBUF => 3;
use constant FIELD_TYPE_NUMERIC => 2;
use constant FIELD_TYPE_STRING => 3;
use constant FIELD_TYPE_DATE => 4;
use constant FIELD_TYPE_TIME => 5;
use constant FIELD_TYPE_DATETIME => 6;
use constant FIELD_TYPE_TIMESTAMP => 7;
use constant FIELD_TYPE_YEAR => 8;
use constant FIELD_TYPE_ENUM => 9;
use constant FIELD_TYPE_SET => 10;
use constant FIELD_TYPE_BLOB => 11;
use constant FIELD_TYPE_DIGIT => 12;
use constant FIELD_TYPE_LETTER => 13;
use constant FIELD_TYPE_NULL => 14;
use constant FIELD_TYPE_DICT => 15;
use constant FIELD_TYPE_ASCII => 16;
use constant FIELD_TYPE_EMPTY => 17;
use constant FIELD_TYPE_HEX => 18;
use constant FIELD_TYPE_QUID => 19;
use constant FIELD_TYPE_BIT => 20;
my %dict_exists;
my %dict_data;
my %data_dirs;
my %name2type = (
'bit' => FIELD_TYPE_BIT,
'bool' => FIELD_TYPE_NUMERIC,
'boolean' => FIELD_TYPE_NUMERIC,
'tinyint' => FIELD_TYPE_NUMERIC,
'smallint' => FIELD_TYPE_NUMERIC,
'mediumint' => FIELD_TYPE_NUMERIC,
'int' => FIELD_TYPE_NUMERIC,
'integer' => FIELD_TYPE_NUMERIC,
'bigint' => FIELD_TYPE_NUMERIC,
'float' => FIELD_TYPE_NUMERIC,
'double' => FIELD_TYPE_NUMERIC,
'double precision' => FIELD_TYPE_NUMERIC,
'decimal' => FIELD_TYPE_NUMERIC,
'dec' => FIELD_TYPE_NUMERIC,
'numeric' => FIELD_TYPE_NUMERIC,
'fixed' => FIELD_TYPE_NUMERIC,
'char' => FIELD_TYPE_STRING,
'varchar' => FIELD_TYPE_STRING,
'binary' => FIELD_TYPE_BLOB,
'varbinary' => FIELD_TYPE_BLOB,
'tinyblob' => FIELD_TYPE_BLOB,
'blob' => FIELD_TYPE_BLOB,
'mediumblob' => FIELD_TYPE_BLOB,
'longblob' => FIELD_TYPE_BLOB,
'tinytext' => FIELD_TYPE_STRING,
'text' => FIELD_TYPE_STRING,
'mediumtext' => FIELD_TYPE_STRING,
'longtext' => FIELD_TYPE_STRING,
'date' => FIELD_TYPE_DATE,
'time' => FIELD_TYPE_TIME,
'datetime' => FIELD_TYPE_DATETIME,
'timestamp' => FIELD_TYPE_TIMESTAMP,
'year' => FIELD_TYPE_YEAR,
'enum' => FIELD_TYPE_ENUM,
'set' => FIELD_TYPE_SET,
'null' => FIELD_TYPE_NULL,
'letter' => FIELD_TYPE_LETTER,
'digit' => FIELD_TYPE_DIGIT,
'data' => FIELD_TYPE_BLOB,
'ascii' => FIELD_TYPE_ASCII,
'string' => FIELD_TYPE_STRING,
'empty' => FIELD_TYPE_EMPTY,
'hex' => FIELD_TYPE_HEX,
'quid' => FIELD_TYPE_QUID
);
my $cwd = cwd();
# Min and max values for integer data types
my %name2range = (
'bool' => [0, 1],
'boolean' => [0, 1],
'tinyint' => [-128, 127],
'smallint' => [-32768, 32767],
'mediumint' => [-8388608, 8388607],
'int' => [-2147483648, 2147483647],
'integer' => [-2147483648, 2147483647],
'bigint' => [-9223372036854775808, 9223372036854775807],
'tinyint_unsigned' => [0, 255],
'smallint_unsigned' => [0, 65535],
'mediumint_unsigned' => [0, 16777215],
'int_unsigned' => [0, 4294967295],
'integer_unsigned' => [0, 4294967295],
'bigint_unsigned' => [0, 18446744073709551615]
);
my $prng_class;
1;
sub new {
my $class = shift;
my $prng = $class->SUPER::new({
'seed' => RANDOM_SEED,
'varchar_length' => RANDOM_VARCHAR_LENGTH
}, @_ );
$prng->setSeed($prng->seed() > 0 ? $prng->seed() : 1);
# say("Initializing PRNG with seed '".$prng->seed()."' ...");
$prng->[RANDOM_GENERATOR] = $prng->seed();
return $prng;
}
sub seed {
return $_[0]->[RANDOM_SEED];
}
sub setSeed {
$_[0]->[RANDOM_SEED] = $_[1];
$_[0]->[RANDOM_GENERATOR] = $_[1];
}
### Random unsigned integer. 16 bit on 32-bit platforms, 48 bit on
### 64-bit platforms. For internal use in Random.pm. Use int() or
### uint16() instead.
sub urand {
use integer;
$_[0]->[RANDOM_GENERATOR] =
$_[0]->[RANDOM_GENERATOR] * 1103515245 + 12345;
## The lower bits are of bad statsictical quality in an LCG, so we
## just use the higher bits.
## Unfortunetaly, >> is an arithemtic shift so we shift right 15
## bits and have take the absoulte value off that to get a 16-bit
## unsigned random value.
my $rand = $_[0]->[RANDOM_GENERATOR] >> 15;
## Can't use abs() since abs() is a function that use float (SIC!)
if ($rand < 0) {
return -$rand;
} else {
return $rand;
}
}
### Random unsigned 16-bit integer
sub uint16 {
use integer;
# urand() is manually inlined for efficiency
$_[0]->[RANDOM_GENERATOR] =
$_[0]->[RANDOM_GENERATOR] * 1103515245 + 12345;
return $_[1] +
((($_[0]->[RANDOM_GENERATOR] >> 15) & 0xFFFF) % ($_[2] - $_[1] + 1));
}
### Signed 64-bit integer of any range.
### Slower, so use uint16 wherever possible.
sub int {
my $rand;
{
use integer;
# urand() is manually inlined for efficiency
$_[0]->[RANDOM_GENERATOR] =
$_[0]->[RANDOM_GENERATOR] * 1103515245 + 12345;
# Since this may be a 64-bit platform, we mask down to 16 bit
# to ensure the division below becomes correct.
$rand = ($_[0]->[RANDOM_GENERATOR] >> 15) & 0xFFFF;
}
return int($_[1] + (($rand / 0x10000) * ($_[2] - $_[1] + 1)));
}
sub digit {
return $_[0]->uint16(0, 9);
}
sub letter {
return $_[0]->string(1);
}
sub hex {
my ($prng, $length) = @_;
$length = 4 if not defined $length;
return '0x'.join ('', map { (0..9,'A'..'F')[$prng->int(0,15)] } (1..$prng->int(1,$length)) );
}
sub date {
my $prng = shift;
return sprintf('%04d-%02d-%02d',
$prng->uint16(2000,2009),
$prng->uint16(1,12),
$prng->uint16(1,28));
}
sub year {
my $prng = shift;
return $prng->uint16(2000,2009);
}
sub time {
my $prng = shift;
return sprintf('%02d:%02d:%02d.%06d',
$prng->uint16(0,23),
$prng->uint16(0,59),
$prng->uint16(0,59),
$prng->uint16(0,999999));
}
sub datetime {
my $prng = shift;
return $prng->date()." ".$prng->time();
}
sub timestamp {
my $prng = shift;
return sprintf('%04d%02d%02d%02d%02d%02d.%06d',
$prng->uint16(2000,2009),
$prng->uint16(1,12),
$prng->uint16(1,28),
$prng->uint16(0,23),
$prng->uint16(0,59),
$prng->uint16(0,59),
$prng->uint16(0,999999));
}
sub enum {
my $prng = shift;
return $prng->letter();
}
sub set {
my $prng = shift;
return join(',', map { $prng->letter() } (0..$prng->digit() ) );
}
sub string {
use constant RANDOM_STRBUF_SIZE => 1024;
use integer;
my ($prng, $len, $range) = @_;
$len = 1 if not defined $len;
$range = [97, 122] if not defined $range;
$len = $prng->[RANDOM_VARCHAR_LENGTH] if defined $prng->[RANDOM_VARCHAR_LENGTH];
# If the length is 0 or negative, return a zero-length string
return '' if $len <= 0;
# If the length is 1, just return one random character
if ($len == 1) {
return chr($prng->uint16($range->[0],$range->[1]));
}
# We store a random string of length RANDOM_STRBUF_SIZE which we fill with
# random bytes. Each time a new string is requested, we shift the
# string one byte right and generate a new string at the beginning
# of the string.
my $rnd;
my ($min, $max) = ($range->[0], $range->[1]);
my $modulus = $max - $min + 1;
my $actual_length = $prng->uint16(1,$len);
if (not defined $prng->[RANDOM_STRBUF]) {
# Fill the buffer with random bytes.
@{$prng->[RANDOM_STRBUF]} =
map {$prng->uint16(0,255)} (1..RANDOM_STRBUF_SIZE);
} else {
# Shift right and put a new byte at the front
pop(@{$prng->[RANDOM_STRBUF]});
unshift(@{$prng->[RANDOM_STRBUF]},$prng->uint16(0,255));
}
if ($actual_length <= RANDOM_STRBUF_SIZE) {
## If the wanted length fit in the buffer, just return a slice of it.
return pack("c*",
map {($min + ($_ % $modulus))}
@{$prng->[RANDOM_STRBUF]}[1..$actual_length]);
} else {
## Otherwise wil fill repeatedly from the buffer
my $res = "";
while ($actual_length > RANDOM_STRBUF_SIZE){
$res .= pack("c*",
map {($min + ($_ % $modulus))}
@{$prng->[RANDOM_STRBUF]});
$actual_length -= RANDOM_STRBUF_SIZE;
}
return $res . pack("c*",
map {($min + ($_ % $modulus))}
@{$prng->[RANDOM_STRBUF]}[1..$actual_length]);
}
}
sub quid {
my $prng = shift;
return pack("c*", map {
$prng->uint16(65,90);
} (1..5));
}
sub bit {
my ($prng, $length) = @_;
$length = 1 if not defined $length;
return 'b\''.join ('', map { $prng->int(0,1) } (1..$prng->int(1,$length)) ).'\'';
}
#
# Return a random array element from an array reference
#
sub arrayElement {
## To avoid mod zero-problems in uint16 (See Bug#45857)
return undef if $#{$_[1]} < 0;
return $_[1]->[ $_[0]->uint16(0, $#{$_[1]}) ];
}
#
# Return a random value appropriate for this type of field
#
sub fieldType {
my ($rand, $field_def) = @_;
$field_def =~ s{ }{_}o;
$field_def =~ s{^_}{}o;
my ($field_base_type) = $field_def =~ m{^([A-Za-z]*)}o;
my ($field_full_type) = $field_def =~ m{^([A-Za-z_]*)}o;
my ($field_length) = $field_def =~ m{\((.*?)\)}o;
$field_length = 1 if not defined $field_length;
my $field_type = $name2type{$field_base_type};
if ($field_type == FIELD_TYPE_DIGIT) {
return $rand->digit();
} elsif ($field_type == FIELD_TYPE_LETTER) {
return $rand->string(1);
} elsif ($field_type == FIELD_TYPE_NUMERIC) {
return $rand->int(@{$name2range{$field_full_type}});
} elsif ($field_type == FIELD_TYPE_STRING) {
return $rand->string($field_length);
} elsif ($field_type == FIELD_TYPE_DATE) {
return $rand->date();
} elsif ($field_type == FIELD_TYPE_YEAR) {
return $rand->year();
} elsif ($field_type == FIELD_TYPE_TIME) {
return $rand->time();
} elsif ($field_type == FIELD_TYPE_DATETIME) {
return $rand->datetime();
} elsif ($field_type == FIELD_TYPE_TIMESTAMP) {
return $rand->timestamp();
} elsif ($field_type == FIELD_TYPE_ENUM) {
return $rand->enum();
} elsif ($field_type == FIELD_TYPE_SET) {
return $rand->set();
} elsif ($field_type == FIELD_TYPE_BLOB) {
return $rand->file("$cwd/data");
} elsif ($field_type == FIELD_TYPE_NULL) {
return undef;
} elsif ($field_type == FIELD_TYPE_ASCII) {
return $rand->string($field_length, [0, 255]);
} elsif ($field_type == FIELD_TYPE_EMPTY) {
return '';
} elsif ($field_type == FIELD_TYPE_HEX) {
return $rand->hex($field_length);
} elsif ($field_type == FIELD_TYPE_QUID) {
return $rand->quid();
} elsif ($field_type == FIELD_TYPE_DICT) {
return $rand->fromDictionary($field_base_type);
} elsif ($field_type == FIELD_TYPE_BIT) {
return $rand->bit($field_length);
} else {
die ("unknown field type $field_def");
}
}
sub file {
my ($prng, $dir) = @_;
if (not exists $data_dirs{$dir}) {
my @files = <$dir/*>;
$data_dirs{$dir} = \@files;
}
return "LOAD_FILE('".$prng->arrayElement($data_dirs{$dir})."')";
}
sub isFieldType {
my ($rand, $field_def) = @_;
return undef if not defined $field_def;
$field_def =~ s{^_}{}o;
my ($field_name) = $field_def =~ m{^([A-Za-z]*)}o;
if (exists $name2type{$field_name}) {
return $name2type{$field_name};
} elsif ($rand->isDictionary($field_name)) {
$name2type{$field_name} = FIELD_TYPE_DICT;
return FIELD_TYPE_DICT;
} else {
return undef;
}
}
sub isDictionary {
my ($rand, $dict_name) = @_;
if ($dict_exists{$dict_name}) {
return 1;
} else {
my $dict_file = $ENV{RQG_HOME} ne '' ? $ENV{RQG_HOME}."/dict/$dict_name.txt" : "dict/$dict_name.txt";
if (-e $dict_file) {
$dict_exists{$dict_name} = 1;
return 1;
} else {
return undef;
}
}
}
sub fromDictionary {
my ($rand, $dict_name) = @_;
if (not exists $dict_data{$dict_name}) {
my $dict_file = $ENV{RQG_HOME} ne '' ? $ENV{RQG_HOME}."/dict/$dict_name.txt" : "dict/$dict_name.txt";
open (DICT, $dict_file) or warn "# Unable to load $dict_file: $!";
my @dict_data = map { chop; $_ } <DICT>;
close DICT;
$dict_data{$dict_name} = \@dict_data;
}
return $rand->arrayElement($dict_data{$dict_name});
}
sub shuffleArray {
my ($rand, $array) = @_;
my $i;
for ($i = @$array; --$i; ) {
my $j = $rand->uint16(0, $i);
next if $i == $j;
@$array[$i,$j] = @$array[$j,$i];
}
return $array;
}
1;
|