Skip to content

Commit

Permalink
Fix Int* and UInt* return only 0s
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswdr committed Feb 18, 2024
1 parent d66c622 commit 9d2fee3
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 11 deletions.
25 changes: 15 additions & 10 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,52 +103,57 @@ func (f Faker) Int() int {

// Int8 returns a fake Int8 number for Faker
func (f Faker) Int8() int8 {
return int8(f.Int())
return int8(f.IntBetween(0, math.MaxInt8))
}

// Int16 returns a fake Int16 number for Faker
func (f Faker) Int16() int16 {
return int16(f.Int())
return int16(f.IntBetween(0, math.MaxInt16))
}

// Int32 returns a fake Int32 number for Faker
func (f Faker) Int32() int32 {
return int32(f.Int())
return int32(f.IntBetween(0, math.MaxInt32))
}

// Int64 returns a fake Int64 number for Faker
func (f Faker) Int64() int64 {
return int64(f.Int())
return int64(f.IntBetween(0, math.MaxInt64))
}

// UInt returns a fake UInt number for Faker
func (f Faker) UInt() uint {
max := math.MaxInt
return uint(f.IntBetween(0, max))
return uint(f.IntBetween(0, math.MaxInt))
}

// UInt8 returns a fake UInt8 number for Faker
func (f Faker) UInt8() uint8 {
return uint8(f.Int())
return uint8(f.IntBetween(0, math.MaxUint8))
}

// UInt16 returns a fake UInt16 number for Faker
func (f Faker) UInt16() uint16 {
return uint16(f.Int())
return uint16(f.IntBetween(0, math.MaxUint16))
}

// UInt32 returns a fake UInt32 number for Faker
func (f Faker) UInt32() uint32 {
return uint32(f.Int())
return uint32(f.IntBetween(0, math.MaxUint32))
}

// UInt64 returns a fake UInt64 number for Faker
func (f Faker) UInt64() uint64 {
return uint64(f.Int())
// Using MaxUint32 to avoid overflow
return uint64(f.IntBetween(0, math.MaxUint32))
}

// IntBetween returns a fake Int between a given minimum and maximum values for Faker
func (f Faker) IntBetween(min, max int) int {
if min > max {
// Swap values
return f.IntBetween(max, min)
}

diff := max - min

var value int
Expand Down
181 changes: 180 additions & 1 deletion faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,80 @@ func TestInt8(t *testing.T) {
Expect(t, fmt.Sprintf("%T", value), "int8")
}

func TestInt8ReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.Int8()
if value > 0 {
nonZero = true
break
}
}

Expect(t, nonZero, true)
}

func TestInt16(t *testing.T) {
f := New()
value := f.Int16()
Expect(t, fmt.Sprintf("%T", value), "int16")
}

func TestInt16ReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.Int16()
if value > 0 {
nonZero = true
break
}
}

Expect(t, nonZero, true)
}

func TestInt32(t *testing.T) {
f := New()
value := f.Int32()
Expect(t, fmt.Sprintf("%T", value), "int32")
}

func TestInt32ReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.Int32()
if value > 0 {
nonZero = true
break
}
}

Expect(t, nonZero, true)
}

func TestInt64(t *testing.T) {
f := New()
value := f.Int64()
Expect(t, fmt.Sprintf("%T", value), "int64")
}

func TestInt64ReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.Int64()
if value > 0 {
nonZero = true
break
}
}

Expect(t, nonZero, true)
}

