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 // float64(math.MaxInt64) rounds up to 2^63, which is out of range.
114 name: "float64(math.MaxInt64) to int64",
115 value: float64(math.MaxInt64),
116 typ: int64(0),
117 ok: false, // overflow
118 },
119 {
120 // The largest float64 that is a valid int64.
121 name: "float64(9223372036854774784) to int64",
122 value: float64(9223372036854774784),
123 typ: int64(0),
124 ok: true,
125 expected: int64(9223372036854774784),
126 },
127 {
128 // float64(math.MinInt64) is exact, so this is in range.
129 name: "float64(math.MinInt64) to int64",
130 value: float64(math.MinInt64),
131 typ: int64(0),
132 ok: true,
133 expected: int64(math.MinInt64),
134 },
135 {
136 name: "float64(32767) to int16",
137 value: float64(32767),
138 typ: int16(0),
139 ok: true,
140 expected: int16(32767),
141 },
142 // From float32 to int.
143 {
144 name: "float32(1.5) to int",
145 value: float32(1.5),
146 typ: int(0),
147 ok: false, // loss of precision
148 },
149 {
150 name: "float32(1.0) to int",
151 value: float32(1.0),
152 typ: int(0),
153 ok: true,
154 expected: int(1),
155 },
156 {
157 name: "float32(math.MaxFloat32) to int16",
158 value: float32(math.MaxFloat32),
159 typ: int16(0),
160 ok: false, // overflow
161 },
162 {
163 name: "float32(math.MaxFloat32) to int64",
164 value: float32(math.MaxFloat32),
165 typ: int64(0),
166 ok: false, // overflow
167 },
168 {
169 name: "float32(math.MaxInt16) to int16",
170 value: float32(math.MaxInt16),
171 typ: int16(0),
172 ok: true,
173 expected: int16(32767),
174 },
175 {
176 name: "float32(math.MaxInt16+1) to int16",
177 value: float32(math.MaxInt16 + 1),
178 typ: int16(0),
179 ok: false, // overflow
180 },
181 // Int to float.
182 {
183 name: "int16(32767) to float32",
184 value: int16(32767),
185 typ: float32(0),
186 ok: true,
187 expected: float32(32767),
188 },
189 {
190 name: "int64(32767) to float32",
191 value: int64(32767),
192 typ: float32(0),
193 ok: true,
194 expected: float32(32767),
195 },
196 {
197 name: "int64(math.MaxInt64) to float32",
198 value: int64(math.MaxInt64),
199 typ: float32(0),
200 ok: false, // loss of precision
201 },
202 {
203 name: "int64(math.MaxInt64) to float64",
204 value: int64(math.MaxInt64),
205 typ: float64(0),
206 ok: false, // loss of precision
207 },
208 {
209 name: "int64(1<<53) to float64",
210 value: int64(1 << 53),
211 typ: float64(0),
212 ok: true,
213 expected: float64(1 << 53),
214 },
215 {
216 name: "int64(1<<53+1) to float64",
217 value: int64(1<<53 + 1),
218 typ: float64(0),
219 ok: false, // loss of precision
220 },
221 {
222 name: "int64(1<<60) to float64",
223 value: int64(1 << 60),
224 typ: float64(0),
225 ok: true,
226 expected: float64(1 << 60),
227 },
228 {
229 name: "int64(math.MinInt64) to float64",
230 value: int64(math.MinInt64),
231 typ: float64(0),
232 ok: true,
233 expected: float64(math.MinInt64),
234 },
235 {
236 name: "int64(1<<24+1) to float32",
237 value: int64(1<<24 + 1),
238 typ: float32(0),
239 ok: false, // loss of precision
240 },
241 // Int to uint.
242 {
243 name: "int16(32767) to uint16",
244 value: int16(32767),
245 typ: uint16(0),
246 ok: true,
247 expected: uint16(32767),
248 },
249 {
250 name: "int16(32767) to uint8",
251 value: int16(32767),
252 typ: uint8(0),
253 ok: false,
254 },
255 {
256 name: "float64(3.14) to uint64",
257 value: float64(3.14),
258 typ: uint64(0),
259 ok: false,
260 },
261 {
262 name: "float64(3.0) to uint64",
263 value: float64(3.0),
264 typ: uint64(0),
265 ok: true,
266 expected: uint64(3),
267 },
268 {
269 // float64(math.MaxUint64) rounds up to 2^64, which is out of range.
270 name: "float64(math.MaxUint64) to uint64",
271 value: float64(math.MaxUint64),
272 typ: uint64(0),
273 ok: false, // overflow
274 },
275 {
276 // The largest float64 that is a valid uint64.
277 name: "float64(18446744073709549568) to uint64",
278 value: float64(18446744073709549568),
279 typ: uint64(0),
280 ok: true,
281 expected: uint64(18446744073709549568),
282 },
283 // From uint to float.
284 {
285 name: "uint64(math.MaxInt16) to float64",
286 value: uint64(math.MaxInt16),
287 typ: float64(0),
288 ok: true,
289 expected: float64(math.MaxInt16),
290 },
291 {
292 name: "uint64(1<<63) to float64",
293 value: uint64(1 << 63),
294 typ: float64(0),
295 ok: true,
296 expected: float64(1 << 63),
297 },
298 {
299 name: "uint64(math.MaxUint64) to float64",
300 value: uint64(math.MaxUint64),
301 typ: float64(0),
302 ok: false, // loss of precision
303 },
304 {
305 name: "uint64(1<<24+1) to float32",
306 value: uint64(1<<24 + 1),
307 typ: float32(0),
308 ok: false, // loss of precision
309 },
310 // Float to float.
311 {
312 name: "float64(3.14) to float32",
313 value: float64(3.14),
314 typ: float32(0),
315 ok: false, // loss of precision
316 },
317 {
318 name: "float64(0.5) to float32",
319 value: float64(0.5),
320 typ: float32(0),
321 ok: true,
322 expected: float32(0.5),
323 },
324 {
325 name: "float64(math.MaxFloat64) to float32",
326 value: float64(math.MaxFloat64),
327 typ: float32(0),
328 ok: false, // overflow
329 },
330 {
331 name: "float32(3.14) to float64",
332 value: float32(3.14),
333 typ: float64(0),
334 ok: true,
335 expected: float64(3.14),
336 },
337 {
338 name: "float64(3.14) to float64",
339 value: float64(3.14),
340 typ: float64(0),
341 ok: true,
342 expected: float64(3.14),
343 },
344 } {
345
346 v, ok := ConvertIfPossible(reflect.ValueOf(test.value), reflect.TypeOf(test.typ))
347 c.Assert(ok, qt.Equals, test.ok, qt.Commentf("test case: %s", test.name))
348 if test.ok {
349 c.Assert(v.Interface(), hqt.IsSameNumber, test.expected, qt.Commentf("test case: %s", test.name))
350 }
351 }
352 }
353
354 func TestConvertIfPossibleMisc(t *testing.T) {
355 c := qt.New(t)
356 type s string
357
358 var (
359 i = int32(42)
360 i64 = int64(i)
361 iv any = i
362 ip = &i
363 shello = s("hello")
364 )
365
366 convertOK := func(v reflect.Value, typ any) any {
367 rv, ok := ConvertIfPossible(v, reflect.TypeOf(typ))
368 c.Assert(ok, qt.IsTrue)
369 return rv.Interface()
370 }
371
372 c.Assert(convertOK(reflect.ValueOf(shello), ""), qt.Equals, "hello")
373 c.Assert(convertOK(reflect.ValueOf(iv), int64(0)), qt.Equals, i64)
374
375 // Elements of a []any are of kind Interface.
376 c.Assert(convertOK(reflect.ValueOf([]any{iv}).Index(0), int64(0)), qt.Equals, i64)
377 c.Assert(convertOK(reflect.ValueOf([]any{nil}).Index(0), int64(0)), qt.Equals, int64(0))
378
379 // Pointers are not dereferenced.
380 _, ok := ConvertIfPossible(reflect.ValueOf(ip), reflect.TypeOf(int64(0)))
381 c.Assert(ok, qt.IsFalse)
382 _, ok = ConvertIfPossible(reflect.ValueOf((*int32)(nil)), reflect.TypeOf(int64(0)))
383 c.Assert(ok, qt.IsFalse)
384 }
385
386 func BenchmarkToInt64(b *testing.B) {
387 v := reflect.ValueOf(int(42))
388 for b.Loop() {
389 ToInt64(v)
390 }
391 }