Skip to content

Commit 9a79a6d

Browse files
committed
add Float tests
1 parent 142a684 commit 9a79a6d

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

float.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package gp
55
*/
66
import "C"
77

8+
// Float represents a Python float object. It provides methods to convert between
9+
// Go float types and Python float objects, as well as checking numeric properties.
810
type Float struct {
911
Object
1012
}
@@ -21,6 +23,10 @@ func (f Float) Float64() float64 {
2123
return float64(C.PyFloat_AsDouble(f.obj))
2224
}
2325

26+
func (f Float) Float32() float32 {
27+
return float32(C.PyFloat_AsDouble(f.obj))
28+
}
29+
2430
func (f Float) IsInteger() Bool {
2531
fn := Cast[Func](f.Attr("is_integer"))
2632
return Cast[Bool](fn.callNoArgs())

float_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package gp
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestFloat(t *testing.T) {
8+
t.Run("MakeFloat and conversions", func(t *testing.T) {
9+
// Test creating float and converting back
10+
f := MakeFloat(3.14159)
11+
12+
// Test Float64 conversion
13+
if got := f.Float64(); got != 3.14159 {
14+
t.Errorf("Float64() = %v, want %v", got, 3.14159)
15+
}
16+
17+
// Test Float32 conversion
18+
if got := f.Float32(); float64(got) != float64(float32(3.14159)) {
19+
t.Errorf("Float32() = %v, want %v", got, float32(3.14159))
20+
}
21+
})
22+
23+
t.Run("IsInteger", func(t *testing.T) {
24+
// Test integer float
25+
intFloat := MakeFloat(5.0)
26+
27+
if !intFloat.IsInteger().Bool() {
28+
t.Errorf("IsInteger() for 5.0 = false, want true")
29+
}
30+
31+
// Test non-integer float
32+
fracFloat := MakeFloat(5.5)
33+
34+
if fracFloat.IsInteger().Bool() {
35+
t.Errorf("IsInteger() for 5.5 = true, want false")
36+
}
37+
})
38+
39+
t.Run("Zero and special values", func(t *testing.T) {
40+
// Test zero
41+
zero := MakeFloat(0.0)
42+
43+
if got := zero.Float64(); got != 0.0 {
44+
t.Errorf("Float64() = %v, want 0.0", got)
45+
}
46+
47+
// Test very large number
48+
large := MakeFloat(1e308)
49+
50+
if got := large.Float64(); got != 1e308 {
51+
t.Errorf("Float64() = %v, want 1e308", got)
52+
}
53+
})
54+
}

0 commit comments

Comments
 (0)