Skip to content

Commit 6a2bdfc

Browse files
committed
add Long tests
1 parent 031ad85 commit 6a2bdfc

File tree

2 files changed

+164
-15
lines changed

2 files changed

+164
-15
lines changed

long.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,6 @@ func MakeLong(i int64) Long {
1818
return newLong(C.PyLong_FromLongLong(C.longlong(i)))
1919
}
2020

21-
func (l Long) Int64() int64 {
22-
return int64(C.PyLong_AsLongLong(l.obj))
23-
}
24-
25-
func (l Long) Uint64() uint64 {
26-
return uint64(C.PyLong_AsUnsignedLongLong(l.obj))
27-
}
28-
29-
func (l Long) AsFloat64() float64 {
30-
return float64(C.PyLong_AsDouble(l.obj))
31-
}
32-
3321
func LongFromFloat64(v float64) Long {
3422
return newLong(C.PyLong_FromDouble(C.double(v)))
3523
}
@@ -45,12 +33,27 @@ func LongFromUnicode(u Object, base int) Long {
4533
return newLong(C.PyLong_FromUnicodeObject(u.Obj(), C.int(base)))
4634
}
4735

48-
func (l Long) AsUint64() uint64 {
36+
func (l Long) Int() int {
37+
return int(l.Int64())
38+
}
39+
40+
func (l Long) Int64() int64 {
41+
return int64(C.PyLong_AsLongLong(l.obj))
42+
}
43+
44+
func (l Long) Uint() uint {
45+
return uint(l.Uint64())
46+
}
47+
48+
func (l Long) Uint64() uint64 {
4949
return uint64(C.PyLong_AsUnsignedLongLong(l.obj))
5050
}
5151

52-
func (l Long) AsUintptr() uintptr {
53-
return uintptr(C.PyLong_AsLong(l.obj))
52+
func (l Long) Uintptr() uintptr {
53+
return uintptr(l.Int64())
54+
}
55+
func (l Long) Float64() float64 {
56+
return float64(C.PyLong_AsDouble(l.obj))
5457
}
5558

5659
func LongFromUintptr(v uintptr) Long {

long_test.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package gp
2+
3+
import (
4+
"math"
5+
"testing"
6+
)
7+
8+
func TestLongCreation(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
input int64
12+
expected int64
13+
}{
14+
{"zero", 0, 0},
15+
{"positive", 42, 42},
16+
{"negative", -42, -42},
17+
{"max_int64", math.MaxInt64, math.MaxInt64},
18+
{"min_int64", math.MinInt64, math.MinInt64},
19+
}
20+
21+
for _, tt := range tests {
22+
t.Run(tt.name, func(t *testing.T) {
23+
l := MakeLong(tt.input)
24+
if got := l.Int64(); got != tt.expected {
25+
t.Errorf("MakeLong(%d) = %d; want %d", tt.input, got, tt.expected)
26+
}
27+
})
28+
}
29+
}
30+
31+
func TestLongFromFloat64(t *testing.T) {
32+
tests := []struct {
33+
name string
34+
input float64
35+
expected int64
36+
}{
37+
{"integer_float", 42.0, 42},
38+
{"truncated_float", 42.9, 42},
39+
{"negative_float", -42.9, -42},
40+
}
41+
42+
for _, tt := range tests {
43+
t.Run(tt.name, func(t *testing.T) {
44+
l := LongFromFloat64(tt.input)
45+
if got := l.Int64(); got != tt.expected {
46+
t.Errorf("LongFromFloat64(%f) = %d; want %d", tt.input, got, tt.expected)
47+
}
48+
})
49+
}
50+
}
51+
52+
func TestLongFromString(t *testing.T) {
53+
tests := []struct {
54+
name string
55+
input string
56+
base int
57+
expected int64
58+
}{
59+
{"decimal", "42", 10, 42},
60+
{"hex", "2A", 16, 42},
61+
{"binary", "101010", 2, 42},
62+
{"octal", "52", 8, 42},
63+
{"negative", "-42", 10, -42},
64+
}
65+
66+
for _, tt := range tests {
67+
t.Run(tt.name, func(t *testing.T) {
68+
l := LongFromString(tt.input, tt.base)
69+
if got := l.Int64(); got != tt.expected {
70+
t.Errorf("LongFromString(%q, %d) = %d; want %d", tt.input, tt.base, got, tt.expected)
71+
}
72+
})
73+
}
74+
}
75+
76+
func TestLongConversions(t *testing.T) {
77+
l := MakeLong(42)
78+
79+
t.Run("Int", func(t *testing.T) {
80+
if got := l.Int(); got != 42 {
81+
t.Errorf("Int() = %d; want 42", got)
82+
}
83+
})
84+
85+
t.Run("Uint", func(t *testing.T) {
86+
if got := l.Uint(); got != 42 {
87+
t.Errorf("Uint() = %d; want 42", got)
88+
}
89+
})
90+
91+
t.Run("Uint64", func(t *testing.T) {
92+
if got := l.Uint64(); got != 42 {
93+
t.Errorf("Uint64() = %d; want 42", got)
94+
}
95+
})
96+
97+
t.Run("Float64", func(t *testing.T) {
98+
if got := l.Float64(); got != 42.0 {
99+
t.Errorf("Float64() = %f; want 42.0", got)
100+
}
101+
})
102+
}
103+
104+
func TestLongFromUintptr(t *testing.T) {
105+
tests := []struct {
106+
name string
107+
input uintptr
108+
expected int64
109+
}{
110+
{"zero", 0, 0},
111+
{"positive", 42, 42},
112+
{"large_number", 1 << 30, 1 << 30},
113+
}
114+
115+
for _, tt := range tests {
116+
t.Run(tt.name, func(t *testing.T) {
117+
l := LongFromUintptr(tt.input)
118+
if got := l.Int64(); got != tt.expected {
119+
t.Errorf("LongFromUintptr(%d) = %d; want %d", tt.input, got, tt.expected)
120+
}
121+
})
122+
}
123+
}
124+
125+
func TestLongFromUnicode(t *testing.T) {
126+
tests := []struct {
127+
name string
128+
input string
129+
base int
130+
expected int64
131+
}{
132+
{"unicode_decimal", "42", 10, 42},
133+
{"unicode_hex", "2A", 16, 42},
134+
}
135+
136+
for _, tt := range tests {
137+
t.Run(tt.name, func(t *testing.T) {
138+
// Create Unicode object from string
139+
u := MakeStr(tt.input)
140+
l := LongFromUnicode(u.Object, tt.base)
141+
if got := l.Int64(); got != tt.expected {
142+
t.Errorf("LongFromUnicode(%q, %d) = %d; want %d", tt.input, tt.base, got, tt.expected)
143+
}
144+
})
145+
}
146+
}

0 commit comments

Comments
 (0)