github.com/qxnw/lib4go@v0.0.0-20180426074627-c80c7e84b925/influxdb/models/inline_strconv_parse_test.go (about) 1 package models 2 3 import ( 4 "strconv" 5 "testing" 6 "testing/quick" 7 ) 8 9 func TestParseIntBytesEquivalenceFuzz(t *testing.T) { 10 f := func(b []byte, base int, bitSize int) bool { 11 exp, expErr := strconv.ParseInt(string(b), base, bitSize) 12 got, gotErr := parseIntBytes(b, base, bitSize) 13 14 return exp == got && checkErrs(expErr, gotErr) 15 } 16 17 cfg := &quick.Config{ 18 MaxCount: 10000, 19 } 20 21 if err := quick.Check(f, cfg); err != nil { 22 t.Fatal(err) 23 } 24 } 25 26 func TestParseIntBytesValid64bitBase10EquivalenceFuzz(t *testing.T) { 27 buf := []byte{} 28 f := func(n int64) bool { 29 buf = strconv.AppendInt(buf[:0], n, 10) 30 31 exp, expErr := strconv.ParseInt(string(buf), 10, 64) 32 got, gotErr := parseIntBytes(buf, 10, 64) 33 34 return exp == got && checkErrs(expErr, gotErr) 35 } 36 37 cfg := &quick.Config{ 38 MaxCount: 10000, 39 } 40 41 if err := quick.Check(f, cfg); err != nil { 42 t.Fatal(err) 43 } 44 } 45 46 func TestParseFloatBytesEquivalenceFuzz(t *testing.T) { 47 f := func(b []byte, bitSize int) bool { 48 exp, expErr := strconv.ParseFloat(string(b), bitSize) 49 got, gotErr := parseFloatBytes(b, bitSize) 50 51 return exp == got && checkErrs(expErr, gotErr) 52 } 53 54 cfg := &quick.Config{ 55 MaxCount: 10000, 56 } 57 58 if err := quick.Check(f, cfg); err != nil { 59 t.Fatal(err) 60 } 61 } 62 63 func TestParseFloatBytesValid64bitEquivalenceFuzz(t *testing.T) { 64 buf := []byte{} 65 f := func(n float64) bool { 66 buf = strconv.AppendFloat(buf[:0], n, 'f', -1, 64) 67 68 exp, expErr := strconv.ParseFloat(string(buf), 64) 69 got, gotErr := parseFloatBytes(buf, 64) 70 71 return exp == got && checkErrs(expErr, gotErr) 72 } 73 74 cfg := &quick.Config{ 75 MaxCount: 10000, 76 } 77 78 if err := quick.Check(f, cfg); err != nil { 79 t.Fatal(err) 80 } 81 } 82 83 func TestParseBoolBytesEquivalence(t *testing.T) { 84 var buf []byte 85 for _, s := range []string{"1", "t", "T", "TRUE", "true", "True", "0", "f", "F", "FALSE", "false", "False", "fail", "TrUe", "FAlSE", "numbers", ""} { 86 buf = append(buf[:0], s...) 87 88 exp, expErr := strconv.ParseBool(s) 89 got, gotErr := parseBoolBytes(buf) 90 91 if got != exp || !checkErrs(expErr, gotErr) { 92 t.Errorf("Failed to parse boolean value %q correctly: wanted (%t, %v), got (%t, %v)", s, exp, expErr, got, gotErr) 93 } 94 } 95 } 96 97 func checkErrs(a, b error) bool { 98 if (a == nil) != (b == nil) { 99 return false 100 } 101 102 return a == nil || a.Error() == b.Error() 103 }