-
Notifications
You must be signed in to change notification settings - Fork 1
/
text_decode_test.go
107 lines (84 loc) · 2.77 KB
/
text_decode_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package gg_test
import (
"testing"
"time"
u "unsafe"
"github.com/mitranim/gg"
"github.com/mitranim/gg/gtest"
)
func TestParseTo(t *testing.T) {
defer gtest.Catch(t)
gtest.PanicStr(`unsupported kind interface`, func() { gg.ParseTo[any](``) })
gtest.PanicStr(`unsupported kind struct`, func() { gg.ParseTo[SomeModel](``) })
gtest.PanicStr(`unsupported kind slice`, func() { gg.ParseTo[[]string](``) })
gtest.PanicStr(`unsupported kind chan`, func() { gg.ParseTo[chan struct{}](``) })
gtest.PanicStr(`unsupported kind func`, func() { gg.ParseTo[func()](``) })
gtest.PanicStr(`unsupported kind uintptr`, func() { gg.ParseTo[uintptr](``) })
gtest.PanicStr(`unsupported kind unsafe.Pointer`, func() { gg.ParseTo[u.Pointer](``) })
gtest.PanicStr(`invalid syntax`, func() { gg.ParseTo[int](``) })
gtest.PanicStr(`invalid syntax`, func() { gg.ParseTo[*int](``) })
gtest.Equal(gg.ParseTo[string](``), ``)
gtest.Equal(gg.ParseTo[string](`str`), `str`)
gtest.Equal(gg.ParseTo[*string](``), gg.Ptr(``))
gtest.Equal(gg.ParseTo[*string](`str`), gg.Ptr(`str`))
gtest.Equal(gg.ParseTo[int](`0`), 0)
gtest.Equal(gg.ParseTo[int](`123`), 123)
gtest.Equal(gg.ParseTo[*int](`0`), gg.Ptr(0))
gtest.Equal(gg.ParseTo[*int](`123`), gg.Ptr(123))
gtest.Equal(
gg.ParseTo[time.Time](`1234-05-23T12:34:56Z`),
time.Date(1234, 5, 23, 12, 34, 56, 0, time.UTC),
)
gtest.Equal(
gg.ParseTo[*time.Time](`1234-05-23T12:34:56Z`),
gg.Ptr(time.Date(1234, 5, 23, 12, 34, 56, 0, time.UTC)),
)
}
func BenchmarkParseTo_int(b *testing.B) {
for ind := 0; ind < b.N; ind++ {
gg.Nop1(gg.ParseTo[int](`123`))
}
}
func BenchmarkParseTo_int_ptr(b *testing.B) {
for ind := 0; ind < b.N; ind++ {
gg.Nop1(gg.ParseTo[*int](`123`))
}
}
func BenchmarkParseTo_Parser(b *testing.B) {
for ind := 0; ind < b.N; ind++ {
gg.Nop1(gg.ParseTo[ParserStr](`863872f79b1d4cc9a45e8027a6ad66ad`))
}
}
func BenchmarkParseTo_Parser_ptr(b *testing.B) {
for ind := 0; ind < b.N; ind++ {
gg.Nop1(gg.ParseTo[*ParserStr](`863872f79b1d4cc9a45e8027a6ad66ad`))
}
}
func BenchmarkParseTo_Unmarshaler(b *testing.B) {
src := []byte(`863872f79b1d4cc9a45e8027a6ad66ad`)
for ind := 0; ind < b.N; ind++ {
gg.Nop1(gg.ParseTo[UnmarshalerBytes](src))
}
}
func BenchmarkParseTo_Unmarshaler_ptr(b *testing.B) {
src := []byte(`863872f79b1d4cc9a45e8027a6ad66ad`)
for ind := 0; ind < b.N; ind++ {
gg.Nop1(gg.ParseTo[*UnmarshalerBytes](src))
}
}
func BenchmarkParseTo_time_Time(b *testing.B) {
for ind := 0; ind < b.N; ind++ {
gg.Nop1(gg.ParseTo[time.Time](`1234-05-23T12:34:56Z`))
}
}
func BenchmarkParseTo_time_Time_ptr(b *testing.B) {
for ind := 0; ind < b.N; ind++ {
gg.Nop1(gg.ParseTo[*time.Time](`1234-05-23T12:34:56Z`))
}
}
func BenchmarkParse(b *testing.B) {
var val int
for ind := 0; ind < b.N; ind++ {
gg.Parse(`123`, &val)
}
}