github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/misc/cgo/test/issue12030.go (about)

     1  // Copyright 2015 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 12030. sprintf is defined in both ntdll and msvcrt,
     6  // Normally we want the one in the msvcrt.
     7  
     8  package cgotest
     9  
    10  /*
    11  #include <stdio.h>
    12  #include <stdlib.h>
    13  void issue12030conv(char *buf, double x) {
    14  	sprintf(buf, "d=%g", x);
    15  }
    16  */
    17  import "C"
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  	"unsafe"
    23  )
    24  
    25  func test12030(t *testing.T) {
    26  	buf := (*C.char)(C.malloc(256))
    27  	defer C.free(unsafe.Pointer(buf))
    28  	for _, f := range []float64{1.0, 2.0, 3.14} {
    29  		C.issue12030conv(buf, C.double(f))
    30  		got := C.GoString(buf)
    31  		if want := fmt.Sprintf("d=%g", f); got != want {
    32  			t.Fatalf("C.sprintf failed for %g: %q != %q", f, got, want)
    33  		}
    34  	}
    35  }