| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package clonetest_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/google/go-cmp/cmp" |
| 11 |
"golang.org/x/tools/gopls/internal/clonetest" |
| 12 |
) |
| 13 |
|
| 14 |
func Test(t *testing.T) { |
| 15 |
doTest(t, true, false) |
| 16 |
type B bool |
| 17 |
doTest(t, B(true), false) |
| 18 |
doTest(t, 1, 0) |
| 19 |
doTest(t, int(1), 0) |
| 20 |
doTest(t, int8(1), 0) |
| 21 |
doTest(t, int16(1), 0) |
| 22 |
doTest(t, int32(1), 0) |
| 23 |
doTest(t, int64(1), 0) |
| 24 |
doTest(t, uint(1), 0) |
| 25 |
doTest(t, uint8(1), 0) |
| 26 |
doTest(t, uint16(1), 0) |
| 27 |
doTest(t, uint32(1), 0) |
| 28 |
doTest(t, uint64(1), 0) |
| 29 |
doTest(t, uintptr(1), 0) |
| 30 |
doTest(t, float32(1), 0) |
| 31 |
doTest(t, float64(1), 0) |
| 32 |
doTest(t, complex64(1), 0) |
| 33 |
doTest(t, complex128(1), 0) |
| 34 |
doTest(t, [3]int{1, 1, 1}, [3]int{0, 0, 0}) |
| 35 |
doTest(t, ".", "") |
| 36 |
m1, m2 := map[string]int{".": 1}, map[string]int{".": 0} |
| 37 |
doTest(t, m1, m2) |
| 38 |
doTest(t, &m1, &m2) |
| 39 |
doTest(t, []int{1}, []int{0}) |
| 40 |
i, j := 1, 0 |
| 41 |
doTest(t, &i, &j) |
| 42 |
k, l := &i, &j |
| 43 |
doTest(t, &k, &l) |
| 44 |
|
| 45 |
s1, s2 := []int{1}, []int{0} |
| 46 |
doTest(t, &s1, &s2) |
| 47 |
|
| 48 |
type S struct { |
| 49 |
Field int |
| 50 |
} |
| 51 |
doTest(t, S{1}, S{0}) |
| 52 |
|
| 53 |
doTest(t, []*S{{1}}, []*S{{0}}) |
| 54 |
|
| 55 |
|
| 56 |
type LinkedList[T any] struct { |
| 57 |
V T |
| 58 |
Next *LinkedList[T] |
| 59 |
} |
| 60 |
doTest(t, &LinkedList[int]{V: 1}, &LinkedList[int]{V: 0}) |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
func doTest[T any](t *testing.T, nonzero, zero T) { |
| 66 |
got := clonetest.NonZero[T]() |
| 67 |
if diff := cmp.Diff(nonzero, got); diff != "" { |
| 68 |
t.Fatalf("NonZero() returned unexpected diff (-want +got):\n%s", diff) |
| 69 |
} |
| 70 |
clonetest.ZeroOut(&got) |
| 71 |
if diff := cmp.Diff(zero, got); diff != "" { |
| 72 |
t.Errorf("ZeroOut() returned unexpected diff (-want +got):\n%s", diff) |
| 73 |
} |
| 74 |
} |