Skip to content

Commit

Permalink
A few minor stylistic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Nov 16, 2021
1 parent 96912c0 commit 79893e4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
9 changes: 3 additions & 6 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ type Primitive struct {
context Key
}

// The significand precision for float32 and float64 is 24 and
// 53 bits respectfully. 2^prec-1 gives us the range an integer
// can be stored within a float without loss of data.
// The significand precision for float32 and float64 is 24 and 53 bits; this is
// the range a natural number can be stored in a float without loss of data.
const (
maxSafeFloat32Int = 16777215 // 2^24-1
maxSafeFloat64Int = 9007199254740991 // 2^53-1
Expand Down Expand Up @@ -225,9 +224,7 @@ func (md *MetaData) unify(data interface{}, rv reflect.Value) error {
return e("unsupported type %s", rv.Type())
}
return md.unifyAnything(data, rv)
case reflect.Float32:
fallthrough
case reflect.Float64:
case reflect.Float32, reflect.Float64:
return md.unifyFloat64(data, rv)
}
return e("unsupported type %s", rv.Kind())
Expand Down
43 changes: 20 additions & 23 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,49 +268,46 @@ func TestDecodeIntOverflow(t *testing.T) {
}

func TestDecodeFloatOverflow(t *testing.T) {
type table struct {
F32 float32
F64 float64
}

tests := []struct {
value string
overflow bool
}{
{fmt.Sprintf(`F32 = %f`, math.MaxFloat64), true},
{fmt.Sprintf(`F32 = %f`, -math.MaxFloat64), true},
{fmt.Sprintf(`F32 = %f`, math.MaxFloat32), false},
{fmt.Sprintf(`F32 = %f`, -math.MaxFloat32), false},
{fmt.Sprintf(`F32 = %f`, math.MaxFloat32*1.1), true},
{fmt.Sprintf(`F32 = %f`, -math.MaxFloat32*1.1), true},

{fmt.Sprintf(`F32 = %d`, maxSafeFloat32Int), false},
{fmt.Sprintf(`F32 = %d`, -maxSafeFloat32Int), false},
{fmt.Sprintf(`F32 = %d`, maxSafeFloat32Int+1), true},
{fmt.Sprintf(`F32 = %d`, -maxSafeFloat32Int-1), true},
{fmt.Sprintf(`F64 = %d`, maxSafeFloat64Int+1), true},
{fmt.Sprintf(`F64 = %d`, -maxSafeFloat64Int-1), true},

{fmt.Sprintf(`F32 = %f`, math.MaxFloat32), false},
{fmt.Sprintf(`F32 = %f`, -math.MaxFloat32), false},
{fmt.Sprintf(`F32 = %d`, maxSafeFloat32Int), false},
{fmt.Sprintf(`F32 = %d`, -maxSafeFloat32Int), false},
{fmt.Sprintf(`F64 = %f`, math.MaxFloat64), false},
{fmt.Sprintf(`F64 = %f`, -math.MaxFloat64), false},
{fmt.Sprintf(`F64 = %f`, math.MaxFloat32), false},
{fmt.Sprintf(`F64 = %f`, -math.MaxFloat32), false},

{fmt.Sprintf(`F64 = %d`, maxSafeFloat64Int), false},
{fmt.Sprintf(`F64 = %d`, -maxSafeFloat64Int), false},
{fmt.Sprintf(`F64 = %d`, maxSafeFloat64Int+1), true},
{fmt.Sprintf(`F64 = %d`, -maxSafeFloat64Int-1), true},
}

for _, tc := range tests {
var tab table
_, err := Decode(tc.value, &tab)

if tc.overflow && err == nil {
t.Fatalf("expected error for %q", tc.value)
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
var tab struct {
F32 float32
F64 float64
}
_, err := Decode(tt.value, &tab)

if !tc.overflow && err != nil {
t.Fatalf("unexpected error for %q: %v", tc.value, err)
}
if tt.overflow && err == nil {
t.Fatal("expected error, but err is nil")
}
if (tt.overflow && !errorContains(err, "out of range")) || (!tt.overflow && err != nil) {
t.Fatalf("unexpected error:\n%v", err)
}
})
}
}

Expand Down

0 comments on commit 79893e4

Please sign in to comment.