Debian Codegraph @

common/hreflect/convert_test.go

raw
1 // Copyright 2025 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package hreflect
15
16 import (
17 "math"
18 "reflect"
19 "testing"
20
21 qt "github.com/frankban/quicktest"
22 "github.com/gohugoio/hugo/htesting/hqt"
23 )
24
25 func TestToFuncs(t *testing.T) {
26 c := qt.New(t)
27
28 c.Assert(ToInt64(reflect.ValueOf(int(42))), qt.Equals, int64(42))
29 c.Assert(ToFloat64(reflect.ValueOf(float32(3.14))), hqt.IsSameFloat64, float64(3.14))
30 c.Assert(ToString(reflect.ValueOf("hello")), qt.Equals, "hello")
31 }
32
33 func TestConvertIfPossible(t *testing.T) {
34 c := qt.New(t)
35
36 for _, test := range []struct {
37 name string
38 value any
39 typ any
40 expected any
41 ok bool
42 }{
43 // From uint to int.
44 {
45 name: "uint64(math.MaxUint64) to int16",
46 value: uint64(math.MaxUint64),
47 typ: int16(0),
48 ok: false, // overflow
49 },
50
51 {
52 name: "uint64(math.MaxUint64) to int64",
53 value: uint64(math.MaxUint64),
54 typ: int64(0),
55 ok: false, // overflow
56 },
57 {
58 name: "uint64(math.MaxInt16) to int16",
59 value: uint64(math.MaxInt16),
60 typ: int64(0),
61 ok: true,
62 expected: int64(math.MaxInt16),
63 },
64 // From int to int.
65 {
66 name: "int64(math.MaxInt64) to int16",
67 value: int64(math.MaxInt64),
68 typ: int16(0),
69 ok: false, // overflow
70 },
71 {
72 name: "int64(math.MaxInt16) to int",
73 value: int64(math.MaxInt16),
74 typ: int(0),
75 ok: true,
76 expected: int(math.MaxInt16),
77 },
78
79 {
80 name: "int64(math.MaxInt16) to int",
81 value: int64(math.MaxInt16),
82 typ: int(0),
83 ok: true,
84 expected: int(math.MaxInt16),
85 },
86 // From float64 to int.
87 {
88 name: "float64(1.5) to int",
89 value: float64(1.5),
90 typ: int(0),
91 ok: false, // loss of precision
92 },
93 {
94 name: "float64(1.0) to int",
95 value: float64(1.0),
96 typ: int(0),
97 ok: true,
98 expected: int(1),
99 },
100 {
101 name: "float64(math.MaxInt16+1) to int16",
102 value: float64(math.MaxInt16 + 1),
103 typ: int16(0),
104 ok: false, // overflow
105 },
106 {
107 name: "float64(math.MaxFloat64) to int64",
108 value: float64(math.MaxFloat64),
109 typ: int64(0),
110 ok: false, // overflow
111 },
112 {
113 name: "float64(32767) to int16",
114 value: float64(32767),
115 typ: int16(0),
116 ok: true,
117 expected: int16(32767),
118 },
119 // From float32 to int.
120 {
121 name: "float32(1.5) to int",
122 value: float32(1.5),
123 typ: int(0),
124 ok: false, // loss of precision
125 },
126 {
127 name: "float32(1.0) to int",
128 value: float32(1.0),
129 typ: int(0),
130 ok: true,
131 expected: int(1),
132 },
133 {
134 name: "float32(math.MaxFloat32) to int16",
135 value: float32(math.MaxFloat32),
136 typ: int16(0),
137 ok: false, // overflow
138 },
139 {
140 name: "float32(math.MaxFloat32) to int64",
141 value: float32(math.MaxFloat32),
142 typ: int64(0),
143 ok: false, // overflow
144 },
145 {
146 name: "float32(math.MaxInt16) to int16",
147 value: float32(math.MaxInt16),
148 typ: int16(0),
149 ok: true,
150 expected: int16(32767),
151 },
152 {
153 name: "float32(math.MaxInt16+1) to int16",
154 value: float32(math.MaxInt16 + 1),
155 typ: int16(0),
156 ok: false, // overflow
157 },
158 // Int to float.
159 {
160 name: "int16(32767) to float32",
161 value: int16(32767),
162 typ: float32(0),
163 ok: true,
164 expected: float32(32767),
165 },
166 {
167 name: "int64(32767) to float32",
168 value: int64(32767),
169 typ: float32(0),
170 ok: true,
171 expected: float32(32767),
172 },
173 {
174 name: "int64(math.MaxInt64) to float32",
175 value: int64(math.MaxInt64),
176 typ: float32(0),
177 ok: true,
178 expected: float32(math.MaxInt64),
179 },
180 {
181 name: "int64(math.MaxInt64) to float64",
182 value: int64(math.MaxInt64),
183 typ: float64(0),
184 ok: true,
185 expected: float64(math.MaxInt64),
186 },
187 // Int to uint.
188 {
189 name: "int16(32767) to uint16",
190 value: int16(32767),
191 typ: uint16(0),
192 ok: true,
193 expected: uint16(32767),
194 },
195 {
196 name: "int16(32767) to uint8",
197 value: int16(32767),
198 typ: uint8(0),
199 ok: false,
200 },
201 {
202 name: "float64(3.14) to uint64",
203 value: float64(3.14),
204 typ: uint64(0),
205 ok: false,
206 },
207 {
208 name: "float64(3.0) to uint64",
209 value: float64(3.0),
210 typ: uint64(0),
211 ok: true,
212 expected: uint64(3),
213 },
214 // From uint to float.
215 {
216 name: "uint64(math.MaxInt16) to float64",
217 value: uint64(math.MaxInt16),
218 typ: float64(0),
219 ok: true,
220 expected: float64(math.MaxInt16),
221 },
222 // Float to float.
223 {
224 name: "float64(3.14) to float32",
225 value: float64(3.14),
226 typ: float32(0),
227 ok: true,
228 expected: float32(3.14),
229 },
230 {
231 name: "float32(3.14) to float64",
232 value: float32(3.14),
233 typ: float64(0),
234 ok: true,
235 expected: float64(3.14),
236 },
237 {
238 name: "float64(3.14) to float64",
239 value: float64(3.14),
240 typ: float64(0),
241 ok: true,
242 expected: float64(3.14),
243 },
244 } {
245
246 v, ok := ConvertIfPossible(reflect.ValueOf(test.value), reflect.TypeOf(test.typ))
247 c.Assert(ok, qt.Equals, test.ok, qt.Commentf("test case: %s", test.name))
248 if test.ok {
249 c.Assert(v.Interface(), hqt.IsSameNumber, test.expected, qt.Commentf("test case: %s", test.name))
250 }
251 }
252 }
253
254 func TestConvertIfPossibleMisc(t *testing.T) {
255 c := qt.New(t)
256 type s string
257
258 var (
259 i = int32(42)
260 i64 = int64(i)
261 iv any = i
262 ip = &i
263 inil any = (*int32)(nil)
264 shello = s("hello")
265 )
266
267 convertOK := func(v any, typ any) any {
268 rv, ok := ConvertIfPossible(reflect.ValueOf(v), reflect.TypeOf(typ))
269 c.Assert(ok, qt.IsTrue)
270 return rv.Interface()
271 }
272
273 c.Assert(convertOK(shello, ""), qt.Equals, "hello")
274 c.Assert(convertOK(ip, int64(0)), qt.Equals, i64)
275 c.Assert(convertOK(iv, int64(0)), qt.Equals, i64)
276 c.Assert(convertOK(inil, int64(0)), qt.Equals, int64(0))
277 }
278
279 func BenchmarkToInt64(b *testing.B) {
280 v := reflect.ValueOf(int(42))
281 for b.Loop() {
282 ToInt64(v)
283 }
284 }