github.com/stingnevermore/go@v0.0.0-20180120041312-3810f5bfed72/misc/cgo/test/issue21809.go (about)

     1  // Copyright 2017 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  package cgotest
     6  
     7  // Issue 21809.  Compile C `typedef` to go type aliases.
     8  
     9  // typedef long MySigned_t;
    10  // /* tests alias-to-alias */
    11  // typedef MySigned_t MySigned2_t;
    12  //
    13  // long takes_long(long x) { return x * x; }
    14  // MySigned_t takes_typedef(MySigned_t x) { return x * x; }
    15  import "C"
    16  
    17  import "testing"
    18  
    19  func test21809(t *testing.T) {
    20  	longVar := C.long(3)
    21  	typedefVar := C.MySigned_t(4)
    22  	typedefTypedefVar := C.MySigned2_t(5)
    23  
    24  	// all three should be considered identical to `long`
    25  	if ret := C.takes_long(longVar); ret != 9 {
    26  		t.Errorf("got %v but expected %v", ret, 9)
    27  	}
    28  	if ret := C.takes_long(typedefVar); ret != 16 {
    29  		t.Errorf("got %v but expected %v", ret, 16)
    30  	}
    31  	if ret := C.takes_long(typedefTypedefVar); ret != 25 {
    32  		t.Errorf("got %v but expected %v", ret, 25)
    33  	}
    34  
    35  	// They should also be identical to the typedef'd type
    36  	if ret := C.takes_typedef(longVar); ret != 9 {
    37  		t.Errorf("got %v but expected %v", ret, 9)
    38  	}
    39  	if ret := C.takes_typedef(typedefVar); ret != 16 {
    40  		t.Errorf("got %v but expected %v", ret, 16)
    41  	}
    42  	if ret := C.takes_typedef(typedefTypedefVar); ret != 25 {
    43  		t.Errorf("got %v but expected %v", ret, 25)
    44  	}
    45  }