pollution v0.1.1 Pollution.VG

Summary

Functions

Return a stream of random booleans (true or false)

Each time a value is needed, randomly choose a generator from the list and invoke it

Return a stream of random floating point numbers

Return a stream of random integers

Return a stream of lists. Each list will have a random length (within limits), and each element in each list will be randomly chosen from the specified types

Return a stream of floats not greater than -1.0. (Arguably this should be “not greater than -epsilon”). Same as float(max: -1.0)

Return a stream of integers less than 0. Same as int(max: -1)

Return a stream of floats greater than or equal to zero. Same as float(min: 0.0)

Return a stream of integers greater than or equal to 0. Same as int(min: 0)

Randomly chooses a generator from the list, and then returns a stream of values that it produces. This choice is made only once—call pick_one again to get a different result

Return a stream of floats not less than 1.0. (Arguably this should be “not less than epsilon”). Same as float(min: 1.0)

Return a stream of integers not less than 1. Same as int(min: 1)

Give seq a list of generators (using the of: option). It will cycle through these as it streams values

Return a stream of strings of randomly varying length

Generate a stream of tuples. The default is to create tuples of varying sizes with varying content, which is unlikely to be useful. You’ll more likely want to use the like: option, which sets a template for the tuples

Generates an infinite stream where each element is its parameter

Functions

atom(options \\ [])
bool()

Return a stream of random booleans (true or false).

Example

iex> import Pollution.{Generator, VG}
  iex> bool |> as_stream |> Enum.take(5)
  [true, false, true, true, false]
choose(options)

Each time a value is needed, randomly choose a generator from the list and invoke it.

Example

iex> import Pollution.{Generator, VG}
  iex> choose(from: [ int(min: 3, max: 7), bool ]) |> as_stream |> Enum.take(5)
  [6, false, 4, true, true]
float(options \\ [])

Return a stream of random floating point numbers.

Example

iex> import Pollution.{Generator, VG}
  iex> float |> as_stream |> Enum.take(5)
  [0.0, -1.0, 1.0, 5.0e-324, -5.0e-324]

Options

  • min: value

    The minimum value that will be generated (default: -1e6).

  • max: value

    The maximum value that will be generated (default: 1e6).

  • must_have: [ _value,_ … ]

    Values that must be included in the results. The default is

    [ 0.0, -1.0, 1.0, _epsilon_, _-epsilon_ ]

    (where epsilon is the smallest expressible float)

    Must have values are automatically adjusted to account for the min and max values. For example, if you specify min: 0.5 then only the 1.0 must-have value will be generated.

See also

positive_float()negative_floatnon_negative_float

int(options \\ [])

Return a stream of random integers.

Example

iex> import Pollution.{Generator, VG}
  iex> int |> as_stream |> Enum.take(5)
  [0, -1, 1, 215, -401]

Options

  • min: value

    The minimum value that will be generated (default: -1000).

  • max: value

    The maximum value that will be generated (default: 1000).

  • must_have: [ _value,_ … ]

    Values that must be included in the results. The default is

    [ 0, -1, 1 ]

    Must have values are automatically adjusted to account for the min and max values. For example, if you specify min: 0 then only the 0 and 1 must-have values will be generated.

See also

positive_int()negative_intnon_negative_int

list()

Return a stream of lists. Each list will have a random length (within limits), and each element in each list will be randomly chosen from the specified types.

Example

iex> import Pollution.{Generator, VG}
iex> list(of: bool, max: 7) |> as_stream|> Enum.take(5)
[
 [],
 [false, false, false],
 [false, true, true, false, true],
 [false, true, true, true, true, false, true],
 [true, true, false, false, false]
]

There are a few special-case constructors:

  • list(length)

    Return lists of the given length

  • list(generator)

    Return lists whose elements are created by generator

    iex> list(bool) |> as_stream|> Enum.take(5)

