aboutsummaryrefslogtreecommitdiff
path: root/go/ssa/interp/testdata/zeros.go
blob: 509c78a36ecfb8beb0a5758386448fff111f3ba8 (plain)
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
// Copyright 2022 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.

// Test interpretation on zero values with type params.
package zeros

func assert(cond bool, msg string) {
	if !cond {
		panic(msg)
	}
}

func tp0[T int | string | float64]() T { return T(0) }

func tpFalse[T ~bool]() T { return T(false) }

func tpEmptyString[T string | []byte]() T { return T("") }

func tpNil[T *int | []byte]() T { return T(nil) }

func main() {
	// zero values
	var zi int
	var zf float64
	var zs string

	assert(zi == int(0), "zero value of int is int(0)")
	assert(zf == float64(0), "zero value of float64 is float64(0)")
	assert(zs != string(0), "zero value of string is not string(0)")

	assert(zi == tp0[int](), "zero value of int is int(0)")
	assert(zf == tp0[float64](), "zero value of float64 is float64(0)")
	assert(zs != tp0[string](), "zero value of string is not string(0)")

	assert(zf == -0.0, "constant -0.0 is converted to 0.0")

	assert(!tpFalse[bool](), "zero value of bool is false")

	assert(tpEmptyString[string]() == zs, `zero value of string is string("")`)
	assert(len(tpEmptyString[[]byte]()) == 0, `[]byte("") is empty`)

	assert(tpNil[*int]() == nil, "nil is nil")
	assert(tpNil[[]byte]() == nil, "nil is nil")
}