1// Copyright 2013 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// golang.org/issue/6612
6// Test new scheme for deciding whether C.name is an expression, type, constant.
7// Clang silences some warnings when the name is a #defined macro, so test those too
8// (even though we now use errors exclusively, not warnings).
9
10package cgotest
11
12/*
13void myfunc(void) {}
14int myvar = 5;
15const char *mytext = "abcdef";
16typedef int mytype;
17enum {
18	myenum = 1234,
19};
20
21#define myfunc_def myfunc
22#define myvar_def myvar
23#define mytext_def mytext
24#define mytype_def mytype
25#define myenum_def myenum
26#define myint_def 12345
27#define myfloat_def 1.5
28#define mystring_def "hello"
29*/
30import "C"
31
32import "testing"
33
34func testNaming(t *testing.T) {
35	C.myfunc()
36	C.myfunc_def()
37	if v := C.myvar; v != 5 {
38		t.Errorf("C.myvar = %d, want 5", v)
39	}
40	if v := C.myvar_def; v != 5 {
41		t.Errorf("C.myvar_def = %d, want 5", v)
42	}
43	if s := C.GoString(C.mytext); s != "abcdef" {
44		t.Errorf("C.mytext = %q, want %q", s, "abcdef")
45	}
46	if s := C.GoString(C.mytext_def); s != "abcdef" {
47		t.Errorf("C.mytext_def = %q, want %q", s, "abcdef")
48	}
49	if c := C.myenum; c != 1234 {
50		t.Errorf("C.myenum = %v, want 1234", c)
51	}
52	if c := C.myenum_def; c != 1234 {
53		t.Errorf("C.myenum_def = %v, want 1234", c)
54	}
55	{
56		const c = C.myenum
57		if c != 1234 {
58			t.Errorf("C.myenum as const = %v, want 1234", c)
59		}
60	}
61	{
62		const c = C.myenum_def
63		if c != 1234 {
64			t.Errorf("C.myenum as const = %v, want 1234", c)
65		}
66	}
67	if c := C.myint_def; c != 12345 {
68		t.Errorf("C.myint_def = %v, want 12345", c)
69	}
70	{
71		const c = C.myint_def
72		if c != 12345 {
73			t.Errorf("C.myint as const = %v, want 12345", c)
74		}
75	}
76
77	if c := C.myfloat_def; c != 1.5 {
78		t.Errorf("C.myint_def = %v, want 1.5", c)
79	}
80	{
81		const c = C.myfloat_def
82		if c != 1.5 {
83			t.Errorf("C.myint as const = %v, want 1.5", c)
84		}
85	}
86
87	if s := C.mystring_def; s != "hello" {
88		t.Errorf("C.mystring_def = %q, want %q", s, "hello")
89	}
90}
91