github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/misc/cgo/test/issue4029.go (about)

     1  // Copyright 2012 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  // +build !windows
     6  
     7  package cgotest
     8  
     9  /*
    10  #include <dlfcn.h>
    11  #cgo linux LDFLAGS: -ldl
    12  
    13  extern void call4029(void *arg);
    14  */
    15  import "C"
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  var callbacks int
    22  
    23  //export IMPIsOpaque
    24  func IMPIsOpaque() {
    25  	callbacks++
    26  }
    27  
    28  //export IMPInitWithFrame
    29  func IMPInitWithFrame() {
    30  	callbacks++
    31  }
    32  
    33  //export IMPDrawRect
    34  func IMPDrawRect() {
    35  	callbacks++
    36  }
    37  
    38  //export IMPWindowResize
    39  func IMPWindowResize() {
    40  	callbacks++
    41  }
    42  
    43  func test4029(t *testing.T) {
    44  	loadThySelf(t, "IMPWindowResize")
    45  	loadThySelf(t, "IMPDrawRect")
    46  	loadThySelf(t, "IMPInitWithFrame")
    47  	loadThySelf(t, "IMPIsOpaque")
    48  	if callbacks != 4 {
    49  		t.Errorf("got %d callbacks, expected 4", callbacks)
    50  	}
    51  }
    52  
    53  func loadThySelf(t *testing.T, symbol string) {
    54  	this_process := C.dlopen(nil, C.RTLD_NOW)
    55  	if this_process == nil {
    56  		t.Error("dlopen:", C.GoString(C.dlerror()))
    57  		return
    58  	}
    59  	defer C.dlclose(this_process)
    60  
    61  	symbol_address := C.dlsym(this_process, C.CString(symbol))
    62  	if symbol_address == nil {
    63  		t.Error("dlsym:", C.GoString(C.dlerror()))
    64  		return
    65  	}
    66  	t.Log(symbol, symbol_address)
    67  	C.call4029(symbol_address)
    68  }