-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin_test.go
90 lines (77 loc) · 2.27 KB
/
builtin_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
package nstd_test
import (
"reflect"
"testing"
"go101.org/nstd"
)
type blank = struct{} // used internally
func TestZeroOf(t *testing.T) {
testZeroOf(1, 0, t)
testZeroOf("go", "", t)
testZeroOf(true, false, t)
testZeroOf(struct{}{}, struct{}{}, t)
testZeroOf([2]byte{1, 2}, [2]byte{}, t)
testZeroOf([]byte{1, 2}, nil, t)
}
func testZeroOf[T any](v, zero T, t *testing.T) {
if z := nstd.ZeroOf(v); !reflect.DeepEqual(z, zero) {
t.Fatalf("Zero(%v) != %v (but %v)", v, z, zero)
}
}
func TestZero(t *testing.T) {
testZero(1, 0, t)
testZero("go", "", t)
testZero(true, false, t)
testZero(struct{}{}, struct{}{}, t)
testZero([2]byte{1, 2}, [2]byte{}, t)
testZero([]byte{1, 2}, nil, t)
}
func testZero[T any](v, zero T, t *testing.T) {
var old = v
var p = &v
if nstd.Zero(p); !reflect.DeepEqual(v, zero) {
t.Fatalf("ZeroIt(&%v) != %v (but %v)", old, v, zero)
}
}
func TestNew(t *testing.T) {
testNew(1, t)
testNew("go", t)
testNew(true, t)
testNew(struct{}{}, t)
testNew([2]byte{1, 2}, t)
testNew([]byte{1, 2}, t)
}
func testNew[T any](v T, t *testing.T) {
var pv = &v
if p := nstd.New(v); p == pv {
t.Fatalf("New(&%v) == &%v", v, v)
} else if !reflect.DeepEqual(*p, *pv) {
t.Fatalf("*New(&%v) != %v (but %v)", v, v, *p)
}
}
func TestTypeAssert(t *testing.T) {
testTypeAssert(1, (*int)(nil), true, t)
testTypeAssert(1, nstd.New(123), true, t)
testTypeAssert(1, nstd.New(true), false, t)
testTypeAssert(true, nstd.New(true), true, t)
testTypeAssert(1, nstd.New(true), false, t)
testTypeAssert(true, nstd.New(any(0)), true, t)
}
func testTypeAssert[T any](v any, p *T, shouldOkay bool, t *testing.T) {
if nstd.TypeAssert(v, p) != shouldOkay {
t.Fatalf("*TypeAssert[%T](%v, %T) != %v", *p, v, p, shouldOkay)
} else if shouldOkay && p != nil && !reflect.DeepEqual(v, *p) {
t.Fatalf("*TypeAssert[%T](%v, %T) fails. (got %v)", *p, v, p, *p)
}
}
func TestHasEntry(t *testing.T) {
testHasEntry(map[int]int{1: 2}, 1, true, t)
testHasEntry(map[int]int{1: 2}, 2, false, t)
testHasEntry(map[bool]int{true: 2}, true, true, t)
testHasEntry(map[bool]int{false: 2}, true, false, t)
}
func testHasEntry[K comparable, E any](m map[K]E, key K, shouldOkay bool, t *testing.T) {
if nstd.HasEntry(m, key) != shouldOkay {
t.Fatalf("*HasEntry(%v, %v) != %v", m, key, shouldOkay)
}
}