Skip to content

Commit 7c7a782

Browse files
authored
Fix Int* and UInt* return only 0s (#167)
1 parent d66c622 commit 7c7a782

File tree

2 files changed

+212
-16
lines changed

2 files changed

+212
-16
lines changed

faker.go

+33-11
Original file line numberDiff line numberDiff line change
@@ -103,53 +103,75 @@ func (f Faker) Int() int {
103103

104104
// Int8 returns a fake Int8 number for Faker
105105
func (f Faker) Int8() int8 {
106-
return int8(f.Int())
106+
return int8(f.IntBetween(0, math.MaxInt8))
107107
}
108108

109109
// Int16 returns a fake Int16 number for Faker
110110
func (f Faker) Int16() int16 {
111-
return int16(f.Int())
111+
return int16(f.IntBetween(0, math.MaxInt16))
112112
}
113113

114114
// Int32 returns a fake Int32 number for Faker
115115
func (f Faker) Int32() int32 {
116-
return int32(f.Int())
116+
return int32(f.IntBetween(0, math.MaxInt32))
117117
}
118118

119119
// Int64 returns a fake Int64 number for Faker
120120
func (f Faker) Int64() int64 {
121-
return int64(f.Int())
121+
return int64(f.IntBetween(0, math.MaxInt64))
122122
}
123123

124124
// UInt returns a fake UInt number for Faker
125125
func (f Faker) UInt() uint {
126-
max := math.MaxInt
127-
return uint(f.IntBetween(0, max))
126+
return uint(f.IntBetween(0, math.MaxInt))
128127
}
129128

130129
// UInt8 returns a fake UInt8 number for Faker
131130
func (f Faker) UInt8() uint8 {
132-
return uint8(f.Int())
131+
return uint8(f.IntBetween(0, math.MaxUint8))
133132
}
134133

135134
// UInt16 returns a fake UInt16 number for Faker
136135
func (f Faker) UInt16() uint16 {
137-
return uint16(f.Int())
136+
return uint16(f.IntBetween(0, math.MaxUint16))
138137
}
139138

140139
// UInt32 returns a fake UInt32 number for Faker
141140
func (f Faker) UInt32() uint32 {
142-
return uint32(f.Int())
141+
return uint32(f.IntBetween(0, math.MaxUint32))
143142
}
144143

145144
// UInt64 returns a fake UInt64 number for Faker
146145
func (f Faker) UInt64() uint64 {
147-
return uint64(f.Int())
146+
// Using MaxUint32 to avoid overflow
147+
return uint64(f.IntBetween(0, math.MaxUint32))
148148
}
149149

150150
// IntBetween returns a fake Int between a given minimum and maximum values for Faker
151151
func (f Faker) IntBetween(min, max int) int {
152-
diff := max - min
152+
if min > max {
153+
// Swap values
154+
return f.IntBetween(max, min)
155+
}
156+
157+
diff := 0
158+
// Edge case when min and max are actual min and max integers,
159+
// since we cannot store 2 * math.MaxInt, we instead split the range in:
160+
// - 50% chance to return a negative number
161+
// - 50% chance to return a positive number
162+
if min == math.MinInt64 && max == math.MaxInt64 {
163+
if f.Bool() {
164+
// negatives
165+
max = 0
166+
diff = math.MaxInt
167+
} else {
168+
// positives
169+
min = 0
170+
diff = math.MaxInt
171+
}
172+
} else {
173+
diff = max - min
174+
}
153175

154176
var value int
155177
if diff == 0 {

faker_test.go

+179-5
Original file line numberDiff line numberDiff line change
@@ -125,24 +125,80 @@ func TestInt8(t *testing.T) {
125125
Expect(t, fmt.Sprintf("%T", value), "int8")
126126
}
127127

128+
func TestInt8ReturnsNonZeroValues(t *testing.T) {
129+
f := New()
130+
nonZero := false
131+
for i := 0; i < 100; i++ {
132+
value := f.Int8()
133+
if value > 0 {
134+
nonZero = true
135+
break
136+
}
137+
}
138+
139+
Expect(t, nonZero, true)
140+
}
141+
128142
func TestInt16(t *testing.T) {
129143
f := New()
130144
value := f.Int16()
131145
Expect(t, fmt.Sprintf("%T", value), "int16")
132146
}
133147

148+
func TestInt16ReturnsNonZeroValues(t *testing.T) {
149+
f := New()
150+
nonZero := false
151+
for i := 0; i < 100; i++ {
152+
value := f.Int16()
153+
if value > 0 {
154+
nonZero = true
155+
break
156+
}
157+
}
158+
159+
Expect(t, nonZero, true)
160+
}
161+
134162
func TestInt32(t *testing.T) {
135163
f := New()
136164
value := f.Int32()
137165
Expect(t, fmt.Sprintf("%T", value), "int32")
138166
}
139167

168+
func TestInt32ReturnsNonZeroValues(t *testing.T) {
169+
f := New()
170+
nonZero := false
171+
for i := 0; i < 100; i++ {
172+
value := f.Int32()
173+
if value > 0 {
174+
nonZero = true
175+
break
176+
}
177+
}
178+
179+
Expect(t, nonZero, true)
180+
}
181+
140182
func TestInt64(t *testing.T) {
141183
f := New()
142184
value := f.Int64()
143185
Expect(t, fmt.Sprintf("%T", value), "int64")
144186
}
145187

188+
func TestInt64ReturnsNonZeroValues(t *testing.T) {
189+
f := New()
190+
nonZero := false
191+
for i := 0; i < 100; i++ {
192+
value := f.Int64()
193+
if value > 0 {
194+
nonZero = true
195+
break
196+
}
197+
}
198+
199+
Expect(t, nonZero, true)
200+
}
201+
146202
func TestIntBetween(t *testing.T) {
147203
f := New()
148204
value := f.IntBetween(1, 100)
@@ -151,6 +207,13 @@ func TestIntBetween(t *testing.T) {
151207
Expect(t, true, value <= 100)
152208
}
153209

210+
func TestIntBetweenWithSameValues(t *testing.T) {
211+
f := New()
212+
value := f.IntBetween(1, 1)
213+
Expect(t, fmt.Sprintf("%T", value), "int")
214+
Expect(t, 1, value)
215+
}
216+
154217
func TestIntBetweenNegativeValues(t *testing.T) {
155218
f := New()
156219
value := f.IntBetween(-100, -50)
@@ -159,12 +222,53 @@ func TestIntBetweenNegativeValues(t *testing.T) {
159222
Expect(t, true, value <= -50)
160223
}
161224

162-
func TestIntBetweenWithMaxValues(t *testing.T) {
225+
func TestIntBetweenWithNegativeMinGeneratesNegativeValues(t *testing.T) {
163226
f := New()
164-
value := f.IntBetween(math.MinInt, math.MaxInt)
165-
Expect(t, fmt.Sprintf("%T", value), "int")
166-
Expect(t, true, value >= math.MinInt)
167-
Expect(t, true, value <= math.MaxInt)
227+
foundNegative := false
228+
for i := 0; i < 100; i++ {
229+
value := f.IntBetween(-100, 100)
230+
if value < 0 {
231+
foundNegative = true
232+
break
233+
}
234+
}
235+
236+
Expect(t, true, foundNegative)
237+
}
238+
239+
func TestIntBetweenWithMinMaxIntReturnDifferentValues(t *testing.T) {
240+
f := New()
241+
value1 := f.IntBetween(math.MinInt, math.MaxInt)
242+
value2 := f.IntBetween(math.MinInt, math.MaxInt)
243+
Expect(t, value1 != value2, true, value1, value2)
244+
}
245+
246+
func TestIntBetweenWithMinMaxInt8ReturnDifferentValues(t *testing.T) {
247+
f := New()
248+
value1 := f.IntBetween(math.MinInt8, math.MaxInt8)
249+
value2 := f.IntBetween(math.MinInt8, math.MaxInt8)
250+
Expect(t, value1 != value2, true, value1, value2)
251+
}
252+
253+
func TestIntBetweenWithMinMaxInt16ReturnDifferentValues(t *testing.T) {
254+
f := New()
255+
value1 := f.IntBetween(math.MinInt16, math.MaxInt16)
256+
value2 := f.IntBetween(math.MinInt16, math.MaxInt16)
257+
Expect(t, value1 != value2, true, value1, value2)
258+
}
259+
260+
func TestIntBetweenWithMinMaxInt32ReturnDifferentValues(t *testing.T) {
261+
f := New()
262+
value1 := f.IntBetween(math.MinInt32, math.MaxInt32)
263+
value2 := f.IntBetween(math.MinInt32, math.MaxInt32)
264+
Expect(t, value1 != value2, true, value1, value2)
265+
}
266+
267+
func TestIntBetweenWithMinMaxInt64ReturnDifferentValues(t *testing.T) {
268+
f := New()
269+
value1 := f.IntBetween(math.MinInt64, math.MaxInt64)
270+
value2 := f.IntBetween(math.MinInt64, math.MaxInt64)
271+
Expect(t, value1 != value2, true, value1, value2)
168272
}
169273

170274
func TestIntBetweenWithInvalidInterval(t *testing.T) {
@@ -205,30 +309,100 @@ func TestUint(t *testing.T) {
205309
Expect(t, fmt.Sprintf("%T", value), "uint")
206310
}
207311

312+
func TestUIntReturnsNonZeroValues(t *testing.T) {
313+
f := New()
314+
nonZero := false
315+
for i := 0; i < 100; i++ {
316+
value := f.UInt()
317+
if value > 0 {
318+
nonZero = true
319+
break
320+
}
321+
}
322+
323+
Expect(t, nonZero, true)
324+
}
325+
208326
func TestUint8(t *testing.T) {
209327
f := New()
210328
value := f.UInt8()
211329
Expect(t, fmt.Sprintf("%T", value), "uint8")
212330
}
213331

332+
func TestUInt8ReturnsNonZeroValues(t *testing.T) {
333+
f := New()
334+
nonZero := false
335+
for i := 0; i < 100; i++ {
336+
value := f.UInt8()
337+
if value > 0 {
338+
nonZero = true
339+
break
340+
}
341+
}
342+
343+
Expect(t, nonZero, true)
344+
}
345+
214346
func TestUint16(t *testing.T) {
215347
f := New()
216348
value := f.UInt16()
217349
Expect(t, fmt.Sprintf("%T", value), "uint16")
218350
}
219351

352+
func TestUInt16ReturnsNonZeroValues(t *testing.T) {
353+
f := New()
354+
nonZero := false
355+
for i := 0; i < 100; i++ {
356+
value := f.UInt16()
357+
if value > 0 {
358+
nonZero = true
359+
break
360+
}
361+
}
362+
363+
Expect(t, nonZero, true)
364+
}
365+
220366
func TestUint32(t *testing.T) {
221367
f := New()
222368
value := f.UInt32()
223369
Expect(t, fmt.Sprintf("%T", value), "uint32")
224370
}
225371

372+
func TestUInt32ReturnsNonZeroValues(t *testing.T) {
373+
f := New()
374+
nonZero := false
375+
for i := 0; i < 100; i++ {
376+
value := f.UInt32()
377+
if value > 0 {
378+
nonZero = true
379+
break
380+
}
381+
}
382+
383+
Expect(t, nonZero, true)
384+
}
385+
226386
func TestUint64(t *testing.T) {
227387
f := New()
228388
value := f.UInt64()
229389
Expect(t, fmt.Sprintf("%T", value), "uint64")
230390
}
231391

392+
func TestUInt64ReturnsNonZeroValues(t *testing.T) {
393+
f := New()
394+
nonZero := false
395+
for i := 0; i < 100; i++ {
396+
value := f.UInt64()
397+
if value > 0 {
398+
nonZero = true
399+
break
400+
}
401+
}
402+
403+
Expect(t, nonZero, true)
404+
}
405+
232406
func TestUIntBetween(t *testing.T) {
233407
f := New()
234408
value := f.UIntBetween(1, 100)

0 commit comments

Comments
 (0)