Skip to content

Commit

Permalink
Merge branch 'master' into add-random-map-key-and-value
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswdr authored Nov 1, 2024
2 parents 017255b + 75dde12 commit eee328e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
2 changes: 1 addition & 1 deletion address.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var (

citySuffix = []string{"town", "ton", "land", "ville", "berg", "burgh", "borough", "bury", "view", "port", "mouth", "stad", "furt", "chester", "mouth", "fort", "haven", "side", "shire"}

buildingNumber = []string{"%####", "%###", "%##"}
buildingNumber = []string{"####", "###", "##"}

streetSuffix = []string{
"Alley", "Avenue",
Expand Down
50 changes: 37 additions & 13 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,54 @@ func (f Faker) RandomNumber(size int) int {

// RandomFloat returns a fake random float number for Faker
func (f Faker) RandomFloat(maxDecimals, min, max int) float64 {
s := fmt.Sprintf("%d.%d", f.IntBetween(min, max-1), f.IntBetween(1, maxDecimals))
value, _ := strconv.ParseFloat(s, 32)
return value
value := float64(f.IntBetween(min, max-1))
if maxDecimals < 1 {
return value
}

p := int(math.Pow10(maxDecimals))
decimals := float64(f.IntBetween(0, p)) / float64(p)

return value + decimals
}

// Float returns a fake random float number for Faker
func (f Faker) Float(maxDecimals, min, max int) float64 {
s := fmt.Sprintf("%d.%d", f.IntBetween(min, max-1), f.IntBetween(1, maxDecimals))
value, _ := strconv.ParseFloat(s, 32)
return value
value := float64(f.IntBetween(min, max-1))
if maxDecimals < 1 {
return value
}

p := int(math.Pow10(maxDecimals))
decimals := float64(f.IntBetween(0, p)) / float64(p)

return value + decimals
}

// Float32 returns a fake random float64 number for Faker
// Float32 returns a fake random float32 number for Faker
func (f Faker) Float32(maxDecimals, min, max int) float32 {
s := fmt.Sprintf("%d.%d", f.IntBetween(min, max-1), f.IntBetween(1, maxDecimals))
value, _ := strconv.ParseFloat(s, 32)
return float32(value)
value := float32(f.IntBetween(min, max-1))
if maxDecimals < 1 {
return value
}

p := int(math.Pow10(maxDecimals))
decimals := float32(f.IntBetween(0, p)) / float32(p)

return value + decimals
}

// Float64 returns a fake random float64 number for Faker
func (f Faker) Float64(maxDecimals, min, max int) float64 {
s := fmt.Sprintf("%d.%d", f.IntBetween(min, max-1), f.IntBetween(1, maxDecimals))
value, _ := strconv.ParseFloat(s, 32)
return float64(value)
value := float64(f.IntBetween(min, max-1))
if maxDecimals < 1 {
return value
}

p := int(math.Pow10(maxDecimals))
decimals := float64(f.IntBetween(0, p)) / float64(p)

return value + decimals
}

// Int returns a fake Int number for Faker
Expand Down
31 changes: 30 additions & 1 deletion faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,39 @@ func TestUInt64Between(t *testing.T) {

func TestRandomFloat(t *testing.T) {
f := New()
value := f.RandomFloat(1, 1, 100)
value := f.RandomFloat(2, 1, 100)
Expect(t, fmt.Sprintf("%T", value), "float64")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
Expect(t, math.Round(value*100)/100, value)
}

func TestFloat(t *testing.T) {
f := New()
value := f.Float(2, 1, 100)
Expect(t, fmt.Sprintf("%T", value), "float64")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
Expect(t, math.Round(value*100)/100, value)
}

func TestFloat32(t *testing.T) {
f := New()
value := f.Float32(2, 1, 100)
Expect(t, fmt.Sprintf("%T", value), "float32")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
rounded := float32(math.Round(float64(value*100)) / 100)
Expect(t, rounded, value)
}

func TestFloat64(t *testing.T) {
f := New()
value := f.Float64(2, 1, 100)
Expect(t, fmt.Sprintf("%T", value), "float64")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
Expect(t, math.Round(value*100)/100, value)
}

func TestLetter(t *testing.T) {
Expand Down

0 comments on commit eee328e

Please sign in to comment.