github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/misc/cgo/test/issue6612.go (about)

     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  
    10  package cgotest
    11  
    12  /*
    13  void myfunc(void) {}
    14  int myvar = 5;
    15  const char *mytext = "abcdef";
    16  typedef int mytype;
    17  enum {
    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  */
    30  import "C"
    31  
    32  import "testing"
    33  
    34  func 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  	// This would be nice, but it has never worked.
    78  	/*
    79  		if c := C.myfloat_def; c != 1.5 {
    80  			t.Errorf("C.myint_def = %v, want 1.5", c)
    81  		}
    82  		{
    83  			const c = C.myfloat_def
    84  			if c != 1.5 {
    85  			t.Errorf("C.myint as const = %v, want 1.5", c)
    86  			}
    87  		}
    88  	*/
    89  
    90  	if s := C.mystring_def; s != "hello" {
    91  		t.Errorf("C.mystring_def = %q, want %q", s, "hello")
    92  	}
    93  }