github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/misc/cgo/test/issue14838.go (about)

     1  // Copyright 2016 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  // Issue 14838. add CBytes function
     6  
     7  package cgotest
     8  
     9  /*
    10  #include <stdlib.h>
    11  
    12  int check_cbytes(char *b, size_t l) {
    13  	int i;
    14  	for (i = 0; i < l; i++) {
    15  		if (b[i] != i) {
    16  			return 0;
    17  		}
    18  	}
    19  	return 1;
    20  }
    21  */
    22  import "C"
    23  
    24  import (
    25  	"testing"
    26  	"unsafe"
    27  )
    28  
    29  func test14838(t *testing.T) {
    30  	data := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    31  	cData := C.CBytes(data)
    32  	defer C.free(cData)
    33  
    34  	if C.check_cbytes((*C.char)(cData), C.size_t(len(data))) == 0 {
    35  		t.Fatalf("mismatched data: expected %v, got %v", data, (*(*[10]byte)(unsafe.Pointer(cData)))[:])
    36  	}
    37  }