Otherwise, pass options:

  • min: length

    Minimum length of the lists returned. Default 0

  • max: length

    Maximum length of the lists returned. Default 100

  • must_have: [ _value_, … ]

    Values that must be returned. Defaults to returning an empty list (so the parameter is must_have: [ [] ] if the minimum length is zero, nothing otherwise.

  • of: generator

    Specifies the generator used to populate the lists.

    ## Examples

    iex> import Pollution.{Generator, VG}

    iex> list(of: int, min: 1, max: 5) |> as_stream |> Enum.take(4) [[0, -1, 1, -546], [442], [150], [-836, 540, -979]]

    iex> list(of: int, min: 1, max: 5) |> as_stream |> Enum.take(4) [[0], [-1, 1, 984, -206], [-246], [433, 125, -757]]

    iex> list(of: choose(from: [value(1), value(2)]), min: 1, max: 5) …> |> as_stream |> Enum.take(4) [[2], [1, 1, 2], [2, 2, 1, 1, 1], [2, 2, 1]]

    iex> list(of: seq(of: [value(1), value(2)]), min: 1, max: 5) …> |> as_stream |> Enum.take(4) [[1, 2], [1, 2, 1, 2], [1], [2, 1]]

list(size)
list(min, max)
negative_float()

Return a stream of floats not greater than -1.0. (Arguably this should be “not greater than -epsilon”). Same as float(max: -1.0)

negative_int()

Return a stream of integers less than 0. Same as int(max: -1)

nonnegative_float()

Return a stream of floats greater than or equal to zero. Same as float(min: 0.0)

nonnegative_int()

Return a stream of integers greater than or equal to 0. Same as int(min: 0)

pick_one(options)

Randomly chooses a generator from the list, and then returns a stream of values that it produces. This choice is made only once—call pick_one again to get a different result.

Examples

iex> import Pollution.{Generator, VG}
iex> stream = pick_one(from: [int, bool]) |> as_stream
iex> Enum.take(stream, 5)
[0, -1, 1, -223, 72]
iex> Enum.take(stream, 5)
[0, -1, 1, -553, 847]
iex> Enum.take(stream, 5)
[0, -1, 1, -518, -692]
iex> Enum.take(stream, 5)
[0, -1, 1, 580, 668]
iex> Enum.take(stream, 5)
[0, -1, 1, -989, -353]
iex> stream = pick_one(from: [int, bool]) |> as_stream
iex> Enum.take(stream, 5)
[true, false, false, false, false]
iex> Enum.take(stream, 5)
[false, true, false, false, false]
positive_float()

Return a stream of floats not less than 1.0. (Arguably this should be “not less than epsilon”). Same as float(min: 1.0)

positive_int()

Return a stream of integers not less than 1. Same as int(min: 1)

seq(options)

Give seq a list of generators (using the of: option). It will cycle through these as it streams values.

Examples

iex> import Pollution.{Generator, VG}
iex> seq(of: [int, bool, float]) |> as_stream |> Enum.take(10)
[0, true, 0.0, -1, true, -1.0, 1, true, 1.0, -702]
string(options \\ [])

Return a stream of strings of randomly varying length.

Examples

iex> import Pollution.{Generator, VG}
iex> string(max: 4) |> as_stream |> Enum.take(5)
["", " ", "墍勧", "㘃牸ྥ姷", ""]
iex> string(chars: :digits, max: 4) |> as_stream |> Enum.take(5)
["33", "", "7", "6223", "55"]

Options

  • min: length

    The minimum length of the returned string (default 0)

  • max: length

    The maximum length of the returned string (default 300)

  • chars: :ascii | :digits | :lower | :printable | :upper | :utf

    The set of characters that may be included in the result:

    :ascii0..127
    :digits?0..?9
    :lower?a..?z
    :printable32..126
    :upper?A..?Z
    :utf0..0xd7af

    The default is :utf8.

  • must_have: list

    A list of strings that must be in the result stream. Defaults to ["", "␠"], filtered by the maximum and minimum lengths.

tuple(options \\ [])

Generate a stream of tuples. The default is to create tuples of varying sizes with varying content, which is unlikely to be useful. You’ll more likely want to use the like: option, which sets a template for the tuples.

Example

iex> import Pollution.{Generator, VG}
iex> tuple(like: { value("insert"), string(chars: :upper, max: 10)}) |>
...> as_stream |> Enum.take(3)
[{"insert", "M"}, {"insert", "GFOHZNDER"}, {"insert", "FCDO"}]

Options

  • min: sizemax: size

    Set the minimum and maximum sizes of the returned tuples. The defaults are 0 and 6, but this is overridden by the actual size if the like: option is specified.

  • like: { template }

    A template of generators used to fill the tuple. The generated tuples will have the same size as the template, and each element wil be generated from the corresponding generator in the template. For example, a Keyword list could be generated using

    iex> list(of: tuple(like: { atom, string(chars: lower, max: 10) })) |> as_stream |> Enum.take(5)

value(val)

Generates an infinite stream where each element is its parameter.

Example

iex> import Pollution.{Generator, VG}
iex> value("nom") |> as_stream |> Enum.take(3)
["nom", "nom", "nom"]