Skip to content

Commit

Permalink
Add the range tests for all the 53 and 24 bits shifted up to 64th bit…
Browse files Browse the repository at this point in the history
… -> float
  • Loading branch information
ldemailly committed Sep 19, 2024
1 parent 9690f26 commit 9c233a8
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion safecast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,32 @@ func FindNumIntBits[T safecast.Float](t *testing.T) int {
panic("bug... didn't fine num bits")
}

func TestFloatBounds(t *testing.T) {
func TestFloat32Bounds(t *testing.T) {
float32bits := FindNumIntBits[float32](t)
t.Logf("float32: %d bits", float32bits)
float32int := uint64(1<<(float32bits) - 1) // 24 bits
for i := 0; i <= 64-float32bits; i++ {
t.Logf("float32int %b %d", float32int, float32int)
f := safecast.MustConvert[float32](float32int)
t.Logf("float32int -> %.0f", f)
float32int <<= 1
}
}

func TestFloat64Bounds(t *testing.T) {
float64bits := FindNumIntBits[float64](t)
t.Logf("float64: %d bits", float64bits)
float64int := uint64(1<<(float64bits) - 1) // 53 bits
for i := 0; i <= 64-float64bits; i++ {
t.Logf("float64int %b %d", float64int, float64int)
f := safecast.MustConvert[float64](float64int)
t.Logf("float64int -> %.0f", f)
float64int <<= 1
}
}

// MaxUint64 special case and also MaxInt64+1.
func TestMaxInt64(t *testing.T) {
f32, err := safecast.Convert[float32](all64bitsOne)
if err == nil {
t.Errorf("expected error, got %d -> %.0f", all64bitsOne, f32)
Expand Down

0 comments on commit 9c233a8

Please sign in to comment.