Debian Codegraph @

pointer/pointer_test.go

raw
1 /*
2 Copyright 2018 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package pointer
18
19 import (
20 "fmt"
21 "testing"
22 "time"
23 )
24
25 func TestAllPtrFieldsNil(t *testing.T) {
26 testCases := []struct {
27 obj any
28 expected bool
29 }{
30 {struct{}{}, true},
31 {struct{ Foo int }{12345}, true},
32 {&struct{ Foo int }{12345}, true},
33 {struct{ Foo *int }{nil}, true},
34 {&struct{ Foo *int }{nil}, true},
35 {struct {
36 Foo int
37 Bar *int
38 }{12345, nil}, true},
39 {&struct {
40 Foo int
41 Bar *int
42 }{12345, nil}, true},
43 {struct {
44 Foo *int
45 Bar *int
46 }{nil, nil}, true},
47 {&struct {
48 Foo *int
49 Bar *int
50 }{nil, nil}, true},
51 {struct{ Foo *int }{new(int)}, false},
52 {&struct{ Foo *int }{new(int)}, false},
53 {struct {
54 Foo *int
55 Bar *int
56 }{nil, new(int)}, false},
57 {&struct {
58 Foo *int
59 Bar *int
60 }{nil, new(int)}, false},
61 {(*struct{})(nil), true},
62 }
63 for i, tc := range testCases {
64 name := fmt.Sprintf("case[%d]", i)
65 t.Run(name, func(t *testing.T) {
66 if actual := AllPtrFieldsNil(tc.obj); actual != tc.expected {
67 t.Errorf("%s: expected %t, got %t", name, tc.expected, actual)
68 }
69 })
70 }
71 }
72
73 func TestInt(t *testing.T) {
74 val := int(0)
75 ptr := Int(val)
76 if *ptr != val {
77 t.Errorf("expected %d, got %d", val, *ptr)
78 }
79
80 val = int(1)
81 ptr = Int(val)
82 if *ptr != val {
83 t.Errorf("expected %d, got %d", val, *ptr)
84 }
85 }
86
87 func TestIntDeref(t *testing.T) {
88 var val, def int = 1, 0
89
90 out := IntDeref(&val, def)
91 if out != val {
92 t.Errorf("expected %d, got %d", val, out)
93 }
94
95 out = IntDeref(nil, def)
96 if out != def {
97 t.Errorf("expected %d, got %d", def, out)
98 }
99 }
100
101 func TestInt32(t *testing.T) {
102 val := int32(0)
103 ptr := Int32(val)
104 if *ptr != val {
105 t.Errorf("expected %d, got %d", val, *ptr)
106 }
107
108 val = int32(1)
109 ptr = Int32(val)
110 if *ptr != val {
111 t.Errorf("expected %d, got %d", val, *ptr)
112 }
113 }
114
115 func TestInt32Deref(t *testing.T) {
116 var val, def int32 = 1, 0
117
118 out := Int32Deref(&val, def)
119 if out != val {
120 t.Errorf("expected %d, got %d", val, out)
121 }
122
123 out = Int32Deref(nil, def)
124 if out != def {
125 t.Errorf("expected %d, got %d", def, out)
126 }
127 }
128
129 func TestInt32Equal(t *testing.T) {
130 if !Int32Equal(nil, nil) {
131 t.Errorf("expected true (nil == nil)")
132 }
133 if !Int32Equal(Int32(123), Int32(123)) {
134 t.Errorf("expected true (val == val)")
135 }
136 if Int32Equal(nil, Int32(123)) {
137 t.Errorf("expected false (nil != val)")
138 }
139 if Int32Equal(Int32(123), nil) {
140 t.Errorf("expected false (val != nil)")
141 }
142 if Int32Equal(Int32(123), Int32(456)) {
143 t.Errorf("expected false (val != val)")
144 }
145 }
146
147 func TestUint(t *testing.T) {
148 val := uint(0)
149 ptr := Uint(val)
150 if *ptr != val {
151 t.Errorf("expected %d, got %d", val, *ptr)
152 }
153
154 val = uint(1)
155 ptr = Uint(val)
156 if *ptr != val {
157 t.Errorf("expected %d, got %d", val, *ptr)
158 }
159 }
160
161 func TestUintDeref(t *testing.T) {
162 var val, def uint = 1, 0
163
164 out := UintDeref(&val, def)
165 if out != val {
166 t.Errorf("expected %d, got %d", val, out)
167 }
168
169 out = UintDeref(nil, def)
170 if out != def {
171 t.Errorf("expected %d, got %d", def, out)
172 }
173 }
174
175 func TestUint32(t *testing.T) {
176 val := uint32(0)
177 ptr := Uint32(val)
178 if *ptr != val {
179 t.Errorf("expected %d, got %d", val, *ptr)
180 }
181
182 val = uint32(1)
183 ptr = Uint32(val)
184 if *ptr != val {
185 t.Errorf("expected %d, got %d", val, *ptr)
186 }
187 }
188
189 func TestUint32Deref(t *testing.T) {
190 var val, def uint32 = 1, 0
191
192 out := Uint32Deref(&val, def)
193 if out != val {
194 t.Errorf("expected %d, got %d", val, out)
195 }
196
197 out = Uint32Deref(nil, def)
198 if out != def {
199 t.Errorf("expected %d, got %d", def, out)
200 }
201 }
202
203 func TestUint32Equal(t *testing.T) {
204 if !Uint32Equal(nil, nil) {
205 t.Errorf("expected true (nil == nil)")
206 }
207 if !Uint32Equal(Uint32(123), Uint32(123)) {
208 t.Errorf("expected true (val == val)")
209 }
210 if Uint32Equal(nil, Uint32(123)) {
211 t.Errorf("expected false (nil != val)")
212 }
213 if Uint32Equal(Uint32(123), nil) {
214 t.Errorf("expected false (val != nil)")
215 }
216 if Uint32Equal(Uint32(123), Uint32(456)) {
217 t.Errorf("expected false (val != val)")
218 }
219 }
220
221 func TestInt64(t *testing.T) {
222 val := int64(0)
223 ptr := Int64(val)
224 if *ptr != val {
225 t.Errorf("expected %d, got %d", val, *ptr)
226 }
227
228 val = int64(1)
229 ptr = Int64(val)
230 if *ptr != val {
231 t.Errorf("expected %d, got %d", val, *ptr)
232 }
233 }
234
235 func TestInt64Deref(t *testing.T) {
236 var val, def int64 = 1, 0
237
238 out := Int64Deref(&val, def)
239 if out != val {
240 t.Errorf("expected %d, got %d", val, out)
241 }
242
243 out = Int64Deref(nil, def)
244 if out != def {
245 t.Errorf("expected %d, got %d", def, out)
246 }
247 }
248
249 func TestInt64Equal(t *testing.T) {
250 if !Int64Equal(nil, nil) {
251 t.Errorf("expected true (nil == nil)")
252 }
253 if !Int64Equal(Int64(123), Int64(123)) {
254 t.Errorf("expected true (val == val)")
255 }
256 if Int64Equal(nil, Int64(123)) {
257 t.Errorf("expected false (nil != val)")
258 }
259 if Int64Equal(Int64(123), nil) {
260 t.Errorf("expected false (val != nil)")
261 }
262 if Int64Equal(Int64(123), Int64(456)) {
263 t.Errorf("expected false (val != val)")
264 }
265 }
266
267 func TestUint64(t *testing.T) {
268 val := uint64(0)
269 ptr := Uint64(val)
270 if *ptr != val {
271 t.Errorf("expected %d, got %d", val, *ptr)
272 }
273
274 val = uint64(1)
275 ptr = Uint64(val)
276 if *ptr != val {
277 t.Errorf("expected %d, got %d", val, *ptr)
278 }
279 }
280
281 func TestUint64Deref(t *testing.T) {
282 var val, def uint64 = 1, 0
283
284 out := Uint64Deref(&val, def)
285 if out != val {
286 t.Errorf("expected %d, got %d", val, out)
287 }
288
289 out = Uint64Deref(nil, def)
290 if out != def {
291 t.Errorf("expected %d, got %d", def, out)
292 }
293 }
294
295 func TestUint64Equal(t *testing.T) {
296 if !Uint64Equal(nil, nil) {
297 t.Errorf("expected true (nil == nil)")
298 }
299 if !Uint64Equal(Uint64(123), Uint64(123)) {
300 t.Errorf("expected true (val == val)")
301 }
302 if Uint64Equal(nil, Uint64(123)) {
303 t.Errorf("expected false (nil != val)")
304 }
305 if Uint64Equal(Uint64(123), nil) {
306 t.Errorf("expected false (val != nil)")
307 }
308 if Uint64Equal(Uint64(123), Uint64(456)) {
309 t.Errorf("expected false (val != val)")
310 }
311 }
312
313 func TestBool(t *testing.T) {
314 val := false
315 ptr := Bool(val)
316 if *ptr != val {
317 t.Errorf("expected %t, got %t", val, *ptr)
318 }
319
320 val = true
321 ptr = Bool(true)
322 if *ptr != val {
323 t.Errorf("expected %t, got %t", val, *ptr)
324 }
325 }
326
327 func TestBoolDeref(t *testing.T) {
328 val, def := true, false
329
330 out := BoolDeref(&val, def)
331 if out != val {
332 t.Errorf("expected %t, got %t", val, out)
333 }
334
335 out = BoolDeref(nil, def)
336 if out != def {
337 t.Errorf("expected %t, got %t", def, out)
338 }
339 }
340
341 func TestBoolEqual(t *testing.T) {
342 if !BoolEqual(nil, nil) {
343 t.Errorf("expected true (nil == nil)")
344 }
345 if !BoolEqual(Bool(true), Bool(true)) {
346 t.Errorf("expected true (val == val)")
347 }
348 if BoolEqual(nil, Bool(true)) {
349 t.Errorf("expected false (nil != val)")
350 }
351 if BoolEqual(Bool(true), nil) {
352 t.Errorf("expected false (val != nil)")
353 }
354 if BoolEqual(Bool(true), Bool(false)) {
355 t.Errorf("expected false (val != val)")
356 }
357 }
358
359 func TestString(t *testing.T) {
360 val := ""
361 ptr := String(val)
362 if *ptr != val {
363 t.Errorf("expected %s, got %s", val, *ptr)
364 }
365
366 val = "a"
367 ptr = String(val)
368 if *ptr != val {
369 t.Errorf("expected %s, got %s", val, *ptr)
370 }
371 }
372
373 func TestStringDeref(t *testing.T) {
374 val, def := "a", ""
375
376 out := StringDeref(&val, def)
377 if out != val {
378 t.Errorf("expected %s, got %s", val, out)
379 }
380
381 out = StringDeref(nil, def)
382 if out != def {
383 t.Errorf("expected %s, got %s", def, out)
384 }
385 }
386
387 func TestStringEqual(t *testing.T) {
388 if !StringEqual(nil, nil) {
389 t.Errorf("expected true (nil == nil)")
390 }
391 if !StringEqual(String("abc"), String("abc")) {
392 t.Errorf("expected true (val == val)")
393 }
394 if StringEqual(nil, String("abc")) {
395 t.Errorf("expected false (nil != val)")
396 }
397 if StringEqual(String("abc"), nil) {
398 t.Errorf("expected false (val != nil)")
399 }
400 if StringEqual(String("abc"), String("def")) {
401 t.Errorf("expected false (val != val)")
402 }
403 }
404
405 func TestFloat32(t *testing.T) {
406 val := float32(0)
407 ptr := Float32(val)
408 if *ptr != val {
409 t.Errorf("expected %f, got %f", val, *ptr)
410 }
411
412 val = float32(0.1)
413 ptr = Float32(val)
414 if *ptr != val {
415 t.Errorf("expected %f, got %f", val, *ptr)
416 }
417 }
418
419 func TestFloat32Deref(t *testing.T) {
420 var val, def float32 = 0.1, 0
421
422 out := Float32Deref(&val, def)
423 if out != val {
424 t.Errorf("expected %f, got %f", val, out)
425 }
426
427 out = Float32Deref(nil, def)
428 if out != def {
429 t.Errorf("expected %f, got %f", def, out)
430 }
431 }
432
433 func TestFloat32Equal(t *testing.T) {
434 if !Float32Equal(nil, nil) {
435 t.Errorf("expected true (nil == nil)")
436 }
437 if !Float32Equal(Float32(1.25), Float32(1.25)) {
438 t.Errorf("expected true (val == val)")
439 }
440 if Float32Equal(nil, Float32(1.25)) {
441 t.Errorf("expected false (nil != val)")
442 }
443 if Float32Equal(Float32(1.25), nil) {
444 t.Errorf("expected false (val != nil)")
445 }
446 if Float32Equal(Float32(1.25), Float32(4.5)) {
447 t.Errorf("expected false (val != val)")
448 }
449 }
450
451 func TestFloat64(t *testing.T) {
452 val := float64(0)
453 ptr := Float64(val)
454 if *ptr != val {
455 t.Errorf("expected %f, got %f", val, *ptr)
456 }
457
458 val = float64(0.1)
459 ptr = Float64(val)
460 if *ptr != val {
461 t.Errorf("expected %f, got %f", val, *ptr)
462 }
463 }
464
465 func TestFloat64Deref(t *testing.T) {
466 var val, def float64 = 0.1, 0
467
468 out := Float64Deref(&val, def)
469 if out != val {
470 t.Errorf("expected %f, got %f", val, out)
471 }
472
473 out = Float64Deref(nil, def)
474 if out != def {
475 t.Errorf("expected %f, got %f", def, out)
476 }
477 }
478
479 func TestFloat64Equal(t *testing.T) {
480 if !Float64Equal(nil, nil) {
481 t.Errorf("expected true (nil == nil)")
482 }
483 if !Float64Equal(Float64(1.25), Float64(1.25)) {
484 t.Errorf("expected true (val == val)")
485 }
486 if Float64Equal(nil, Float64(1.25)) {
487 t.Errorf("expected false (nil != val)")
488 }
489 if Float64Equal(Float64(1.25), nil) {
490 t.Errorf("expected false (val != nil)")
491 }
492 if Float64Equal(Float64(1.25), Float64(4.5)) {
493 t.Errorf("expected false (val != val)")
494 }
495 }
496
497 func TestDuration(t *testing.T) {
498 val := time.Duration(0)
499 ptr := Duration(val)
500 if *ptr != val {
501 t.Errorf("expected %s, got %s", val, *ptr)
502 }
503
504 val = time.Duration(42)
505 ptr = Duration(val)
506 if *ptr != val {
507 t.Errorf("expected %s, got %s", val, *ptr)
508 }
509 }
510
511 func TestDurationDeref(t *testing.T) {
512 var val, def time.Duration = 42, 0
513
514 out := DurationDeref(&val, def)
515 if out != val {
516 t.Errorf("expected %s, got %s", val, out)
517 }
518
519 out = DurationDeref(nil, def)
520 if out != def {
521 t.Errorf("expected %s, got %s", def, out)
522 }
523 }
524
525 func TestDurationEqual(t *testing.T) {
526 if !DurationEqual(nil, nil) {
527 t.Errorf("expected true (nil == nil)")
528 }
529 if !DurationEqual(Duration(42), Duration(42)) {
530 t.Errorf("expected true (val == val)")
531 }
532 if DurationEqual(nil, Duration(42)) {
533 t.Errorf("expected false (nil != val)")
534 }
535 if DurationEqual(Duration(42), nil) {
536 t.Errorf("expected false (val != nil)")
537 }
538 if DurationEqual(Duration(42), Duration(43)) {
539 t.Errorf("expected false (val != val)")
540 }
541 }