summaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/gc/testdata/chan.go
blob: 0766fcda5ba92f429c250055a08f5ad1e4fa9257 (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
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
// Copyright 2015 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.

// chan_ssa.go tests chan operations.
package main

import "fmt"

var failed = false

//go:noinline
func lenChan_ssa(v chan int) int {
	return len(v)
}

//go:noinline
func capChan_ssa(v chan int) int {
	return cap(v)
}

func testLenChan() {

	v := make(chan int, 10)
	v <- 1
	v <- 1
	v <- 1

	if want, got := 3, lenChan_ssa(v); got != want {
		fmt.Printf("expected len(chan) = %d, got %d", want, got)
		failed = true
	}
}

func testLenNilChan() {

	var v chan int
	if want, got := 0, lenChan_ssa(v); got != want {
		fmt.Printf("expected len(nil) = %d, got %d", want, got)
		failed = true
	}
}

func testCapChan() {

	v := make(chan int, 25)

	if want, got := 25, capChan_ssa(v); got != want {
		fmt.Printf("expected cap(chan) = %d, got %d", want, got)
		failed = true
	}
}

func testCapNilChan() {

	var v chan int
	if want, got := 0, capChan_ssa(v); got != want {
		fmt.Printf("expected cap(nil) = %d, got %d", want, got)
		failed = true
	}
}

func main() {
	testLenChan()
	testLenNilChan()

	testCapChan()
	testCapNilChan()

	if failed {
		panic("failed")
	}
}