Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Adding test and fix for ^uint64(0) -> float #8

Merged
merged 7 commits into from
Sep 19, 2024
Next Next commit
Adding failing test for ^uint64(0) -> float (should fail, by some def…
…inition)
ldemailly committed Sep 18, 2024
commit 06e187fc6cb9ed240a9ac5442bc7896607b086f0
29 changes: 29 additions & 0 deletions safecast_test.go
Original file line number Diff line number Diff line change
@@ -9,6 +9,35 @@ import (

// TODO: steal the tests from https://github.com/ccoVeille/go-safecast

const all64bitsOne = ^uint64(0)

func FindNumIntBits[T safecast.Float](t *testing.T) int {
var v T
for i := 0; i < 64; i++ {
bits := (all64bitsOne >> i)
v = T(bits)
if v != v-1 {
return 64 - i
}
}
panic("bug... didn't fine num bits")
}

func TestFloatBounds(t *testing.T) {
float32bits := FindNumIntBits[float32](t)
float64bits := FindNumIntBits[float64](t)
t.Logf("float32: %d bits", float32bits)
t.Logf("float64: %d bits", float64bits)
f32, err := safecast.Convert[float32](all64bitsOne)
if err == nil {
t.Errorf("expected error, got %d -> %.0f", all64bitsOne, f32)
}
f64, err := safecast.Convert[float64](all64bitsOne)
if err == nil {
t.Errorf("expected error, got %d -> %.0f", all64bitsOne, f64)
}
}

func TestConvert(t *testing.T) {
var inp uint32 = 42
out, err := safecast.Convert[int8](inp)