1// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
18	"fmt"
19	"log"
20	"os"
21	"time"
22	"unsafe"
23)
24
25type FooInterface interface {
26	Bar()
27}
28
29type FooStruct struct {
30	a int
31	b string
32}
33
34func (f *FooStruct) Bar() {}
35
36type myInt int16
37
38var (
39	Z_bool_false          bool        = false
40	Z_bool_true           bool        = true
41	Z_int                 int         = -21
42	Z_int8                int8        = -121
43	Z_int16               int16       = -32321
44	Z_int32               int32       = -1987654321
45	Z_int64               int64       = -9012345678987654321
46	Z_int_typedef         myInt       = 88
47	Z_uint                uint        = 21
48	Z_uint8               uint8       = 231
49	Z_uint16              uint16      = 54321
50	Z_uint32              uint32      = 3217654321
51	Z_uint64              uint64      = 12345678900987654321
52	Z_uintptr             uintptr     = 21
53	Z_float32             float32     = 1.54321
54	Z_float64             float64     = 1.987654321
55	Z_complex64           complex64   = 1.54321 + 2.54321i
56	Z_complex128          complex128  = 1.987654321 - 2.987654321i
57	Z_array               [5]int8     = [5]int8{-121, 121, 3, 2, 1}
58	Z_array_empty         [0]int8     = [0]int8{}
59	Z_array_of_empties    [2]struct{} = [2]struct{}{{}, {}}
60	Z_channel             chan int16  = make(chan int16)
61	Z_channel_2           chan int16  = make(chan int16)
62	Z_channel_buffered    chan int16  = make(chan int16, 10)
63	Z_channel_nil         chan int16
64	Z_func_bar                              = (*FooStruct).Bar
65	Z_func_int8_r_int8                      = func(x int8) int8 { return x + 1 }
66	Z_func_int8_r_pint8                     = func(x int8) *int8 { y := x + 1; return &y }
67	Z_func_nil            func(x int8) int8 = nil
68	Z_interface           FooInterface      = &Z_struct
69	Z_interface_typed_nil FooInterface      = Z_pointer_nil
70	Z_interface_nil       FooInterface
71	Z_map                 map[int8]float32 = map[int8]float32{-21: 3.54321}
72	Z_map_2               map[int16]int8   = map[int16]int8{1024: 1}
73	Z_map_3               map[int16]int8   = map[int16]int8{1024: 1, 512: -1}
74	Z_map_empty           map[int8]float32 = map[int8]float32{}
75	Z_map_nil             map[int8]float32
76	Z_pointer             *FooStruct = &Z_struct
77	Z_pointer_nil         *FooStruct
78	Z_slice               []byte = []byte{'s', 'l', 'i', 'c', 'e'}
79	Z_slice_2             []int8 = Z_array[0:2]
80	Z_slice_nil           []byte
81	Z_string              string         = "I'm a string"
82	Z_struct              FooStruct      = FooStruct{a: 21, b: "hi"}
83	Z_unsafe_pointer      unsafe.Pointer = unsafe.Pointer(&Z_uint)
84	Z_unsafe_pointer_nil  unsafe.Pointer
85)
86
87func foo(x int16, y float32) {
88	var (
89		local_array               [5]int8    = [5]int8{-121, 121, 3, 2, 1}
90		local_bool_false          bool       = false
91		local_bool_true           bool       = true
92		local_channel             chan int16 = Z_channel
93		local_channel_buffered    chan int16 = Z_channel_buffered
94		local_channel_nil         chan int16
95		local_complex128          complex128        = 1.987654321 - 2.987654321i
96		local_complex64           complex64         = 1.54321 + 2.54321i
97		local_float32             float32           = 1.54321
98		local_float64             float64           = 1.987654321
99		local_func_bar                              = (*FooStruct).Bar
100		local_func_int8_r_int8                      = func(x int8) int8 { return x + 1 }
101		local_func_int8_r_pint8                     = func(x int8) *int8 { y := x + 1; return &y }
102		local_func_nil            func(x int8) int8 = nil
103		local_int                 int               = -21
104		local_int16               int16             = -32321
105		local_int32               int32             = -1987654321
106		local_int64               int64             = -9012345678987654321
107		local_int8                int8              = -121
108		local_int_typedef         myInt             = 88
109		local_interface           FooInterface      = &Z_struct
110		local_interface_nil       FooInterface
111		local_interface_typed_nil FooInterface     = Z_pointer_nil
112		local_map                 map[int8]float32 = map[int8]float32{-21: 3.54321}
113		local_map_2               map[int16]int8   = map[int16]int8{1024: 1}
114		local_map_3               map[int16]int8   = map[int16]int8{1024: 1, 512: -1}
115		local_map_empty           map[int8]float32 = map[int8]float32{}
116		local_map_nil             map[int8]float32
117		local_pointer             *FooStruct = &Z_struct
118		local_pointer_nil         *FooStruct
119		local_slice               []byte = []byte{'s', 'l', 'i', 'c', 'e'}
120		local_slice_2             []int8 = Z_array[0:2]
121		local_slice_nil           []byte
122		local_string              string         = "I'm a string"
123		local_struct              FooStruct      = FooStruct{a: 21, b: "hi"}
124		local_uint                uint           = 21
125		local_uint16              uint16         = 54321
126		local_uint32              uint32         = 3217654321
127		local_uint64              uint64         = 12345678900987654321
128		local_uint8               uint8          = 231
129		local_uintptr             uintptr        = 21
130		local_unsafe_pointer      unsafe.Pointer = unsafe.Pointer(&Z_uint)
131		local_unsafe_pointer_nil  unsafe.Pointer
132	)
133	fmt.Println(Z_bool_false, Z_bool_true)
134	fmt.Println(Z_int, Z_int8, Z_int16, Z_int32, Z_int64, Z_int_typedef)
135	fmt.Println(Z_uint, Z_uint8, Z_uint16, Z_uint32, Z_uint64, Z_uintptr)
136	fmt.Println(Z_float32, Z_float64, Z_complex64, Z_complex128)
137	fmt.Println(Z_array, Z_array_empty, Z_array_of_empties)
138	fmt.Println(Z_channel, Z_channel_buffered, Z_channel_nil)
139	fmt.Println(Z_func_bar, Z_func_int8_r_int8, Z_func_int8_r_pint8, Z_func_nil)
140	fmt.Println(Z_interface, Z_interface_nil, Z_interface_typed_nil)
141	fmt.Println(Z_map, Z_map_2, Z_map_3, Z_map_empty, Z_map_nil)
142	fmt.Println(Z_pointer, Z_pointer_nil)
143	fmt.Println(Z_slice, Z_slice_2, Z_slice_nil)
144	fmt.Println(Z_string, Z_struct)
145	fmt.Println(Z_unsafe_pointer, Z_unsafe_pointer_nil)
146	fmt.Println(local_bool_false, local_bool_true)
147	fmt.Println(local_int, local_int8, local_int16, local_int32, local_int64, local_int_typedef)
148	fmt.Println(local_uint, local_uint8, local_uint16, local_uint32, local_uint64, local_uintptr)
149	fmt.Println(local_float32, local_float64, local_complex64, local_complex128, local_array)
150	fmt.Println(local_channel, local_channel_buffered, local_channel_nil)
151	fmt.Println(local_func_bar, local_func_int8_r_int8, local_func_int8_r_pint8, local_func_nil)
152	fmt.Println(local_interface, local_interface_nil, local_interface_typed_nil)
153	fmt.Println(local_map, local_map_2, local_map_3, local_map_empty, local_map_nil)
154	fmt.Println(local_pointer, local_pointer_nil)
155	fmt.Println(local_slice, local_slice_2, local_slice_nil)
156	fmt.Println(local_string, local_struct)
157	fmt.Println(local_unsafe_pointer, local_unsafe_pointer_nil)
158	f1()
159	f2()
160}
161
162func f1() {
163	fmt.Println()
164}
165
166func f2() {
167	fmt.Println()
168}
169
170func bar() {
171	foo(42, 1.5)
172	fmt.Print()
173}
174
175func populateChannels() {
176	go func() {
177		Z_channel_2 <- 8
178	}()
179	go func() {
180		for i := int16(0); i < 14; i++ {
181			Z_channel_buffered <- i
182		}
183	}()
184	go func() {
185		for i := 0; i < 8; i++ {
186			<-Z_channel_buffered
187		}
188	}()
189	time.Sleep(time.Second / 20)
190}
191
192func main() {
193	args := os.Args[1:]
194	expected := []string{"some", "arguments"}
195	if len(args) != 2 || args[0] != expected[0] || args[1] != expected[1] {
196		log.Fatalf("got command-line args %v, expected %v", args, expected)
197	}
198	populateChannels()
199	for ; ; time.Sleep(2 * time.Second) {
200		bar()
201	}
202	select {}
203}
204