Module: Radagen
- Extended by:
- Radagen
- Included in:
- Radagen
- Defined in:
- lib/radagen.rb,
lib/radagen/version.rb,
lib/radagen/generator.rb
Defined Under Namespace
Classes: Generator
Constant Summary collapse
- VERSION =
"0.3.6"
Instance Method Summary collapse
-
#array(gen, opts = {}) ⇒ Radagen::Generator
Creates a generator that when called returns a fixed or varying length Array with values realized from the passed in generator.
-
#bind(gen, &block) ⇒ Radagen::Generator
Takes a generator and a Proc.
-
#boolean ⇒ Radagen::Generator
Returns a generator that will return either true or false.
-
#byte_string ⇒ Radagen::Generator
Returns a generator that will return bytes represented as a string of hex characters.
-
#bytes ⇒ Radagen::Generator
Returns a generator that will return Byte array representations.
-
#char ⇒ Radagen::Generator
Returns a generator that will create characters from codepoints 0-255.
-
#char_alpha ⇒ Radagen::Generator
Returns a generator that will create alpha characters from codepoint ranges: 65-90, 97-122.
-
#char_alphanumeric ⇒ Radagen::Generator
Returns a generator that will create alphanumeric characters from codepoint ranges: 48-57, 65-90, 97-122.
-
#char_ascii ⇒ Radagen::Generator
Returns a generator that will create ascii characters from codepoints 32-126.
-
#char_numeric ⇒ Radagen::Generator
Returns a generator that will create numeric characters from codepoints 48-57.
-
#choose(lower, upper) ⇒ Object
Creates a generator that will choose (inclusive) at random a number between lower and upper bounds.
-
#elements(coll) ⇒ Radagen::Generator
Returns a generator that takes a single element from the passed in collection.
-
#fixnum ⇒ Radagen::Generator
Returns a generator that will return fixnums.
-
#fixnum_neg ⇒ Radagen::Generator
Returns a generator that will return negative fixnums.
-
#fixnum_pos ⇒ Radagen::Generator
Returns a generator that will return positive fixnums.
-
#float ⇒ Radagen::Generator
Returns a generator that will return Floats.
-
#fmap(gen, &block) ⇒ Radagen::Generator
Takes a generator and a Proc.
-
#frequency(weighted_hash) ⇒ Radagen::Generator
Returns a generator that will select a generator from the weighted hash basing the sampling probability on the weights.
-
#gen?(obj) ⇒ Boolean
Predicate to check if obj is an instance of Gen::Generator.
-
#hash(model_hash) ⇒ Radagen::Generator
Creates a generator that produces Hashes based on the model_hash passed in.
-
#hash_map(key_gen, value_gen) ⇒ Radagen::Generator
Creates a generator that when called will return hashes containing keys taken from key_gen and values taken from value_gen.
-
#identity(value) ⇒ Radagen::Generator
(also: #return)
Returns a generator that when called always returns the value passed in.
-
#natural ⇒ Radagen::Generator
Returns a generator that will return natural fixnums.
-
#not_empty(gen) ⇒ Radagen::Generator
Creates a generator that empty values from the provided generator are disregarded.
-
#one_of(generator, generator, ...) ⇒ Radagen::Generator
Creates a generator that when called will select one of the passed in generators returning it's realized value.
-
#rational ⇒ Radagen::Generator
Returns a generator that will return Rationals.
-
#resize(gen, size) ⇒ Radagen::Generator
Creates a generator that allows you to pin the size of a generator to specified value.
-
#scale(gen, &block) ⇒ Radagen::Generator
Creates a generator that allows you to modify the size parameter of the passed in generator with the block.
-
#set(gen, opts = {}) ⇒ Radagen::Generator
Creates a generator that when called returns a varying length Set with values realized from the passed in generator.
-
#shuffle(coll) ⇒ Radagen::Generator
Returns a generator that will create a collection of same length with the elements reordered.
-
#simple_printable ⇒ Radagen::Generator
Returns a random selection of a screen printable simple type.
-
#simple_type ⇒ Radagen::Generator
Returns a random selection of a simple type.
-
#sized(&block) ⇒ Radagen::Generator
Creates a generator that 'exposes' the size value passed to the generator.
-
#some_of(generator, generator, ...) ⇒ Radagen::Generator
Creates a generator that when called will select 1..n generators provided and return and Array of realized values from those chosen generators.
-
#string ⇒ Radagen::Generator
Returns a generator that will create strings from characters within codepoints 0-255.
-
#string_alpha ⇒ Radagen::Generator
Returns a generator that will create strings from alpha characters within codepoint ranges: 65-90, 97-122.
-
#string_alphanumeric ⇒ Radagen::Generator
Returns a generator that will create strings from alphanumeric characters within codepoint ranges: 48-57, 65-90, 97-122.
-
#string_ascii ⇒ Radagen::Generator
Returns a generator that will create strings from ascii characters within codepoints 32-126.
-
#string_numeric ⇒ Radagen::Generator
Returns a generator that will create strings from alpha characters within codepoints 48-57.
-
#such_that(gen, tries = 10, &pred) ⇒ Radagen::Generator
Creates a generator taking values from the passed in generator and applies them to the pred block, returning only the values that satisfy the predicate.
-
#symbol ⇒ Radagen::Generator
Returns a generator that will return Ruby Symbols.
-
#tuple(generator, generator, ...) ⇒ Radagen::Generator
Takes n generators returning an n-tuple of realized values from generators in arity order.
-
#uuid ⇒ Radagen::Generator
Returns a random type 4 uuid.
Instance Method Details
#array(gen, opts = {}) ⇒ Radagen::Generator
If you provide a :min value then it is good practice to also provide a :max value as you can't be sure that the size passed to the generator will be greater or equal to :min.
If you provide a :count value then :min and :max values if passed in are ignored.
Creates a generator that when called returns a fixed or varying length Array with values realized from the passed in generator. Excepts options Hash that can contain the keys :min, :max, :count. These will define the minimum, maximum or fixed amount of values in the realized Array.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/radagen.rb', line 198 def array(gen, opts = {}) count = { count: nil }.merge(opts)[:count] raise ArgumentError, "count must be an Integer or Nil" unless count.is_a?(Integer) or count.is_a?(NilClass) if count gens = (0...count).map { gen } tuple(*gens) else size_gen = sized do |size| min, max = { min: 0, max: size }.merge(opts).values_at(:min, :max) raise RangeError.new, "max value (#{max}) needs to be larger than or equal to min value (#{min})" unless max >= min choose(min, max) end bind(size_gen) do |_size| gens = (0..._size).map { gen } tuple(*gens) end end end |
#bind(gen, &block) ⇒ Radagen::Generator
The Proc has to return a generator.
Takes a generator and a Proc. The Proc is passed a value taken from calling the generator. You then can use that value as an input to the generator returned.
144 145 146 147 148 149 |
# File 'lib/radagen.rb', line 144 def bind(gen, &block) RG.new do |prng, size| inner_gen = block.call(gen.call(prng, size)) inner_gen.call(prng, size) end end |
#boolean ⇒ Radagen::Generator
Returns a generator that will return either true or false.
595 596 597 |
# File 'lib/radagen.rb', line 595 def boolean elements([false, true]) end |
#byte_string ⇒ Radagen::Generator
Returns a generator that will return bytes represented as a string of hex characters. Similar to Random.new.bytes
691 692 693 |
# File 'lib/radagen.rb', line 691 def byte_string fmap(not_empty(string)) { |s| [s].pack('H*') } end |
#bytes ⇒ Radagen::Generator
Returns a generator that will return Byte array representations.
679 680 681 |
# File 'lib/radagen.rb', line 679 def bytes fmap(not_empty(string)) { |s| s.bytes } end |
#char ⇒ Radagen::Generator
Defaults to UTF-8 encoding
Returns a generator that will create characters from codepoints 0-255.
447 448 449 450 451 |
# File 'lib/radagen.rb', line 447 def char fmap(choose(0, 255)) do |v| v.chr(CHAR_ENCODING) end end |
#char_alpha ⇒ Radagen::Generator
Defaults to UTF-8 encoding
Returns a generator that will create alpha characters from codepoint ranges: 65-90, 97-122.
491 492 493 494 495 |
# File 'lib/radagen.rb', line 491 def char_alpha fmap(one_of(choose(65, 90), choose(97, 122))) do |v| v.chr(CHAR_ENCODING) end end |
#char_alphanumeric ⇒ Radagen::Generator
Defaults to UTF-8 encoding
Returns a generator that will create alphanumeric characters from codepoint ranges: 48-57, 65-90, 97-122.
476 477 478 479 480 |
# File 'lib/radagen.rb', line 476 def char_alphanumeric fmap(one_of(choose(48, 57), choose(65, 90), choose(97, 122))) do |v| v.chr(CHAR_ENCODING) end end |
#char_ascii ⇒ Radagen::Generator
Defaults to UTF-8 encoding
Returns a generator that will create ascii characters from codepoints 32-126.
461 462 463 464 465 |
# File 'lib/radagen.rb', line 461 def char_ascii fmap(choose(32, 126)) do |v| v.chr(CHAR_ENCODING) end end |
#char_numeric ⇒ Radagen::Generator
Defaults to UTF-8 encoding
Returns a generator that will create numeric characters from codepoints 48-57
506 507 508 509 510 |
# File 'lib/radagen.rb', line 506 def char_numeric fmap(choose(48, 57)) do |v| v.chr(CHAR_ENCODING) end end |
#choose(Fixnum, Fixnum) ⇒ Radagen::Generator #choose(Float, Float) ⇒ Radagen::Generator
This is a low level generator and is used to build up most of the generator abstractions
Creates a generator that will choose (inclusive) at random a number between lower and upper bounds. The sampling is with replacement.
37 38 39 40 |
# File 'lib/radagen.rb', line 37 def choose(lower, upper) raise RangeError.new, 'upper needs to be greater than or equal to lower value' unless upper >= lower RG.new { |prng, _| prng.rand(lower..upper) } end |
#elements(coll) ⇒ Radagen::Generator
Sampling is with replacement.
Returns a generator that takes a single element from the passed in collection. Works with object that implement .to_a.
386 387 388 389 390 391 392 |
# File 'lib/radagen.rb', line 386 def elements(coll) _coll = coll.to_a bind(choose(0, _coll.count - 1)) do |i| identity(_coll.fetch(i)) end end |
#fixnum ⇒ Radagen::Generator
Returns a generator that will return fixnums.
606 607 608 |
# File 'lib/radagen.rb', line 606 def fixnum sized { |size| choose(-size, size) } end |
#fixnum_neg ⇒ Radagen::Generator
0 is excluded
Returns a generator that will return negative fixnums.
630 631 632 |
# File 'lib/radagen.rb', line 630 def fixnum_neg fmap(fixnum_pos) { |f| f * -1 } end |
#fixnum_pos ⇒ Radagen::Generator
0 is excluded
Returns a generator that will return positive fixnums.
618 619 620 |
# File 'lib/radagen.rb', line 618 def fixnum_pos such_that(natural) { |f| f > 0 } end |
#float ⇒ Radagen::Generator
Returns a generator that will return Floats.
653 654 655 |
# File 'lib/radagen.rb', line 653 def float sized { |size| choose(-size.to_f, size.to_f) } end |
#fmap(gen, &block) ⇒ Radagen::Generator
A value (not a generator) needs to be returned from the Proc.
Takes a generator and a Proc. The Proc is passed a value taken from calling the generator. You then are free to transform the value before it is returned.
122 123 124 125 126 |
# File 'lib/radagen.rb', line 122 def fmap(gen, &block) RG.new do |prng, size| block.call(gen.call(prng, size)) end end |
#frequency(weighted_hash) ⇒ Radagen::Generator
Returns a generator that will select a generator from the weighted hash basing the sampling probability on the weights. The weighted_hash is a hash where the keys are generators and values are the sampling weight relative to all other weights, allowing you to control the probability of value sampling.
429 430 431 432 433 434 435 436 437 |
# File 'lib/radagen.rb', line 429 def frequency(weighted_hash) _weights = weighted_hash.values _gens = weighted_hash.keys raise ArgumentError.new 'all keys in kvs hash need to be Gen::Generator' unless _gens.all? { |g| gen? g } bind(choose(0, _weights.reduce(&:+) - 1)) do |r| frequency_helper(_gens, _weights, r, idx=0, sum=0) end end |
#gen?(obj) ⇒ Boolean
Predicate to check if obj is an instance of Gen::Generator
12 13 14 |
# File 'lib/radagen.rb', line 12 def gen?(obj) obj.is_a?(Radagen::Generator) end |
#hash(model_hash) ⇒ Radagen::Generator
Creates a generator that produces Hashes based on the model_hash passed in. This model_hash is a Hash of scalar keys and generator values. The hashes returned will have values realized from the generators provided.
252 253 254 255 256 257 258 259 260 |
# File 'lib/radagen.rb', line 252 def hash(model_hash) ks = model_hash.keys vs = model_hash.values raise ArgumentError.new 'all values in hash need to be a Gen::Generator' unless vs.all? { |g| gen? g } fmap(tuple(*vs)) do |vs| Hash[ks.zip(vs)] end end |
#hash_map(key_gen, value_gen) ⇒ Radagen::Generator
This will create hashes of various sizes and will grow along with the keys and values.
Creates a generator that when called will return hashes containing keys taken from key_gen and values taken from value_gen.
273 274 275 276 277 |
# File 'lib/radagen.rb', line 273 def hash_map(key_gen, value_gen) fmap(array(tuple(key_gen, value_gen))) do |tuple_array| Hash[tuple_array] end end |
#identity(value) ⇒ Radagen::Generator Also known as: return
Returns a generator that when called always returns the value passed in. The identity generator.
366 367 368 |
# File 'lib/radagen.rb', line 366 def identity(value) RG.new { |_, _| value } end |
#natural ⇒ Radagen::Generator
0 is included
Returns a generator that will return natural fixnums.
642 643 644 |
# File 'lib/radagen.rb', line 642 def natural fmap(fixnum) { |f| f.abs } end |
#not_empty(gen) ⇒ Radagen::Generator
Of course this will throw if you pass it a generator who's values don't produce types that respond to #empty?
Creates a generator that empty values from the provided generator are disregarded. Literally calls #empty? on object. This is a convenience generator for dealing with strings and collection types.
315 316 317 |
# File 'lib/radagen.rb', line 315 def not_empty(gen) such_that(gen) { |x| not x.empty? } end |
#one_of(generator, generator, ...) ⇒ Radagen::Generator
Creates a generator that when called will select one of the passed in generators returning it's realized value.
329 330 331 332 333 |
# File 'lib/radagen.rb', line 329 def one_of(*gens) bind(choose(0, gens.count - 1)) do |i| gens.fetch(i) end end |
#rational ⇒ Radagen::Generator
Returns a generator that will return Rationals.
664 665 666 667 668 669 670 |
# File 'lib/radagen.rb', line 664 def rational denom_gen = such_that(fixnum) { |f| not f == 0 } fmap(tuple(fixnum, denom_gen)) do |(n, d)| Rational(n, d) end end |
#resize(gen, size) ⇒ Radagen::Generator
Creates a generator that allows you to pin the size of a generator to specified value. Think of the size as being the upper bound the generator's size can be.
79 80 81 82 83 |
# File 'lib/radagen.rb', line 79 def resize(gen, size) RG.new do |prng, _| gen.call(prng, size) end end |
#scale(gen, &block) ⇒ Radagen::Generator
This is intended to be used for generators that accept a size. This is partly a convenience function, as you can accomplish much the same with sized.
Creates a generator that allows you to modify the size parameter of the passed in generator with the block. The block accepts the size and allows you to change the size at different rates other than linear.
103 104 105 106 107 108 |
# File 'lib/radagen.rb', line 103 def scale(gen, &block) sized do |size| _size = block.call(size) resize(gen, _size) end end |
#set(gen, opts = {}) ⇒ Radagen::Generator
If the provided generator generates two of the same values then the set will only contain a single representation of that value.
Creates a generator that when called returns a varying length Set with values realized from the passed in generator. Excepts the same values and provides similar behavior of Array options.
235 236 237 238 239 |
# File 'lib/radagen.rb', line 235 def set(gen, opts={}) fmap(array(gen, opts)) do |array| Set[*array] end end |
#shuffle(coll) ⇒ Radagen::Generator
Returns a generator that will create a collection of same length with the elements reordered.
403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/radagen.rb', line 403 def shuffle(coll) _coll = coll.to_a _idx_gen = choose(0, _coll.length - 1) fmap(array(tuple(_idx_gen, _idx_gen), {max: _coll.length * 3})) do |swap_indexes| swap_indexes.reduce(_coll.clone) do |coll, (i, j)| temp = coll[i] coll[i] = coll[j] coll[j] = temp coll end end end |
#simple_printable ⇒ Radagen::Generator
size does not effect derived UUID
Returns a random selection of a screen printable simple type.
751 752 753 |
# File 'lib/radagen.rb', line 751 def simple_printable one_of(fixnum, rational, float, boolean, symbol, char_ascii, string_ascii, char_alphanumeric, string_alphanumeric, uuid) end |
#simple_type ⇒ Radagen::Generator
size does not effect derived UUID
Returns a random selection of a simple type.
739 740 741 |
# File 'lib/radagen.rb', line 739 def simple_type one_of(fixnum, rational, bytes, float, boolean, symbol, char, string, uuid) end |
#sized(&block) ⇒ Radagen::Generator
Creates a generator that 'exposes' the size value passed to the generator. block is a Proc that takes a fixnum and returns a generator. This can be used to see, manipulate or wrap another generator with the passed in size.
57 58 59 60 61 62 |
# File 'lib/radagen.rb', line 57 def sized(&block) RG.new do |prng, size| sized_gen = block.call(size) sized_gen.call(prng, size) end end |
#some_of(generator, generator, ...) ⇒ Radagen::Generator
Order of generator selection will be shuffled
Creates a generator that when called will select 1..n generators provided and return and Array of realized values from those chosen generators.
347 348 349 350 351 352 353 354 355 |
# File 'lib/radagen.rb', line 347 def some_of(*gens) bind(tuple(*gens)) do |_vals| bind(choose(1, _vals.count)) do |_count| fmap(shuffle(_vals)) do |__vals| __vals.take(_count) end end end end |
#string ⇒ Radagen::Generator
Defaults to UTF-8 encoding
Returns a generator that will create strings from characters within codepoints 0-255
521 522 523 524 525 |
# File 'lib/radagen.rb', line 521 def string fmap(array(char)) do |char_array| char_array.join end end |
#string_alpha ⇒ Radagen::Generator
Defaults to UTF-8 encoding
Returns a generator that will create strings from alpha characters within codepoint ranges: 65-90, 97-122
567 568 569 570 571 |
# File 'lib/radagen.rb', line 567 def string_alpha fmap(array(char_alpha)) do |char_array| char_array.join end end |
#string_alphanumeric ⇒ Radagen::Generator
Defaults to UTF-8 encoding
Returns a generator that will create strings from alphanumeric characters within codepoint ranges: 48-57, 65-90, 97-122
552 553 554 555 556 |
# File 'lib/radagen.rb', line 552 def string_alphanumeric fmap(array(char_alphanumeric)) do |char_array| char_array.join end end |
#string_ascii ⇒ Radagen::Generator
Defaults to UTF-8 encoding
Returns a generator that will create strings from ascii characters within codepoints 32-126
536 537 538 539 540 |
# File 'lib/radagen.rb', line 536 def string_ascii fmap(array(char_ascii)) do |char_array| char_array.join end end |
#string_numeric ⇒ Radagen::Generator
Defaults to UTF-8 encoding
Returns a generator that will create strings from alpha characters within codepoints 48-57
582 583 584 585 586 |
# File 'lib/radagen.rb', line 582 def string_numeric fmap(array(char_numeric)) do |char_array| char_array.join end end |
#such_that(gen, tries = 10, &pred) ⇒ Radagen::Generator
Creates a generator taking values from the passed in generator and applies them to the pred block, returning only the values that satisfy the predicate. This acts much the same way as enumerable's #select method. By default it will try 10 times to satisfy the predicate with different sizes passed to the generator. You can provide a count of the number of tries.
294 295 296 297 298 |
# File 'lib/radagen.rb', line 294 def such_that(gen, tries=10, &pred) RG.new do |prng, size| select_helper(gen, tries, prng, size, &pred) end end |
#symbol ⇒ Radagen::Generator
Returns a generator that will return Ruby Symbols.
702 703 704 705 706 |
# File 'lib/radagen.rb', line 702 def symbol fmap(not_empty(array(char_alphanumeric))) do |char_array| char_array.join.intern end end |
#tuple(generator, generator, ...) ⇒ Radagen::Generator
Takes n generators returning an n-tuple of realized values from generators in arity order.
161 162 163 164 165 166 167 168 169 |
# File 'lib/radagen.rb', line 161 def tuple(*gens) raise ArgumentError.new 'all arguments need to be generators' unless gens.all? { |g| gen? g } RG.new do |prng, size| gens.map do |gen| gen.call(prng, size) end end end |
#uuid ⇒ Radagen::Generator
size does not effect derived UUIDs
Returns a random type 4 uuid.
716 717 718 719 720 721 722 723 724 725 726 727 728 729 |
# File 'lib/radagen.rb', line 716 def uuid fmap(array(choose(0, 15), {min: 31, max: 31})) do |nibbles| rhex = (8 + (nibbles[15] & 3)).to_s(16) [nibbles[0].to_s(16), nibbles[1].to_s(16), nibbles[2].to_s(16), nibbles[3].to_s(16), nibbles[4].to_s(16), nibbles[5].to_s(16), nibbles[6].to_s(16), nibbles[7].to_s(16), '-', nibbles[8].to_s(16), nibbles[9].to_s(16), nibbles[10].to_s(16), nibbles[11].to_s(16), '-', 4, nibbles[12].to_s(16), nibbles[13].to_s(16), nibbles[14].to_s(16), '-', rhex, nibbles[16].to_s(16), nibbles[17].to_s(16), nibbles[18].to_s(16), '-', nibbles[19].to_s(16), nibbles[20].to_s(16), nibbles[21].to_s(16), nibbles[22].to_s(16), nibbles[23].to_s(16), nibbles[24].to_s(16), nibbles[25].to_s(16), nibbles[26].to_s(16), nibbles[27].to_s(16), nibbles[28].to_s(16), nibbles[29].to_s(16), nibbles[30].to_s(16)].join end end |