more platform stuff
This commit is contained in:
1 parent
4b9a882018
commit
fc5eefbe5e
7 files changed
+756
No files matched your search
@@ -0,0 +1,90 @@
|
||||
package conv
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUint32(t *testing.T) {
|
||||
tests := []struct {
|
||||
input any
|
||||
expected uint32
|
||||
ok bool
|
||||
}{
|
||||
{uint32(123), 123, true},
|
||||
{float64(456.0), 456, true},
|
||||
{float64(-1.0), 0, false},
|
||||
{float32(789.0), 789, true},
|
||||
{100, 100, true},
|
||||
{int64(200), 200, true},
|
||||
{uint64(300), 300, true},
|
||||
{uint64(0xFFFFFFFF + 1), 0, false},
|
||||
{json.Number("42"), 42, true},
|
||||
{json.Number("invalid"), 0, false},
|
||||
{"500", 500, true},
|
||||
{" 600 ", 600, true},
|
||||
{"invalid", 0, false},
|
||||
{nil, 0, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
val, ok := Uint32(tt.input)
|
||||
if val != tt.expected || ok != tt.ok {
|
||||
t.Errorf("Uint32(%v) = (%d, %t), expected (%d, %t)", tt.input, val, ok, tt.expected, tt.ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
tests := []struct {
|
||||
input any
|
||||
expected string
|
||||
}{
|
||||
{"hello", "hello"},
|
||||
{123, "123"},
|
||||
{int64(456), "456"},
|
||||
{uint32(789), "789"},
|
||||
{uint64(100), "100"},
|
||||
{float64(12.34), "12.34"},
|
||||
{float64(56.0), "56"},
|
||||
{float32(78.0), "78"},
|
||||
{json.Number("42.2"), "42.2"},
|
||||
{true, "true"},
|
||||
{false, "false"},
|
||||
{nil, ""},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
val := String(tt.input)
|
||||
if val != tt.expected {
|
||||
t.Errorf("String(%v) = %q, expected %q", tt.input, val, tt.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64(t *testing.T) {
|
||||
tests := []struct {
|
||||
input any
|
||||
expected float64
|
||||
ok bool
|
||||
}{
|
||||
{float64(12.34), 12.34, true},
|
||||
{float32(12.5), 12.5, true},
|
||||
{100, 100.0, true},
|
||||
{int64(200), 200.0, true},
|
||||
{uint32(300), 300.0, true},
|
||||
{uint64(400), 400.0, true},
|
||||
{"12.34", 12.34, true},
|
||||
{" 56.78 ", 56.78, true},
|
||||
{"invalid", 0.0, false},
|
||||
{json.Number("42.2"), 42.2, true},
|
||||
{nil, 0.0, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
val, ok := Float64(tt.input)
|
||||
if val != tt.expected || ok != tt.ok {
|
||||
t.Errorf("Float64(%v) = (%f, %t), expected (%f, %t)", tt.input, val, ok, tt.expected, tt.ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user