1// run
2
3// Copyright 2017 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7package main
8
9import "fmt"
10
11// issue 17446
12const (
13	_ = real(0) // from bug report
14	_ = imag(0) // from bug report
15
16	// same as above, but exported for #43891
17	Real0 = real(0)
18	Imag0 = imag(0)
19
20	// if the arguments are untyped, the results must be untyped
21	// (and compatible with types that can represent the values)
22	_ int = real(1)
23	_ int = real('a')
24	_ int = real(2.0)
25	_ int = real(3i)
26
27	_ float32 = real(1)
28	_ float32 = real('a')
29	_ float32 = real(2.1)
30	_ float32 = real(3.2i)
31
32	_ float64 = real(1)
33	_ float64 = real('a')
34	_ float64 = real(2.1)
35	_ float64 = real(3.2i)
36
37	_ int = imag(1)
38	_ int = imag('a')
39	_ int = imag(2.1 + 3i)
40	_ int = imag(3i)
41
42	_ float32 = imag(1)
43	_ float32 = imag('a')
44	_ float32 = imag(2.1 + 3.1i)
45	_ float32 = imag(3i)
46
47	_ float64 = imag(1)
48	_ float64 = imag('a')
49	_ float64 = imag(2.1 + 3.1i)
50	_ float64 = imag(3i)
51)
52
53var tests = []struct {
54	code      string
55	got, want interface{}
56}{
57	{"real(1)", real(1), 1.0},
58	{"real('a')", real('a'), float64('a')},
59	{"real(2.0)", real(2.0), 2.0},
60	{"real(3.2i)", real(3.2i), 0.0},
61
62	{"imag(1)", imag(1), 0.0},
63	{"imag('a')", imag('a'), 0.0},
64	{"imag(2.1 + 3.1i)", imag(2.1 + 3.1i), 3.1},
65	{"imag(3i)", imag(3i), 3.0},
66}
67
68func main() {
69	// verify compile-time evaluated constant expressions
70	for _, test := range tests {
71		if test.got != test.want {
72			panic(fmt.Sprintf("%s: %v (%T) != %v (%T)", test.code, test.got, test.got, test.want, test.want))
73		}
74	}
75}
76