]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: stop thinking that MaxFloat32 overflows float32.
authorRémy Oudompheng <oudomphe@phare.normalesup.org>
Fri, 26 Oct 2012 06:39:36 +0000 (08:39 +0200)
committerRémy Oudompheng <oudomphe@phare.normalesup.org>
Fri, 26 Oct 2012 06:39:36 +0000 (08:39 +0200)
Fixes #4282.

R=golang-dev, minux.ma, rsc
CC=golang-dev
https://golang.org/cl/6759052

src/pkg/reflect/all_test.go
src/pkg/reflect/value.go

index 842b5075072c0997d3f2c4c4e980ab2a343d59e8..4ebe055305d599a65dfc53f5dbe37ecf17f7c7df 100644 (file)
@@ -2664,6 +2664,45 @@ func TestConvert(t *testing.T) {
        }
 }
 
+func TestOverflow(t *testing.T) {
+       if ovf := V(float64(0)).OverflowFloat(1e300); ovf {
+               t.Errorf("%v wrongly overflows float64", 1e300)
+       }
+
+       maxFloat32 := float64((1<<24 - 1) << (127 - 23))
+       if ovf := V(float32(0)).OverflowFloat(maxFloat32); ovf {
+               t.Errorf("%v wrongly overflows float32", maxFloat32)
+       }
+       ovfFloat32 := float64((1<<24-1)<<(127-23) + 1<<(127-52))
+       if ovf := V(float32(0)).OverflowFloat(ovfFloat32); !ovf {
+               t.Errorf("%v should overflow float32", ovfFloat32)
+       }
+       if ovf := V(float32(0)).OverflowFloat(-ovfFloat32); !ovf {
+               t.Errorf("%v should overflow float32", -ovfFloat32)
+       }
+
+       maxInt32 := int64(0x7fffffff)
+       if ovf := V(int32(0)).OverflowInt(maxInt32); ovf {
+               t.Errorf("%v wrongly overflows int32", maxInt32)
+       }
+       if ovf := V(int32(0)).OverflowInt(-1 << 31); ovf {
+               t.Errorf("%v wrongly overflows int32", -int64(1)<<31)
+       }
+       ovfInt32 := int64(1 << 31)
+       if ovf := V(int32(0)).OverflowInt(ovfInt32); !ovf {
+               t.Errorf("%v should overflow int32", ovfInt32)
+       }
+
+       maxUint32 := uint64(0xffffffff)
+       if ovf := V(uint32(0)).OverflowUint(maxUint32); ovf {
+               t.Errorf("%v wrongly overflows uint32", maxUint32)
+       }
+       ovfUint32 := uint64(1 << 32)
+       if ovf := V(uint32(0)).OverflowUint(ovfUint32); !ovf {
+               t.Errorf("%v should overflow uint32", ovfUint32)
+       }
+}
+
 type B1 struct {
        X int
        Y int
index be5d03504ed9e755730c57ca69ea6c34986ec7d1..2d217a5180e1641a39a2721aa0b1bdefa4ef9fa7 100644 (file)
@@ -1179,7 +1179,7 @@ func overflowFloat32(x float64) bool {
        if x < 0 {
                x = -x
        }
-       return math.MaxFloat32 <= x && x <= math.MaxFloat64
+       return math.MaxFloat32 < x && x <= math.MaxFloat64
 }
 
 // OverflowInt returns true if the int64 x cannot be represented by v's type.