File: clonetest_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (74 lines) | stat: -rw-r--r-- 1,829 bytes parent folder | download
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
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package clonetest_test

import (
	"testing"

	"github.com/google/go-cmp/cmp"
	"golang.org/x/tools/gopls/internal/clonetest"
)

func Test(t *testing.T) {
	doTest(t, true, false)
	type B bool
	doTest(t, B(true), false)
	doTest(t, 1, 0)
	doTest(t, int(1), 0)
	doTest(t, int8(1), 0)
	doTest(t, int16(1), 0)
	doTest(t, int32(1), 0)
	doTest(t, int64(1), 0)
	doTest(t, uint(1), 0)
	doTest(t, uint8(1), 0)
	doTest(t, uint16(1), 0)
	doTest(t, uint32(1), 0)
	doTest(t, uint64(1), 0)
	doTest(t, uintptr(1), 0)
	doTest(t, float32(1), 0)
	doTest(t, float64(1), 0)
	doTest(t, complex64(1), 0)
	doTest(t, complex128(1), 0)
	doTest(t, [3]int{1, 1, 1}, [3]int{0, 0, 0})
	doTest(t, ".", "")
	m1, m2 := map[string]int{".": 1}, map[string]int{".": 0}
	doTest(t, m1, m2)
	doTest(t, &m1, &m2)
	doTest(t, []int{1}, []int{0})
	i, j := 1, 0
	doTest(t, &i, &j)
	k, l := &i, &j
	doTest(t, &k, &l)

	s1, s2 := []int{1}, []int{0}
	doTest(t, &s1, &s2)

	type S struct {
		Field int
	}
	doTest(t, S{1}, S{0})

	doTest(t, []*S{{1}}, []*S{{0}})

	// An arbitrary recursive type.
	type LinkedList[T any] struct {
		V    T
		Next *LinkedList[T]
	}
	doTest(t, &LinkedList[int]{V: 1}, &LinkedList[int]{V: 0})
}

// doTest checks that the result of NonZero matches the nonzero argument, and
// that zeroing out that result matches the zero argument.
func doTest[T any](t *testing.T, nonzero, zero T) {
	got := clonetest.NonZero[T]()
	if diff := cmp.Diff(nonzero, got); diff != "" {
		t.Fatalf("NonZero() returned unexpected diff (-want +got):\n%s", diff)
	}
	clonetest.ZeroOut(&got)
	if diff := cmp.Diff(zero, got); diff != "" {
		t.Errorf("ZeroOut() returned unexpected diff (-want +got):\n%s", diff)
	}
}