Initial commit

This commit is contained in:
Hayden 2022-08-29 18:30:36 -08:00
commit 29f583e936
135 changed files with 18463 additions and 0 deletions

View file

@ -0,0 +1,37 @@
package faker
import (
"math/rand"
"time"
)
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
type Faker struct {
}
func NewFaker() *Faker {
rand.Seed(time.Now().UnixNano())
return &Faker{}
}
func (f *Faker) RandomString(length int) string {
b := make([]rune, length)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func (f *Faker) RandomEmail() string {
return f.RandomString(10) + "@email.com"
}
func (f *Faker) RandomBool() bool {
return rand.Intn(2) == 1
}
func (f *Faker) RandomNumber(min, max int) int {
return rand.Intn(max-min) + min
}

View file

@ -0,0 +1,95 @@
package faker
import (
"testing"
)
const Loops = 500
func ValidateUnique(values []string) bool {
for i := 0; i < len(values); i++ {
for j := i + 1; j < len(values); j++ {
if values[i] == values[j] {
return false
}
}
}
return true
}
func Test_GetRandomString(t *testing.T) {
t.Parallel()
// Test that the function returns a string of the correct length
var generated = make([]string, Loops)
faker := NewFaker()
for i := 0; i < Loops; i++ {
generated[i] = faker.RandomString(10)
}
if !ValidateUnique(generated) {
t.Error("Generated values are not unique")
}
}
func Test_GetRandomEmail(t *testing.T) {
t.Parallel()
// Test that the function returns a string of the correct length
var generated = make([]string, Loops)
faker := NewFaker()
for i := 0; i < Loops; i++ {
generated[i] = faker.RandomEmail()
}
if !ValidateUnique(generated) {
t.Error("Generated values are not unique")
}
}
func Test_GetRandomBool(t *testing.T) {
t.Parallel()
var trues = 0
var falses = 0
faker := NewFaker()
for i := 0; i < Loops; i++ {
if faker.RandomBool() {
trues++
} else {
falses++
}
}
if trues == 0 || falses == 0 {
t.Error("Generated boolean don't appear random")
}
}
func Test_RandomNumber(t *testing.T) {
t.Parallel()
f := NewFaker()
const MIN = 0
const MAX = 100
last := MIN - 1
for i := 0; i < Loops; i++ {
n := f.RandomNumber(MIN, MAX)
if n == last {
t.Errorf("RandomNumber() failed to generate unique number")
}
if n < MIN || n > MAX {
t.Errorf("RandomNumber() failed to generate a number between %v and %v", MIN, MAX)
}
}
}