commit 10e811e30455352e6ee9a953afd77f643895ec2f Author: Charles Iliya Krempeaux Date: Thu Jan 29 00:45:50 2015 -0800 initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ff0d4ea --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2015 Charles Iliya Krempeaux :: http://changelog.ca/ + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..5fcdabb --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# go-arbitrary + +**go-arbitrary** is a Go library which provides tools for generating arbitrary data. + +## Example +``` +src := rand.NewSource( time.Now().UTC().UnixNano() ) +arb := arbitrary.New(src) + +// Generate an arbitrary bool. +// Might be false, might be true. +bl := arb.Bool() + +// Generate an arbitrary string. +s := arb.String() +``` diff --git a/arbitrary.go b/arbitrary.go new file mode 100644 index 0000000..a2ae939 --- /dev/null +++ b/arbitrary.go @@ -0,0 +1,19 @@ +package arbitrary + + +import "math/rand" + + +// A T is a source of arbitrary data. +type T struct { + randomness *rand.Rand +} + + +// New returns a new T. +func New(src rand.Source) T { + + randomness := rand.New(src) + + return T{randomness: randomness} +} diff --git a/bool.go b/bool.go new file mode 100644 index 0000000..61ce620 --- /dev/null +++ b/bool.go @@ -0,0 +1,7 @@ +package arbitrary + + +// Bool returns an arbitrary bool. +func (arb T) Bool() bool { + return 0 == arb.randomness.Intn(2) +} diff --git a/bool_test.go b/bool_test.go new file mode 100644 index 0000000..72d47d5 --- /dev/null +++ b/bool_test.go @@ -0,0 +1,37 @@ +package arbitrary + + +import "testing" + +import "math/rand" +import "time" + + +func TestBool(t *testing.T) { + + src := rand.NewSource( time.Now().UTC().UnixNano() ) + + arb := New(src) + + seen_false := false + seen_true := false + + const limit = 50 + for i:=0; i num_arbitrary_runes { +/////////////////////// RETURN + return []rune{} + } + + // Construct the arbitrary []rune. + arbitrary_runes := make([]rune, num_arbitrary_runes) + + for i,_ := range arbitrary_runes { + arbitrary_runes[i] = runes[arb.randomness.Intn(num_arbitrary_runes)] + } + + // Return. + return arbitrary_runes +} diff --git a/runes_test.go b/runes_test.go new file mode 100644 index 0000000..e7da43b --- /dev/null +++ b/runes_test.go @@ -0,0 +1,141 @@ +package arbitrary + + +import "testing" + +import "math/rand" +import "strings" +import "time" + + +func TestRunes(t *testing.T) { + + src := rand.NewSource( time.Now().UTC().UnixNano() ) + + arb := New(src) + + seen := make(map[string]int) + + const limit = 50 + for i:=0; i,.?/` + + arb := New(src) + + seen := make(map[string]int) + + const limit = 50 + for i:=0; i,.?/`) + + arb := New(src) + + seen := make(map[string]int) + + const limit = 50 + for i:=0; i,.?/` + + arb := New(src) + + seen := make(map[string]int) + + const limit = 50 + for i:=0; i,.?/`) + + arb := New(src) + + seen := make(map[string]int) + + const limit = 50 + for i:=0; i