Giter Site home page Giter Site logo

fuzz's Introduction

Motoko fuzzing library

Install

mops add fuzz

Import

import Fuzz "mo:fuzz";

Initialize

Default (uses Time.now() as a seed)

let fuzz = Fuzz.Fuzz();

or init from seed

let seed = 123456789;
let fuzz = Fuzz.fromSeed(seed);

or init from a random blob

let blob = await Random.blob();
let fuzz = Fuzz.fromBlob(blob);

or provide a function that generates a random Nat

type Generator = {
	next() : Nat;
}
let fuzz = Fuzz.create(generator);

Usage

Numbers

Nat, Nat8, Nat16, Nat32, Nat64, Int, Int8, Int16, Int32, Int64, Float

Generate a random number

let randNat = fuzz.nat.random();
let randNat32 = fuzz.nat32.random();
let randInt16 = fuzz.int16.random();
let randFloat = fuzz.float.random();

Generate a random number in the range (both bounds inclusive)

let randNat = fuzz.nat.randomRange(2**128, 2**256);
let randNat32 = fuzz.nat32.randomRange(1_100, 10_000);
let randInt16 = fuzz.int16.randomRange(8, 99);
let randFloat = fuzz.float.randomRange(0.5, 10.88);

Get min/max value for the type

let nat16Min = fuzz.nat.min();
let int32Min = fuzz.int32.min();
let int32Max = fuzz.int32.max();

Array

Generate a random array of size 1000 with random values of Nat8

let arrayNat8 = fuzz.array.randomArray(1000, fuzz.nat8.random);

Generate a random array of size 500 with custom type values

type MyType = {
	x : Nat;
	b : Bool;
};
let randArray = fuzz.array.randomArray<MyType>(500, func() {
	return {
		x = fuzz.nat.random();
		b = fuzz.bool.random();
	};
});

Get random value from array

let myArr = [1,2,4,4,4,55,1,2];

let item = fuzz.array.randomValue(myArr);
// or
let (index, item) = fuzz.array.randomEntry(myArr);

Blob

Generate a random blob of size 1024

let randBlob = fuzz.blob.randomBlob(1024);

Bool

Generate a random bool value

let randBool = fuzz.bool.random();

Option

Randomly wrap option

let myVal = { x = 3 };
let randOpt = fuzz.option.optOrNot(myVal); // => either ?{ x = 3 } or null

Char

Return a random alphabetic char ([a-zA-Z])

let char = fuzz.char.randomAlphabetic();

Return a random alphanumeric char ([a-zA-Z0-9])

let char = fuzz.char.randomAlphanumeric();

Return a random ascii char ([a-zA-Z0-9!"#$%&'()*+,-./,':;<=>?[\]^_`{|}~])

let char = fuzz.char.randomAscii();

Return a random char from a given set

let char = fuzz.char.randomUnicode(['a', 'b', '%', '.', ',', '0', '1']);

Text

Return random text of given size

let text = fuzz.text.randomText(17); // => "Lorem ipsum dolor"
let text = fuzz.text.randomText(8); // => "Lorem ip"

Return random alphabetic text of given size ([a-zA-Z])

let text = fuzz.text.randomAlphabetic(5); // => "aadkd"
let text = fuzz.text.randomAlphabetic(5); // => "kfiky"

Return random alphanumeric text of given size ([a-zA-Z0-9])

let text = fuzz.text.randomAlphanumeric(3); // => "po8"
let text = fuzz.text.randomAlphanumeric(3); // => "68r"

Return random ascii text of given size ([a-zA-Z0-9!"#$%&'()*+,-./,':;<=>?[\]^_`{|}~])

let text = fuzz.text.randomAscii(2); // => "t@"
let text = fuzz.text.randomAscii(2); // => "pl"

Principal

Generate a random Principal of a given blob size

let principal = fuzz.principal.randomPrincipal(10);
Principal.toText(principal); // => "4u4hq-3aaae-bagaz-jaeba-kaq"

ICRC1

Generate random ICRC1 account

type Account = {
	owner: Principal;
	subaccount: ?Blob;
};
let account: Account = fuzz.icrc1.randomAccount();
// or just
let account2 = fuzz.icrc1.randomAccount();

Generate random ICRC1 subaccount

let subaccount: Blob = fuzz.icrc1.randomSubaccount();

fuzz's People

Contributors

tomijaga avatar zenvoich avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

fuzz's Issues

randomAlphanumeric(30) returns a string of 31 chars

Hi Zen,

Was using this package today and strangely the randomAlphanumeric(30) is returning 31 chars.
Eg: eYuerqcSz8WPWG3kUOP1kUyVPR9jnBJ

Can you double check what might be happening?

Thanks for this lib ๐Ÿ‘

Shuffle Buffer or Mutable Array

Hey @ZenVoich,
I implemented thisshuffle function in one of my unit tests and thought it would be a good addition to this library.
It could potentially be included as a .shuffle() method in a new FuzzBuffer module, a FuzzArrayMut module, or perhaps even both.
If you're okay with it, I could submit a PR to add this feature.

   func shuffle_buffer<A>(fuzz : Fuzz.Fuzzer, buffer : Buffer.Buffer<A>) {
       for (i in Iter.range(0, buffer.size() - 3)) {
           let j = fuzz.nat.randomRange(i + 1, buffer.size() - 1);
           let tmp = buffer.get(i);
           buffer.put(i, buffer.get(j));
           buffer.put(j, tmp);
       };
   };

The buffer-size() - 3 might be confusing but here's how the selection would look in the last iteration:
buffer -> [..., i, i + 1, buffer.size() - 1]

j could be i + 1 or buffer.size() - 1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.