From 8930726562a73a608b9160e05e0f96b8d73938fa Mon Sep 17 00:00:00 2001 From: Mathieu Lemay <23462228+mathieu-lemay@users.noreply.github.com> Date: Wed, 9 Oct 2024 11:04:20 -0400 Subject: [PATCH 1/3] fix: Float functions should return the proper number of decimals (#178) --- faker.go | 50 +++++++++++++++++++++++++++++++++++++------------- faker_test.go | 31 ++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 14 deletions(-) diff --git a/faker.go b/faker.go index 641c163..e2492e2 100644 --- a/faker.go +++ b/faker.go @@ -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 diff --git a/faker_test.go b/faker_test.go index 5f7324e..6d29914 100644 --- a/faker_test.go +++ b/faker_test.go @@ -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) { From 8a60dcc1eab9fbefe27df3ea62183c0d5d93d0b1 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Wed, 9 Oct 2024 20:28:51 +0100 Subject: [PATCH 2/3] style: format code with Go fmt and Gofumpt (#179) This commit fixes the style issues introduced in 8930726 according to the output from Go fmt and Gofumpt. Details: None Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com> --- faker_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/faker_test.go b/faker_test.go index 6d29914..7ec0844 100644 --- a/faker_test.go +++ b/faker_test.go @@ -467,7 +467,7 @@ func TestFloat32(t *testing.T) { 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) + rounded := float32(math.Round(float64(value*100)) / 100) Expect(t, rounded, value) } From 75dde12631740945e650b9a5b2b2cffe4554a2d4 Mon Sep 17 00:00:00 2001 From: Jonathan Schweder Date: Tue, 29 Oct 2024 11:52:30 +0000 Subject: [PATCH 3/3] Remove % from address building number (#181) --- address.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/address.go b/address.go index f828fe5..8a56d4e 100644 --- a/address.go +++ b/address.go @@ -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",