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: true,
201 expected: float32(math.MaxInt64),
202 },
203 {
204 name: "int64(math.MaxInt64) to float64",
205 value: int64(math.MaxInt64),
206 typ: float64(0),
207 ok: true,
208 expected: float64(math.MaxInt64),
209 },
210 // Int to uint.
211 {
212 name: "int16(32767) to uint16",
213 value: int16(32767),
214 typ: uint16(0),
215 ok: true,
216 expected: uint16(32767),
217 },
218 {
219 name: "int16(32767) to uint8",
220 value: int16(32767),
221 typ: uint8(0),
222 ok: false,
223 },
224 {
225 name: "float64(3.14) to uint64",
226 value: float64(3.14),
227 typ: uint64(0),
228 ok: false,
229 },
230 {
231 name: "float64(3.0) to uint64",
232 value: float64(3.0),
233 typ: uint64(0),
234 ok: true,
235 expected: uint64(3),
236 },
237 {
238 // float64(math.MaxUint64) rounds up to 2^64, which is out of range.
239 name: "float64(math.MaxUint64) to uint64",
240 value: float64(math.MaxUint64),
241 typ: uint64(0),
242 ok: false, // overflow
243 },
244 {
245 // The largest float64 that is a valid uint64.
246 name: "float64(18446744073709549568) to uint64",
247 value: float64(18446744073709549568),
248 typ: uint64(0),
249 ok: true,
250 expected: uint64(18446744073709549568),
251 },
252 // From uint to float.
253 {
254 name: "uint64(math.MaxInt16) to float64",
255 value: uint64(math.MaxInt16),
256 typ: float64(0),
257 ok: true,
258 expected: float64(math.MaxInt16),
259 },
260 // Float to float.
261 {
262 name: "float64(3.14) to float32",
263 value: float64(3.14),
264 typ: float32(0),
265 ok: true,
266 expected: float32(3.14),
267 },
268 {
269 name: "float32(3.14) to float64",
270 value: float32(3.14),
271 typ: float64(0),
272 ok: true,
273 expected: float64(3.14),
274 },
275 {
276 name: "float64(3.14) to float64",
277 value: float64(3.14),
278 typ: float64(0),
279 ok: true,
280 expected: float64(3.14),
281 },
282 } {
283
284 v, ok := ConvertIfPossible(reflect.ValueOf(test.value), reflect.TypeOf(test.typ))
285 c.Assert(ok, qt.Equals, test.ok, qt.Commentf("test case: %s", test.name))
286 if test.ok {
287 c.Assert(v.Interface(), hqt.IsSameNumber, test.expected, qt.Commentf("test case: %s", test.name))
288 }
289 }
290 }
291
292 func TestConvertIfPossibleMisc(t *testing.T) {
293 c := qt.New(t)
294 type s string
295
296 var (
297 i = int32(42)
298 i64 = int64(i)
299 iv any = i
300 ip = &i
301 inil any = (*int32)(nil)
302 shello = s("hello")
303 )
304
305 convertOK := func(v any, typ any) any {
306 rv, ok := ConvertIfPossible(reflect.ValueOf(v), reflect.TypeOf(typ))
307 c.Assert(ok, qt.IsTrue)
308 return rv.Interface()
309 }
310
311 c.Assert(convertOK(shello, ""), qt.Equals, "hello")
312 c.Assert(convertOK(ip, int64(0)), qt.Equals, i64)
313 c.Assert(convertOK(iv, int64(0)), qt.Equals, i64)
314 c.Assert(convertOK(inil, int64(0)), qt.Equals, int64(0))
315 }
316
317 func BenchmarkToInt64(b *testing.B) {
318 v := reflect.ValueOf(int(42))
319 for b.Loop() {
320 ToInt64(v)
321 }
322 }