Skip to content

Commit

Permalink
strconv: add tests and fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Oct 28, 2023
1 parent 181fab8 commit c379872
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 3 deletions.
16 changes: 13 additions & 3 deletions strconv/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ func TestParseDecimal(t *testing.T) {
{"5.1", 5.1},
{"0.0000000000000000000000000005", 5e-28},
{"18446744073709551620", 18446744073709551620.0},
//{"1000000000000000000000000.0000", 1e24}, // TODO
//{"1000000000000000000000000000000000000000000", 1e42}, // TODO
{"1000000000000000000000000.0000", 1e24}, // TODO
{"1000000000000000000000000000000000000000000", 1e42}, // TODO
}
for _, tt := range tests {
t.Run(fmt.Sprint(tt.f), func(t *testing.T) {
f, n := ParseDecimal([]byte(tt.f))
test.T(t, n, len(tt.f))
test.T(t, f, tt.expected)
test.Float(t, f, tt.expected)
})
}
}
Expand All @@ -47,3 +47,13 @@ func TestParseDecimalError(t *testing.T) {
})
}
}

func FuzzParseDecimal(f *testing.F) {

Check failure on line 51 in strconv/decimal_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: testing.F
f.Add("5")
f.Add("5.1")
f.Add("18446744073709551620")
f.Add("0.0000000000000000000000000005")
f.Fuzz(func(t *testing.T, s string) {
ParseDecimal([]byte(s))
})
}
60 changes: 60 additions & 0 deletions strconv/float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,66 @@ func TestAppendFloat(t *testing.T) {
test.String(t, string(b[:5]), "12.34", "in buffer")
}

func FuzzParseFloat(f *testing.F) {

Check failure on line 118 in strconv/float_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: testing.F
f.Add("5")
f.Add("99")
f.Add("18446744073709551615")
f.Add("5")
f.Add("5.1")
f.Add("-5.1")
f.Add("5.1e-2")
f.Add("5.1e+2")
f.Add("0.0e1")
f.Add("18446744073709551620")
f.Add("1e23")
f.Fuzz(func(t *testing.T, s string) {
ParseFloat([]byte(s))
})
}

func FuzzAppendFloat(f *testing.F) {

Check failure on line 135 in strconv/float_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: testing.F
f.Add(0.0, 6)
f.Add(1.0, 6)
f.Add(9.0, 6)
f.Add(9.99999, 6)
f.Add(123.0, 6)
f.Add(0.123456, 6)
f.Add(0.066, 6)
f.Add(0.0066, 6)
f.Add(12e2, 6)
f.Add(12e3, 6)
f.Add(0.1, 6)
f.Add(0.001, 6)
f.Add(0.0001, 6)
f.Add(-1.0, 6)
f.Add(-123.0, 6)
f.Add(-123.456, 6)
f.Add(-12e3, 6)
f.Add(-0.1, 6)
f.Add(-0.0001, 6)
f.Add(0.000100009, 10)
f.Add(0.0001000009, 10)
f.Add(1e18, 0)
f.Add(1e1, 0)
f.Add(1e2, 1)
f.Add(1e3, 2)
f.Add(1e10, -1)
f.Add(1e15, -1)
f.Add(1e-5, 6)
f.Add(math.NaN(), 0)
f.Add(math.Inf(1), 0)
f.Add(math.Inf(-1), 0)
f.Add(0.0, 19)
f.Add(0.000923361977200859392, -1)
f.Add(1234.0, 2)
f.Add(12345.0, 2)
f.Add(12.345, 2)
f.Add(12.345, 3)
f.Fuzz(func(t *testing.T, f float64, prec int) {
AppendFloat([]byte{}, f, prec)
})
}

////////////////////////////////////////////////////////////////

func TestAppendFloatRandom(t *testing.T) {
Expand Down
19 changes: 19 additions & 0 deletions strconv/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ func TestParseIntError(t *testing.T) {
}
}

func FuzzParseInt(f *testing.F) {

Check failure on line 56 in strconv/int_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: testing.F
f.Add("5")
f.Add("-99")
f.Add("9223372036854775807")
f.Add("-9223372036854775808")
f.Fuzz(func(t *testing.T, s string) {
ParseInt([]byte(s))
})
}

func TestParseUint(t *testing.T) {
intTests := []struct {
i string
Expand Down Expand Up @@ -91,6 +101,15 @@ func TestParseUintError(t *testing.T) {
}
}

func FuzzParseUint(f *testing.F) {

Check failure on line 104 in strconv/int_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: testing.F
f.Add("5")
f.Add("99")
f.Add("18446744073709551615")
f.Fuzz(func(t *testing.T, s string) {
ParseUint([]byte(s))
})
}

func TestLenInt(t *testing.T) {
lenIntTests := []struct {
i int64
Expand Down
34 changes: 34 additions & 0 deletions strconv/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,37 @@ func TestAppendNumber(t *testing.T) {
// coverage
test.String(t, string(AppendNumber(make([]byte, 0, 7), 12345, 1, 3, -1, -1)), "1.234,5")
}

func FuzzParseNumber(f *testing.F) {

Check failure on line 66 in strconv/number_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: testing.F
f.Add("5")
f.Add("-5")
f.Add("5,0")
f.Add("5,0a")
f.Add("-1000,00")
f.Add("9223372036854775807")
f.Add("-9223372036854775807")
f.Add("-9223372036854775808")
f.Add("92233720368547758070")
f.Add("-92233720368547758080")
f.Fuzz(func(t *testing.T, s string) {
ParseNumber([]byte(s), '.', ',')
})
}

func FuzzAppendNumber(f *testing.F) {

Check failure on line 82 in strconv/number_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: testing.F
f.Add(int64(0), 0)
f.Add(int64(0), -1)
f.Add(int64(0), 2)
f.Add(int64(1), 2)
f.Add(int64(-1), 2)
f.Add(int64(100), 2)
f.Add(int64(-100), 2)
f.Add(int64(1000), 0)
f.Add(int64(100000), 2)
f.Add(int64(123456789012), 2)
f.Add(int64(9223372036854775807), 2)
f.Add(int64(-9223372036854775808), 2)
f.Fuzz(func(t *testing.T, num int64, dec int) {
AppendNumber([]byte{}, num, dec, 3, '.', ',')
})
}

0 comments on commit c379872

Please sign in to comment.