func TestIntBetween(t *testing.T) {
f := New()
value := f.IntBetween(1, 100)
Expand All @@ -151,6 +207,13 @@ func TestIntBetween(t *testing.T) {
Expect(t, true, value <= 100)
}

func TestIntBetweenWithSameValues(t *testing.T) {
f := New()
value := f.IntBetween(1, 1)
Expect(t, fmt.Sprintf("%T", value), "int")
Expect(t, 1, value)
}

func TestIntBetweenNegativeValues(t *testing.T) {
f := New()
value := f.IntBetween(-100, -50)
Expand All @@ -159,14 +222,60 @@ func TestIntBetweenNegativeValues(t *testing.T) {
Expect(t, true, value <= -50)
}

func TestIntBetweenWithMaxValues(t *testing.T) {
func TestIntBetweenWithNegativeMinGeneratesNegativeValues(t *testing.T) {
f := New()
foundNegative := false
for i := 0; i < 100; i++ {
value := f.IntBetween(-100, 100)
if value < 0 {
foundNegative = true
break
}
}

Expect(t, true, foundNegative)
}

func TestIntBetweenWithMinMaxValues(t *testing.T) {
f := New()
value := f.IntBetween(math.MinInt, math.MaxInt)
Expect(t, fmt.Sprintf("%T", value), "int")
Expect(t, true, value >= math.MinInt)
Expect(t, true, value <= math.MaxInt)
}

func TestIntBetweenWithMinMaxInt8Values(t *testing.T) {
f := New()
value := f.IntBetween(math.MinInt8, math.MaxInt8)
Expect(t, fmt.Sprintf("%T", value), "int")
Expect(t, true, value >= math.MinInt8)
Expect(t, true, value <= math.MaxInt8)
}

func TestIntBetweenWithMinMaxInt16Values(t *testing.T) {
f := New()
value := f.IntBetween(math.MinInt16, math.MaxInt16)
Expect(t, fmt.Sprintf("%T", value), "int")
Expect(t, true, value >= math.MinInt16)
Expect(t, true, value <= math.MaxInt16)
}

func TestIntBetweenWithMinMaxInt32Values(t *testing.T) {
f := New()
value := f.IntBetween(math.MinInt32, math.MaxInt32)
Expect(t, fmt.Sprintf("%T", value), "int")
Expect(t, true, value >= math.MinInt32)
Expect(t, true, value <= math.MaxInt32)
}

func TestIntBetweenWithMinMaxInt64Values(t *testing.T) {
f := New()
value := f.IntBetween(math.MinInt64, math.MaxInt64)
Expect(t, fmt.Sprintf("%T", value), "int")
Expect(t, true, value >= math.MinInt64)
Expect(t, true, value <= math.MaxInt64)
}

func TestIntBetweenWithInvalidInterval(t *testing.T) {
f := New()
value := f.IntBetween(100, 50)
Expand Down Expand Up @@ -205,30 +314,100 @@ func TestUint(t *testing.T) {
Expect(t, fmt.Sprintf("%T", value), "uint")
}

func TestUIntReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.UInt()
if value > 0 {
nonZero = true
break
}
}

Expect(t, nonZero, true)
}

func TestUint8(t *testing.T) {
f := New()
value := f.UInt8()
Expect(t, fmt.Sprintf("%T", value), "uint8")
}

func TestUInt8ReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.UInt8()
if value > 0 {
nonZero = true
break
}
}

Expect(t, nonZero, true)
}

func TestUint16(t *testing.T) {
f := New()
value := f.UInt16()
Expect(t, fmt.Sprintf("%T", value), "uint16")
}

func TestUInt16ReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.UInt16()
if value > 0 {
nonZero = true
break
}
}

Expect(t, nonZero, true)
}

func TestUint32(t *testing.T) {
f := New()
value := f.UInt32()
Expect(t, fmt.Sprintf("%T", value), "uint32")
}

func TestUInt32ReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.UInt32()
if value > 0 {
nonZero = true
break
}
}

Expect(t, nonZero, true)
}

func TestUint64(t *testing.T) {
f := New()
value := f.UInt64()
Expect(t, fmt.Sprintf("%T", value), "uint64")
}

func TestUInt64ReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.UInt64()
if value > 0 {
nonZero = true
break
}
}

Expect(t, nonZero, true)
}

func TestUIntBetween(t *testing.T) {
f := New()
value := f.UIntBetween(1, 100)
Expand Down

0 comments on commit 9d2fee3

Please sign in to comment.